π The fast, Pythonic way to build MCP servers and clients.
Pass task-creation results through ToolResult-only middleware
Jeremiah Lowin601903436b37
diff --git a/fastmcp_slim/fastmcp/server/middleware/caching.py b/fastmcp_slim/fastmcp/server/middleware/caching.pyindex 92fd7f277..cbf48b849 100644--- a/fastmcp_slim/fastmcp/server/middleware/caching.py+++ b/fastmcp_slim/fastmcp/server/middleware/caching.py@@ -445,6 +445,15 @@ class ResponseCachingMiddleware(Middleware):if isinstance(tool_result, InputRequiredToolResult):return tool_result+ # A task-augmented call returns a CreateTaskResult (the tasks extension)+ # up through this middleware β an acknowledgement that the work was+ # enqueued, not a cacheable answer, and without a ToolResult's+ # content/structured_content. Pass any non-ToolResult straight through+ # rather than crash wrapping it (the crash would fire after the task is+ # already enqueued, so a client retry could duplicate side effects).+ if not isinstance(tool_result, ToolResult):+ return tool_result+cacheable_tool_result: CacheableToolResult = CacheableToolResult.wrap(value=tool_result)diff --git a/fastmcp_slim/fastmcp/server/middleware/response_limiting.py b/fastmcp_slim/fastmcp/server/middleware/response_limiting.pyindex 204559f59..c62ec0243 100644--- a/fastmcp_slim/fastmcp/server/middleware/response_limiting.py+++ b/fastmcp_slim/fastmcp/server/middleware/response_limiting.py@@ -118,6 +118,12 @@ class ResponseLimitingMiddleware(Middleware):if isinstance(result, InputRequiredToolResult):return result+ # A task-augmented call returns a CreateTaskResult (the tasks extension)+ # up through this middleware β a small acknowledgement with no tool+ # content to measure or truncate. Pass any non-ToolResult through.+ if not isinstance(result, ToolResult):+ return result+# Check if we should limit this toolif self.tools is not None and context.message.name not in self.tools:return resultdiff --git a/tests/tasks/server/test_task_middleware.py b/tests/tasks/server/test_task_middleware.pynew file mode 100644index 000000000..c9324fc2a--- /dev/null+++ b/tests/tasks/server/test_task_middleware.py@@ -0,0 +1,37 @@+"""Task-augmented calls flow through ToolResult-inspecting middleware safely.++A `tools/call` the tasks extension turns into a background task returns a+`CreateTaskResult` up through the middleware chain. Middleware that post-process+a `ToolResult` (response caching, response limiting) must pass that+acknowledgement through untouched rather than crash after the task is enqueued.+"""++from __future__ import annotations++from fastmcp_tasks.models import CreateTaskResult++from fastmcp import FastMCP+from fastmcp.server.middleware.caching import ResponseCachingMiddleware+from fastmcp.server.middleware.response_limiting import ResponseLimitingMiddleware+from fastmcp_tasks import TasksExtension+from tests.tasks.task_helpers import running_task_server, submit_task, wait_for_task+++async def test_tasked_call_survives_result_inspecting_middleware():+ mcp = FastMCP("tasks-mw")+ mcp.add_extension(TasksExtension())+ mcp.add_middleware(ResponseCachingMiddleware())+ mcp.add_middleware(ResponseLimitingMiddleware(max_size=1_000_000))++ @mcp.tool(task=True)+ async def crunch(n: int) -> int:+ return n * n++ async with running_task_server(mcp):+ created = await submit_task(mcp, "crunch", {"n": 9})+ assert isinstance(created, CreateTaskResult)+ final = await wait_for_task(mcp, created.task_id)++ assert final.status == "completed"+ assert final.result is not None+ assert final.result["structuredContent"] == {"result": 81}