Skip to main content
When some tools are public and others need sign-in, optionalBearerAuth validates a token if one is sent but lets anonymous requests through. Each tool then enforces its own securitySchemes against extra.authInfo.

Example

The server accepts a token when one is sent, so public tools run for anyone while gated tools check extra.authInfo themselves.
server.ts

Signature

Parameters

options

Returns

An Express RequestHandler to pass to server.use, typically on /mcp.
  • No Authorization header: the request proceeds with no authInfo.
  • A valid token: the request proceeds, and handlers read it from extra.authInfo.
  • A token that is present but invalid or expired: the same 401 / 403 as requireBearerAuth. Sending a bad token is still a client error.

requireBearerAuth

Require a token on every request, with the verifier contract

registerTool

Gate individual tools with securitySchemes

Authenticate Users

Add sign-in to your app end to end