go-sdk icon indicating copy to clipboard operation
go-sdk copied to clipboard

Oauth Support / Best practices

Open findleyr opened this issue 7 months ago • 10 comments

Add the hooks and utilities necessary to implement the OAuth aspects of the spec, including the OAuth resource server classification and client-side resource indicators added in the 6/18 version of the spec.

See https://github.com/modelcontextprotocol/go-sdk/issues/19#issuecomment-3352959772

findleyr avatar Jun 24 '25 21:06 findleyr

is there an existing spec discussion on how we plan to implement this? would be happy to contribute if not already work in progress.

gannaramu avatar Jun 27 '25 20:06 gannaramu

I have some work in progress. I should write a doc. Thanks for offering to help. I will probably get to this next week (Thursday and Friday are holidays).

jba avatar Jul 01 '25 12:07 jba

@jba Quick check on oauth since you're already working on this.

I see you already have a PR on the .well-known endpoints. As part of the OAuth 2.1 spec implementation, I'd like to suggest a few considerations for the SDK:

Token Validation

  • First-class support for validating tokens via JWKs (which you may already be considering)
  • A custom validator interface, since tokens from external auth servers aren't always JWTs - they could be JWEs, custom strings/UUIDs, or other formats
  • The custom validator pattern would also enable validation of custom headers beyond just bearer, technically not in scope, but could fall under the Authorization component if people implement non-standard secondary headers such as csrf or transactionID we should make it easy for them.

Client vs Server Flows Since this SDK supports both client and server modes, it would be valuable to consider:

  • As a client: Bearer auth injection when making requests to downstream servers
  • As a server: Passthrough OAuth 2.1 detection when go-sdk is a proxy for upstream requests

Let me know if this is scope creep for the current work - totally fine to push back. Otherwise, I'd be happy to take a crack at proposing some of these features.

RoyceLeonD avatar Jul 17 '25 21:07 RoyceLeonD

I owe you a fuller response to this and on the other issue, but the idea is to use golang.org/x/oauth2, which hopefully has everything you're asking for. What's missing are the three extensions—Protected Resource Metadata, Auth Server Metadata, and Dynamic Client Registration—which we'll implement here.

We've mostly been thinking of it as client authorizing to MCP server, not passthrough. I think we should stick to what the MCP spec asks for first, before we consider any extensions. That is already a fair bit of work.

Since you seem to be pretty knowledgeable about this, are you interested in contributing? If not, that's fine. Feel free to file proposals for any features that go beyond the spec. But we probably won't get to them for a while, and we may choose not to provide them—that would depend a lot on how many people need them, how easy they are to work around, and so on.

jba avatar Jul 17 '25 22:07 jba

Yeh I can lead this. I'll try and get a proposal out this weekend/early next week. And attach it here to bring in golang.org/x/oauth2. I do agree that spec-compatability should be the primary priority. Once I get peer reviews from more folks I can knock out a PR.

RoyceLeonD avatar Jul 17 '25 23:07 RoyceLeonD

Any prospect of SAML2 Support? 👀 I'm willing to work on it.

katesclau avatar Jul 22 '25 21:07 katesclau

@katesclau We are going to stick to implementing the MCP spec.

jba avatar Jul 23 '25 11:07 jba

@jba Quick check on oauth since you're already working on this.

I see you already have a PR on the .well-known endpoints. As part of the OAuth 2.1 spec implementation, I'd like to suggest a few considerations for the SDK:

Token Validation

  • First-class support for validating tokens via JWKs (which you may already be considering)

Do you mean JWTs? This is a server-side concern. I'm not sure that's in scope for an MCP SDK.

  • A custom validator interface, since tokens from external auth servers aren't always JWTs - they could be JWEs, custom strings/UUIDs, or other formats

Why couldn't all validation be done inside the resource handler, which is supplied by the server author anyway? Perhaps one reason is that all details of the transport, like headers, are gone by that point.

  • The custom validator pattern would also enable validation of custom headers beyond just bearer, technically not in scope, but could fall under the Authorization component if people implement non-standard secondary headers such as csrf or transactionID we should make it easy for them.

Perhaps StreamableServerTransportOptions should have a validator callback on it. We could give that the headers, or perhaps the entire http.Response. Would you like to file an issue about that?

Client vs Server Flows Since this SDK supports both client and server modes, it would be valuable to consider:

  • As a client: Bearer auth injection when making requests to downstream servers

Can you elaborate what this means? #176 does bearer injection for the connected MCP server.

Let me know if this is scope creep for the current work - totally fine to push back. Otherwise, I'd be happy to take a crack at proposing some of these features.

I think that even if we ultimately decide that the server side is in scope, I want to finish the client side first. There are still open questions there.

jba avatar Jul 25 '25 18:07 jba

Blocked by #493.

findleyr avatar Sep 18 '25 22:09 findleyr

Trying to summarize what is left for the 6-18 OAuth spec: (WIP, will update as I continue to audit the spec)

Overview Diagram:

Image
  • [x] Extract resource_metadata from WWW-authenticate: WIP #544
  • [ ] Enforce PKCE https://modelcontextprotocol.io/specification/2025-06-18/basic/authorization#authorization-code-protection
  • [ ] Resource Parameter Implementation https://modelcontextprotocol.io/specification/2025-06-18/basic/authorization#resource-parameter-implementation
  • [ ] A full working example of the authorization flow as shown in: https://modelcontextprotocol.io/specification/2025-06-18/basic/authorization#authorization-flow-steps [blocked by the previous issues]
  • [ ] Standard JWT fields #512
  • [ ] Default OAuth Handler

For the draft spec:

  • [ ] OpenID discovery: https://modelcontextprotocol.io/specification/draft/basic/authorization#authorization-server-metadata-discovery

We can probably use some help from the community in completing this work. I will open issues as needed and mark them as help wanted.

Help wanted:

  • [ ] #526
  • [ ] #473

samthanawalla avatar Sep 30 '25 16:09 samthanawalla