Skip to content

Commit 4f78231

Browse files
Test GET /mcp with the Accept values that get 406 from the SDK (#4248)
## Summary The MCP Python SDK checks the `Accept` header before the request method. Without our 405 route, a `GET /mcp` with `Accept: application/json`, or with no `Accept` header, gets 406 instead of 405. Clients that probe with those headers, such as the MCP TypeScript SDK, treat only 405 as "no stream, use POST," so a 406 aborts their handshake. Another deployment reported this on modelcontextprotocol/python-sdk#2474 (comment). Our route already answers every GET before the SDK's checks run, and the live server returns 405 for all of these `Accept` values. The existing tests sent only `Accept: text/event-stream`, though, so they couldn't catch a change that moved our route behind the `Accept` check, or an upstream fix with the same ordering problem. This adds three cases to the GET tests: `Accept: */*`, `Accept: application/json`, and no `Accept` header. TestClient adds `Accept: */*` to every request, so the no-`Accept` case removes the header by hand in its own test. No server code changes. ## Testing - All 7 tests in `scripts/test_mcp_transport.py` pass. - With the 405 route removed from `scripts/mcp-server.py`, all 5 GET cases fail. `text/event-stream` and `*/*` hang until the 10-second timeout, and `application/json` and no `Accept` fail at once with 406. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-authored-by: Claude Opus 5.5 <[email protected]>
1 parent a55ba63 commit 4f78231

1 file changed

Lines changed: 26 additions & 1 deletion

File tree

‎scripts/test_mcp_transport.py‎

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,13 +81,25 @@ def client():
8181
yield test_client
8282

8383

84+
# The Accept cases matter because the SDK checks Accept before the method: a
85+
# GET with Accept: application/json, or with no Accept header, gets 406
86+
# instead of 405. Clients that probe with those headers, such as the MCP
87+
# TypeScript SDK, treat only 405 as "no stream, use POST" and abort the
88+
# handshake on 406. Our route must answer before that check runs.
8489
@pytest.mark.parametrize(
8590
"headers",
8691
[
8792
{"Accept": "text/event-stream", "MCP-Protocol-Version": PROTOCOL_VERSION},
8893
{"Accept": "text/event-stream"},
94+
{"Accept": "*/*", "MCP-Protocol-Version": PROTOCOL_VERSION},
95+
{"Accept": "application/json", "MCP-Protocol-Version": PROTOCOL_VERSION},
96+
],
97+
ids=[
98+
"with-protocol-version",
99+
"without-protocol-version",
100+
"accept-any",
101+
"accept-json",
89102
],
90-
ids=["with-protocol-version", "without-protocol-version"],
91103
)
92104
# If the 405 route regresses, the GET opens a stream that never ends and
93105
# TestClient waits forever for its body. The timeout (pytest-timeout) turns
@@ -100,6 +112,19 @@ def test_get_is_refused_with_405(client, headers):
100112
assert response.headers["allow"] == "POST"
101113

102114

115+
@pytest.mark.timeout(10)
116+
def test_get_without_accept_is_refused_with_405(client):
117+
"""A GET with no Accept header gets our 405, not the SDK's 406."""
118+
request = client.build_request(
119+
"GET", "/mcp", headers={"MCP-Protocol-Version": PROTOCOL_VERSION}
120+
)
121+
# TestClient adds "Accept: */*" to every request, so remove it by hand.
122+
del request.headers["accept"]
123+
response = client.send(request)
124+
assert response.status_code == 405
125+
assert response.headers["allow"] == "POST"
126+
127+
103128
def test_initialize_is_stateless_and_json(client):
104129
"""POST initialize still works, mints no session, and replies in JSON."""
105130
response = client.post("/mcp", headers=POST_HEADERS, json=INITIALIZE)

0 commit comments

Comments
 (0)