mcp Python SDK streamable HTTP returns 421 Misdirected Request 'Invalid Host header' behind a tunnel, reverse proxy or container hostname: DNS-rebinding check from host='127.0.0.1'
Fix: if the server is reached through any Host other than localhost (a cloudflared/ngrok tunnel, a reverse proxy, a compose service name, a LAN IP), pass host="0.0.0.0" when building the app, or set explicit allowed hosts with transport_security:
mcp_app = mcp.streamable_http_app(streamable_http_path="/", host="0.0.0.0")Reproduced on mcp 2.3.0 (MCPServer + Starlette TestClient, POST initialize with Accept: application/json, text/event-stream):
- host left at its default '127.0.0.1', request Host: localhost:9090 -> 200 (SSE event: message)
- same app, Host: myhost.example:9090 -> 421 Invalid Host header, and the server logs Invalid Host header: myhost.example:9090
- host="0.0.0.0" -> 200 for both hosts.
Cause: when the app thinks it binds to loopback, the SDK enables DNS-rebinding protection and accepts only localhost-style Host headers. That's correct for a laptop-only server but rejects proxied traffic. The error looks like a proxy misconfiguration, so it's easy to spend time on the tunnel instead. Starlette's TestClient sends Host: testserver by default, so unit tests hit the same 421 unless you pass base_url="http://localhost".
Only relax it when something else guards the endpoint. In our case that was a bearer-token middleware in front of the MCP app (an MCP wrapper over a banking sandbox API). Behind a public tunnel, prefer listing the tunnel hostname in transport_security allowed hosts over turning the check off completely. Verify with curl -si -H 'Host: your.public.name' -H 'Accept: application/json, text/event-stream' -H 'Content-Type: application/json' -d '{"jsonrpc":"2.0","id":1,"method":"initialize","params":{"protocolVersion":"2025-06-18","capabilities":{},"clientInfo":{"name":"c","version":"0"}}}' http://127.0.0.1:9090/.