-
Notifications
You must be signed in to change notification settings - Fork 3.1k
feat(mcp): add nonblocking_tools to MCPToolset #5694
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
longcw
wants to merge
4
commits into
main
Choose a base branch
from
longc/mcp-toolset-async-mode
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
61710cb
feat(mcp): add async_mode to MCPToolset
longcw 1a1f557
Update examples/voice_agents/mcp/mcp-agent.py
longcw 1c71887
refactor(mcp): replace async_mode with nonblocking_tools bool|list[str]
longcw 7bc5a5b
Merge remote-tracking branch 'origin/main' into longc/mcp-toolset-asy…
longcw File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,12 +1,51 @@ | ||
| from mcp.server.fastmcp import FastMCP | ||
| import asyncio | ||
| import random | ||
|
|
||
| from mcp.server.fastmcp import Context, FastMCP | ||
|
|
||
| mcp = FastMCP("Demo 🚀") | ||
|
|
||
|
|
||
| @mcp.tool() | ||
| def get_weather(location: str) -> str: | ||
| """Get the current weather for a location.""" | ||
| return f"The weather in {location} is a perfect sunny 70°F today. Enjoy your day!" | ||
|
|
||
|
|
||
| @mcp.tool() | ||
| async def book_flight(origin: str, destination: str, date: str, ctx: Context) -> str: | ||
| """Book a flight. Takes a couple of minutes — emits progress notifications | ||
| while searching airlines and confirming the booking.""" | ||
| await ctx.report_progress( | ||
| 0.0, | ||
| 1.0, | ||
| f"Searching flights from {origin} to {destination} on {date}. " | ||
| "This will take a couple of minutes.", | ||
| ) | ||
|
|
||
| # Phase 1: searching airlines | ||
| await asyncio.sleep(30) | ||
|
|
||
| airlines = random.sample(["United", "Delta", "American", "JetBlue", "Southwest", "Alaska"], k=3) | ||
| prices = {a: random.randint(180, 650) for a in airlines} | ||
| cheapest = min(prices, key=lambda a: prices[a]) | ||
|
|
||
| await ctx.report_progress( | ||
| 0.5, | ||
| 1.0, | ||
| f"Found {len(airlines)} options. Best price: ${prices[cheapest]} on {cheapest}. " | ||
| "Confirming the booking now.", | ||
| ) | ||
|
|
||
| # Phase 2: confirming booking | ||
| await asyncio.sleep(40) | ||
|
|
||
| confirmation = f"FL-{random.randint(100000, 999999)}" | ||
| return ( | ||
| f"Flight booked! {cheapest} from {origin} to {destination} on {date}. " | ||
| f"Price: ${prices[cheapest]}. Confirmation: {confirmation}." | ||
| ) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| mcp.run(transport="sse") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🟡 Legacy
mcp_serverspath silently gets non-blocking behavior and extra LLM-visible toolsThe deprecated
mcp_serverspath inagent_activity.py:700createsMCPToolsetwith the defaultnonblocking_tools=True. Before this PR,MCPToolsetextendedToolset(purely blocking). Now it extendsAsyncToolset, so all MCP tools from the legacy path become non-blocking by default. This means: (1)get_running_tasksandcancel_tasktools are exposed to the LLM, (2) a_lk_agents_confirm_duplicateparameter is injected into every tool schema, and (3) duplicate-call detection (mode"confirm") is active — which could reject legitimate repeated calls. For tools without MCP progress notifications the end result is functionally similar (the wrapper blocks on_pending_futuntil the tool completes), but the schema changes and extra exposed tools could confuse the LLM or subtly change behavior for users on the deprecated API.Prompt for agents
Was this helpful? React with 👍 or 👎 to provide feedback.