fix: create MCP server per request (SDK v1.26+ compat)#694
Open
sparrowfm wants to merge 1 commit intoslopus:mainfrom
Open
fix: create MCP server per request (SDK v1.26+ compat)#694sparrowfm wants to merge 1 commit intoslopus:mainfrom
sparrowfm wants to merge 1 commit intoslopus:mainfrom
Conversation
@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>
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.
Summary
change_title) failing on every call after the firstinitializehandshakesimpleStatelessStreamableHttp.tsexampleMcpServer+StreamableHTTPServerTransportper HTTP request and cleans up on connection closeRoot cause
@modelcontextprotocol/sdkv1.26+ throws"Already connected to a transport"ifProtocol.connect()is called when_transportis already set. The previous code created a singleMcpServerandStreamableHTTPServerTransportat startup and reused them for all requests, breaking after the first one.Test plan
change_titleMCP tool works on the first message"Already connected to a transport"or"Server already initialized"errors in logsFixes #634
🤖 Generated with Claude Code