Skip to content

Fix httpstreamable errorcode #1194

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions src/mcp/client/streamable_http.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
from mcp.shared._httpx_utils import McpHttpClientFactory, create_mcp_http_client
from mcp.shared.message import ClientMessageMetadata, SessionMessage
from mcp.types import (
INVALID_REQUEST,
ErrorData,
InitializeResult,
JSONRPCError,
Expand Down Expand Up @@ -266,6 +267,14 @@ async def _handle_post_request(self, ctx: RequestContext) -> None:
logger.debug("Received 202 Accepted")
return

if response.status_code == 400:
if isinstance(message.root, JSONRPCRequest):
await self._send_invalid_request_error(
ctx.read_stream_writer,
message.root.id,
)
return

if response.status_code == 404:
if isinstance(message.root, JSONRPCRequest):
await self._send_session_terminated_error(
Expand Down Expand Up @@ -359,6 +368,20 @@ async def _send_session_terminated_error(
session_message = SessionMessage(JSONRPCMessage(jsonrpc_error))
await read_stream_writer.send(session_message)

async def _send_invalid_request_error(
self,
read_stream_writer: StreamWriter,
request_id: RequestId,
) -> None:
"""Send an invalid request error response."""
jsonrpc_error = JSONRPCError(
jsonrpc="2.0",
id=request_id,
error=ErrorData(code=INVALID_REQUEST, message="Invalid request"),
)
session_message = SessionMessage(JSONRPCMessage(jsonrpc_error))
await read_stream_writer.send(session_message)

async def post_writer(
self,
client: httpx.AsyncClient,
Expand Down
Loading