requireBearerAuth locks your server behind sign-in. Unauthenticated requests are turned away before any tool runs, and your tools receive the signed-in user.
Example
Every tool requires a signed-in user with theshop.read scope: the middleware validates the token before any handler runs.
server.ts
Signature
Parameters
options
Returns
An ExpressRequestHandler to pass to server.use, typically on /mcp.
- A valid token: the request proceeds, and handlers read it from
extra.authInfo. - A missing, invalid, or expired token: a 401 with a
WWW-Authenticateheader. A token missing a required scope: a 403.
A valid token only proves who the caller is. Authorizing what they can do, and scoping data to them, stays the handler’s job: read
extra.authInfo and never trust a client-supplied identifier.optionalBearerAuth
Accept a token when present, allow anonymous otherwise
mcpAuthMetadataRouter
Advertise your authorization server for discovery
Authenticate Users
Add sign-in to your app end to end