docs(examples): add external auth resource server example (closes #658)#1967
Conversation
|
|
Note on the changeset-bot warning: this PR only modifies the |
8f8f3ee to
0df3379
Compare
…elcontextprotocol#658) New `examples/server/src/externalAuthStreamableHttp.ts` shows the production OAuth pattern where the MCP server is a pure resource server that validates JWT bearer tokens minted by an external Authorization Server (Auth0, Okta, Keycloak, Entra ID, Cognito, in-house IdP, ...) via JWKS. RFC 8707 audience binding and RFC 9728 Protected Resource Metadata are demonstrated. No DIY OAuth server code is added; trust anchors come from environment variables. Also adds a row to `examples/server/README.md` and pulls `jose` from the existing `runtimeClientOnly` catalog into the examples-server package.
0df3379 to
b1cdd1f
Compare
@modelcontextprotocol/client
@modelcontextprotocol/server
@modelcontextprotocol/express
@modelcontextprotocol/fastify
@modelcontextprotocol/hono
@modelcontextprotocol/node
commit: |
Why
Closes #658.
The current server examples either run without auth or co-locate an Authorization Server with the MCP server (
simpleStreamableHttp.ts --oauth). In production, the AS is almost always a separate system (Auth0, Okta, Keycloak, Entra ID, AWS Cognito, an in-house IdP, ...) and the MCP server is a pure OAuth 2.0 resource server that validates incoming bearer tokens. Issue #658 asks for an example that demonstrates that pattern. None exists today.A previous attempt (#1693) was closed because it shipped a 500+ line DIY OAuth server. This PR takes the opposite approach: no new auth server code, just one focused resource-server example that points at any external AS via env vars.
What
New file
examples/server/src/externalAuthStreamableHttp.ts(~310 LOC) - a Streamable HTTP MCP server that:jose.createRemoteJWKSet+jwtVerify)MCP_AUDIENCE)/.well-known/oauth-protected-resource/<path>so clients can auto-discover the ASWWW-Authenticateheader that points at the metadata documentreq.authin the SDK's canonicalAuthInfoshape so the SDK threads it intoctx.http?.authInfofor tool handlerswhoamirequiresmcp:read,echorequiresmcp:write)scopestring,scparray)examples/server/README.md- adds a row to the example index and a short "External Authorization Server (resource-server pattern)" section with a copy-pasteable Auth0 invocation.examples/server/package.json- pullsjosefrom the existingruntimeClientOnlycatalog (no new lockfile entry;jose@^6.1.3is already present transitively).No changes to SDK packages. No new shared utilities. Examples package is in
.changeset/config.json's ignore list, so no changeset is needed.Tested
packages/serverandpackages/middleware/node(AuthInfo,ServerContext.http?.authInfo,req.authflow)http://localhost:3000/mcpandhttps://api.example.com/mcp