fix: tool cache refresh with nested handler invocation#1299
Closed
matthicksj wants to merge 3 commits intomodelcontextprotocol:mainfrom
Closed
fix: tool cache refresh with nested handler invocation#1299matthicksj wants to merge 3 commits intomodelcontextprotocol:mainfrom
matthicksj wants to merge 3 commits intomodelcontextprotocol:mainfrom
Conversation
Fix MCP SDK issue modelcontextprotocol#1298 where tool handlers fail to execute properly when streaming context is present in the parent process. The fix stores a direct reference to the list_tools function to avoid nested handler invocation which can disrupt async execution flow in streaming contexts. 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
- Remove trailing whitespace - Consolidate multi-line Tool() constructors - Remove unused imports from anyio.streams.memory - Add type annotations for test interceptor functions - Apply ruff formatting rules 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
- Add type annotation for handler_invocations list - Import CallToolResult for proper type narrowing - Add explicit isinstance check for asyncio.gather results - Fixes type errors that were preventing CI from passing
Contributor
|
Closing as the underlying issue (#1298) has been closed. |
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Fix MCP SDK issue #1298 where tool handlers fail to execute properly when streaming context is present in the parent process.
The fix stores a direct reference to the list_tools function to avoid nested handler invocation which can disrupt async execution flow in streaming contexts.
Motivation and Context
Tool handlers were failing when streaming context was present in the parent process. The root cause was that cache refresh used nested handler invocation via
self.request_handlers[ListToolsRequest](None), which disrupts async execution flow in streaming contexts. This fix resolves issue #1298 by using a direct function reference for cache refresh instead of invoking through the request handler.How Has This Been Tested?
tests/server/test_tool_cache_refresh_bug.pythat verifies cache refresh works without nested handler invocationBreaking Changes
None. This is an internal implementation fix that maintains the same external API and behavior.
Types of changes
Checklist
Additional context
The fix modifies
src/mcp/server/lowlevel/server.pyto store a direct reference to thelist_toolsfunction when it's registered via the decorator. During cache refresh, this direct reference is used instead of going through the request handler system, avoiding the nested invocation issue while maintaining identical functionality.