Skip to content

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

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

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

Conversation

@rameshreddy-adutla

Copy link
Copy Markdown
Contributor

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

markInitialized() and reconnect() ran before the status check, so a 405
from a server without Streamable HTTP still triggered a GET and left a
duplicate SSE session during transport fallback. Only do this on 2xx.
Fixes modelcontextprotocol#773.
@rameshreddy-adutla
rameshreddy-adutla force-pushed the fix/773-reconnect-before-status-check branch from 5c47844 to 128c06f Compare September 28, 2026 12:37

This branch has not been deployed

No deployments
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