Securing a Python MCP server
In the Python MCP SDK, a streamable HTTP server accepts only localhost Host headers unless you pass transport_security with your real hostnames. A 421 Invalid Host header means that check fired. Add allowed hosts and origins instead of disabling it, and configure proxy headers behind TLS termination. Sources were read on 16 Nov 2026.
Facts read on .
What does the invalid host header error mean?
It means the server's DNS-rebinding protection rejected a request whose Host header it did not expect. In the Python SDK, a streamable HTTP app created with no transport_security setting accepts a request only if its Host header is 127.0.0.1, localhost or [::1] with a port, and it answers anything else with 421 Misdirected Request and the body Invalid Host header.
The Python client does not show those words. It reports MCPError: Server returned an error response, and the reason appears only in the server log as a warning from mcp.server.transport_security. The error is most common right after a first deployment behind a real hostname.
After a deployment, request the public URL using the hostname you configured and read the server log. A warning for a hostname you do not recognize means a request arrived with a Host header you did not plan for, which is worth tracing before you widen the list.
Sources: MCP Python SDK documentation: Troubleshooting; MCP Python SDK documentation: Deploy and scale.
How do you turn on transport security?
Transport security is already on by default, so the task is to tell it which hostnames are yours. Pass transport_security=TransportSecuritySettings(allowed_hosts=[...], allowed_origins=[...]) with the names you actually serve.
The SDK documentation gives three details. Entries in allowed_hosts are exact strings, so mcp.example.com matches a bare Host header and mcp.example.com:* matches any port, and you should list both. The allowed_origins list matters only for browsers, because nothing else sends an Origin header. Switching the check off is described as the honest configuration only behind a reverse proxy that already controls the Host header.
List only production names. A wildcard or a long list copied from a test environment widens the set of Host headers the server accepts, which is the exposure the check exists to limit.
Sources: MCP Python SDK documentation: Deploy and scale; MCP Python SDK documentation: Troubleshooting.
Why does the host check matter on a laptop?
It stops a malicious web page from driving your local server through a DNS name that was rebound to 127.0.0.1. The MCP transport specification says servers must validate the Origin header on all incoming connections, must respond 403 when it is present and invalid, and should bind only to localhost rather than all interfaces.
Local convenience is the usual reason the check gets removed. Keep it on for any server that holds file or credential access.
Sources: MCP Python SDK documentation: Deploy and scale; MCP specification: Transports (2025-11-25).
Which deployment settings change behind a proxy?
Run uvicorn with proxy headers enabled and a list of the proxy addresses you trust. The SDK documentation says that when TLS ends at an ingress, load balancer or reverse proxy and uvicorn serves plain HTTP behind it, uvicorn must trust the proxy's X-Forwarded headers, or its redirects point at http:// and the client refuses them.
The same page warns that the wildcard value for forwarded-allow-ips trusts every hop and is right only when nothing but the proxy can reach uvicorn. Restrict network access to the server port accordingly.
What else does the SDK documentation warn about?
Duplicate tool names resolve silently: when two registrations use the same name, the first one wins, the second is dropped and a warning in the server log is the only signal. Keep that warning on and read the log after startup, since a tool you thought was live may be a different implementation.
Transport security is not authentication. A server with non-public tools still needs token checks that validate the audience, and every tool input is untrusted, as OWASP's cheat sheet puts it.
Sources: MCP Python SDK documentation: Troubleshooting; OWASP MCP Security Cheat Sheet; MCP specification: Authorization (2025-11-25).
Sources
- MCP Python SDK documentation: Troubleshooting (raw.githubusercontent.com), read
- MCP Python SDK documentation: Deploy and scale (raw.githubusercontent.com), read
- MCP specification: Transports (2025-11-25) (modelcontextprotocol.io), read
- OWASP MCP Security Cheat Sheet (cheatsheetseries.owasp.org), read
- MCP specification: Authorization (2025-11-25) (modelcontextprotocol.io), read