Skip to content

[Client] Check HTTP status codes in HttpTransport - #425

Open
ez-lbz wants to merge 2 commits into
modelcontextprotocol:mainfrom
ez-lbz:client-http-status-checks
Open

[Client] Check HTTP status codes in HttpTransport#425
ez-lbz wants to merge 2 commits into
modelcontextprotocol:mainfrom
ez-lbz:client-http-status-checks

Conversation

@ez-lbz

@ez-lbz ez-lbz commented Aug 16, 2026

Copy link
Copy Markdown

HttpTransport::send() never looked at the HTTP status code of the response. A 404 JSON error body was parsed as a regular message, and non-JSON error bodies (e.g. text/plain) were dropped silently, leaving the caller waiting on the request timeout.

This change checks the status code before dispatching the body:

  • 404 with a session id set means the session is gone: the local session id is cleared and SessionExpiredException is thrown so the application can re-initialize.
  • any other non-2xx status throws HttpTransportException (new, in Mcp\Client\Exception) carrying the status code and a snippet of the body.

It also sends the MCP-Protocol-Version header on every POST once the initialize handshake has negotiated a version, as the streamable HTTP spec requires; before negotiation the header is omitted.

Tests in HttpTransportTest cover the 404-with-session, 404-without-session, 500 text/plain, 200 application/json and header-present/absent cases using mocked HTTP clients.

HttpTransport::send() never looked at the HTTP status code of the
response. A 404 JSON error body was parsed as a regular message, and
non-JSON error bodies (e.g. text/plain) were dropped silently, leaving
the caller waiting on the request timeout.

Check the status code before dispatching the body:
- 404 with a session id set means the session is gone: clear the local
  session id and throw SessionExpiredException so the application can
  re-initialize.
- any other non-2xx status throws HttpTransportException carrying the
  status code and a snippet of the body.

Also send the MCP-Protocol-Version header once the initialize handshake
has negotiated a version, as the streamable HTTP spec requires.

Tests cover the 404-with-session, 404-without-session, 500, 200 and
header-present/absent cases with mocked HTTP clients.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant