You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
server-everything's Streamable HTTP transport keeps one server instance, transport and event store per 2025-era session, and removed one only when its client sent a DELETE (or at shutdown). A session whose client initialized and went away stayed in memory for the life of the process.
A session with no request in flight and no stream open for SESSION_IDLE_TIMEOUT_MILLIS (default 30 minutes, 0 disables it) is now closed by the server. Closing the transport runs the session's existing onclose path, the one a DELETE takes, so it leaves the map and its cleanup() runs (timers, subscriptions, simulated-update intervals). Its next request gets the existing 404 Session not found.
transports/idle-sessions.ts (new) decides when: every request served for a session is held as activity until its response closes, and the idle clock starts when the last one closes. A client holding its standalone GET stream open, or waiting on a long tool call, is never evicted. Idle sessions are looked for on one unref'd timer, every tenth of the timeout (between 1 s and 60 s), cleared on shutdown.
transports/streamableHttp.ts supplies what: it registers sessions, holds the POST and GET requests, and closes an idle session's transport. createApp() takes an optional { sessionIdleTimeoutMillis } so a test can serve a short timeout.
transports/sse.ts is not changed. The issue asks for the same on HTTP+SSE, but an HTTP+SSE session is its GET /sse stream: the SDK's SSEServerTransport closes on the response's close event, which already removes the session and runs cleanup() (pinned by the existing "Client Disconnected" test in sse.test.ts). A session there cannot be abandoned without its socket closing, and one whose socket is open is a live stream, which this change deliberately never evicts. Measured below.
No maximum session count (the issue's "possibly"): it is a second setting with its own refusal semantics, and idle eviction is what the issue's acceptance criteria ask for.
Server Details
Server: everything
Changes to: the Streamable HTTP transport (2025-era sessions), a new environment variable, README and docs/
Motivation and Context
#5558. The spec lets a server do this: "The server MAY terminate the session at any time, after which it MUST respond to requests containing that session ID with HTTP 404 Not Found", and a client that gets that 404 "MUST start a new session by sending a new InitializeRequest" (2025-11-25 transports, session management). So besides bounding memory, a short timeout gives a client developer a way to exercise their 404 recovery against this server. The SDK (@modelcontextprotocol/server 2.4.0, @modelcontextprotocol/node 2.1.2) offers no idle or session timeout of its own. The other TypeScript servers here serve 2025-era HTTP statelessly and hold no sessions, so everything is the only one with this to manage.
Reported with a reproduction and measurements by Pierpaolo1223 in a comment on the issue.
How Has This Been Tested?
Measured, before (origin/v2/main @ 7f383a8, my own script: createApp() served in-process under --expose-gc, heap read after forced GC):
Scenario
Sessions
Heap before → after
Per session
Streamable HTTP, initialize only, never closed
500
18.4 → 64.2 MiB
93.9 KiB
Streamable HTTP, initialize only, never closed
1500
18.4 → 145.1 MiB
86.5 KiB
Streamable HTTP, initialize + initialized, never closed
500
18.4 → 65.0 MiB
95.5 KiB
Streamable HTTP, each session ended with DELETE
3000
18.4 → 22.5 MiB
1.4 KiB
HTTP+SSE, GET /sse opened then the socket dropped
3000
19.2 → 22.7 MiB
1.2 KiB
That is consistent with the reporter's figure (about 1,265 sessions exhausting a 128 MiB heap), and shows HTTP+SSE already releases a dropped session.
Measured, after (this branch, idle timeout 2 s): 1000 abandoned sessions took the heap from 18.4 to 105.4 MiB; after the timeout it was back to 24.5 MiB, the server logged 1000 evictions, and a ping on the last session id returned 404 {"jsonrpc":"2.0","error":{"code":-32001,"message":"Session not found"},"id":99}.
Tests (fake timers, no real sleeps, no retries):
__tests__/idle-sessions.test.ts (new): how SESSION_IDLE_TIMEOUT_MILLIS is read; a session evicted once, only after the timeout; a session with an open response kept, and timed from when its last response closes; the clock restarting on each request; the sweep interval's bounds; 0 running no timer; stop().
__tests__/streamable-http.test.ts, "idle sessions (everything: evict idle Streamable HTTP and SSE sessions #5558)": against a real createApp() over HTTP with only setInterval and Date faked, an abandoned session that had started simulated logging is closed, its logging timer is gone, and its next request gets the 404, while a session idle just as long with an open GET stream is still served, and is evicted in its turn once that stream closes; a session in regular use outlives several timeouts; 0 evicts nothing; a close that fails is logged; shutdown stops the timer.
Coverage: idle-sessions.ts 100 on all four dimensions; streamableHttp.ts 100 / 97.82 (branches) / 100 / 100.
Clients, against the built server started with SESSION_IDLE_TIMEOUT_MILLIS=3000 PORT=3917 node src/everything/dist/index.js streamableHttp:
Inspector CLI 2.12.0, 2025-11-25 (--protocol-era legacy --method tools/call --tool-name echo --tool-arg message=legacy): {"result":{"content":[{"type":"text","text":"Echo: legacy"}]}}, exit 0. The CLI exits without sending a DELETE, so it leaves exactly the abandoned session the issue describes; the server then logged Session 332ba19a-… idle for 3000 ms, closing it and Transport closed for session 332ba19a-…, removing from transports map.
Inspector CLI 2.12.0, 2026-07-28 (--protocol-era modern, same call): the Echo: modern result with io.modelcontextprotocol/serverInfo in _meta, exit 0; no session is created, so nothing is tracked or evicted.
Raw HTTP, 2025-11-25: initialize, then ping → 200 {"result":{},"jsonrpc":"2.0","id":2}; five seconds later the same ping → 404 {"jsonrpc":"2.0","error":{"code":-32001,"message":"Session not found"},"id":2}. With SESSION_IDLE_TIMEOUT_MILLIS=0 the second ping is 200.
SDK client, 2025-11-25 (Client over StreamableHTTPClientTransport, which keeps the standalone GET stream open): connected, sent nothing for 7 s (more than twice the timeout), then echo → [{"type":"text","text":"Echo: still here"}]. An open stream keeps the session.
LLM client, Claude Code 2.1.327 over {"type":"http","url":"http://localhost:3917/mcp"} with --strict-mcp-config, prompted to run sleep 8 and then "call the echo tool of the everything server with the message 'llm-smoke' and reply with exactly the text it returned": Echo: llm-smoke. The server log shows no session initialized, so it negotiated 2026-07-28 and is unaffected; this client gives no way to select the 2025 era, so the 2025-era runs are the three above.
Not run: the Inspector web UI through Playwright. The change has no UI-specific surface; the 2025-era open-stream case it would show is the SDK client run above.
Breaking Changes
No configuration change is needed. One behavior changes for 2025-era Streamable HTTP clients: a session left with no open stream and no request for 30 minutes is ended, and its next request gets 404, which the spec requires a client to answer by initializing again. SESSION_IDLE_TIMEOUT_MILLIS=0 restores the old behavior.
Types of changes
Bug fix (non-breaking change which fixes an issue)
New feature (non-breaking change which adds functionality)
Breaking change (fix or feature that would cause existing functionality to change)
I have added a changeset (npm run changeset) if this changes what a TypeScript server publishes
I have tested this with an LLM client
My code follows the repository's style guidelines
New and existing tests pass locally
I have added appropriate error handling
I have documented all environment variables and configuration options
Additional context
A session is evicted up to a tenth of the timeout late (60 s at most), because one timer sweeps all sessions rather than each session holding its own.
A server-to-client request left unanswered by a client that has gone (the roots request sent after initialized, say) does not count as activity: with no stream open and no request in flight, nobody is there to answer it. It is rejected when the session closes, and logged as it already was on any close.
Idle eviction bounds what abandoned sessions cost over time; it does not cap how many sessions can be opened inside one timeout. The issue records that this is not treated as a security boundary for this server (loopback by default, no authentication, a test server).
A 2025-era Streamable HTTP session holds a server instance, a transport and
an event store until its client sends a DELETE. A client that initializes
and goes away never does, so each abandoned session stayed in memory for the
life of the process: about 90 KiB of heap apiece, with no bound.
A session with no request in flight and no stream open for
SESSION_IDLE_TIMEOUT_MILLIS (30 minutes by default; 0 disables it) is now
closed by the server, which runs the same onclose path as a DELETE, so it is
removed and its cleanup() runs. Its next request is answered 404 "Session
not found", the spec's signal for the client to initialize again. A request
counts as activity until its response closes, so a session with an open SSE
stream or a running tool call is never evicted.
The HTTP+SSE transport is unchanged: its session is its GET /sse stream, and
it is already cleaned up when that stream's socket closes. 2026-07-28
requests have no sessions.
Reported, with measurements, by Pierpaolo1223 on the issue.
Closes#5558
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Signed-off-by: cliffhall <cliff@futurescale.com>
Base: 7f383a8e0f34 (Merge pull request #5597 from modelcontextprotocol/changeset-release/v2/main)
Head: the build in this checkout, at ca570c28e569 (Merge c8e998e into 7f383a8)
Diffed because it was named (--server).
✅ No interface changes detected.
Tools
Prompts
Resources
Resource templates
Base
22
4
8
5
Head
22
4
8
5
Updated by this run for c8e998e9872a9f21208e543c5044abc70ef07cba.
The reason will be displayed to describe this comment to others. Learn more.
🟢 Approval recommended
No active review issues are blocking the configurable idle-session eviction change.
No open issues 🎉
What changed in this PR
This PR addresses #5558 by adding configurable idle-session eviction to server-everything’s 2025-era Streamable HTTP transport, releasing resources held by abandoned sessions.
Changes:
Adds a 30-minute default idle timeout, configurable through SESSION_IDLE_TIMEOUT_MILLIS; 0 disables eviction.
Preserves sessions with open responses or streams and uses the existing transport cleanup path when evicting idle sessions.
Adds timing and HTTP integration tests, documentation, and a minor changeset. Modern sessionless requests and HTTP+SSE remain unchanged.
File
Description
src/everything/transports/streamableHttp.ts
Integrates activity tracking, idle eviction, and shutdown handling.
src/everything/transports/idle-sessions.ts
Implements timeout parsing, response tracking, and periodic eviction checks.
src/everything/README.md
Documents configuration, idle behavior, and client recovery.
src/everything/docs/structure.md
Describes the new helper and transport integration.
src/everything/docs/startup.md
Explains idle eviction in the legacy session lifecycle.
Copilot round 1: clean (no findings, no suppressed comments). The review loop ends here on the first clean round; nothing was pushed after a1137b8e, whose npm run local:gate exited 0.
…without a mention
The idle-session change had re-aligned both environment-variable tables in
the README, which Markdown is not formatted for in this repo, so the diff
touched the unrelated GZIP table. The tables are back in their existing
form, with only the SESSION_IDLE_TIMEOUT_MILLIS row added. The changeset
credits the reporter with a profile link rather than a bare mention, which
would notify them from the changelog and from every regeneration of the
Version Packages PR.
Part of #5558
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Signed-off-by: cliffhall <cliff@futurescale.com>
The reason will be displayed to describe this comment to others. Learn more.
🔵 Needs a closer look
A failed close leaves the transport retained but untracked, preventing later idle-eviction retries, and the rejection test verifies logging rather than recovery.
No open issues 🎉
1 previously missed issue
In code that hasn't changed since last review
Failed idle close leaves the transport retained and untracked
If close() rejects, watchIdleSessions has already deleted the session's activity entry, but this catch leaves its transport in transports. Subsequent sweeps never retry it, and idle.hold() becomes a no-op for that session, so it remains retained until an explicit DELETE or shutdown—the permanent retention this change is meant to prevent. Keep failed evictions tracked while preserving any in-flight activity, or explicitly remove and clean up the failed transport; extend the rejection test to verify recovery rather than only logging.
When closing an idle session's transport rejected, the session stayed in the
transport map but was no longer watched, so no later look tried it again and
it was kept until a DELETE or shutdown: the retention this change removes.
The session is now watched again after a failed close, so it is tried again
after another idle timeout. The test for the failure now also checks that
the session is still served, and is evicted at the next timeout.
Part of #5558
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Signed-off-by: cliffhall <cliff@futurescale.com>
Copilot round 2 (on 780f0c97, pushed after rollup reconciliation for tracker #5607 restored the README tables' existing form and replaced the changeset's bare mention of the reporter with a profile link): one finding, in the review's summary with no thread, so it is answered here.
Failed idle close leaves the transport retained and untracked (streamableHttp.ts): fixed in c8e998e9. It is a defect in what this PR added: a session whose close() rejected stayed in the transport map but was no longer watched, so it was kept until a DELETE or shutdown. The session is now watched again after a failed close and tried again after another idle timeout. The failure test now also checks that the session is still served after the failed close and is evicted at the next timeout.
npm run local:gate exited 0 on c8e998e9. Requesting round 3.
Copilot round 3 (on c8e998e9): clean, no findings and no suppressed comments. The review loop ends here on the first clean round after the last push; npm run local:gate exited 0 on c8e998e9, and nothing was pushed after it.
This branch has not been deployed
No deployments
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
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 #5558
Description
server-everything's Streamable HTTP transport keeps one server instance, transport and event store per 2025-era session, and removed one only when its client sent aDELETE(or at shutdown). A session whose client initialized and went away stayed in memory for the life of the process.A session with no request in flight and no stream open for
SESSION_IDLE_TIMEOUT_MILLIS(default 30 minutes,0disables it) is now closed by the server. Closing the transport runs the session's existingonclosepath, the one aDELETEtakes, so it leaves the map and itscleanup()runs (timers, subscriptions, simulated-update intervals). Its next request gets the existing404 Session not found.transports/idle-sessions.ts(new) decides when: every request served for a session is held as activity until its response closes, and the idle clock starts when the last one closes. A client holding its standaloneGETstream open, or waiting on a long tool call, is never evicted. Idle sessions are looked for on one unref'd timer, every tenth of the timeout (between 1 s and 60 s), cleared on shutdown.transports/streamableHttp.tssupplies what: it registers sessions, holds the POST and GET requests, and closes an idle session's transport.createApp()takes an optional{ sessionIdleTimeoutMillis }so a test can serve a short timeout.transports/sse.tsis not changed. The issue asks for the same on HTTP+SSE, but an HTTP+SSE session is itsGET /ssestream: the SDK'sSSEServerTransportcloses on the response'scloseevent, which already removes the session and runscleanup()(pinned by the existing "Client Disconnected" test insse.test.ts). A session there cannot be abandoned without its socket closing, and one whose socket is open is a live stream, which this change deliberately never evicts. Measured below.Server Details
everythingdocs/Motivation and Context
#5558. The spec lets a server do this: "The server MAY terminate the session at any time, after which it MUST respond to requests containing that session ID with HTTP 404 Not Found", and a client that gets that 404 "MUST start a new session by sending a new
InitializeRequest" (2025-11-25 transports, session management). So besides bounding memory, a short timeout gives a client developer a way to exercise their 404 recovery against this server. The SDK (@modelcontextprotocol/server2.4.0,@modelcontextprotocol/node2.1.2) offers no idle or session timeout of its own. The other TypeScript servers here serve 2025-era HTTP statelessly and hold no sessions, soeverythingis the only one with this to manage.Reported with a reproduction and measurements by Pierpaolo1223 in a comment on the issue.
How Has This Been Tested?
Measured, before (
origin/v2/main@ 7f383a8, my own script:createApp()served in-process under--expose-gc, heap read after forced GC):initializeonly, never closedinitializeonly, never closedinitialize+initialized, never closedDELETEGET /sseopened then the socket droppedThat is consistent with the reporter's figure (about 1,265 sessions exhausting a 128 MiB heap), and shows HTTP+SSE already releases a dropped session.
Measured, after (this branch, idle timeout 2 s): 1000 abandoned sessions took the heap from 18.4 to 105.4 MiB; after the timeout it was back to 24.5 MiB, the server logged 1000 evictions, and a
pingon the last session id returned404 {"jsonrpc":"2.0","error":{"code":-32001,"message":"Session not found"},"id":99}.Tests (fake timers, no real sleeps, no retries):
__tests__/idle-sessions.test.ts(new): howSESSION_IDLE_TIMEOUT_MILLISis read; a session evicted once, only after the timeout; a session with an open response kept, and timed from when its last response closes; the clock restarting on each request; the sweep interval's bounds;0running no timer;stop().__tests__/streamable-http.test.ts, "idle sessions (everything: evict idle Streamable HTTP and SSE sessions #5558)": against a realcreateApp()over HTTP with onlysetIntervalandDatefaked, an abandoned session that had started simulated logging is closed, its logging timer is gone, and its next request gets the 404, while a session idle just as long with an openGETstream is still served, and is evicted in its turn once that stream closes; a session in regular use outlives several timeouts;0evicts nothing; a close that fails is logged; shutdown stops the timer.idle-sessions.ts100 on all four dimensions;streamableHttp.ts100 / 97.82 (branches) / 100 / 100.Clients, against the built server started with
SESSION_IDLE_TIMEOUT_MILLIS=3000 PORT=3917 node src/everything/dist/index.js streamableHttp:--protocol-era legacy --method tools/call --tool-name echo --tool-arg message=legacy):{"result":{"content":[{"type":"text","text":"Echo: legacy"}]}}, exit 0. The CLI exits without sending aDELETE, so it leaves exactly the abandoned session the issue describes; the server then loggedSession 332ba19a-… idle for 3000 ms, closing itandTransport closed for session 332ba19a-…, removing from transports map.--protocol-era modern, same call): theEcho: modernresult withio.modelcontextprotocol/serverInfoin_meta, exit 0; no session is created, so nothing is tracked or evicted.initialize, thenping→200 {"result":{},"jsonrpc":"2.0","id":2}; five seconds later the sameping→404 {"jsonrpc":"2.0","error":{"code":-32001,"message":"Session not found"},"id":2}. WithSESSION_IDLE_TIMEOUT_MILLIS=0the secondpingis200.ClientoverStreamableHTTPClientTransport, which keeps the standaloneGETstream open): connected, sent nothing for 7 s (more than twice the timeout), thenecho→[{"type":"text","text":"Echo: still here"}]. An open stream keeps the session.{"type":"http","url":"http://localhost:3917/mcp"}with--strict-mcp-config, prompted to runsleep 8and then "call the echo tool of the everything server with the message 'llm-smoke' and reply with exactly the text it returned":Echo: llm-smoke. The server log shows no session initialized, so it negotiated 2026-07-28 and is unaffected; this client gives no way to select the 2025 era, so the 2025-era runs are the three above.Breaking Changes
No configuration change is needed. One behavior changes for 2025-era Streamable HTTP clients: a session left with no open stream and no request for 30 minutes is ended, and its next request gets
404, which the spec requires a client to answer by initializing again.SESSION_IDLE_TIMEOUT_MILLIS=0restores the old behavior.Types of changes
Checklist
npm run changeset) if this changes what a TypeScript server publishesAdditional context
initialized, say) does not count as activity: with no stream open and no request in flight, nobody is there to answer it. It is rejected when the session closes, and logged as it already was on any close.🤖 Generated with Claude Code