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

@ -10,6 +10,12 @@ ORCHESTRATOR_BIND=0.0.0.0
# install.sh generates a strong value automatically. # install.sh generates a strong value automatically.
LWSS_API_KEY= LWSS_API_KEY=
# Hostnames allowed to reach the MCP transport (DNS-rebinding protection).
# Behind a reverse proxy, set this to your public domain or the MCP endpoint
# returns 421 "Invalid Host header". Comma-separated; "*" disables the check.
# Leave empty for localhost-only.
MCP_ALLOWED_HOSTS=
# Cache TTLs (seconds) # Cache TTLs (seconds)
RENDER_CACHE_TTL=3600 RENDER_CACHE_TTL=3600
SEARCH_CACHE_TTL=300 SEARCH_CACHE_TTL=300

View file

@ -16,6 +16,7 @@ services:
EXTRACT_CACHE_TTL: ${EXTRACT_CACHE_TTL:-3600} EXTRACT_CACHE_TTL: ${EXTRACT_CACHE_TTL:-3600}
STAGE_TIMEOUT_S: ${STAGE_TIMEOUT_S:-60} STAGE_TIMEOUT_S: ${STAGE_TIMEOUT_S:-60}
LWSS_API_KEY: ${LWSS_API_KEY:-} LWSS_API_KEY: ${LWSS_API_KEY:-}
MCP_ALLOWED_HOSTS: ${MCP_ALLOWED_HOSTS:-}
depends_on: depends_on:
- searcher - searcher
- renderer - renderer

View file

@ -8,4 +8,7 @@ COPY app ./app
ENV PORT=8000 ENV PORT=8000
EXPOSE 8000 EXPOSE 8000
CMD ["sh", "-c", "uvicorn app.main:app --host 0.0.0.0 --port ${PORT}"] # --proxy-headers + trusting forwarded IPs makes Starlette honor X-Forwarded-Proto
# from the reverse proxy, so mount redirects (/mcp -> /mcp/) stay https instead
# of downgrading to http.
CMD ["sh", "-c", "uvicorn app.main:app --host 0.0.0.0 --port ${PORT} --proxy-headers --forwarded-allow-ips='*'"]

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). # Optional bearer-token auth. Empty = auth disabled (trusted networks only).
API_KEY = os.environ.get("LWSS_API_KEY", "") 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()
]

View file

@ -6,12 +6,31 @@ Mounted into the FastAPI app at /mcp (streamable HTTP, stateless).
import json import json
from mcp.server.fastmcp import FastMCP from mcp.server.fastmcp import FastMCP
from mcp.server.transport_security import TransportSecuritySettings
from . import pipeline from . import config, pipeline
# Populated by main.py's lifespan with the shared httpx client + cache. # Populated by main.py's lifespan with the shared httpx client + cache.
runtime: dict = {"client": None, "cache": None} runtime: dict = {"client": None, "cache": None}
def _transport_security() -> TransportSecuritySettings:
"""DNS-rebinding protection tuned for reverse-proxy deployment.
Default allowed_hosts is empty, which rejects every non-localhost Host with
421 'Invalid Host header' — so a proxied public domain must be allowlisted
(MCP_ALLOWED_HOSTS) or protection disabled with '*'.
"""
hosts = config.MCP_ALLOWED_HOSTS
if hosts == ["*"]:
return TransportSecuritySettings(enable_dns_rebinding_protection=False)
if not hosts:
return TransportSecuritySettings() # localhost-only default
allowed_hosts = list(hosts) + ["localhost", "127.0.0.1"]
allowed_origins = [f"https://{h}" for h in hosts] + [f"http://{h}" for h in hosts]
return TransportSecuritySettings(allowed_hosts=allowed_hosts, allowed_origins=allowed_origins)
mcp = FastMCP( mcp = FastMCP(
"local-web-search", "local-web-search",
instructions=( instructions=(
@ -22,6 +41,7 @@ mcp = FastMCP(
), ),
streamable_http_path="/", streamable_http_path="/",
stateless_http=True, stateless_http=True,
transport_security=_transport_security(),
) )