-
Notifications
You must be signed in to change notification settings - Fork 17
Pass AuthInfo from middleware to Mcp Server + Error handling #36
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
hey @allenzhou101 thanks for the PR! looks good codewise, and checked out the linked PR's -- makes sense! wondering if you have a recommended way to minimally test this? when i try this out -- I don't see the global |
Hey @quuu! Thanks for the review! I tested it using this PR/branch of the MCP Inspector that supports the draft auth spec: modelcontextprotocol/inspector#469 On the MCP Server side I used Descope as the OAuth IdP. One weird IdP-specific note was that the AS Issuer included in the Protected Resource Metadata was used to construct the Authorization Server Metadata weirdly, as mentioned here, but there's a straightforward workaround. |
And I focused on testing Streamable HTTP in this PR. Also, here's the server implementation I'm testing with: https://github.com/allenzhou101/mcp-server-nextjs |
Switched from resource metadata param from path to url since the prm spec doesn't mandate that the url be relative to the root/origin. Also, wonder if the types from the Anthropic MCP TS SDK should be exported out of this to the fullest extent possible so only installing this package is required, even for more advanced use cases. |
Probably the adapter should also handle hosting the PRM well known, but can be a different PR |
i was wondering this too, but to abide by the .well-known/... pattern it wouldn't be that easy to route everything together through this overall this looks pretty good! was wondering if the |
It should be agnostic to any auth provider, since it really just requires the |
makes sense -- looks great! I just merged another PR into main: #39 and it had some conflicts, mind rebasing on top of it -- specifically to add in your more robust error handling? |
@quuu Interesting. Why the choice in #39 to keep only the
That way this one piece of boilerplate that everyone needs with auth wouldn't have to be reimplemented. |
good point, im open to including the token in the signature -- it was removed to give control to the server developer to decide how to handle the token, as some auth services have more complex than string tokens (several strings or objects) |
maybe overloading is a good compromise?
Would say token only should account for most use cases and the req can be included for more advanced? |
I think extracting token from the
|
But also to reiterate: it's good to have defaults. |
@allenzhou101 as an added compromise, how about this signature:
so it's explicit that it's Bearer i agree with you that based on the official spec, Bearer tokens should be first class supported: |
I agree at large, but with MCP in particular, the official spec defines the auth header in this way as the intended mechanism, so I'm inclined to support it and throw errors by default, maybe with a way to override and prevent. However, I suppose ultimately either way is probably fine. |
This looks good to me! cc @dvoytenko |
@allenzhou101 can you run |
@quuu Done! |
Thanks for contributing @allenzhou101 ! |
Will update here after I test + bump the package version |
Thank y'all for reviewing! @quuu @dvoytenko |
Deployed! Try the latest 0.8.3 @allenzhou101 |
Awesome! Looks great. |
One note while using this is that maybe the 'required' field shouldn't exist, or it should default true--probably the latter. I'd expect token verification to be required by default when I'm adding the auth function. This is how I've seen it work in the official implementation as well. |
A common use case when building Mcp Servers is to pass the auth info from the OAuth token through to the Mcp Server tool call. This PR adjust the middleware to set the
auth
object on the request so that it's automatically handled, as implemented in the Anthropic Mcp TS SDK here and here.Also, this PR makes the error handling of the
withMcpAuth
function more robust, accounting for insufficient scopes, etc. The return type of thewithMcpAuth
function is adjusted to be theAuthInfo
object instead of boolean to support this case.