Add explicit task_meta parameter to FastMCP.call_tool()#2749
Conversation
Adds a `task_meta: TaskMeta | None` parameter to `call_tool()` for explicit control over sync vs background task execution. When `task_meta` is provided, the tool executes as a background task and returns `CreateTaskResult`; when `None` (default), it executes synchronously and returns `ToolResult`. This replaces implicit contextvar-based task determination for tools, making the return type predictable at the call site. Resources and prompts still use the contextvar approach for backwards compatibility.
WalkthroughThis change introduces explicit task metadata propagation throughout the tool execution pipeline. A new Possibly related PRs
Pre-merge checks and finishing touches✅ Passed checks (3 passed)
✨ Finishing touches
🧪 Generate unit tests (beta)
📜 Recent review detailsConfiguration used: Organization UI Review profile: CHILL Plan: Pro ⛔ Files ignored due to path filters (5)
📒 Files selected for processing (7)
🧰 Additional context used📓 Path-based instructions (1)src/fastmcp/**/*.py📄 CodeRabbit inference engine (AGENTS.md)
Files:
🧠 Learnings (2)📚 Learning: 2025-12-25T15:53:07.646ZApplied to files:
📚 Learning: 2025-12-25T15:53:07.646ZApplied to files:
🧬 Code graph analysis (5)src/fastmcp/server/tasks/__init__.py (1)
src/fastmcp/server/providers/fastmcp_provider.py (2)
src/fastmcp/tools/tool.py (3)
src/fastmcp/server/tasks/handlers.py (3)
src/fastmcp/server/tasks/routing.py (5)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (4)
🔇 Additional comments (11)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
Adds a
task_meta: TaskMeta | Noneparameter tocall_tool()for explicit control over sync vs background task execution. This makes the return type predictable at the call site through overloads:Tools can now call other tools as background tasks with explicit control:
The
TaskMetadataclass supports:ttl: int | None- TTL in milliseconds (uses server default if None)fn_key: str | None- Docket function key (auto-populated from tool name if None)Resources and prompts still use contextvar-based task routing for backwards compatibility.