fix: detect Context parameter in callable class instances#2178
Closed
giulio-leone wants to merge 2 commits intomodelcontextprotocol:mainfrom
Closed
fix: detect Context parameter in callable class instances#2178giulio-leone wants to merge 2 commits intomodelcontextprotocol:mainfrom
giulio-leone wants to merge 2 commits intomodelcontextprotocol:mainfrom
Conversation
added 2 commits
February 28, 2026 15:41
find_context_parameter() uses typing.get_type_hints(fn) to find Context-typed parameters. However, get_type_hints() doesn't introspect the __call__ method of callable class instances, so ctx: Context is exposed as a visible tool parameter instead of being injected by the framework. The fix checks if fn is a callable class instance (not a function or method) and inspects fn.__call__ instead. Fixes modelcontextprotocol#1974
Exercise the branch in context_injection.py where the input is neither a function/method nor a callable class, ensuring 100% branch coverage.
Author
|
Closing to reduce noise — focusing on the most impactful PRs. Happy to reopen if there's interest. |
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.
Problem
When registering a callable class instance as an MCP tool via
add_tool(), thectx: Contextparameter is incorrectly exposed as a visible tool parameter in the JSON schema, instead of being injected by the framework.This happens because
find_context_parameter()usestyping.get_type_hints(fn), which doesn't introspect the__call__method of callable class instances.Fix
In
find_context_parameter(), detect callable class instances (not functions or methods) and inspectfn.__call__instead offndirectly.Tests
Added two tests:
test_context_detection_callable_class: Verifiesctxis detected and excluded from schematest_context_injection_callable_class: Verifiesctxis properly injected at runtimeFixes #1974