Skip to content

Commit 983351e

Browse files
authored
Merge pull request #96 from shira-ayal/add-bearer-token-example
Add bearer token example
2 parents 83c9180 + ed1e4be commit 983351e

File tree

3 files changed

+61
-1
lines changed

3 files changed

+61
-1
lines changed

docs/03_authentication_and_authorization.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ mcp = FastApiMCP(
4949
mcp.mount()
5050
```
5151

52+
For a complete working example of authorization header, check out the [auth_example_token_passthrough.py](/examples/08_auth_example_token_passthrough.py) in the examples folder.
53+
5254
## OAuth Flow
5355

5456
FastAPI-MCP supports the full OAuth 2 flow, compliant with [MCP Spec 2025-03-26](https://modelcontextprotocol.io/specification/2025-03-26/basic/authorization).
@@ -138,7 +140,7 @@ For this to work, you have to make sure mcp-remote is running [on a fixed port](
138140

139141
## Working Example with Auth0
140142

141-
For a complete working example of OAuth integration with Auth0, check out the [auth_example_auth0.py](/examples/08_auth_example_auth0.py) in the examples folder. This example demonstrates the simple case of using Auth0 as an OAuth provider, with a working example of the OAuth flow.
143+
For a complete working example of OAuth integration with Auth0, check out the [auth_example_auth0.py](/examples/09_auth_example_auth0.py) in the examples folder. This example demonstrates the simple case of using Auth0 as an OAuth provider, with a working example of the OAuth flow.
142144

143145
For it to work, you need an .env file in the root of the project with the following variables:
144146

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
"""
2+
This example shows how to reject any request without a valid token passed in the Authorization header.
3+
4+
In order to configure the auth header, the config file for the MCP server should looks like this:
5+
```json
6+
{
7+
"mcpServers": {
8+
"remote-example": {
9+
"command": "npx",
10+
"args": [
11+
"mcp-remote",
12+
"http://localhost:8000/mcp",
13+
"--header",
14+
"Authorization:${AUTH_HEADER}"
15+
]
16+
},
17+
"env": {
18+
"AUTH_HEADER": "Bearer <your-token>"
19+
}
20+
}
21+
}
22+
```
23+
"""
24+
from examples.shared.apps.items import app # The FastAPI app
25+
from examples.shared.setup import setup_logging
26+
27+
from fastapi import Depends
28+
from fastapi.security import HTTPBearer
29+
30+
from fastapi_mcp import FastApiMCP, AuthConfig
31+
32+
setup_logging()
33+
34+
# Scheme for the Authorization header
35+
token_auth_scheme = HTTPBearer()
36+
37+
# Create a private endpoint
38+
@app.get("/private")
39+
async def private(token = Depends(token_auth_scheme)):
40+
return token.credentials
41+
42+
# Create the MCP server with the token auth scheme
43+
mcp = FastApiMCP(
44+
app,
45+
name="Protected MCP",
46+
auth_config=AuthConfig(
47+
dependencies=[Depends(token_auth_scheme)],
48+
),
49+
)
50+
51+
# Mount the MCP server
52+
mcp.mount()
53+
54+
55+
if __name__ == "__main__":
56+
import uvicorn
57+
58+
uvicorn.run(app, host="0.0.0.0", port=8000)
File renamed without changes.

0 commit comments

Comments
 (0)