Skip to content

Conversation

matthicksj
Copy link

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?

  • Added comprehensive test in tests/server/test_tool_cache_refresh_bug.py that verifies cache refresh works without nested handler invocation
  • Test confirms the fix prevents async execution flow disruption
  • All existing tests pass

Breaking Changes

None. This is an internal implementation fix that maintains the same external API and behavior.

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Documentation update

Checklist

  • I have read the MCP Documentation
  • My code follows the repository's style guidelines
  • New and existing tests pass locally
  • I have added appropriate error handling
  • I have added or updated documentation as needed

Additional context

The fix modifies src/mcp/server/lowlevel/server.py to store a direct reference to the list_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.

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 <[email protected]>
@matthicksj matthicksj requested a review from a team as a code owner August 23, 2025 18:02
@matthicksj matthicksj requested a review from ochafik August 23, 2025 18:02
matthicksj and others added 2 commits August 23, 2025 12:38
- 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 <[email protected]>
- 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant