mcpAuthentication policy handles token validation, exposes OAuth resource metadata, and optionally adapts non-spec-compliant identity providers.
How it works
WhenmcpAuthentication is configured on a route:
- The gateway exposes an OAuth protected resource metadata endpoint at
/.well-known/oauth-protected-resource/<path> - Unauthenticated requests receive
401 Unauthorizedwith aWWW-Authenticateheader pointing to the metadata endpoint - MCP clients follow the OAuth flow to obtain a token from your authorization server
- Requests with a valid bearer token are forwarded to the upstream MCP server
Provider scenarios
- Spec-compliant server
- Keycloak
- Auth0
- Remote MCP over HTTPS
Use this configuration when your authorization server fully implements the MCP Authorization spec. Agentgateway acts as the resource server and validates tokens directly.The route must also match the well-known metadata path:
JWKS configuration
The gateway loads JSON Web Key Sets (JWKS) either from a URL or from a local file:JWT validation options
By default, theexp (expiration) claim is required in every token. You can customize which RFC 7519 registered claims must be present using jwtValidationOptions.requiredClaims.
Only the following claim names are recognized: exp, nbf, aud, iss, sub. Any other value is silently ignored.
This setting only enforces presence. Standard claims like
exp are always validated when present — an expired token is rejected regardless of requiredClaims.Running the authentication example
1
Start the demo dependencies
The authentication example uses Keycloak and a mock authorization server. Start them with:This starts the mock authorization server on
http://localhost:9000 and Keycloak on http://localhost:7080.2
Start the gateway
3
Test unauthenticated access
A request without a token should return The response includes a
401 Unauthorized:WWW-Authenticate header with a link to the resource metadata endpoint.4
Test with MCP Inspector
http://localhost:3000/stdio/mcp. The MCP Authorization flow starts automatically after the initial 401 response. For Keycloak, use credentials testuser / testpass.What the provider adapter does
When you set aprovider, the gateway acts as an Authorization Server facade for MCP clients:
- Exposes resource metadata at
/.well-known/oauth-protected-resource/... - Exposes authorization server metadata at
/.well-known/oauth-authorization-server/...— pointing back to the gateway itself - Fetches the real AS metadata from your
issuerand rewrites it per-provider to smooth over protocol gaps - Proxies client registration (Keycloak only) at
.../client-registration
provider block entirely when your authorization server is already spec-compliant.
Troubleshooting
- Ensure
issuermatches theissclaim in your tokens exactly. - Ensure each entry in
audiencesmatches theaudclaim clients request. - Verify the resource metadata is reachable at
/.well-known/oauth-protected-resource/...and that theresourcevalue matches theaudiencesentry. - Check that the JWKS URL is accessible from the gateway process.