Repository navigation
fix(core): terminate the Streamable HTTP session on disconnect - #2926
Merged
Merged
Conversation
InspectorClient.disconnect() closed the SDK client but never sent the spec's session-terminating DELETE, so a stateful server kept every session the Inspector opened. Send it best-effort and bounded before close(), via the SDK transport's terminateSession() so it carries the session's own fetch stack and auth/custom headers (a 405 counts as success; any failure or hang falls through to local teardown). The web client's RemoteClientTransport has no session of its own, so the web backend now terminates the upstream session on /api/mcp/disconnect and on the detach-grace reap of a closed tab. The HTTP test server gains a DELETE /mcp route plus getSessionTerminations() and getActiveSessionIds() so tests can assert the DELETE arrived and the server-side session is gone. Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: cliffhall <[email protected]>
There was a problem hiding this comment.
🟡 Changes recommended
Synchronous terminateSession() throws can bypass transport closure and state cleanup, while the integration test’s 50ms grace can prematurely reap sessions and cause false failures.
2 open issues
What changed in this PR
Addresses #2924 by sending a best-effort Streamable HTTP session-termination request before local teardown, reducing server-side session leaks across Inspector clients and load runs.
Changes:
- Adds a shared termination helper with a three-second timeout.
- Integrates termination into direct disconnects and web backend disconnect and detach cleanup.
- Adds test-server tracking, unit tests, and live integration coverage.
| File | Description |
|---|---|
test-servers/src/test-server-http.ts |
Handles session DELETE requests and exposes termination records and active sessions. |
core/mcp/sessionTermination.ts |
Adds bounded, best-effort session termination. |
core/mcp/remote/node/server.ts |
Terminates upstream sessions on disconnect and detach-grace expiry. |
core/mcp/inspectorClient.ts |
Requests session termination before closing the SDK client. |
clients/web/src/test/integration/mcp/inspectorClient-session-termination.test.ts |
Exercises direct and remote termination against a real server. |
clients/web/src/test/core/mcp/sessionTermination.test.ts |
Covers no-op transports, success, rejection, and timeout behavior. |
Review effort: Balanced
…ce in test Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: cliffhall <[email protected]>
Member
Author
|
Copilot round 1: 2 findings, both fixed in 4c31b0c.
No suppressed comments. Requesting round 2. |
Member
Author
|
Copilot review loop closed. Round 2 was clean: no inline comments, no suppressed comments, approval recommended. Both round-1 findings are resolved. |
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 #2924
What
InspectorClient.disconnect()closed the SDK client but never sent the Streamable HTTP session-terminatingDELETE, so a stateful server kept every session the Inspector opened (and--load-connections/--load-churnexhausted session caps). Now every disconnect path ends the server-side session first.core/mcp/sessionTermination.ts(new):terminateSessionBestEffort(transport)calls the SDK transport'sterminateSession()when the transport has one and holds a session id. It goes through the transport's own fetch stack and_commonHeaders(), so theDELETEcarries the same auth (Authorizationfrom the auth provider), custom headers andmcp-protocol-versionas the session's other requests. The SDK already treats 405 as success. The helper never rejects, and it's capped at 3s, so a refused or hungDELETEcan't block local cleanup. After a timeout,close()aborts the request on the transport's controller.InspectorClient.disconnect(): calls it beforeclient.close(), whiledisconnectingis still set. If theDELETEfails, the SDK raisesonerror, and the client ignores it instead of switching toerror. This one change covers the CLI, TUI, mcpdo,--load(including churn), and the direct node transport.core/mcp/remote/node/server.ts): the browser'sRemoteClientTransporthas no session of its own, so the backend now terminates the upstream session on/api/mcp/disconnect(explicit disconnect and pagehide release) and on the detach-grace reap of a closed tab.test-servers/src/test-server-http.ts): adds aDELETE /mcproute on the legacy Streamable HTTP leg, plusgetSessionTerminations()(with headers and status) andgetActiveSessionIds().Not changed:
dropCachedTransport(). It only runs after a pre-auth connect failed, so no session was established. stdio, legacy SSE and the modern (sessionless) era pass through as no-ops.Note on the issue's closing remark:
disconnect()stays non-throwing, as the issue's Expected section asks ("other failures must not block local cleanup"). So churn'scloseFailedwon't count termination failures. Making it count them would mean changingdisconnect()'s contract for every caller.Tests
integration/mcp/inspectorClient-session-termination.test.tsruns against a real Streamable HTTP test server:DELETEarrives withmcp-session-id, a custom settings header andmcp-protocol-version, and the server's session map is empty afterwards.DELETE(500): teardown completes, status endsdisconnected, and noerrorevent fires./api/mcp/disconnect: the backend sends theDELETEand the session is gone.DELETE.Without the fix, the three positive cases fail.
core/mcp/sessionTermination.test.ts: no-op cases, success, rejection, timeout (fake timers), no unhandled rejection after a timeout.npm run formatandnpm run local:gateare green, rebased on #2923.🤖 Generated with Claude Code