Skip to content

Commit 8284879

Browse files
committed
fix(streamable_http): use 499 (Client Closed Request) instead of 202 for ClientDisconnect
499 is the nginx convention for 'Client Closed Request' — more semantically correct than 202 Accepted for a cancelled request.
1 parent ab6589c commit 8284879

2 files changed

Lines changed: 5 additions & 4 deletions

File tree

src/mcp/server/streamable_http.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -654,7 +654,8 @@ async def sse_writer():
654654
if writer is not None:
655655
with suppress(Exception):
656656
await writer.send(ClientDisconnect())
657-
response = self._create_json_response(None, HTTPStatus.ACCEPTED)
657+
# 499 = Client Closed Request (nginx convention, not in stdlib HTTPStatus)
658+
response = self._create_json_response(None, 499) # type: ignore[arg-type]
658659
with suppress(Exception):
659660
await response(scope, receive, send)
660661
return

tests/server/streamable_http/test_client_disconnect_post.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -121,9 +121,9 @@ async def dummy_send(message):
121121
assert len(send_calls) >= 1, (
122122
f"Expected at least 1 ASGI send (response), got {len(send_calls)}"
123123
)
124-
# First send should be http.response.start with 202
124+
# First send should be http.response.start with 499 (Client Closed Request)
125125
assert send_calls[0]["type"] == "http.response.start"
126-
assert send_calls[0]["status"] == 202
126+
assert send_calls[0]["status"] == 499
127127

128128
@pytest.mark.anyio
129129
async def test_client_disconnect_notifies_writer(self):
@@ -202,4 +202,4 @@ async def dummy_send(message):
202202
# Response is still sent even though writer was broken
203203
assert len(send_calls) >= 1
204204
assert send_calls[0]["type"] == "http.response.start"
205-
assert send_calls[0]["status"] == 202
205+
assert send_calls[0]["status"] == 499

0 commit comments

Comments
 (0)