Skip to content

fix(core): terminate the Streamable HTTP session on disconnect - #2926

Merged
cliffhall merged 2 commits into
v2/mainfrom
v2/fix/2924-terminate-http-session
Nov 5, 2026
Merged

cliffhall merged 2 commits into
v2/mainfrom
v2/fix/2924-terminate-http-session

Conversation

@cliffhall

Copy link
Copy Markdown
Member

Closes #2924

What

InspectorClient.disconnect() closed the SDK client but never sent the Streamable HTTP session-terminating DELETE, so a stateful server kept every session the Inspector opened (and --load-connections / --load-churn exhausted session caps). Now every disconnect path ends the server-side session first.

  • core/mcp/sessionTermination.ts (new): terminateSessionBestEffort(transport) calls the SDK transport's terminateSession() when the transport has one and holds a session id. It goes through the transport's own fetch stack and _commonHeaders(), so the DELETE carries the same auth (Authorization from the auth provider), custom headers and mcp-protocol-version as 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 hung DELETE can't block local cleanup. After a timeout, close() aborts the request on the transport's controller.
  • InspectorClient.disconnect(): calls it before client.close(), while disconnecting is still set. If the DELETE fails, the SDK raises onerror, and the client ignores it instead of switching to error. This one change covers the CLI, TUI, mcpdo, --load (including churn), and the direct node transport.
  • Web backend (core/mcp/remote/node/server.ts): the browser's RemoteClientTransport has 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 server (test-servers/src/test-server-http.ts): adds a DELETE /mcp route on the legacy Streamable HTTP leg, plus getSessionTerminations() (with headers and status) and getActiveSessionIds().

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's closeFailed won't count termination failures. Making it count them would mean changing disconnect()'s contract for every caller.

Tests

  • integration/mcp/inspectorClient-session-termination.test.ts runs against a real Streamable HTTP test server:

    • direct: the DELETE arrives with mcp-session-id, a custom settings header and mcp-protocol-version, and the server's session map is empty afterwards.
    • direct, refused DELETE (500): teardown completes, status ends disconnected, and no error event fires.
    • remote (web), /api/mcp/disconnect: the backend sends the DELETE and the session is gone.
    • remote (web), detach-grace reap of a closed tab: the backend sends the 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 format and npm run local:gate are green, rebased on #2923.

🤖 Generated with Claude Code

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]>
@cliffhall cliffhall added the v2 Issues and PRs for v2 label Nov 5, 2026
@cliffhall
cliffhall requested a balanced review from Copilot November 5, 2026 17:49

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 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

Comment thread core/mcp/sessionTermination.ts Resolved
…ce in test

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: cliffhall <[email protected]>
@cliffhall

Copy link
Copy Markdown
Member Author

Copilot round 1: 2 findings, both fixed in 4c31b0c.

  • Reap test grace (50ms bounded the connect → events attach too): now 3s attachment-safe, 15s reap wait.
  • A synchronous terminateSession() throw could reject the helper and skip teardown. It's now invoked inside a Promise executor, and a unit test covers it.

No suppressed comments. Requesting round 2.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

No active review issues remain for the changes that terminate server-side sessions before local disconnect cleanup.

No open issues 🎉

2 resolved since last review

Review effort: Balanced

@cliffhall

Copy link
Copy Markdown
Member Author

Copilot review loop closed. Round 2 was clean: no inline comments, no suppressed comments, approval recommended. Both round-1 findings are resolved.

@cliffhall
cliffhall merged commit 2a1fb91 into v2/main Nov 5, 2026
12 checks passed
@cliffhall
cliffhall deleted the v2/fix/2924-terminate-http-session branch November 5, 2026 18:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

v2 Issues and PRs for v2

Projects

None yet

Development

Successfully merging this pull request may close these issues.

InspectorClient.disconnect() never terminates a Streamable HTTP session (no DELETE)

2 participants