Skip to main content
Protect your MCP server with bearer token authentication. Invalid tokens return HTTP 401 responses before reaching your MCP handlers.

What Gets Protected

When you configure auth=MyAuthProvider(), all MCP endpoints are automatically protected:
  • All tools
  • All resources
  • All prompts
No additional code is needed in individual tools. The middleware validates the token at the HTTP layer and rejects invalid requests with 401 before your code runs.

Quick Start

How It Works

BearerAuthProvider

Extend this class and implement verify_token():

AccessToken

claims

The claims dictionary stores user information that your tools can access during request handling. You decide what to include based on your application’s needs. Common fields:
  • sub - User ID (standard JWT claim)
  • email - User’s email address
  • name - Display name
  • Custom fields like plan, org_id, role, etc.
Accessing claims in tools:

scopes

Use for permission checks in your tools:
Check in tools via "admin" in token.scopes.

Accessing Token Data in Tools

Since the middleware already validates authentication, these helpers are for accessing user information from the token - not for protection.

Client Configuration

HTTP Responses

All 401 responses include the WWW-Authenticate: Bearer header.
All MCP protocol traffic (/mcp/*) requires authentication - this includes all tools, resources, and prompts. Debug paths like /docs, /inspector, /health, and /openmcp.json are excluded by default.