fix: handle callable class instances in find_context_parameter()#2042
Open
hashwnath wants to merge 1 commit intomodelcontextprotocol:mainfrom
Open
fix: handle callable class instances in find_context_parameter()#2042hashwnath wants to merge 1 commit intomodelcontextprotocol:mainfrom
hashwnath wants to merge 1 commit intomodelcontextprotocol:mainfrom
Conversation
When registering a callable class instance as an MCP tool via FastMCP.add_tool(), the ctx: Context parameter was incorrectly exposed as an externally visible tool parameter instead of being injected by the framework. Root cause: typing.get_type_hints() was called on the class instance rather than its __call__ method, so Context type hints were not resolved. Fix: detect non-function/non-method callables and target their __call__ method, mirroring the existing pattern in _is_async_callable(). Fixes modelcontextprotocol#1974
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.
Summary
Fix
find_context_parameter()to correctly detectContextparameters in callable class instances registered viaFastMCP.add_tool().Motivation and Context
When registering a callable class instance as an MCP tool, the
ctx: Contextparameter is incorrectly exposed as an externally visible tool parameter in the JSON schema instead of being injected by the framework.Root cause:
typing.get_type_hints(fn)is called on the class instance rather than its__call__method, so the Context type hint is not resolved.Fix: Detect non-function/non-method callables and target their
__call__method for type hint resolution. This mirrors the existing pattern in_is_async_callable().Resolves #1974
What Has Been Changed?
src/mcp/server/mcpserver/utilities/context_injection.py: Added callable class instance handling beforeget_type_hints()calltests/server/mcpserver/test_tool_manager.py: Added 3 tests:test_context_parameter_detection_callable_class— sync callable classtest_context_parameter_detection_async_callable_class— async callable classtest_context_injection_callable_class— full injection + schema exclusionHow Has This Been Tested?
test_tool_manager.pypass (including 3 new tests)context_kwargis correctly set to"ctx"ctxis excluded from tool parameters JSON schemaBreaking Changes
No breaking changes.
Types of changes
Checklist