-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Fix @mcp.tool()
Failing to Detect Context Argument When Type Variables Are Used in Context
#358
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think this solves the issue. Add a test, and you'll see.
Actually, it passes the current tests and also works on my local server. The cause of #357 is that it only checks if This part is for handling cases where if get_origin(param.annotation) is not None:
continue |
I'm asking to add a test where we use the generic params for This should work for: context: Context[SessionParam] # missing lifespan, since it has a default
context: Context[SessionParam, LifespanParam] # both
context: Context[SessionParam, LifespanParam] | None = None # union
context: Annotated[Context[Session, LifespanParam], "metadata"] # Not necessary for now |
I've added a test for the second case. Regarding the first case: Context[SessionParam] This is not only useless but also invalid. It raises the following exception:
About the third case: I don't believe this situation should occur. If a tool requests a context, it should always be provided. |
Previously, the code only checked for exact matches with `Context`. This update ensures that subclasses of `Context` are also correctly identified, improving flexibility and reliability.
Motivation and Context
When defining tools to safely access the app context (lifespan context) using
Context
, it is necessary to include type variables.However, the current implementation of
@mcp.tool()
fails to detect the context argument when type variables are specified (as detailed in #357).This PR addresses and resolves this issue.
How Has This Been Tested?
The following commands were executed locally to verify the fix:
Breaking Changes
No.
Types of changes
Checklist