Bug: MCP HTTP server crashes on second request with "Stateless transport cannot be reused"#166
Open
Onyelaudochukwuka wants to merge 1 commit intotobi:mainfrom
Open
Bug: MCP HTTP server crashes on second request with "Stateless transport cannot be reused"#166Onyelaudochukwuka wants to merge 1 commit intotobi:mainfrom
Onyelaudochukwuka wants to merge 1 commit intotobi:mainfrom
Conversation
|
@Onyelaudochukwuka Thanks! This solves the error for me! |
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.
Fixes #163
Description
The MCP HTTP server (
qmd mcp --http) crashes when handling a second request with the following error:Stack trace:
Root Cause
In
src/mcp.ts:556-558, a singleWebStandardStreamableHTTPServerTransportis created at server startup and reused for all incoming HTTP requests:When no
sessionIdGeneratoris provided, the transport operates in stateless mode. The MCP SDK'sWebStandardStreamableHTTPServerTransportsets an internal_hasHandledRequestflag after the first call tohandleRequest()and throws on all subsequent calls to prevent message ID collisions between requests.This design is intentional for serverless/lambda deployments where each invocation creates a fresh transport, but breaks for persistent HTTP servers like
Bun.serve().Expected Behavior
The MCP HTTP server should handle multiple concurrent requests without crashing.
Actual Behavior
"Stateless transport cannot be reused across requests"Reproduction
Solution
Add
sessionIdGeneratorto enable stateful mode, which is the correct pattern for persistent HTTP servers:Why this works:
Mcp-Session-Idheader (part of the MCP Streamable HTTP spec)stop()cleanup remains clean—just one transport to closeFiles Changed
src/mcp.tsline 557 — addsessionIdGenerator: () => crypto.randomUUID()Testing
After fix:
References