---
title: How to add OAuth to a remote MCP server
description: Publish protected resource metadata, return 401 with WWW-Authenticate, and accept only tokens whose audience is your server. What the auth server needs.
canonical: https://mcp1.com/blog/add-oauth-to-mcp-server
last-updated: 2026-11-24
---

# How to add OAuth to a remote MCP server

Publish protected resource metadata, return 401 with WWW-Authenticate, and accept only tokens whose audience is your server. What the auth server needs.

![](https://avatars.githubusercontent.com/u/1297371?s=96&v=4)Eldad FuxFounder and CEO, Appwrite

 POST /mcp

HTTP/1.1 401 Unauthorized
WWW-Authenticate: Bearer
  resource_metadata=".../oauth-protected-resource/mcp"

aud = https://mcp.example.com/mcpValid

 POST /mcp

HTTP/1.1 401 Unauthorized
WWW-Authenticate: Bearer
  resource_metadata=".../oauth-protected-resource/mcp"

aud = https://mcp.example.com/mcpValid

To add OAuth to a remote MCP server, make the server an OAuth resource server: publish protected resource metadata that names your authorization server, answer unauthenticated requests with a 401 that points to it, and accept only tokens issued for your server’s URL. The sign-in itself happens at an authorization server, which is usually an identity provider you already run, not code in the MCP server.

## What does the MCP spec require for OAuth?

The spec splits the work in two. The MCP server checks tokens; a separate authorization server issues them. Clients discover everything from URLs, so the parts have to line up:

PartWhat it must do

MCP server

Publish RFC 9728 metadata, return 401 with WWW-Authenticate, validate each token’s audience, never forward the token upstream.

Auth server

OAuth 2.1 with PKCE (S256), RFC 8414 or OpenID Connect discovery, and a way for clients to get a client ID.

Client

Find the auth server from your metadata, register or identify itself, send resource=<your URL> when asking for tokens.

The current revision, 2026-07-28, deprecates dynamic client registration in favor of client ID metadata documents. Clients still fall back to it, so keep it if your provider offers it.

## How do clients find my authorization server?

From your protected resource metadata. Serve it at the well-known path with your endpoint’s path appended, so a server at `/mcp` serves `/.well-known/oauth-protected-resource/mcp`. Clients also try the root path, `/.well-known/oauth-protected-resource`, so serve both if you can:

GET /.well-known/oauth-protected-resource/mcp

```
{
  "resource": "https://mcp.example.com/mcp",
  "authorization_servers": ["https://auth.example.com"],
  "scopes_supported": ["tickets:read", "tickets:write"],
  "bearer_methods_supported": ["header"]
}
```

`resource` must be the exact URL clients connect to, and `authorization_servers` needs at least one issuer. Leave `offline_access` out of `scopes_supported`; the spec asks servers not to list it.

## What should the 401 response look like?

A request with no token, or an expired or invalid one, gets a 401 with a `WWW-Authenticate` header carrying the metadata URL and, ideally, the scope the request needs:

Response

```
HTTP/1.1 401 Unauthorized
WWW-Authenticate: Bearer resource_metadata="https://mcp.example.com/.well-known/oauth-protected-resource/mcp", scope="tickets:read"
```

Clients treat the scope in that challenge as authoritative and fall back to `scopes_supported` when it’s missing. When a valid token lacks a scope, answer 403 with `error="insufficient_scope"` and list every scope the operation needs in one challenge, so the client can ask once instead of in rounds.

## How do I validate the access token?

Check the signature, the issuer, the expiry, and above all the audience. The token must name your server’s URL in `aud`, because a token minted for some other resource from the same identity provider would otherwise work on yours. Clients send `resource=https://mcp.example.com/mcp` (RFC 8707) in the authorization and token requests, and your authorization server has to put that value in the token. A middleware with `jose`:

auth.ts

```
import { createRemoteJWKSet, jwtVerify } from 'jose'

const RESOURCE = 'https://mcp.example.com/mcp'
const ISSUER = 'https://auth.example.com'
const METADATA = 'https://mcp.example.com/.well-known/oauth-protected-resource/mcp'
const jwks = createRemoteJWKSet(new URL(ISSUER + '/.well-known/jwks.json'))

function challenge(status: 401 | 403, extra = '') {
  return new Response(null, {
    status,
    headers: { 'WWW-Authenticate': `Bearer resource_metadata="${METADATA}"${extra}` },
  })
}

export async function authenticate(req: Request, needs = 'tickets:read') {
  const token = req.headers.get('authorization')?.match(/^Bearer (.+)$/)?.[1]
  if (!token) return challenge(401, `, scope="${needs}"`)
  try {
    // audience: the token must have been issued for this server, not any server
    const { payload } = await jwtVerify(token, jwks, { issuer: ISSUER, audience: RESOURCE })
    const scopes = String(payload.scope ?? '').split(' ')
    if (!scopes.includes(needs)) return challenge(403, `, error="insufficient_scope", scope="${needs}"`)
    return { user: payload.sub, scopes }
  } catch {
    return challenge(401, `, error="invalid_token"`)
  }
}
```

Three more rules from the spec that are easy to miss:

- Accept the token only in the Authorization header, on every request. Never in the query string.
- Never pass the client’s token to an upstream API. If your server calls another service, it uses its own credential for that service.
- Return 401, not 403 or 500, for an expired token, so clients know to refresh and retry.

## Which authorization server should I use?

Use one you already have rather than writing one. The questions that decide whether a provider works with MCP clients:

1. 1Does it publish metadata at `/.well-known/oauth-authorization-server` or `/.well-known/openid-configuration`, with an `issuer` that exactly matches the URL in your resource metadata? Clients drop metadata whose issuer doesn’t match.
2. 2Does it list `S256` in `code_challenge_methods_supported`? Clients are told to refuse a server that doesn’t advertise PKCE support.
3. 3How do clients get a client ID? Client ID metadata documents (`client_id_metadata_document_supported`), dynamic registration (`registration_endpoint`) or neither. With neither, every user has to paste a pre-registered client ID into their client.
4. 4Can it set the token audience from the `resource` parameter? If it can only issue tokens for a fixed audience, configure that audience as your server’s URL.

Redirect URIs

If your provider makes you allowlist redirect URIs, add the clients you expect. Claude uses `https://claude.ai/api/mcp/auth_callback`. Claude Code and other local clients listen on `http://localhost` with a port that changes unless the user fixes it.

## How do I test MCP OAuth before shipping?

Run the official MCP Inspector, enter your server URL, and use its guided OAuth flow to step through discovery, registration and the token exchange one request at a time:

Terminal

```
npx @modelcontextprotocol/inspector
```

Then try a real client. In Claude Code, add the server and run `/mcp` to sign in:

Terminal

```
claude mcp add --transport http tickets https://mcp.example.com/mcp
```

If your provider has no client ID metadata documents and no dynamic registration, Claude Code takes a pre-registered client instead:

Terminal

```
claude mcp add --transport http --client-id <client-id> --client-secret \
  --callback-port 8080 tickets https://mcp.example.com/mcp
```

Leaving `--client-secret` without a value makes Claude Code prompt for it. Test an expired token too: the server should return 401 and the client should refresh without asking the user to sign in again.

## Skip the OAuth work with a gateway

If your server is internal, or today it takes an API key, you don’t have to build any of this. MCP1 is a gateway that signs people in with OAuth 2.1, including PKCE and dynamic client registration, and holds the server’s key encrypted on the other side. Add the server to a workspace by URL with its key, share a gateway with a group, and every client connects to `https://mcp1.com/mcp/<workspace>/<gateway>` with its own user’s sign-in. Each call is checked against your rules and recorded in the audit log, with the person’s name rather than one shared key. [How we built MCP1 on Appwrite](https://mcp1.com/blog/how-we-built-mcp1-on-appwrite) describes our own authorization server, the [docs](https://mcp1.com/docs) cover adding servers, and you can [start free](https://mcp1.com/signup).

## Common questions

### Do I have to build my own OAuth server for MCP?

No. The MCP server only validates tokens. Any authorization server that supports OAuth 2.1 with PKCE, publishes discovery metadata and can issue tokens for your server’s URL works.

### Is dynamic client registration still required for MCP?

No. The 2026-07-28 revision deprecates it in favor of client ID metadata documents, and pre-registered clients are also allowed. Clients still fall back to it when an authorization server offers it.

### Can an MCP server forward the user’s token to another API?

No. The spec forbids token passthrough. The server must accept only tokens issued for itself and use its own credential for any upstream service.

### Where should protected resource metadata live?

At /.well-known/oauth-protected-resource followed by your endpoint’s path, like /.well-known/oauth-protected-resource/mcp, or at the root path. Pointing to it from the 401 WWW-Authenticate header covers both.

Give your team one URL

Create a workspace, install your servers and share a gateway with a group. Free for individuals.

[Start with MCP1](https://mcp1.com/signup)[Read the docs](https://mcp1.com/docs)

## More from the blog

[ Terminal$ claude mcp add --transport http sentry mcp.sentry.dev/mcp/acme/websentryAuthenticatedHow to connect Sentry MCP to Claude CodeRun claude mcp add with https://mcp.sentry.dev/mcp/<org>/<project>, then /mcp to sign in. Skills, permission rules, tokens for CI and self-hosted Sentry.![](https://avatars.githubusercontent.com/u/1297371?s=96&v=4)Eldad FuxFounder and CEO, Appwrite](https://mcp1.com/blog/sentry-mcp-claude-code)[ \~/.codex/config.toml[mcp_servers.github] url = "https://api.githubcopilot.com/mcp/" bearer_token_env_var = "GITHUB_PAT"githubEnabledHow to connect GitHub MCP to CodexRun codex mcp add with https://api.githubcopilot.com/mcp/ and --bearer-token-env-var. Read-only mode, toolsets, approvals and a project config.![](https://avatars.githubusercontent.com/u/1297371?s=96&v=4)Eldad FuxFounder and CEO, Appwrite](https://mcp1.com/blog/github-mcp-codex)

---

This is the markdown copy of https://mcp1.com/blog/add-oauth-to-mcp-server. Site index for agents: https://mcp1.com/llms.txt
