Protect your MCP server with AuthWAPI
Your MCP server can use AuthWAPI as its authorization server. An MCP client finds AuthWAPI from your server’s
metadata, signs the user in through AuthWAPI’s hosted pages, and gets an access token that only your server accepts.
This is separate from AuthWAPI’s own admin MCP at BASE_URL/mcp, which never accepts OAuth tokens.
| Authorization server metadata | BASE_URL/.well-known/oauth-authorization-server (also openid-configuration) |
| Keys (JWKS) | BASE_URL/api/auth/jwks |
| Client registration | Client ID Metadata Documents (client_id_metadata_document_supported: true); dynamic client registration is off |
| Flow | Authorization code, PKCE with S256 required, resource = your MCP server’s URL |
| Scopes | offline_access for refresh tokens; openid and email if the client wants them |
1. Register your MCP server and the clients allowed to use it
Section titled “1. Register your MCP server and the clients allowed to use it”The resource is your MCP server’s URL, exactly as clients send it in resource= (for example
https://mcp.example.com/mcp):
docker exec -u 1000 authwapi node src/cli.ts resource add https://mcp.example.com/mcpA client gets no token for a resource it isn’t linked to, so link each MCP client you allow. An MCP client’s
client_id is the https URL of its metadata document:
docker exec -u 1000 authwapi node src/cli.ts resource link https://client.example/oauth/metadata.json https://mcp.example.com/mcpLinking fetches nothing. AuthWAPI uses a client’s metadata document only while the client is linked to at least
one resource: it fetches the document when a user signs in through the client, and refuses any other client_id
URL without a request going out. The URL must be https on a public host, with a path and no fragment or
credentials; loopback and private addresses are always refused. The fetch resolves DNS once, refuses private
addresses, follows no redirects, gives up after 5 seconds and accepts at most 5 KB of JSON.
To take a client’s access away, unlink it. From then on a client linked to no resource gets no new sign-in, and its refresh tokens stop working, even if its document is cached. JWT access tokens it was already issued stay valid at your MCP server until they expire (at most 15 minutes).
docker exec -u 1000 authwapi node src/cli.ts resource unlink https://client.example/oauth/metadata.json https://mcp.example.com/mcpThe consent page leads with the host of the metadata URL, which is where the client really comes from, and then the name the document gives itself: client.example (calls itself “Example MCP Client”) wants to…
The metadata document
Section titled “The metadata document”The client serves it as JSON (Content-Type: application/json) at its client_id URL:
{ "client_id": "https://client.example/oauth/metadata.json", "client_name": "Example MCP Client", "redirect_uris": ["http://localhost:33418/callback", "https://client.example/oauth/callback"], "token_endpoint_auth_method": "none"}client_id: exactly the URL the document is served from.client_name: required, non-empty.redirect_uris: required, non-empty. Each one must be on theclient_id’s origin, on a loopback host (localhost,127.0.0.1,[::1]), or a native app’s private-use scheme (com.example.app:/callback). A document can’t move its callbacks to another origin.post_logout_redirect_urisfollow the same rule, andclient_urimust be on theclient_id’s origin.token_endpoint_auth_method:none(the default; a public client using PKCE) orprivate_key_jwt. A shared secret (client_secret_*,client_secret) is refused.private_key_jwtneeds the client’s public keys: eitherjwksinline (public keys only) orjwks_uri. Ajwks_urimust behttps, on theclient_id’s origin (or on AuthWAPI’s ownBASE_URL/ALLOWED_HOSTSorigins), and is fetched only for a linked client, through the same SSRF-safe fetch as the document.
Clients without a metadata document
Section titled “Clients without a metadata document”A client on localhost can’t publish a metadata document AuthWAPI will fetch. If it runs on a server that can keep
a secret, register it with clients create instead: that makes a confidential client that authenticates with a
client secret (client_secret_basic), as described in Integrate an app. Then
resource link <its client_id> <resource> links it the same way. Don’t use this for a desktop or CLI app that
would have to ship the secret.
2. Publish your server’s metadata
Section titled “2. Publish your server’s metadata”Serve RFC 9728 protected-resource metadata, and point at it from every 401:
GET /.well-known/oauth-protected-resource
{"resource": "https://mcp.example.com/mcp", "authorization_servers": ["http://localhost:4000"]}HTTP/1.1 401 UnauthorizedWWW-Authenticate: Bearer resource_metadata="https://mcp.example.com/.well-known/oauth-protected-resource"resource must equal the identifier you registered, and authorization_servers holds your BASE_URL.
3. Check every token
Section titled “3. Check every token”Access tokens are JWTs (typ: at+jwt) that last 15 minutes. Your server must check the signature, iss, exp
and that aud includes its own URL. A token minted for another resource, or with no aud at all, must be
refused: that is what stops a token for one MCP server working on another. A token with a cnf claim is bound to
a key (DPoP) and is valid only with a proof from that key; a server that takes plain bearer tokens must refuse it.
With
jose:
import { createRemoteJWKSet, jwtVerify } from 'jose';
const AUTHWAPI = 'http://localhost:4000';const jwks = createRemoteJWKSet(new URL(`${AUTHWAPI}/api/auth/jwks`));
export async function verifyAccessToken(token) { const { payload } = await jwtVerify(token, jwks, { issuer: AUTHWAPI, audience: 'https://mcp.example.com/mcp', // this server; a token without aud fails too typ: 'at+jwt', requiredClaims: ['exp'], }); if (payload.cnf !== undefined) throw new Error('DPoP-bound token: this server accepts bearer tokens only'); return payload; // payload.sub is the user, payload.scope the granted scopes}Don’t pass the token on to other APIs: it is for your server only. A JWT stays valid until it expires (15 minutes), even after the user signs out.
Refresh
Section titled “Refresh”Refresh tokens rotate on every use, with no grace window: presenting a used refresh token fails with
invalid_grant and revokes the rest of that client’s refresh tokens for the user.