Skip to content

Comments

fix: create MCP server per request (SDK v1.26+ compat)#694

Open
sparrowfm wants to merge 1 commit intoslopus:mainfrom
sparrowfm:fix/mcp-server-per-request
Open

fix: create MCP server per request (SDK v1.26+ compat)#694
sparrowfm wants to merge 1 commit intoslopus:mainfrom
sparrowfm:fix/mcp-server-per-request

Conversation

@sparrowfm
Copy link

Summary

  • Fixes MCP tools (e.g. change_title) failing on every call after the first initialize handshake
  • Uses the per-request factory pattern from the SDK's own simpleStatelessStreamableHttp.ts example
  • Creates a fresh McpServer + StreamableHTTPServerTransport per HTTP request and cleans up on connection close

Root cause

@modelcontextprotocol/sdk v1.26+ throws "Already connected to a transport" if Protocol.connect() is called when _transport is already set. The previous code created a single McpServer and StreamableHTTPServerTransport at startup and reused them for all requests, breaking after the first one.

Test plan

  • Start a remote session from the Happy mobile app
  • Verify change_title MCP tool works on the first message
  • Send multiple messages — verify the tool continues to work on subsequent calls
  • Verify no "Already connected to a transport" or "Server already initialized" errors in logs

Fixes #634

🤖 Generated with Claude Code

@modelcontextprotocol/sdk v1.26+ throws "Already connected to a
transport" when connect() is called on an McpServer that already has a
transport attached. The previous code created a single McpServer +
StreamableHTTPServerTransport at startup and reused it for every HTTP
request, which broke all MCP tool calls after the first one.

Use the stateless per-request factory pattern from the SDK's own
simpleStatelessStreamableHttp.ts example: create a fresh McpServer and
transport for each incoming request, and clean them up on connection
close.

Fixes slopus#634

Co-Authored-By: Claude Opus 4.6 <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.

MCP tools fail after first request (SDK v1.26+ stateless transport reuse)

1 participant