Skip to content

Commit c143464

Browse files
refactor: added a custom error message
This ensures the error message is more specific and focused to a 400 bad request error only
1 parent 2f365d8 commit c143464

File tree

1 file changed

+23
-2
lines changed

1 file changed

+23
-2
lines changed

src/mcp/client/streamable_http.py

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -266,8 +266,15 @@ async def _handle_post_request(self, ctx: RequestContext) -> None:
266266
logger.debug("Received 202 Accepted")
267267
return
268268

269-
# ensures that all HTTP error codes are handled
270-
if response.status_code >= 400 and response.status_code <= 499:
269+
if response.status_code == 400:
270+
if isinstance(message.root, JSONRPCRequest):
271+
await self._send_bad_request_error(
272+
ctx.read_stream_writer,
273+
message.root.id,
274+
)
275+
return
276+
277+
if response.status_code == 404:
271278
if isinstance(message.root, JSONRPCRequest):
272279
await self._send_session_terminated_error(
273280
ctx.read_stream_writer,
@@ -360,6 +367,20 @@ async def _send_session_terminated_error(
360367
session_message = SessionMessage(JSONRPCMessage(jsonrpc_error))
361368
await read_stream_writer.send(session_message)
362369

370+
async def _send_bad_request_error(
371+
self,
372+
read_stream_writer: StreamWriter,
373+
request_id: RequestId,
374+
) -> None:
375+
"""Send a bad request error response."""
376+
jsonrpc_error = JSONRPCError(
377+
jsonrpc="2.0",
378+
id=request_id,
379+
error=ErrorData(code=32600, message="Bad request"),
380+
)
381+
session_message = SessionMessage(JSONRPCMessage(jsonrpc_error))
382+
await read_stream_writer.send(session_message)
383+
363384
async def post_writer(
364385
self,
365386
client: httpx.AsyncClient,

0 commit comments

Comments
 (0)