View the source code for this module on GitHub: https://github.com/mcp-use/mcp-use/blob/main/libraries/typescript/packages/mcp-use/src/server/oauth/index.ts
mcp-use/server.
When an OAuth provider or proxy is configured on the server (the oauth option of MCPServer), the bearer middleware verifies the token, builds an AuthInfo object, and attaches it to the request. Inside a tool callback the same object is also exposed directly as ctx.auth. The functions below operate on the Hono Context, so they work both in middleware and in tool callbacks that receive the request context.
Functions
getAuth
Reads the authentication info the bearer middleware stored on the request and returns it as anAuthInfo.
AuthInfo object. Works in both middleware and tool callbacks (the tool requestContext is a Hono Context).
The value is read from the context variable auth (set by the middleware after a token is verified). Inside a tool callback you can read the same object directly as ctx.auth, so getAuth is mainly kept for compatibility with code that passes the raw context around. If no token was verified for the request (no provider configured, or the route is unauthenticated), the auth variable is unset and the return value is undefined at runtime even though the static type is AuthInfo.
ReturnsHono context, from middleware or from a tool’srequestContextparameter.
Signature
hasScope
Returnstrue only if the authenticated user has every scope (or permission) in needed (logical AND).
true only if the authenticated user has every scope (or permission) in needed. Both the OAuth scopes array and the Auth0 style permissions array are checked, so a value present in either one counts as granted.
A single string is treated as a one element list. When needed is an array, ALL entries must be present (logical AND). Use this inside a tool callback to check access and return your own error result. It calls getAuth internally, so a request with no verified auth will throw when it tries to destructure the missing AuthInfo.
ReturnsHono context, typically the tool’srequestContextparameter.A single scope/permission, or an array of scopes/permissions that must ALL be present.
Signature
hasAnyScope
Returnstrue if the authenticated user has at least ONE of the scopes (or permissions) in needed (logical OR).
true if the authenticated user has at least ONE of the scopes (or permissions) in needed (logical OR). Like hasScope, it checks both the scopes and permissions arrays, and reads auth via getAuth, so it throws on a request with no verified auth.
Unlike hasScope, this takes an array only.
ReturnsHono context, typically the tool’srequestContextparameter.Array of scopes/permissions; the user needs at least one of them.
Signature
requireScope
Builds a Hono middleware that enforces every scope (or permission) inneeded, returning 403 when the check fails.
needed before the route runs. If the check fails it short-circuits with HTTP 403 and does not call next. If it passes it calls next.
A single string is treated as a one element list; an array requires ALL entries (logical AND), matching hasScope. The 403 JSON body is { error: "insufficient_scope", required, granted_scopes, granted_permissions, message }, where granted_scopes and granted_permissions come from the current AuthInfo. For per-tool checks, prefer calling hasScope inside the tool callback instead of this route middleware.
ReturnsA single scope/permission, or an array of scopes/permissions that must ALL be present.
SignatureA Hono middleware function. Returns a403JSON response when the scope check fails, otherwise resolves after callingnext.
requireAnyScope
Builds a Hono middleware that enforces at least ONE of the scopes (or permissions) inneeded (logical OR), returning 403 when the check fails.
needed (logical OR) before the route runs. If the check fails it short-circuits with HTTP 403; otherwise it calls next.
Takes an array only. The 403 JSON body is { error: "insufficient_scope", required_any, granted_scopes, granted_permissions, message }. For per-tool checks, prefer calling hasAnyScope inside the tool callback instead.
ReturnsArray of scopes/permissions; the user needs at least one of them.
SignatureA Hono middleware function. Returns a403JSON response when the scope check fails, otherwise resolves after callingnext.
jwksVerifier
Builds aVerifyToken function that validates JWTs against a remote JWKS using jose.
jose. The returned function performs signature verification, issuer checking, and, if an audience is configured, audience checking. Pass the result as the verifyToken field of oauthProxy(...) for standard JWT/JWKS providers such as Auth0, Okta, or Google.
createRemoteJWKSet is created eagerly but does not fetch keys until the first verify call, so constructing the verifier is cheap. The factory throws if jwksUrl is missing ("jwksVerifier: jwksUrl is required") or if issuer is missing ("jwksVerifier: issuer is required"). At verify time, a token that is not a signed JWT (it lacks exactly three dot-separated segments, as with the opaque tokens some providers issue when no valid audience is requested) is rejected early with a descriptive error pointing at the audience configuration. Any other verification failure is wrapped and rethrown as JWKS verification failed: <error>.
ReturnsJWKS endpoint, expected issuer, and optional audience. See JwksVerifierConfig.
SignatureAn async function that verifies a bearer token and resolves to its decoded payload, or throws if the token is invalid.
Types
AuthInfo
The authentication information the bearer middleware attaches to each verified request.getAuth returns this shape, and it is also exposed directly as ctx.auth inside tool callbacks.
user is the extracted UserInfo. payload is the raw verified token payload. scopes is parsed from the token’s space-delimited scope claim (empty array when absent). permissions comes from the token’s permissions claim (Auth0 style; empty array when absent).
DefinitionThe authenticated user, extracted from the token payload.The raw verified JWT payload.The bearer access token from theAuthorizationheader.OAuth scopes parsed from thescopeclaim. Empty array if the claim is absent.Permissions from thepermissionsclaim (Auth0 style). Empty array if the claim is absent.
UserInfo
User information extracted from a verified OAuth token.userId is the only required field (mapped from the sub claim by the default extractor); all other fields are optional and depend on the provider and on the token’s claims. The interface has an index signature, so additional custom claims are allowed and preserved (for example WorkOS surfaces organization_id this way).
DefinitionStable user identifier, typically thesubclaim.User email, if present in the token.Display name, if present.Username, if present.Nickname, if present.Avatar/picture URL, if present.Roles, if present.Permissions, if present.Index signature for any additional custom claims.
VerifyToken
A token verification function that resolves to the decoded payload, or throws if the token is invalid.jwksVerifier and accepted by oauthProxy as its verifyToken field. Provide a custom implementation for non-JWT providers (for example, GitHub opaque tokens validated by an API call).
JwksVerifierConfig
Configuration for the built-in JWKS verifier created byjwksVerifier.
jwksVerifier. jwksUrl and issuer are required (the factory throws if either is missing); audience is optional and, when set, rejects tokens without a matching aud claim.
DefinitionJWKS endpoint URL (the provider’s public key set), for examplehttps://your-domain.okta.com/oauth2/default/v1/keys.Expectedissclaim. Tokens whose issuer does not match are rejected.Expectedaudclaim. When set, tokens without a matching audience are rejected.
OAuthProvider
The contract every OAuth provider implements: token verification, user info extraction, and OAuth metadata getters.oauthAuth0Provider and oauthWorkOSProvider) return values of this type, and you pass one as the oauth option of MCPServer. getUserInfoEndpoint and getAudience are optional. OAuthProxy extends this interface with proxy-specific fields for providers without Dynamic Client Registration.
DefinitionVerify and decode a token. Throws if the token is invalid.Extract user information from a verified token payload.Return the OAuth issuer URL.Return the authorization endpoint URL.Return the token endpoint URL.Return the supported OAuth scopes.Return the supported grant types.Optional. Return the user info endpoint URL, orundefinedif not configured.Optional. Return the audience for JWT verification, orundefinedif not configured.
Usage
Configure a provider on the server, then read the authenticated user inside a tool viactx.auth (an AuthInfo). This example uses the Auth0 provider, configured entirely from MCP_USE_OAUTH_* environment variables.
hasScope (or hasAnyScope) inside the callback and return your own error result when the check fails. To gate a custom route, mount requireScope (or requireAnyScope) as middleware.