fix: tool cache refresh with nested handler invocation #1299
+210
−3
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.py
that 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.py
to store a direct reference to thelist_tools
function 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.