Files
mcp-use__mcp-use/docs/python/client/authentication/bearer.mdx
Vincenzo Reina aca5f9c040 feat(python): Adapt OAuth to 2.1 version from new MCP spec (#518)
* feat: Add PRM in the metadata discovery

* fix: fallback anyway to PRM

* feat: Implement complete discovery logic

* feat: Final implementation of CIMD

* feat: Implement PKCE

* feat: Add scope from WWW-Authenticate

* feat: Add all URLs possible styles

* feat: Update tests and example

* docs: Update docs

* fix: linting

* docs: update API documentation

* fix: correct typos in OAuth metadata and error messages

- Updated the comment in ProtectedResourceMetadata to use the correct abbreviation (PRM).
- Fixed a typo in the OAuthAuthenticationError message regarding code challenge support.
- Corrected the comment for legacy client paths to use "pre-registered" instead of "pre-reggistered".

* feat: restructure authentication documentation and add new methods

- Updated the authentication section in  to group authentication methods under a new Authentication category with an icon.
- Removed the old  file and replaced it with new documentation files for Bearer Token, Custom Auth, and OAuth 2.1.
- Introduced an overview page for authentication methods, detailing usage and configuration for each method.
- Enhanced security best practices and examples for each authentication type to improve clarity and usability.

* refactor: improve debug logging for OAuth metadata discovery

- Updated debug messages to provide clearer context by specifying the issuer or host being queried during OAuth and OpenID Connect metadata discovery.
- Enhanced error logging to remove redundant information, focusing on the failure reason without repeating the URL.

* fix: correct abbreviation in ProtectedResourceMetadata documentation

- Updated the abbreviation from PRC to PRM in the ProtectedResourceMetadata section to ensure accuracy in the documentation.

* fix(oauth): correct RFC 8414 well-known URL construction for path-based issuers

  The OAuth metadata discovery was incorrectly constructing .well-known URLs
  by appending to the issuer instead of inserting between host and path.

  RFC 8414 specifies that for an issuer like https://github.com/login/oauth,
  the .well-known URL should be:
    https://github.com/.well-known/oauth-authorization-server/login/oauth

  We were incorrectly building:
    https://github.com/login/oauth/.well-known/oauth-authorization-server

  This fix:
  - Implements correct RFC 8414 URL construction (insert between host/path)
  - Supports OpenID Connect style as fallback (append to issuer)
  - Removes GitHub-specific hardcode since discovery now works correctly
  - Adds documentation comments explaining the two standards

  Ref: https://www.rfc-editor.org/rfc/rfc8414.html#section-3

* chore: oauth examples

* docs: enhance OAuth endpoint discovery documentation

- Updated the OAuth documentation to clarify how `mcp-use` discovers OAuth endpoints using both OAuth 2.0 and OpenID Connect standards.
- Added a table detailing the URL construction methods for each standard.
- Included examples for better understanding and a note on fallback behavior when the issuer has no path.

* response to bs codeql

* fix: update API URL Updated the GitHub API URL in the configuration to the correct endpoint for OAuth operations.

---------

Co-authored-by: Enrico Toniato <2827496+tonxxd@users.noreply.github.com>
Co-authored-by: pietrozullo <pietro.zullo@gmail.com>
2026-01-11 18:09:34 +01:00

141 lines
3.4 KiB
Plaintext

---
title: "Bearer Token"
description: "Simple token-based authentication for MCP servers using API keys or static tokens."
icon: "ticket"
---
Bearer token authentication is the simplest method - just pass your API key or token as a string.
<Info>
`mcp-use` adds `Authorization: Bearer <token>` to every request automatically.
</Info>
## Quick Start
```python
from mcp_use import MCPClient
config = {
"mcpServers": {
"api": {
"url": "https://api.example.com/mcp/sse",
"auth": "sk-your-api-key-here"
}
}
}
client = MCPClient(config=config)
```
## When to Use
<CardGroup cols={2}>
<Card title="API Keys" icon="key">
Services that issue static API keys for authentication
</Card>
<Card title="Service Tokens" icon="server">
Machine-to-machine authentication between services
</Card>
<Card title="Personal Access Tokens" icon="user">
GitHub PATs, GitLab tokens, and similar credentials
</Card>
<Card title="Internal Services" icon="lock">
Pre-shared secrets for private infrastructure
</Card>
</CardGroup>
## Secure Configuration
<Tabs>
<Tab title="Environment Variables">
```python
import os
config = {
"mcpServers": {
"api": {
"url": "https://api.example.com/mcp/sse",
"auth": os.getenv("MCP_API_KEY")
}
}
}
```
</Tab>
<Tab title="python-dotenv">
```python
from dotenv import load_dotenv
import os
load_dotenv()
config = {
"mcpServers": {
"api": {
"url": os.getenv("MCP_SERVER_URL"),
"auth": os.getenv("MCP_API_KEY")
}
}
}
```
**.env file:**
```bash
MCP_SERVER_URL=https://api.example.com/mcp/sse
MCP_API_KEY=sk-your-api-key-here
```
</Tab>
<Tab title="Multiple Servers">
```python
import os
config = {
"mcpServers": {
"service_a": {
"url": "https://service-a.example.com/mcp",
"auth": os.getenv("SERVICE_A_TOKEN")
},
"service_b": {
"url": "https://service-b.example.com/mcp",
"auth": os.getenv("SERVICE_B_TOKEN")
}
}
}
```
</Tab>
</Tabs>
<Warning>
Never hardcode API keys in source code. Always use environment variables or a secrets manager.
</Warning>
## Bearer vs OAuth
| | Bearer Token | OAuth 2.1 |
|---|:---:|:---:|
| **Setup** | Simple | More complex |
| **Token refresh** | Manual | Automatic |
| **User consent** | Not required | Required |
| **Token lifetime** | Long-lived | Short-lived + refresh |
| **Best for** | API keys, services | User authentication |
<Tip>
If your token expires frequently or requires user authorization, consider using [OAuth 2.1](/python/client/authentication/oauth) instead.
</Tip>
## Security Checklist
<Steps>
<Step title="Use environment variables">
Never commit tokens to version control. Use `.env` files locally and secrets management in production.
</Step>
<Step title="Rotate tokens regularly">
Implement a rotation policy to limit exposure from compromised tokens.
</Step>
<Step title="Use minimal permissions">
If the service supports scoped tokens, request only what you need.
</Step>
<Step title="Monitor usage">
Enable logging to detect unauthorized access attempts.
</Step>
</Steps>