MCP servers.
Protect an MCP server with tokens bound to it, connect Claude Code, or verify the JWTs your own sign-in issues.
On this page
oauth.Config.Resources registers an MCP server as a resource (RFC 8707), and the provider binds every token for it to that resource. mcpauth protects the MCP route and accepts only tokens for its resource.
To serve the MCP server in the same process as your app, route its path beside the authhttp.Mux, on a parent http.ServeMux:
root := http.NewServeMux()
protector.Register(root, "/mcp", mcpServer, nil) // protector from mcpauth.New
root.Handle("/", mux) // your authhttp.MuxDon’t register /mcp behind the Mux. The Mux authenticates every request and refuses a token bound to a resource with 401 unless authhttp.WithResource lists that resource. The refusal’s detail says the token is for a different resource, and the log names the resource. Add authhttp.WithResource(uri) only when your app’s own routes should accept the MCP server’s tokens too.
MCP clients get tokens with the authorization code flow and PKCE. The device flow doesn’t issue tokens for a resource: a device authorization request with resource gets invalid_target. See examples/mcpserver.
Connect Claude Code to the example
-
Start the server:
Shellgo run ./examples/mcpserverIt listens on
localhost:8080and useshttp://localhost:8080as its issuer. The provider acceptshttponly forlocalhost. -
Add the server to Claude Code:
Shellclaude mcp add --transport http notes http://localhost:8080/mcp -
In Claude Code, run
/mcp, choosenotes, and choose to authenticate. Claude Code reads the Protected Resource Metadata from the401challenge, finds the authorization server, and identifies itself with its client ID metadata document,https://claude.ai/oauth/claude-code-client-metadata. The default CIMD allowlist admitsclaude.ai, so the server fetches that document and needs no registration. -
Your browser opens the sign-in page. Sign in with any name.
-
The consent page shows:
- the client’s name, Claude Code, which is its own claim, and its verified host,
claude.ai; - the host the approval goes to,
localhost, with a warning, because any program on your computer can listen on a loopback address; - the resource, Notes at
http://localhost:8080/mcp; - the scopes,
notes:readandnotes:write.
Choose Allow. Every approval is recorded before the code is issued, and a loopback redirect never skips this page.
- the client’s name, Claude Code, which is its own claim, and its verified host,
-
Ask Claude Code to add a note and list your notes. It calls
notes_addandnotes_listwith a token whose audience ishttp://localhost:8080/mcp. That token works nowhere else: not at another MCP server, and not at the provider’s userinfo endpoint. -
Open
http://localhost:8080/audit. Each tool call is atool_calledevent with the tool, the decision, and a hash of the arguments, never the arguments themselves. -
Open
http://localhost:8080/connectionsand choose Revoke. The next tool call from Claude Code gets401witherror="invalid_token", at once, because the token is a session that the server checks on every request.
Protect an MCP server with the sign-in you already have
You don’t need the library’s provider to use mcpauth. If your authorization server, such as Auth0, Okta, Keycloak, or Clerk, issues JWT access tokens, verify them with mcpauth.JWT:
v, err := mcpauth.JWT(mcpauth.JWTConfig{
Issuer: "https://example.us.auth0.com/", // exactly as the tokens' iss holds it
Audience: "https://notes.example.com/mcp", // configure your server to put this in aud
})
p, err := mcpauth.New(mcpauth.Options{
Resource: "https://notes.example.com/mcp",
AuthorizationServers: []string{"https://example.us.auth0.com/"},
Verifier: v,
Scopes: []string{"notes:read"},
ToolScopes: map[string][]string{"notes_list": nil, "notes_add": {"notes:write"}},
})
p.Register(mux, "/mcp", server, &mcp.StreamableHTTPOptions{Stateless: true})The verifier finds the keys through the issuer’s metadata, accepts only asymmetric signatures, and requires the issuer, an expiry, and your MCP server as audience. A JWT can’t be revoked before it expires, so keep tokens short or set MaxAge.
If your application has its own login but no OAuth server, authhttp.Bridge puts that login in front of the library’s provider and consent page. See examples/mcpserver with -own-login, and the design.