Protect your MCP server with bearer token authentication. Invalid tokens return HTTP 401 responses before reaching your MCP handlers.Documentation Index
Fetch the complete documentation index at: https://docs.mcp-use.com/llms.txt
Use this file to discover all available pages before exploring further.
What Gets Protected
When you configureauth=MyAuthProvider(), all MCP endpoints are automatically protected:
- All tools
- All resources
- All prompts
Quick Start
How It Works
BearerAuthProvider
Extend this class and implementverify_token():
AccessToken
| Field | Type | Description |
|---|---|---|
token | str | The original bearer token |
claims | dict[str, Any] | User info dictionary |
scopes | list[str] | Permission list (default: []) |
claims
Theclaims 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 addressname- Display name- Custom fields like
plan,org_id,role, etc.
scopes
Use for permission checks in your tools:"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.| Function | Purpose |
|---|---|
get_access_token() | Returns AccessToken or None. Use when auth is optional or to access claims. |
require_auth() | Returns AccessToken or raises AuthenticationError. Use as a safeguard. |
Client Configuration
HTTP Responses
| Scenario | Status | Response |
|---|---|---|
| No token | 401 | {"error": "unauthorized", "error_description": "Authentication required"} |
| Invalid token | 401 | {"error": "invalid_token", "error_description": "Token is invalid or expired"} |
| Valid token | 200 | Request proceeds |
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.