MCP & agents

MCP authorization: protecting an MCP server with OAuth 2.1

DEV: mcp GeekCoding101.com
MCP & agents

The MCP spec treats an HTTP server as an OAuth 2.1 resource server: it publishes Protected Resource Metadata, answers unauthenticated calls with a 401, and validates bearer tokens. A hands-on demo with the Python SDK shows what the library handles and what you still own.

Key points

  • HTTP transports should act as an OAuth 2.1 resource server; stdio servers should read credentials from the environment instead.
  • Unauthenticated calls get a 401 whose `WWW-Authenticate` header carries the `resource_metadata` URL to follow next.
  • The server publishes Protected Resource Metadata (RFC 9728) naming its `authorization_servers`, supported scopes and bearer methods.
  • Clients must use PKCE with S256 and send the RFC 8707 `resource` parameter on both authorization and token requests.
  • An invalid or expired token gets 401; a valid token with too few scopes gets 403 with `insufficient_scope`.
  • In the Python SDK (mcp 2.3.0), a `TokenVerifier` plus `AuthSettings` wires the metadata route and the bearer middleware.

Why it matters

If you have only run MCP servers over stdio, authorization never crossed your mind: credentials came from the environment and the process boundary did the rest. Once the same tools sit behind an HTTP port, they need the OAuth 2.1 machinery of a web API, including the audience checks most people skip.

The list is short: point `issuer_url` at your identity provider, confirm it advertises PKCE with S256, and swap the string comparison for JWT or introspection validation. Keep audience and scope checks on every request, set `validate_token_resource` explicitly because it becomes the default in 3.0, and test discovery at the path-suffixed well-known URL for your endpoint — the root will 404.

Real-world use

  • An MCP server exposed on an HTTP port that only selected clients may call
  • A reverse engineering setup built on Ghidra MCP, where powerful local tooling must not be open to any caller
  • A client that has to run the 401 → metadata → issuer discovery → PKCE sequence against your identity provider
  • A tool that calls an upstream API and therefore needs its own credential instead of the caller's token

This summary was generated by the agent from the original post on DEV: mcp. The full text and all rights belong to its author and publisher. Go to original

Report this story

Is something wrong with the summary, or do you own this content? Your message comes straight to my inbox.

Type of message

Related stories

Feedback

Found a bug or have an idea? Your message lands straight in my inbox.

Type of message