Skip to content

Fix: Move markInitialized() and reconnect() after status code check#774

Open
irwin-chequer wants to merge 1 commit intomodelcontextprotocol:mainfrom
irwin-chequer:fix/reconnect-after-status-check
Open

Fix: Move markInitialized() and reconnect() after status code check#774
irwin-chequer wants to merge 1 commit intomodelcontextprotocol:mainfrom
irwin-chequer:fix/reconnect-after-status-check

Conversation

@irwin-chequer
Copy link

Summary

  • Fix bug where markInitialized() and reconnect() were called before checking the HTTP status code
  • This caused unnecessary GET requests when the server returns error status (e.g., 405 Method Not Allowed)
  • Both HttpClientStreamableHttpTransport and WebClientStreamableHttpTransport are fixed

Problem

When connecting to an MCP server that doesn't support Streamable HTTP transport (returns 405), the transports still called markInitialized() and reconnect(), triggering unnecessary GET requests. This caused duplicate SSE sessions on the upstream server when using transport fallback.

Solution

Move markInitialized() and reconnect() inside the success status code check (2xx), so they are only called for successful responses.

Test plan

  • Existing Streamable HTTP tests pass
  • Code compiles without errors

Fixes #773
Related to #362


🤖 Generated with Claude Code

When connecting to an MCP server that doesn't support Streamable HTTP
transport (returns 405 Method Not Allowed), the transports still called
markInitialized() and reconnect() before checking the status code,
which triggered unnecessary GET requests.

This caused issues when using transport fallback (Streamable HTTP → SSE),
as duplicate SSE sessions were created on the upstream server.

This fix moves markInitialized() and reconnect() inside the success
status code check (2xx), so they are only called for successful responses.

Fixes modelcontextprotocol#773
Related to modelcontextprotocol#362

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.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