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
  1. Connect Claude Code to the example
  2. Protect an MCP server with the sign-in you already have

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:

Go
root := http.NewServeMux()
protector.Register(root, "/mcp", mcpServer, nil) // protector from mcpauth.New
root.Handle("/", mux)                            // your authhttp.Mux

Don’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

  1. Start the server:

    Shell
    go run ./examples/mcpserver

    It listens on localhost:8080 and uses http://localhost:8080 as its issuer. The provider accepts http only for localhost.

  2. Add the server to Claude Code:

    Shell
    claude mcp add --transport http notes http://localhost:8080/mcp
  3. In Claude Code, run /mcp, choose notes, and choose to authenticate. Claude Code reads the Protected Resource Metadata from the 401 challenge, 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 admits claude.ai, so the server fetches that document and needs no registration.

  4. Your browser opens the sign-in page. Sign in with any name.

  5. 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:read and notes:write.

    Choose Allow. Every approval is recorded before the code is issued, and a loopback redirect never skips this page.

  6. Ask Claude Code to add a note and list your notes. It calls notes_add and notes_list with a token whose audience is http://localhost:8080/mcp. That token works nowhere else: not at another MCP server, and not at the provider’s userinfo endpoint.

  7. Open http://localhost:8080/audit. Each tool call is a tool_called event with the tool, the decision, and a hash of the arguments, never the arguments themselves.

  8. Open http://localhost:8080/connections and choose Revoke. The next tool call from Claude Code gets 401 with error="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:

Go
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.

From docs/guide.md, examples/mcpserver/README.md, and README.md at a4e7747.

View as Markdown