-
Notifications
You must be signed in to change notification settings - Fork 95
Add request-level Metadata injection for tool handlers #161
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
base: main
Are you sure you want to change the base?
Conversation
Signed-off-by: Marvin Rütten <[email protected]>
07ea140 to
923e80f
Compare
|
Hi, very well done. I need exactly the same thing. Hopefully it will be merged soon. |
|
Thank you. I appreciate the idea. Correct me if Im wrong, but this is not specified in the mcp protocol, is it? |
|
The spec only states this about metadata: but it's not helpful if you can't access it when used - this is what this PR is about |
|
Exactly! On a related note, maybe this is a good time to revisit the Context object idea. Beyond just Once auth lands, this becomes even more relevant, since handlers will likely need access to user/session state without turning parameters into part of the Tool schema. |
|
In my opinion we should provide the tooling so that users can access part of the request they need, though indeed we shouldn't multiply context value objects that can be injected as its an open door to too many changes (though this would be probably detected through argument resolving). 👍 @CodeWithKyrian a context would be nice, I'm usually preferring array structures then objects when it comes to exposing such context but in our case it may be cleaner with an object. We must think about changes and BC-layers though as the protocol being quite immature it'll probably change a lot, so does this library. I suggest that we should make clear to users that every bits of this context can probably be undefined and that the API should be prepared for that. To me it'd make sense to expose the full |
|
I did implement a single injectable |
This Pull Request implements the feature discussed in Issue #159:
Expose request-level metadata to tool handlers.
• Introduced a new Mcp\Schema\Metadata value object to represent request-scoped metadata from the MCP message envelope.
• Updated the handler invocation logic so that parameters type-hinted as Metadata automatically receive the _meta portion of the incoming request.
• Enhanced CallToolHandler to forward the envelope metadata into the parameter resolver.
• Added comprehensive unit tests covering:
• metadata injection
• fallback behavior when no metadata is provided
• error handling during parameter resolution
• backwards-compatibility with existing tool handlers
• Updated documentation with usage examples showing how tool authors can access metadata such as securitySchema.
Motivation and Context
This change addresses Issue #159, which highlights that tool handlers currently cannot access metadata from the request envelope.
Many real-world use cases depend on this metadata — for example:
• authorization logic based on a securitySchema
• tenant or environment selection
• contextual behavior determined by the client request
Previously, developers had to rely on indirect workarounds (middleware, global state, decorators), because metadata was lost before reaching the handler.
This feature introduces a clean, explicit, opt-in approach based on type-hinting, keeping handler signatures transparent and predictable.
How Has This Been Tested?
• Added new unit tests verifying metadata extraction and injection.
• Validated that existing handlers without a Metadata parameter continue working unchanged.
• Tested optional and missing metadata cases to ensure robust behavior.
• Verified handler execution flow with a real tool using a securitySchema passed via _meta.
Breaking Changes
This change is not breaking.
Existing handlers and configurations continue to work as before.
Metadata is injected only when a parameter explicitly type-hints Mcp\Schema\Metadata.
Types of changes
Checklist
Additional context
This approach is fully backward-compatible and future-proof.
The type-hint-based injection model can be extended later to support additional request-level context types (e.g., full request object, execution context, or transport metadata).