Skip to content

Commit fed7971

Browse files
committed
fix: return HTTP 404 for unknown session IDs instead of 400
Per the MCP Streamable HTTP transport spec, servers MUST respond with HTTP 404 when the session ID is not found. This aligns behavior with the TypeScript SDK implementation. Github-Issue: #1727
1 parent ef96a31 commit fed7971

File tree

1 file changed

+16
-3
lines changed

1 file changed

+16
-3
lines changed

src/mcp/server/streamable_http_manager.py

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from __future__ import annotations
44

55
import contextlib
6+
import json
67
import logging
78
from collections.abc import AsyncIterator
89
from http import HTTPStatus
@@ -277,9 +278,21 @@ async def run_server(*, task_status: TaskStatus[None] = anyio.TASK_STATUS_IGNORE
277278
# Handle the HTTP request and return the response
278279
await http_transport.handle_request(scope, receive, send)
279280
else: # pragma: no cover
280-
# Invalid session ID
281+
# Unknown or expired session ID - return 404 per MCP spec
282+
# Match TypeScript SDK exactly: jsonrpc, error, id order
283+
error_body = json.dumps(
284+
{
285+
"jsonrpc": "2.0",
286+
"error": {
287+
"code": -32001,
288+
"message": "Session not found",
289+
},
290+
"id": None,
291+
}
292+
)
281293
response = Response(
282-
"Bad Request: No valid session ID provided",
283-
status_code=HTTPStatus.BAD_REQUEST,
294+
content=error_body,
295+
status_code=HTTPStatus.NOT_FOUND,
296+
media_type="application/json",
284297
)
285298
await response(scope, receive, send)

0 commit comments

Comments
 (0)