Skip to content

The verify MCP entry

For: a developer of an app with its own MCP server that must accept only a valid access token.

What it does: this page lists the rules of requireMcpAuth, its two sources, and every export of the entry. The export check holds this page to packages/verify/src/mcp.ts.

requireMcpAuth(source, handler, options) answers a function of a Request to a Response. The handler gets the request and a McpCaller. A wrong option throws at construction.

Option Default What it does
resource required The resource of this MCP server: an entry of oauth.resources.
scopes none The scopes that every request needs.
challengeScopes scopes The scopes that the 401 names, so a client asks for them.

The name follows Better Auth 1.7.7. Its older name withMcpAuth is not an export.

The request The answer
A resource that is not https:. Construction accepts http: only on 127.0.0.1 or localhost. Construction throws.
A resource with a query, a fragment, or user info Construction throws.
A scope with a quote or a space, offline_access, or an empty list Construction throws.
No bearer and no DPoP token 401 with the challenge of resource and challengeScopes. A session cookie is no credential here.
A DPoP token with a valid proof The caller passes. A Verifier source needs its dpop option.
A second request with the same proof 401: the wrapper checks one proof per request.
A token of the client_credentials grant 401 with error="invalid_token": the wrapper needs a person.
A bearer that is not an OAuth access token The same 401 with error="invalid_token".
A token for another resource of the same issuer The 401 with error="invalid_token".
A caller without every entry of scopes 403 with error="insufficient_scope". The challenge names every scope of the route. For a DPoP request, the DPoP challenge carries the error.
A source that fails 503 with no body, never 401.
A McpScopeError of the handler 403 for the scopes of the error. Any other throw passes through.
A caller that passes The answer of the handler, unchanged.

The 401 and the 403 come from unauthorizedResponse and insufficientScopeResponse of the verify package. The challenge then has one source.

  • A Verifier of createVerifier, for a server that is not the auth app. The wrapper uses it when the source has verifyRequest. A failure of network or unexpected gives 503.
  • The auth of core, for an MCP route in the same Worker or process as the auth app. The wrapper calls auth.caller and maps its subject to userId. A throw of caller gives 503.

userId is the token’s subject at both sources: the user id for a public client, the pairwise subject for a pairwise client. So a server that moves from one source to the other keeps its data keys.

The second source fetches no key set. So a Worker does not fetch its own JWKS on its own host.

DPoP and token introspection come through the Verifier, with no new option. With a Verifier source, the 401 comes from verifier.unauthorized with the request, so a refused proof names invalid_dpop_proof in the DPoP challenge. Core’s auth gives the wrapper no algs. For a DPoP request, that source writes the DPoP challenge with no algs: beside the Bearer challenge in a 401, and alone in a 403.

Export What it is
requireMcpAuth Wraps the handler of an MCP route.
McpScopeError The error that a handler throws when an operation needs more scopes. Its scopes field holds every scope of the operation.
Area Types
The caller McpCaller
The source McpSource