Add concurrent tool execution with sequential flag#3022
Conversation
Implements parallel tool execution for sampling with configurable concurrency: - Add `sequential` field to SamplingTool (default False) - Add `tool_concurrency` parameter to sample() and sample_step() - tool_concurrency=None: sequential (default, backward compatible) - tool_concurrency=0: unlimited parallel - tool_concurrency=N: at most N concurrent tools - If any tool has sequential=True, all tools execute sequentially - Use anyio.Semaphore for bounded concurrency - Preserve result order regardless of execution order - Add comprehensive tests for all concurrency modes Co-authored-by: Bill Easton <strawgate@users.noreply.github.com>
|
@coderabbitai please review |
|
✅ Actions performedReview triggered.
|
WalkthroughA new optional parameter 🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing touches
🧪 Generate unit tests (beta)
No actionable comments were generated in the recent review. 🎉 🧹 Recent nitpick comments
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 |
There was a problem hiding this comment.
Actionable comments posted: 1
🧹 Nitpick comments (1)
src/fastmcp/server/sampling/run.py (1)
311-317: Simplify therequires_sequentialcheck — avoid constructing dummySamplingToolobjects.The current code creates a full
SamplingToolinstance (with a lambda) just to get a defaultsequential=Falsefor unknown tool names. This can be simplified.♻️ Suggested simplification
- requires_sequential = any( - tool_map.get( - tool_use.name, SamplingTool(name="", parameters={}, fn=lambda: None) - ).sequential - for tool_use in tool_calls - ) + requires_sequential = any( + (tool := tool_map.get(tool_use.name)) is not None and tool.sequential + for tool_use in tool_calls + )
…validation and docs
Overview
Implements parallel tool execution for sampling with configurable concurrency. When an LLM returns multiple tool calls in a single response, they can now be executed concurrently for improved performance.
Key Features
tool_concurrencyparameter to enable parallel executionsequential=Trueto force sequential executionExample
Implementation
anyio.Semaphorefor bounded concurrencyfastmcp.utilities.async_utils.gather()for structured concurrencyCloses #3012
Generated with Claude Code