Run long-running operations asynchronously with progress tracking
New in version 2.14.0FastMCP implements the MCP background task protocol (SEP-1686), giving your servers a production-ready distributed task scheduler with a single decorator change.
What is Docket? FastMCP’s task system is powered by Docket, originally built by Prefect to power Prefect Cloud’s managed task scheduling and execution service, where it processes millions of concurrent tasks every day. Docket is now open-sourced for the community.
In MCP, all component interactions are blocking by default. When a client calls a tool, reads a resource, or fetches a prompt, it sends a request and waits for the response. For operations that take seconds or minutes, this creates a poor user experience.The MCP background task protocol solves this by letting clients:
Start an operation and receive a task ID immediately
Track progress as the operation runs
Retrieve the result when ready
FastMCP handles all of this for you. Add task=True to your decorator, and your function gains full background execution with progress reporting, distributed processing, and horizontal scaling.
You can always use Python’s concurrency primitives (asyncio, threads, multiprocessing) or external task queues in your FastMCP servers. FastMCP is just Python—run code however you like.MCP background tasks are different: they’re protocol-native. This means MCP clients that support the task protocol can start operations, receive progress updates, and retrieve results through the standard MCP interface. The coordination happens at the protocol level, not inside your application code.
Add task=True to any tool, resource, resource template, or prompt decorator. This marks the component as capable of background execution.
Copy
import asynciofrom fastmcp import FastMCPmcp = FastMCP("MyServer")@mcp.tool(task=True)async def slow_computation(duration: int) -> str: """A long-running operation.""" for i in range(duration): await asyncio.sleep(1) return f"Completed in {duration} seconds"
When a client requests background execution, the call returns immediately with a task ID. The work executes in a background worker, and the client can poll for status or wait for the result.
Background tasks require async functions. Attempting to use task=True with a sync function raises a ValueError at registration time.
For fine-grained control over task execution behavior, use TaskConfig instead of the boolean shorthand. The MCP task protocol defines three execution modes:
Mode
Client calls without task
Client calls with task
"forbidden"
Executes synchronously
Error: task not supported
"optional"
Executes synchronously
Executes as background task
"required"
Error: task required
Executes as background task
Copy
from fastmcp import FastMCPfrom fastmcp.server.tasks import TaskConfigmcp = FastMCP("MyServer")# Supports both sync and background execution (default when task=True)@mcp.tool(task=TaskConfig(mode="optional"))async def flexible_task() -> str: return "Works either way"# Requires background execution - errors if client doesn't request task@mcp.tool(task=TaskConfig(mode="required"))async def must_be_background() -> str: return "Only runs as a background task"# No task support (default when task=False or omitted)@mcp.tool(task=TaskConfig(mode="forbidden"))async def sync_only() -> str: return "Never runs as background task"
New in version 2.15.0When clients poll for task status, the server tells them how frequently to check back. By default, FastMCP suggests a 5-second interval, but you can customize this per component:
Copy
from datetime import timedeltafrom fastmcp import FastMCPfrom fastmcp.server.tasks import TaskConfigmcp = FastMCP("MyServer")# Poll every 2 seconds for a fast-completing task@mcp.tool(task=TaskConfig(mode="optional", poll_interval=timedelta(seconds=2)))async def quick_task() -> str: return "Done quickly"# Poll every 30 seconds for a long-running task@mcp.tool(task=TaskConfig(mode="optional", poll_interval=timedelta(seconds=30)))async def slow_task() -> str: return "Eventually done"
Shorter intervals give clients faster feedback but increase server load. Longer intervals reduce load but delay status updates.
To enable background task support for all components by default, pass tasks=True to the constructor. Individual decorators can still override this with task=False.
Copy
mcp = FastMCP("MyServer", tasks=True)
If your server defines any synchronous tools, resources, or prompts, you will need to explicitly set task=False on their decorators to avoid an error.
When a client requests background execution but the component has mode="forbidden", FastMCP executes synchronously and returns the result inline. This follows the SEP-1686 specification for graceful degradation—clients can always request background execution without worrying about server capabilities.Conversely, when a component has mode="required" but the client doesn’t request background execution, FastMCP returns an error indicating that task execution is required.
Every FastMCP server with task-enabled components automatically starts an embedded worker. You do not need to start a separate worker process for tasks to execute.To scale horizontally, add more workers using the CLI:
Copy
fastmcp tasks worker server.py
Each additional worker pulls tasks from the same queue, distributing load across processes. Configure worker concurrency via environment:
Additional workers only work with Redis/Valkey backends. The in-memory backend is single-process only.
Task-enabled components must be defined at server startup to be registered with all workers. Components added dynamically after the server starts will not be available for background execution.
The Progress dependency lets you report progress back to clients. Inject it as a parameter with a default value, and FastMCP will provide the active progress reporter.
Copy
from fastmcp import FastMCPfrom fastmcp.dependencies import Progressmcp = FastMCP("MyServer")@mcp.tool(task=True)async def process_files(files: list[str], progress: Progress = Progress()) -> str: await progress.set_total(len(files)) for file in files: await progress.set_message(f"Processing {file}") # ... do work ... await progress.increment() return f"Processed {len(files)} files"
The progress API:
await progress.set_total(n) — Set the total number of steps
FastMCP exposes Docket’s full dependency injection system within your task-enabled functions. Beyond Progress, you can access the Docket instance, worker information, and use advanced features like retries and timeouts.
With CurrentDocket(), you can schedule additional background tasks, chain work together, and coordinate complex workflows. See the Docket documentation for the complete API, including retry policies, timeouts, and custom dependencies.