Skip to content

Move markInitialized/reconnect after status code check in Streamable HTTP transport#851

Open
rameshreddy-adutla wants to merge 1 commit intomodelcontextprotocol:mainfrom
rameshreddy-adutla:fix/773-reconnect-before-status-check
Open

Move markInitialized/reconnect after status code check in Streamable HTTP transport#851
rameshreddy-adutla wants to merge 1 commit intomodelcontextprotocol:mainfrom
rameshreddy-adutla:fix/773-reconnect-before-status-check

Conversation

@rameshreddy-adutla
Copy link

Summary

Move markInitialized() and reconnect() inside the 2xx success branch in HttpClientStreamableHttpTransport#sendMessage so they are only called when the server actually accepted the connection.

Problem

When connecting to an MCP server that doesn't support Streamable HTTP transport (returns 405 Method Not Allowed), the transport still calls markInitialized() and reconnect() before checking the status code. This triggers an unnecessary GET request, causing duplicate SSE sessions when using transport fallback (Streamable HTTP → SSE).

The root cause is in the flatMap chain at line ~480: markInitialized() and reconnect() execute unconditionally before the status code is evaluated on the next line.

Fix

Move the markInitialized()/reconnect() block inside the if (statusCode >= 200 && statusCode < 300) branch. This ensures:

  • Error responses (4xx, 5xx) no longer trigger session initialization or reconnection
  • Transport fallback works cleanly without duplicate sessions on the upstream server
  • Success path behavior is unchanged — session is initialized and async stream opened only on 2xx

Testing

All 98 Streamable HTTP transport tests pass. The 1 pre-existing failure (startContainer) is a Docker/Testcontainers infrastructure issue unrelated to this change.

Fixes #773

…HTTP transport

When the server returns a non-2xx status code (e.g. 405 Method Not Allowed),
markInitialized() and reconnect() were called before the status code was
checked. This caused an unnecessary GET request, which is problematic when
using transport fallback (Streamable HTTP -> SSE) as it creates duplicate
SSE sessions on the server.

Move markInitialized() and reconnect() inside the 2xx success branch so
they are only called when the server actually accepted the connection.

Fixes modelcontextprotocol#773

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
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.

HttpClientStreamableHttpTransport: reconnect() called before status code check causes duplicate connections on 405

1 participant