File tree Expand file tree Collapse file tree 1 file changed +58
-0
lines changed Expand file tree Collapse file tree 1 file changed +58
-0
lines changed Original file line number Diff line number Diff line change 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 )
You can’t perform that action at this time.
0 commit comments