By Abhijith N M · Reviewed 24 October 2026 · Editorial standards
Your MCP server holds data and performs actions. When a client makes a request, the server needs to know who is actually calling and whether that user granted permission for what the client is asking. Without authorization, any client can access anything. MCP uses OAuth 2.1 to solve this, rather than building a custom auth system.
Three parties participate in every OAuth flow. The MCP server acts as the resource server—it owns the data and decides whether to grant access based on tokens. The MCP client, running inside the host application, acts as the OAuth client on behalf of the user. The authorization server (Auth0, Okta, Entra ID, Cognito, or similar) handles user login and token issuance. Usually the authorization server is separate from your MCP server.
The flow starts when the client makes an unauthenticated request. The server responds with a 401 and metadata pointing to the authorization server. The client discovers endpoints, opens a browser for the user to sign in and grant consent, receives an authorization code, exchanges that code for an access token, then retries the original request with the Bearer token. The server validates the token's signature, expiry, audience, and scopes before responding.
Use the authorization-code flow with PKCE. Never put tokens in URLs. This protects against token leakage through browser history or logs.
Bind tokens to your audience. The authorization server must issue the token for your specific server, not for some other service. Your server must reject any token that was issued for a different audience. This prevents an attacker who steals a token meant for one API from using it against yours.
Never forward the client's token downstream. If your server needs to call another API, obtain your own credentials for that API. Forwarding the user's token creates a confused-deputy vulnerability—the downstream API cannot tell whether the user actually authorized that call.
Request only the scopes you need. Challenge for additional scopes only when a specific tool requires them. This limits the blast radius if a token is compromised.
Validate the issuer. The authorization server includes an iss parameter in responses. Your client must check this against the issuer it recorded during setup. Store credentials by issuer—if you switch authorization servers, re-register.
Historically, clients used Dynamic Client Registration (DCR) to register themselves with each authorization server. As of 2026-07-28, DCR is deprecated. Instead, the client's identifier is an HTTPS URL that hosts a small JSON document describing the app. This Client ID Metadata Document approach is simpler and more portable. DCR remains available only for authorization servers that cannot yet support the new method.
Local stdio servers run on the user's machine and typically skip OAuth entirely. They read credentials from environment variables or the OS keyring—the user already controls that machine, so additional OAuth layers are unnecessary.
Many teams run an API gateway in front of MCP servers to centralize auth (for example, Amazon Bedrock AgentCore Gateway). Even in this setup, your server must still validate the audience and scopes it is willing to accept. Do not assume the gateway has done all the work.
Written and edited by the nexGenCoders team. Spotted a mistake? Tell us and we will fix it.
Don't lose this lesson
Create a free account to save your progress, keep your streak, and work toward a shareable certificate.