forked from modelcontextprotocol/python-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtask_based_tool.py
More file actions
32 lines (24 loc) · 1.03 KB
/
task_based_tool.py
File metadata and controls
32 lines (24 loc) · 1.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
"""Example server demonstrating task-based execution with long-running tools."""
import asyncio
from examples.shared.in_memory_task_store import InMemoryTaskStore
from mcp.server.fastmcp import FastMCP
# Create a task store to enable task-based execution
task_store = InMemoryTaskStore()
mcp = FastMCP(name="Task-Based Tool Example", task_store=task_store)
@mcp.tool()
async def long_running_computation(data: str, delay_seconds: float = 2.0) -> str:
"""
Simulate a long-running computation that benefits from task-based execution.
This tool demonstrates the 'call-now, fetch-later' pattern where clients can:
1. Initiate the task without waiting
2. Disconnect and reconnect later
3. Poll for status and retrieve results when ready
Args:
data: Input data to process
delay_seconds: Simulated processing time
"""
# Simulate long-running work
await asyncio.sleep(delay_seconds)
# Return processed result
result = f"Processed: {data.upper()} (took {delay_seconds}s)"
return result