New in versionDocumentation Index
Fetch the complete documentation index at: https://fastmcp-transfer-to-prefecthq.mintlify.app/llms.txt
Use this file to discover all available pages before exploring further.
2.14.0
FastMCP implements the MCP background task protocol (SEP-1686), giving your servers a production-ready distributed task scheduler with a single decorator change.
What Are MCP Background Tasks?
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
task=True to your decorator, and your function gains full background execution with progress reporting, distributed processing, and horizontal scaling.
MCP Background Tasks vs Python Concurrency
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.Enabling Background Tasks
New in version3.0.0 Background tasks require the tasks extra:
task=True to any tool, resource, resource template, or prompt decorator. This marks the component as capable of background execution.
Execution Modes
For fine-grained control over task execution behavior, useTaskConfig 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 |
task=True→TaskConfig(mode="optional")task=False→TaskConfig(mode="forbidden")
Poll Interval
New in version2.15.0
When 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:
Server-Wide Default
To enable background task support for all components by default, passtasks=True to the constructor. Individual decorators can still override this with task=False.
Graceful Degradation
When a client requests background execution but the component hasmode="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.
Configuration
| Environment Variable | Default | Description |
|---|---|---|
FASTMCP_DOCKET_URL | memory:// | Backend URL (memory:// or redis://host:port/db) |
Backends
FastMCP supports two backends for task execution, each with different tradeoffs.In-Memory Backend (Default)
The in-memory backend (memory://) requires zero configuration and works out of the box.
Advantages:
- No external dependencies
- Simple single-process deployment
- Ephemeral: If the server restarts, all pending tasks are lost
- Higher latency: ~250ms task pickup time vs single-digit milliseconds with Redis
- No horizontal scaling: Single process only—you cannot add additional workers
Redis Backend
For production deployments, use Redis (or Valkey) as your backend by settingFASTMCP_DOCKET_URL=redis://localhost:6379.
Advantages:
- Persistent: Tasks survive server restarts
- Fast: Single-digit millisecond task pickup latency
- Scalable: Add workers to distribute load across processes or machines
Workers
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:Additional workers only work with Redis/Valkey backends. The in-memory backend is single-process only.
Progress Reporting
TheProgress 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.
await progress.set_total(n)— Set the total number of stepsawait progress.increment(amount=1)— Increment progressawait progress.set_message(text)— Update the status message
Docket Dependencies
FastMCP exposes Docket’s full dependency injection system within your task-enabled functions. BeyondProgress, you can access the Docket instance, worker information, and use advanced features like retries and timeouts.
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.
