feat: support stateless HTTP mode for MCP server - #123
Merged
Merged
Conversation
Add MCP_STATELESS_HTTP (default true), passed through to streamable_http_app(stateless_http=...). Stateless mode creates a fresh transport per request instead of tracking MCP sessions, so replicas can sit behind a plain round-robin load balancer with no sticky sessions. The setting only reaches the streamable-http factory — sse_app() has no such parameter and stdio has no HTTP layer, so it is inert for both. A validator refuses to start only when it is set explicitly on one of those transports; the defaulted-on value stays inert so existing sse/stdio deployments are unaffected. Extract build_http_app() out of main() so the transport branch is testable. _wrap_http_lifespan is unchanged and still required: the SDK enters the MCP lifespan once per session manager and reuses that state across stateless requests, so the Qdrant store is not re-initialised per request (verified against a running container). Closes #122 Co-Authored-By: Claude Opus 5 (1M context) <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.
Closes #122.
Adds
MCP_STATELESS_HTTP, passed through asstreamable_http_app(stateless_http=...). Stateless mode creates a fresh transport per request instead of tracking MCP sessions, so replicas can sit behind a plain round-robin load balancer with no sticky sessions.true— behaviour change on upgradeExisting
streamable-httpdeployments become stateless after this lands: clients no longer receive anmcp-session-id, and server→client requests (sampling/elicitation) are unavailable. semcode uses neither today — no tool, prompt or route touches the MCPContext, and the server advertiseslistChanged: falseacross tools, prompts and resources — so this is afeat:rather than a breaking change. Operators who rely on session affinity can setMCP_STATELESS_HTTP=false.Older transports are unaffected
MCP_TRANSPORTstill accepts all three values.stateless_httponly exists on the streamable-http factory —sse_app()has no such parameter and stdio has no HTTP layer — so the setting is inert for both. The validator refuses to start only when it is set explicitly on one of those transports (keyed onmodel_fields_set); aMCP_TRANSPORT=stdiouser who never heard of this setting sees zero change.On the lifespan question raised in the issue
Not a blocker. The streamable app's Starlette lifespan is
session_manager.run(), which enters the MCP lifespan exactly once per manager and caches it as_lifespan_state;stateless=Trueonly changes per-request transport creation and reuses that same state._wrap_http_lifespanis therefore unchanged and still required, and the Qdrant store is not re-initialised per request.Two corrections to the issue's premises: the installed SDK is mcp 2.1.1, not 1.28.1 (the
mcp>=2.0.0pin needs no change), andsse_app()does not acceptstateless_http.Changes
server/config.py—mcp_stateless_httpsetting +_reject_stateless_on_unsupported_transportvalidatorserver/main.py— extractbuild_http_app()out ofmain()so the transport branch is testableREADME.md,docs/docs/configuration.md,.env.example— document the settingtests/test_config.py,tests/test_main.py(new) — 342 tests passVerification
Automated: default / opt-out / inert-for-sse-and-stdio / explicit-conflict-rejected in config, plus
build_http_app()asserting the flag reachessession_manager.statelessand that SSE still builds with the flag at its default.Manual, against a real
docker compose up --buildstack:200with nomcp-session-idheader; two independenttools/listcalls both returned all 10 tools"Qdrant collections ready"appears exactly once in the container logs after many requestsPOST /reindexstill streams NDJSONMCP_STATELESS_HTTP=false→mcp-session-idreturned, opt-out intactMCP_TRANSPORT=ssewith the flag at defaulttrue→ starts clean,/ssestill issues asession_idMCP_TRANSPORT=stdio→ imports clean, per-session lifespan still attachedMCP_TRANSPORT=sse MCP_STATELESS_HTTP=true→ refuses to start with the intended error🤖 Generated with Claude Code