Fix MCP transport for reverse-proxy deployment (Invalid Host header + http redirect)

Two bugs surfaced deploying behind Cloudflare+Caddy at a public domain:

1. MCP streamable-HTTP DNS-rebinding protection has empty allowed_hosts by
   default, so a proxied public Host returns 421 'Invalid Host header'. Added
   MCP_ALLOWED_HOSTS (comma-separated; '*' disables) wiring into the FastMCP
   transport_security settings; localhost added automatically.
2. The /mcp -> /mcp/ mount redirect downgraded to http:// because uvicorn
   ignored X-Forwarded-Proto. Run uvicorn with --proxy-headers
   --forwarded-allow-ips='*' so redirects stay https behind the proxy.

Verified locally: valid Host+token initializes (200 SSE), redirect Location is
https, spoofed Host still 421, missing token still 401.

Set MCP_ALLOWED_HOSTS=websearch.neilbantoc.dev on the deployment and redeploy.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
User 2026-07-06 11:08:48 -07:00
parent fa2cfdf2bf
commit 382cc97e98
5 changed files with 40 additions and 2 deletions

View file

@ -11,3 +11,11 @@ EXTRACT_CACHE_TTL = int(os.environ.get("EXTRACT_CACHE_TTL", "3600"))
# Optional bearer-token auth. Empty = auth disabled (trusted networks only).
API_KEY = os.environ.get("LWSS_API_KEY", "")
# Hostnames allowed to reach the MCP transport (DNS-rebinding protection).
# Comma-separated. Behind a reverse proxy set this to your public domain, e.g.
# "websearch.example.com". A single "*" disables the protection entirely
# (fine when a bearer token + trusted proxy already gate access).
MCP_ALLOWED_HOSTS = [
h.strip() for h in os.environ.get("MCP_ALLOWED_HOSTS", "").split(",") if h.strip()
]