- Enable stateless_http + json_response on FastMCP for scalable HTTP serving without sticky sessions (Traefik-friendly) - Add MCP_TRANSPORT env var to select stdio (default) vs streamable-http - Add bearer token auth middleware (MCP_AUTH_TOKEN) with /health bypass - Add Dockerfile (Python 3.12-slim + uv) and docker-compose.yml with Traefik v2/v3 labels, ACME TLS, and health check routing - Add .env.example with multi-instance config for mini/tn-sb/tn-mr2/p-mba - Add uvicorn + starlette as runtime dependencies Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
34 lines
1.2 KiB
Python
34 lines
1.2 KiB
Python
"""Bearer token authentication middleware for Streamable HTTP transport."""
|
|
|
|
from starlette.middleware.base import BaseHTTPMiddleware
|
|
from starlette.requests import Request
|
|
from starlette.responses import JSONResponse
|
|
|
|
|
|
class BearerAuthMiddleware(BaseHTTPMiddleware):
|
|
"""Validate ``Authorization: Bearer <token>`` on every request.
|
|
|
|
Requests without a valid token receive a 401 response. Health-check
|
|
probes hitting ``/health`` are allowed through unauthenticated so that
|
|
Docker / Traefik health checks work without token configuration.
|
|
"""
|
|
|
|
def __init__(self, app, token: str) -> None:
|
|
super().__init__(app)
|
|
self.token = token
|
|
|
|
async def dispatch(self, request: Request, call_next):
|
|
# Allow unauthenticated health checks.
|
|
if request.url.path == "/health":
|
|
return await call_next(request)
|
|
|
|
auth = request.headers.get("authorization", "")
|
|
if not auth.startswith("Bearer ") or auth[7:] != self.token:
|
|
return JSONResponse(
|
|
{"error": "Invalid or missing bearer token"},
|
|
status_code=401,
|
|
headers={"WWW-Authenticate": "Bearer"},
|
|
)
|
|
|
|
return await call_next(request)
|