How to add OAuth sign-in to your MCP server

Doha I. · Published · Lire en français

How to add OAuth sign-in to your MCP server

An MCP server that acts on user data is an OAuth resource server. It publishes protected resource metadata (RFC 9728) that names an authorization server, answers 401 when a request has no token, and verifies the JWT access token on every request, including that the token's audience is its own URL. The authorization server does the rest: client registration, sign-in, consent and tokens. With SyAuth you register the server, publish one JSON file and add a token check.

AI assistants are becoming a new front door to software. When Claude calls a tool on your MCP server to read an invoice or create a ticket, it acts for a real user, so your server must know who that user is and what they agreed to. The MCP specification answers this with standard OAuth. The good news: almost all of the work belongs to the authorization server, and you do not have to write one.

What the MCP authorization flow looks like

  1. The assistant calls your MCP endpoint without a token. Your server answers 401 with a WWW-Authenticate header that points to its protected resource metadata.
  2. The assistant reads that metadata (RFC 9728) and finds the authorization server it names.
  3. It reads the authorization server's metadata (RFC 8414) and identifies itself: with the https URL of its client ID metadata document, the method the current MCP specification recommends, or by registering (dynamic client registration, RFC 7591).
  4. It opens the sign-in page in the user's browser with the authorization code flow, S256 PKCE and a resource parameter equal to your server URL (RFC 8707).
  5. The user signs in and approves access. The assistant exchanges the code for an access token and a refresh token.
  6. Every later call to your MCP server carries Authorization: Bearer <token>, and your server verifies it.

What is yours, and what is not

Your MCP server only does steps 1 and 6. Steps 2 to 5 (identifying unknown clients safely, sign-in, MFA, consent, issuing and rotating tokens) are the job of the authorization server. That is the part teams underestimate: it is a full OAuth server exposed to clients you have never seen, and it must stay secure as the specification evolves.

Protect an MCP server with SyAuth in four steps

1. Register the server. In the SyAuth dashboard, open MCP Servers and add your server's name and exact URL, for example https://mcp.example.com/mcp. Sign-up while connecting is off by default, so only existing users of your workspace can connect.

2. Publish the metadata. Serve this JSON at https://mcp.example.com/.well-known/oauth-protected-resource/mcp:

{
  "resource": "https://mcp.example.com/mcp",
  "authorization_servers": ["https://api.syauth.com/e/v1/mcp"],
  "bearer_methods_supported": ["header"]
}

3. Answer 401 without a valid token:

WWW-Authenticate: Bearer resource_metadata="https://mcp.example.com/.well-known/oauth-protected-resource/mcp"

4. Verify every token. SyAuth issues RS256 JWT access tokens that last one hour. Check them locally with the published keys, with no call to SyAuth per request:

import jwt

jwks = jwt.PyJWKClient("https://api.syauth.com/e/v1/.well-known/jwks.json")

def verify(token: str) -> dict:
    key = jwks.get_signing_key_from_jwt(token)
    return jwt.decode(token, key.key, algorithms=["RS256"],
                      audience="https://mcp.example.com/mcp",
                      issuer="https://api.syauth.com/e/v1/mcp")

The token's sub is the user's SyAuth ID: use it as your user key. The guide has the TypeScript version and the full list of claims.

Then add your server URL as a custom connector in Claude, or open it in the MCP Inspector. You should see your hosted login, then a consent page naming the assistant and your MCP server, then your tools.

Mistakes to avoid

  • Not checking the audience. A valid SyAuth signature is not enough: a token issued for another MCP server must be rejected. Compare aud with your exact URL.
  • Passing the token on. Never forward the assistant's token to another API. If your tools call other services, they use their own credentials. The MCP specification forbids this "token passthrough".
  • Long-lived tokens. Keep access tokens short. SyAuth's last one hour, and assistants renew them with a refresh token without asking the user again.
  • Only one metadata address. Some clients look for /.well-known/oauth-protected-resource at the root, others insert your endpoint path. Serve both.
  • Open sign-up by accident. Decide whether anyone may create an account through an assistant. In SyAuth, it is a per-server switch that is off by default.

Why SyAuth for this

SyAuth supports both ways assistants identify themselves, so Claude and Claude Code connect without setup on your side; the consent page shows the assistant's domain next to its name. Your users sign in on the same branded hosted login as in your apps, with the sign-in methods your workspace already offers, social login included. MCP server sign-in is included in every plan, from the free plan with 30,000 registered users, and assistants' registrations never count toward your application quota. Disabling a server in the dashboard stops new sign-ins and refreshes at once.

Good to know: accounts served from an existing user database (Enterprise) cannot sign in to MCP servers yet.

Read more on the MCP server sign-in feature page, or create a free account and register your first MCP server.

Free for up to 30,000 registered users

Start on the free plan and upgrade when your project grows.

How to add OAuth sign-in to your MCP server | SyAuth