mirror of
https://github.com/PrefectHQ/fastmcp.git
synced 2026-08-09 23:29:10 +02:00
* Implement MCP background tasks (SEP-1686) using Docket Adds support for background task execution via the MCP task protocol, powered by Docket for task queue management. - Tools, resources, and prompts can be marked with `task=True` to run async - Progress dependency for tracking task progress - CurrentDocket and CurrentWorker dependencies for advanced use cases - Client API with `.call_tool(..., task=True)` returns task handles - Task status notifications via subscriptions - CLI worker command for distributed task processing Configuration via environment: - FASTMCP_ENABLE_DOCKET=true - FASTMCP_ENABLE_TASKS=true - FASTMCP_DOCKET_URL=redis://... (or memory:// for single-process) 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com> * Fix tasks example import (TaskStatusResponse → GetTaskResult) The example was using a non-existent TaskStatusResponse type. Updated to use mcp.types.GetTaskResult which is what the on_status_change callback actually receives. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com> * Fix env var name in Docket error messages The error messages referenced FASTMCP_EXPERIMENTAL_ENABLE_DOCKET but the actual setting is FASTMCP_ENABLE_DOCKET. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com> * Remove deprecated code re-added from pre-#2329 branch - Remove ExtendedEnvSettingsSource (FASTMCP_SERVER_ prefix support) - Remove dependencies parameter from FastMCP.__init__ * Replace fakeredis git pin with PyPI release * Remove redundant fakeredis dev dep (pulled via pydocket) --------- Co-authored-by: Claude <noreply@anthropic.com> Co-authored-by: Jeremiah Lowin <153965+jlowin@users.noreply.github.com>
75 lines
2 KiB
Python
75 lines
2 KiB
Python
"""
|
|
FastMCP Tasks Example Server
|
|
|
|
Demonstrates background task execution with progress tracking using Docket.
|
|
|
|
Setup:
|
|
1. Start Redis: docker compose up -d
|
|
2. Load environment: source .envrc
|
|
3. Run server: fastmcp run server.py
|
|
|
|
The example uses Redis by default to demonstrate distributed task execution
|
|
and the fastmcp tasks CLI commands.
|
|
"""
|
|
|
|
import asyncio
|
|
import logging
|
|
from typing import Annotated
|
|
|
|
from docket import Logged
|
|
|
|
from fastmcp import FastMCP
|
|
from fastmcp.dependencies import Progress
|
|
|
|
# Configure logging
|
|
logging.basicConfig(level=logging.INFO)
|
|
logger = logging.getLogger(__name__)
|
|
|
|
# Create server
|
|
mcp = FastMCP("Tasks Example")
|
|
|
|
|
|
@mcp.tool(task=True)
|
|
async def slow_computation(
|
|
duration: Annotated[int, Logged],
|
|
progress: Progress = Progress(),
|
|
) -> str:
|
|
"""
|
|
Perform a slow computation that takes `duration` seconds.
|
|
|
|
This tool demonstrates progress tracking with background tasks.
|
|
It logs progress every 1-2 seconds and reports progress via Docket.
|
|
|
|
Args:
|
|
duration: Number of seconds the computation should take (1-60)
|
|
|
|
Returns:
|
|
A completion message with the total duration
|
|
"""
|
|
if duration < 1 or duration > 60:
|
|
raise ValueError("Duration must be between 1 and 60 seconds")
|
|
|
|
logger.info(f"Starting slow computation for {duration} seconds")
|
|
|
|
# Set total progress units
|
|
await progress.set_total(duration)
|
|
|
|
# Process each second
|
|
for i in range(duration):
|
|
# Sleep for 1 second
|
|
await asyncio.sleep(1)
|
|
|
|
# Update progress
|
|
elapsed = i + 1
|
|
remaining = duration - elapsed
|
|
await progress.increment()
|
|
await progress.set_message(
|
|
f"Working... {elapsed}/{duration}s ({remaining}s remaining)"
|
|
)
|
|
|
|
# Log every 1-2 seconds
|
|
if elapsed % 2 == 0 or elapsed == duration:
|
|
logger.info(f"Progress: {elapsed}/{duration}s")
|
|
|
|
logger.info(f"Completed computation in {duration} seconds")
|
|
return f"Computation completed successfully in {duration} seconds!"
|