Use this file to discover all available pages before exploring further.
New in version 2.14.0Use this when you need to run long operations asynchronously while doing other work.The MCP task protocol lets you request operations to run in the background. The call returns a Task object immediately, letting you track progress, cancel operations, or await results.
Pass task=True to run an operation as a background task:
from fastmcp import Clientasync with Client(server) as client: # Start a background task task = await client.call_tool("slow_computation", {"duration": 10}, task=True) print(f"Task started: {task.task_id}") # Do other work while it runs... # Get the result when ready result = await task.result()
# Wait up to 30 seconds for completionstatus = await task.wait(timeout=30.0)# Wait for a specific statestatus = await task.wait(state="completed", timeout=30.0)
You can always pass task=True regardless of whether the server supports background tasks. Per the MCP specification, servers without task support execute the operation immediately and return the result inline.
task = await client.call_tool("my_tool", args, task=True)if task.returned_immediately: print("Server executed immediately (no background support)")else: print("Running in background")# Either way, this worksresult = await task.result()
This lets you write task-aware client code without worrying about server capabilities.
import asynciofrom fastmcp import Clientasync def main(): async with Client(server) as client: # Start background task task = await client.call_tool( "slow_computation", {"duration": 10}, task=True, ) # Subscribe to updates def on_update(status): print(f"Progress: {status.statusMessage}") task.on_status_change(on_update) # Do other work while task runs print("Doing other work...") await asyncio.sleep(2) # Wait for completion and get result result = await task.result() print(f"Result: {result.content}")asyncio.run(main())