fix(http): create a fresh MCP server per request in stateless HTTP mode - #118
Open
Defilan wants to merge 1 commit into
Open
fix(http): create a fresh MCP server per request in stateless HTTP mode#118Defilan wants to merge 1 commit into
Defilan wants to merge 1 commit into
Conversation
createHttpApp built a single McpServer at construction and called mcpServer.connect(transport) on every /mcp request. With the stateless StreamableHTTP transport (sessionIdGenerator: undefined) there is no session to reuse, so once one request's transport was connected, any request arriving before it closed called connect() on the already-connected server and threw "Already connected to a transport ... use a separate Protocol instance per connection", returning HTTP 500. This breaks multi-request MCP clients: a client sends initialize then tools/list (and typically keeps a stream open), so its second call 500s and no tools register. A strictly-sequential caller dodges it because res.on(close) closes the transport and frees the shared server between requests. Move server creation into the /mcp handler so each request gets its own McpServer + transport (the SDK's stateless pattern) and close it on response close. Add a regression test asserting a fresh server is created per request.
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
In HTTP mode,
createHttpAppbuilds a singleMcpServerat construction and callsmcpServer.connect(transport)on every/mcprequest. With the stateless streamable-HTTP transport (sessionIdGenerator: undefined) there is no session to reuse, so once one request's transport is connected, any request arriving before it closes callsconnect()on the already-connected server and throwsAlready connected to a transport … use a separate Protocol instance per connection→ HTTP 500.This breaks multi-request MCP clients:
initializesucceeds,tools/list500s, and no tools register. (A strictly-sequentialcurldodges it becauseres.on("close")frees the shared server between requests, which is why it isn't obvious in one-shot testing.)Fixes #117.
Change
createPerplexityServer()into the/mcphandler so each request gets its own server + transport (the SDK's stateless pattern), and close the per-request server on response close.expected "createPerplexityServer" to be called 2 times, but got 0 times) and passes with the fix.Verification
npm test— 91 passing (new test included).npm run build— clean.initializesucceeded buttools/list500'd and no tools registered; after, the fullinitialize → tools/list → tools/callflow works.