[Client] Check HTTP status codes in HttpTransport - #425
Open
ez-lbz wants to merge 2 commits into
Open
Conversation
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.
ez-lbz
requested review from
CodeWithKyrian,
Nyholm,
chr-hertel and
soyuka
as code owners
August 16, 2026 15:05
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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:
SessionExpiredExceptionis thrown so the application can re-initialize.HttpTransportException(new, inMcp\Client\Exception) carrying the status code and a snippet of the body.It also sends the
MCP-Protocol-Versionheader 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
HttpTransportTestcover the 404-with-session, 404-without-session, 500 text/plain, 200 application/json and header-present/absent cases using mocked HTTP clients.