fix(mcp): the endpoint is the exact path, and nothing redirects

`Starlette(routes=[Mount("/mcp", app=handle)])` serves `/mcp/` and
answers `/mcp` with a **307** -- or a 404 with `redirect_slashes` off.
Measured both ways.  Every MCP client configuration spells the URL
without the trailing slash, and a redirected POST has to be re-sent with
its body: `urllib` will not do that at all, and some clients get it
wrong.

So the routing is three lines of its own (`http.Endpoint`) instead: both
spellings are the endpoint, neither is a redirect to the other, and
anything else is a 404.  Starlette's router is gone from the request
path; the lifespan protocol is handled directly, which `manager.run()`
needs anyway because its task group must be exited by the task that
entered it.

This got through the unit suite once, and that is the more useful half of
this commit.  `starlette.testclient` follows redirects by default, so
every assertion in `test_mcp_http.py` passed against a server a real
client could not talk to -- the 307 only surfaced in
`checks/vm-mcp-http.nix`, from a `urllib` client over a real socket.
`post()` now passes `follow_redirects=False`, and
`test_the_exact_path_does_not_redirect` asserts both spellings answer
200 directly.  A test client that papers over a redirect is a test client
that cannot see one.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01KchcvnbfhhZhUHy83YL3Ya
This commit is contained in:
HermenixandClaude Opus 5 committed 2026-10-11 22:41:01 +00:00
1 parent c2ff8fadbd
commit b2a683a560
2 files changed
+132 -38

No files matched your search

+45 -3
View File
@@ -72,12 +72,23 @@ def make_app(store, **kwargs):
return http_transport.build_app(Runner(entry=lambda argv: 0), store, **kwargs)
def post(client, body, token=None, headers=None):
def post(client, body, token=None, headers=None, path="/mcp"):
"""One POST, and **never** following a redirect.
`follow_redirects=False` is load-bearing, not tidiness. The test client
follows redirects by default, which is how a `Mount`-shaped 307 on the
exact path `/mcp` passed this whole file once while failing a real client:
a redirected POST has to be re-sent with its body, and `urllib` will not do
that at all. See `test_the_exact_path_does_not_redirect`.
"""
sent = dict(HEADERS)
if token is not None:
sent["Authorization"] = f"Bearer {token}"
sent.update(headers or {})
return client.post("/mcp", content=json.dumps(body), headers=sent)
return client.post(
path, content=json.dumps(body), headers=sent, follow_redirects=False
)
def session(client, token):
@@ -196,6 +207,33 @@ def test_the_refusal_does_not_say_which_kind_it_was(store):
assert len(set(responses)) == 1
@pytest.mark.parametrize("path", ["/mcp", "/mcp/"])
def test_the_exact_path_does_not_redirect(store, path):
"""Both spellings are the endpoint, and neither is a redirect to the other.
The regression this exists for: `Starlette(routes=[Mount("/mcp", ...)])`
serves `/mcp/` and answers `/mcp` with a 307 — and every MCP client
configuration spells the URL without the trailing slash. The redirect is
invisible to a client that follows it on a POST and fatal to one that does
not, which is why the endpoint matches the path itself instead.
"""
_, token = store.mint()
with TestClient(make_app(store)) as client:
response = post(client, INITIALIZE, token=token, path=path)
assert response.status_code == 200, (
f"{path} answered {response.status_code} "
f"(location: {response.headers.get('location')!r})"
)
def test_a_custom_path_is_honoured(store):
_, token = store.mint()
with TestClient(make_app(store, path="/agent/mcp")) as client:
assert post(client, INITIALIZE, token=token, path="/agent/mcp").status_code == 200
assert post(client, INITIALIZE, token=token, path="/mcp").status_code == 404
def test_an_unknown_path_leaks_nothing_without_a_token(store):
store.mint()
with TestClient(make_app(store)) as client:
@@ -207,7 +245,11 @@ def test_an_unknown_path_leaks_nothing_without_a_token(store):
def test_an_unknown_path_is_a_404_once_authenticated(store):
_, token = store.mint()
with TestClient(make_app(store)) as client:
response = client.get("/elsewhere", headers={"Authorization": f"Bearer {token}"})
response = client.get(
"/elsewhere",
headers={"Authorization": f"Bearer {token}"},
follow_redirects=False,
)
assert response.status_code == 404