Skip to content

Commit 7673905

Browse files
aarthy-dkclaude
andcommitted
fix(mcp): allow the server's own port-less host in the transport allowlist
When BASE_URL carries a non-default port, the netloc and :* wildcard entries match only a Host/Origin that includes a port. Hosted MCP gateways (e.g. Databricks) send a port-less Host/Origin for the server's own host, which was rejected with "invalid Host header" unless the host was re-listed in TG_MCP_EXTRA_ALLOWED_HOSTS. Add the bare base_host and origin to the BASE_URL-derived allowlist so the server accepts its own host regardless of port. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
1 parent 7797ba9 commit 7673905

2 files changed

Lines changed: 19 additions & 0 deletions

File tree

testgen/mcp/server.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,15 +93,20 @@ def _build_transport_security() -> TransportSecuritySettings:
9393
netloc = parsed.netloc
9494
scheme = parsed.scheme or "http"
9595

96+
# base_host (bare) is allowed alongside netloc: when BASE_URL carries a non-default
97+
# port, netloc and the :* wildcard only match a host that includes a port, but some
98+
# clients (e.g. hosted MCP gateways) send a port-less Host/Origin for their own host.
9699
allowed_hosts: set[str] = {
97100
netloc,
101+
base_host,
98102
f"{base_host}:*",
99103
"127.0.0.1:*",
100104
"localhost:*",
101105
"[::1]:*",
102106
}
103107
allowed_origins: set[str] = {
104108
f"{scheme}://{netloc}",
109+
f"{scheme}://{base_host}",
105110
"http://127.0.0.1:*", "https://127.0.0.1:*",
106111
"http://localhost:*", "https://localhost:*",
107112
"http://[::1]:*", "https://[::1]:*",

tests/unit/mcp/test_transport_security.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,20 @@ def test_loopback_and_base_url_always_present():
3030
assert "https://localhost:*" in settings.allowed_origins
3131

3232

33+
def test_ported_base_url_also_allows_bare_host_and_origin():
34+
"""A BASE_URL with a non-default port also allows the bare (port-less) host and origin.
35+
36+
Clients such as hosted MCP gateways may send a port-less Host/Origin for the server's
37+
own host; the `:*` wildcard requires a port, so the bare forms must be present too.
38+
"""
39+
settings = _build_with("https://tg.example.com:8530")
40+
41+
assert "tg.example.com:8530" in settings.allowed_hosts
42+
assert "tg.example.com" in settings.allowed_hosts
43+
assert "https://tg.example.com:8530" in settings.allowed_origins
44+
assert "https://tg.example.com" in settings.allowed_origins
45+
46+
3347
def test_extra_host_without_port_gets_wildcard_and_bare():
3448
"""An extras entry without `:` is allowed both with a `:*` port wildcard and bare.
3549

0 commit comments

Comments
 (0)