Conversation
…essions Both the streamable-HTTP (/graph_mcp) and legacy SSE (/sse) routes wired MCPHttpServer / the SSE handler to a single module-level `graphServer` singleton. The MCP SDK's Server/Protocol only supports one active transport at a time -- calling connect() again on the same instance throws "Already connected to a transport..." (or, on older SDK versions, silently corrupts the previous session instead of throwing at all -- see modelcontextprotocol/typescript-sdk#1405, which is the exact same anti-pattern). In practice this meant the very first client to connect to a long-lived stakgraph-mcp process would work, and every connection after that (a second question, a second user, even a health-check probe hitting /sse) would permanently break until the process restarted. - server.ts: replace the exported `graphServer` singleton with a `createGraphServer()` factory that builds a fresh Server (with all the same request handlers/lifecycle hooks) per call. MCPHttpServer already expected a factory shape (`serverFactory: () => MCPServerLike`) and already creates a new session-keyed entry per connection in createSession() -- it was just being handed a closure that returned the same instance every time. - sse.ts: same fix, plus a related bug this masked -- `currentTransport` was a single shared variable, so even independent of the Server singleton issue, /messages could only ever route to whichever SSE client connected *last*, silently misdirecting every other concurrent client's messages. Track transports in a sessionId-keyed map instead (using SSEServerTransport's own generated sessionId, echoed back via the standard ?sessionId= query param), matching the pattern MCPHttpServer already uses correctly for the streamable-HTTP route. No behavior change for the common case of a single client talking to a short-lived process; this only changes behavior once a second session is attempted against the same running server.
…rkflow_dispatch Upstream's workflow pushes to ghcr.io/stakwork/stakgraph-mcp, which this fork's GITHUB_TOKEN has no write access to. Also drops the separate arm64 job (ubuntu-arm64 runner label isn't guaranteed on a personal-account fork, and the target EKS nodes are amd64 only), and adds workflow_dispatch so a build can be triggered without cutting a GitHub Release first.
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.
Bug
mcp/src/tools/server.tswires both the streamable-HTTP (/graph_mcp) and legacy SSE (/sse) routes to a single, module-levelgraphServersingleton:The MCP SDK's
Server/Protocolonly supports one active transport at a time. Callingconnect()again on the same instance throws:(or, on older
@modelcontextprotocol/sdkversions from before modelcontextprotocol/typescript-sdk#1405 was fixed, it silently corrupts the previous session instead of throwing at all — same root cause, different symptom.)Impact: the very first client to connect to a long-lived
stakgraph-mcpprocess works fine. Every connection after that — a second question, a second user, even a k8s readiness/liveness probe hitting/sse— permanently breaks the MCP endpoint until the process is restarted. This reproduces reliably in any deployment where the process stays up between requests (e.g. a Kubernetes Deployment), which is the norm outside of quick local dev loops where frequent restarts mask it.This regression traces back to #1371 ("refactor: consolidate mcp server code"). Before that PR,
mcp/src/handler/server.ts'sMCPHttpServertook aserverFactory: () => McpServerand actually called it fresh per session — the consolidation accidentally collapsed that into a factory closure that always returns the same object.Fix
server.ts: replace the exportedgraphServersingleton with acreateGraphServer()factory that builds a freshServer(with the same request handlers / lifecycle hooks) on every call.MCPHttpServeralready expects exactly this shape (serverFactory: () => MCPServerLike) and already key's its internal session map correctly — it was just being handed a closure that returned the same instance every time.sse.ts: same fix, plus a related bug this was masking —currentTransportwas a single shared variable, so independent of theServersingleton issue,/messagescould only ever route to whichever SSE client connected last, silently misdirecting every other concurrent client's messages. This now tracks transports in asessionId-keyed map (usingSSEServerTransport's own generatedsessionId, echoed back via the standard?sessionId=query param on/messages), matching the patternMCPHttpServeralready uses correctly for the streamable-HTTP route.No behavior change for the common single-client/short-lived-process case; this only changes behavior once a second session is attempted against the same running server (which now works, instead of throwing/misrouting).
Testing
Verified the change is syntactically valid (
esbuildparse-check on both files, no full local build available in my environment). I don't have a way to run the full test suite here — happy to address CI feedback if anything else needs adjusting.