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 review from a team and 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
@maxisbey maxisbey added bug Something isn't working needs confirmation Needs confirmation that the PR is actually required or needed. labels Sep 19, 2025
@felixweinberger felixweinberger self-assigned this Sep 30, 2025
@felixweinberger felixweinberger added needs maintainer action Potentially serious issue - needs proactive fix and maintainer attention and removed needs confirmation Needs confirmation that the PR is actually required or needed. labels Sep 30, 2025
@felixweinberger felixweinberger removed their assignment Oct 10, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working needs maintainer action Potentially serious issue - needs proactive fix and maintainer attention

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants