mirror of
https://github.com/PrefectHQ/fastmcp.git
synced 2026-08-12 00:29:11 +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>
103 lines
3.2 KiB
Python
103 lines
3.2 KiB
Python
"""
|
|
Tests for server-side tool task behavior.
|
|
|
|
Tests tool-specific task handling, parallel to test_task_prompts.py
|
|
and test_task_resources.py.
|
|
"""
|
|
|
|
import asyncio
|
|
|
|
import pytest
|
|
|
|
from fastmcp import FastMCP
|
|
from fastmcp.client import Client
|
|
|
|
|
|
@pytest.fixture
|
|
async def tool_server():
|
|
"""Create a FastMCP server with task-enabled tools."""
|
|
mcp = FastMCP("tool-task-server")
|
|
|
|
@mcp.tool(task=True)
|
|
async def simple_tool(message: str) -> str:
|
|
"""A simple tool for testing."""
|
|
return f"Processed: {message}"
|
|
|
|
@mcp.tool(task=False)
|
|
async def sync_only_tool(message: str) -> str:
|
|
"""Tool with task=False."""
|
|
return f"Sync: {message}"
|
|
|
|
return mcp
|
|
|
|
|
|
async def test_synchronous_tool_call_unchanged(tool_server):
|
|
"""Tools without task metadata execute synchronously as before."""
|
|
async with Client(tool_server) as client:
|
|
# Regular call without task metadata
|
|
result = await client.call_tool("simple_tool", {"message": "hello"})
|
|
|
|
# Should execute immediately and return result
|
|
assert "Processed: hello" in str(result)
|
|
|
|
|
|
async def test_tool_with_task_metadata_returns_immediately(tool_server):
|
|
"""Tools with task metadata return immediately with ToolTask object."""
|
|
async with Client(tool_server) as client:
|
|
# Call with task metadata
|
|
task = await client.call_tool("simple_tool", {"message": "test"}, task=True)
|
|
assert task
|
|
assert not task.returned_immediately
|
|
|
|
from fastmcp.client.client import ToolTask
|
|
|
|
assert isinstance(task, ToolTask)
|
|
assert isinstance(task.task_id, str)
|
|
assert len(task.task_id) > 0
|
|
|
|
|
|
async def test_tool_task_executes_in_background(tool_server):
|
|
"""Tool task is submitted to Docket and executes in background."""
|
|
execution_started = asyncio.Event()
|
|
execution_completed = asyncio.Event()
|
|
|
|
@tool_server.tool(task=True)
|
|
async def coordinated_tool() -> str:
|
|
"""Tool with coordination points."""
|
|
execution_started.set()
|
|
await execution_completed.wait()
|
|
return "completed"
|
|
|
|
async with Client(tool_server) as client:
|
|
task = await client.call_tool("coordinated_tool", task=True)
|
|
assert task
|
|
assert not task.returned_immediately
|
|
|
|
# Wait for execution to start
|
|
await asyncio.wait_for(execution_started.wait(), timeout=2.0)
|
|
|
|
# Task should still be working
|
|
status = await task.status()
|
|
assert status.status in ["working"]
|
|
|
|
# Signal completion
|
|
execution_completed.set()
|
|
await task.wait(timeout=2.0)
|
|
|
|
result = await task.result()
|
|
assert result.data == "completed"
|
|
|
|
|
|
async def test_graceful_degradation_tool_without_task_flag(tool_server):
|
|
"""Tools with task=False execute synchronously even with task metadata."""
|
|
async with Client(tool_server) as client:
|
|
# Try to call with task metadata - server should execute synchronously
|
|
task = await client.call_tool("sync_only_tool", {"message": "test"}, task=True)
|
|
assert task
|
|
assert task.returned_immediately
|
|
|
|
result = await task.result()
|
|
assert "Sync: test" in str(result)
|
|
|
|
status = await task.status()
|
|
assert status.status == "completed"
|