OAuth for MCP servers: authenticating AI tools the right way
When you wire an AI assistant into an internal tool through the Model Context Protocol, the path of least resistance is a shared API key dropped into a config file. It works in a demo and it is a liability in production: the key never expires, it carries every scope the service grants, it is copied into logs and prompt context, and when it leaks you cannot tell which client or which user was behind the call. The MCP authorization specification exists precisely to replace that pattern. As of the 2025 revisions, an MCP server is treated as an OAuth 2.1 resource server, and compliant clients are expected to obtain scoped, revocable access tokens rather than present a static secret.
What the MCP authorization model actually says
The specification builds on established RFCs rather than inventing anything new. An MCP server declares itself a protected resource and advertises how to authorize against it. Three pieces do the heavy lifting:
- Protected Resource Metadata (RFC 9728) — the MCP server publishes a
.well-known/oauth-protected-resourcedocument naming the authorization server(s) it trusts and the resource identifier clients must request tokens for. - Authorization Server Metadata (RFC 8414) — the authorization server publishes its endpoints so a client can discover them without hard-coded URLs.
- Dynamic Client Registration (RFC 7591) — because MCP clients are numerous and not known in advance, servers are encouraged to let clients register themselves and receive a client ID programmatically.
Two design decisions matter most. First, the MCP server is a resource server, not an authorization server; you are strongly encouraged to delegate token issuance to an existing identity provider rather than bolt a bespoke OAuth stack onto your tool. Second, tokens must be audience-bound. A token minted for your MCP server must carry a resource (or audience) claim that names that server, so a token stolen from one service cannot be replayed against another. This closes the "confused deputy" gap that plagued early integrations.
The authorization flow end to end
A fresh client with no token calls a tool, the server refuses, and the handshake begins:
GET /mcp HTTP/1.1
Host: tools.example.com
HTTP/1.1 401 Unauthorized
WWW-Authenticate: Bearer resource_metadata="https://tools.example.com/.well-known/oauth-protected-resource"The client fetches that metadata, discovers the authorization server, registers if needed, and runs the Authorization Code flow with PKCE. OAuth 2.1 makes PKCE mandatory for all clients, including confidential ones, so there is no implicit flow and no bare authorization code to intercept. The client then presents the resulting bearer token:
POST /mcp HTTP/1.1
Host: tools.example.com
Authorization: Bearer eyJhbGciOiJSUzI1Ni␣...
Content-Type: application/json
{"jsonrpc":"2.0","method":"tools/call","id":7,
"params":{"name":"create_ticket","arguments":{"title":"Disk full"}}}Your server validates the token the same way any resource server would: verify the signature against the authorization server's JWKS, check exp, confirm the aud/resource claim names this server, and confirm the granted scopes cover create_ticket.
Do not accept a token whose audience is not your server. The single most common MCP authorization bug is validating the signature and expiry but skipping the audience check. A model that holds a valid Google or GitHub token should not be able to spend it against your MCP endpoint. If the aud claim does not match your registered resource identifier, reject with 401 even if everything else is valid.
Scope design for tools
Scopes are where least privilege becomes real. Map scopes to tool capabilities, not to broad resource categories. A read-only reporting tool should be reachable with reports:read and nothing more; a tool that mutates infrastructure should demand a distinct scope that an operator consciously grants.
{
"scopes_supported": [
"tickets:read",
"tickets:write",
"deploy:staging",
"deploy:production"
]
}Keep access-token lifetimes short — minutes, not days — and issue refresh tokens so a long-running agent session can continue without a human in the loop while still giving you a revocation point. When a user offboards or an agent misbehaves, revoking the grant at the authorization server stops every token derived from it.
Validate at the edge, authorize in the handler
Token validation (signature, expiry, audience) belongs in middleware that runs before any tool dispatches. Scope-to-action authorization belongs in the tool handler, because only the handler knows which resource instance is being touched. Centralizing both in middleware tempts you to write coarse rules that pass tokens with more authority than the specific call needs.
Common mistakes worth avoiding
- Treating the MCP server as its own IdP. You end up maintaining user records, password resets, and an OAuth endpoint inside a tool server. Delegate to a real authorization server.
- Static long-lived tokens "just for the agent." An agent is exactly the client you least want holding a non-expiring credential, because its inputs are attacker-influenceable.
- Logging the Authorization header. Request logging that captures headers will silently archive bearer tokens. Strip them.
- Ignoring token passthrough. The spec explicitly forbids an MCP server from forwarding a client's token to an upstream API as if it were its own. If your tool calls a third party, it uses its own credential for that hop.
OAuth for MCP is not ceremony for its own sake; it is the difference between an AI integration you can reason about and one you simply have to trust. Scoped, audience-bound, revocable tokens let you say exactly what an agent may do and withdraw it in one move. When the authorization server issuing those tokens is one you run yourself, that revocation point, the scope catalog, and the audit record all stay under your roof rather than a vendor's — which is the whole argument for owning your auth. If you want the OAuth and scope primitives without building them from RFCs, the developer docs cover the resource-server side.
Run it yourself
Bastionary is free, open-source software. Follow the quick start to have it running on your own server, or browse the releases on Git.