Skip to content

Commit 7199c1f

Browse files
committed
fix: use JSON-RPC error format for session not found response
Format the 404 response body as a proper JSON-RPC error for consistency with other transport error responses. Github-Issue: #1727
1 parent 01397f0 commit 7199c1f

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

src/mcp/server/streamable_http_manager.py

Lines changed: 11 additions & 2 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
@@ -288,9 +289,17 @@ async def run_server(
288289
await http_transport.handle_request(scope, receive, send)
289290
else: # pragma: no cover
290291
# Unknown or expired session ID - return 404 per MCP spec
291-
# This tells the client to reinitialize
292+
# Use JSON-RPC error format for consistency with transport errors
293+
error_body = json.dumps(
294+
{
295+
"jsonrpc": "2.0",
296+
"id": "server-error",
297+
"error": {"code": -32600, "message": "Session not found"},
298+
}
299+
)
292300
response = Response(
293-
"Session not found",
301+
content=error_body,
294302
status_code=HTTPStatus.NOT_FOUND,
303+
media_type="application/json",
295304
)
296305
await response(scope, receive, send)

0 commit comments

Comments
 (0)