-
Notifications
You must be signed in to change notification settings - Fork 27
feat: Pass CallContext
(with Request data) to MCP Tool invocation
#50
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
feat: Pass CallContext
(with Request data) to MCP Tool invocation
#50
Conversation
CallContext
(with Request data) to MCP Tool invocationCallContext
(with Request data) to MCP Tool invocation
Hey! This is actually a pretty cool idea. I was initially thinking it might not be necessary given my OAuth plans as I mentioned in #45, but you know what - you're right that handlers should have access to the request context. I like the approach, but a few thoughts: Naming: What goes in it: If the context was to hold just the request, we could just let people type-hint Small tweak: Do you mind making the properties readonly? Follows our pattern with the DTO like classes like the schema classes and means we can pass it around without worrying about modifications: final readonly class Context
{
public function __construct(
public SessionInterface $session,
public ?ServerRequestInterface $request = null,
) {}
} I love that you made request nullable - stdio won't have HTTP stuff available so that's perfect. Alternative approach: Now that I think of it, what if we just allow users to type-hint either On the php-cs-fixer thing: My bad - I accidentally added Generally, I think this is a solid start. Happy to help! |
Agree 👍
Agree 👍
I thought about this too, and while I like the simplicity about it, otoh I think it provokes error-prone usages. WDYT? |
1e1e668
to
8bc9807
Compare
8bc9807
to
1779f4a
Compare
Yeah, that makes total sense actually. I didn’t think of the stdio + nullable mismatch that could easily sneak in. I’m with you, let’s stick with the Context object. It’s cleaner and future-proof too. Go ahead 💪🏽 |
tests/Unit/DispatcherTest.php
Outdated
@@ -2,9 +2,11 @@ | |||
|
|||
namespace PhpMcp\Server\Tests\Unit; | |||
|
|||
use Grpc\Call; |
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.
Not used. It's probably accidental. Let’s remove this.
Looks perfect now. Thanks for making those changes. Merging this in 🚀 |
Requirement
In the context of the MCP Tool execution I would need access to the
Authorization
header being sent with the HTTP request.My MCP Tool will further facilitate an SDK/API for which the authentication data is required.
Implementation
CallContext
object, which can hold also further properties in futureCallContext
property on MCP Tool methods is not exposed in the MCP protocol (seeDiscovery
)CallContext
instance is automatically passed to the method invocation if present in method signatureRemarks:
.php-cs-fixer.php
config file in the repo, but it is defined in thecomposer lint
command 🤷Open Todos
(Please let me know your thoughts, then I will continue with the missing tests)
Example