Title: Guide: Resolving "421 Invalid Host Header" (DNS Rebinding Protection) · Issue #1798 · modelcontextprotocol/python-sdk · GitHub
Open Graph Title: Guide: Resolving "421 Invalid Host Header" (DNS Rebinding Protection) · Issue #1798 · modelcontextprotocol/python-sdk
X Title: Guide: Resolving "421 Invalid Host Header" (DNS Rebinding Protection) · Issue #1798 · modelcontextprotocol/python-sdk
Description: Context A recent update (Commit: 86bb54c, PR: #861) introduced DNS rebinding protection to the MCP Python SDK. While this improves security, it may cause existing setups to fail with a 421 Misdirected Request / Invalid Host Header error ...
Open Graph Description: Context A recent update (Commit: 86bb54c, PR: #861) introduced DNS rebinding protection to the MCP Python SDK. While this improves security, it may cause existing setups to fail with a 421 Misdirec...
X Description: Context A recent update (Commit: 86bb54c, PR: #861) introduced DNS rebinding protection to the MCP Python SDK. While this improves security, it may cause existing setups to fail with a 421 Misdirec...
Opengraph URL: https://github.com/modelcontextprotocol/python-sdk/issues/1798
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Guide: Resolving \"421 Invalid Host Header\" (DNS Rebinding Protection)","articleBody":"### **Context**\n\nA recent update (Commit: `86bb54c`, PR: #861) introduced DNS rebinding protection to the MCP Python SDK. While this improves security, it may cause existing setups to fail with a **421 Misdirected Request / Invalid Host Header** error if the host header doesn't match the allowed list (common when using proxies, gateways, or custom domains).\n\n### **Affected Components**\n\n* **File:** `src/mcp/server/fastmcp/server.py:178`\n\n---\n\n### **Solutions**\n\nDepending on your security requirements, you can resolve this in two ways:\n\n#### **Option 1: Explicitly Allow Specific Hosts (Recommended)**\n\nUse this approach if you are running in production or through a gateway. You can wildcard the ports using `*`.\n\n```python\nfrom mcp.server.fastmcp import FastMCP\nfrom mcp.server.transport_security import TransportSecuritySettings\n\nmcp = FastMCP(\n \"MyServer\",\n transport_security=TransportSecuritySettings(\n enable_dns_rebinding_protection=True,\n # Add your specific gateway or domain here\n allowed_hosts=[\"localhost:*\", \"127.0.0.1:*\", \"your-gateway-host:*\"],\n allowed_origins=[\"http://localhost:*\", \"http://your-gateway-host:*\"],\n )\n)\n\n```\n\n#### **Option 2: Disable DNS Rebinding Protection**\n\nUse this approach for local development or if you are managing security at a different layer of your infrastructure.\n\n```python\nfrom mcp.server.fastmcp import FastMCP\nfrom mcp.server.transport_security import TransportSecuritySettings\n\nmcp = FastMCP(\n \"MyServer\",\n transport_security=TransportSecuritySettings(\n enable_dns_rebinding_protection=False,\n )\n)\n\n```\n\n---\n\n### **References**\n\n* **Original Reported by:** @leoswing in #861\n* **Then Reported by:** @longz30 in #1797\n\n\u003e **Note:** If you are using a reverse proxy (like Nginx or Caddy), ensure your proxy is passing the correct `Host` header to the MCP server.\n\n\n### Additional Context\n\nIf your problem is solved, remember to give a thumbs up 👍!","author":{"url":"https://github.com/Junyi-99","@type":"Person","name":"Junyi-99"},"datePublished":"2025-12-17T18:17:58.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":12},"url":"https://github.com/1798/python-sdk/issues/1798"}
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:41ad63b0-7249-2859-6e81-24c465b6a6ad |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 8620:3774B2:41BA59:5B998B:6A5A4C65 |
| html-safe-nonce | 24d83fdc5e641488c137e841c6397d6c74c753696950f79af21fd06caaa66353 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI4NjIwOjM3NzRCMjo0MUJBNTk6NUI5OThCOjZBNUE0QzY1IiwidmlzaXRvcl9pZCI6IjMyNzMzMjMzNTgzNjUzMDU5NTciLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 2b4195ec25b5b80ea9a570d2a7411e65c016e9d4853807f83aee2da7cfee9712 |
| hovercard-subject-tag | issue:3739953842 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/modelcontextprotocol/python-sdk/1798/issue_layout |
| twitter:image | https://opengraph.githubassets.com/d0d4d6cbe63770801970cfe1d07c5e86e632b3a569948095914970e54edbf29a/modelcontextprotocol/python-sdk/issues/1798 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/d0d4d6cbe63770801970cfe1d07c5e86e632b3a569948095914970e54edbf29a/modelcontextprotocol/python-sdk/issues/1798 |
| og:image:alt | Context A recent update (Commit: 86bb54c, PR: #861) introduced DNS rebinding protection to the MCP Python SDK. While this improves security, it may cause existing setups to fail with a 421 Misdirec... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | Junyi-99 |
| hostname | github.com |
| expected-hostname | github.com |
| None | a793cd1fa898929111edad5857ee1f24ecf4bf7702525e67357cabc9df136fc3 |
| turbo-cache-control | no-preview |
| go-import | github.com/modelcontextprotocol/python-sdk git https://github.com/modelcontextprotocol/python-sdk.git |
| octolytics-dimension-user_id | 182288589 |
| octolytics-dimension-user_login | modelcontextprotocol |
| octolytics-dimension-repository_id | 862584018 |
| octolytics-dimension-repository_nwo | modelcontextprotocol/python-sdk |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 862584018 |
| octolytics-dimension-repository_network_root_nwo | modelcontextprotocol/python-sdk |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 311929384671aa49f385b6fcc680ae3933943203 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width