LibreChat/packages/api
Danny Avila db22188203
👋 fix: Terminate Streamable HTTP MCP Sessions on Teardown (#14252)
* 🧹 fix: Terminate Streamable HTTP MCP sessions on teardown to prevent server-side leaks

The MCP client only called client.close() when discarding a Streamable HTTP
connection, never the spec-mandated HTTP DELETE with the Mcp-Session-Id header.
Stateful MCP servers (the SDK default) keep each session's transport, tasks, and
buffers in memory with no TTL, so every idle eviction, reconnect, or restart
leaked one server-side session.

Add terminateStreamableSession() and call it before client.close() (which aborts
the transport controller and would cancel the in-flight DELETE) on both teardown
paths: disconnect() and the reconnect transport swap. It is bounded by a 5s
timeout so teardown never blocks on a hung server, and swallows errors since
terminateSession() already no-ops without a session id and tolerates a 405.

* 🛡️ fix: Suppress reconnect on failed session DELETE; harden termination tests

Address Codex review on #14252.

- P2: terminateSession() invokes transport.onerror before rejecting on any
  non-405 failure (session already expired, network error, or client.close()
  aborting after the timeout). Our handler turned that into
  connectionChange('error') -> handleReconnection(), reopening the connection
  mid-teardown and re-leaking the session. Detach transport.onerror before
  terminating; the transport is discarded immediately after, so it is safe.
  Add a regression test that fails before this change.
- P3: bind test HTTP servers directly to an ephemeral port (listen(0) + read
  address) instead of probing a port and rebinding, removing a bind/listen
  race under parallel CI.
- P3: null-init the server handles and guard close() so a startup failure
  surfaces the real error instead of a masking TypeError.
2026-07-14 11:58:54 -04:00
..
src 👋 fix: Terminate Streamable HTTP MCP Sessions on Teardown (#14252) 2026-07-14 11:58:54 -04:00
types 🔬 ci: Add TypeScript Type Checks to Backend Workflow and Fix All Type Errors (#12451) 2026-03-28 21:06:39 -04:00
.gitignore
babel.config.cjs
jest.config.mjs 📦 chore: npm audit fix (#13828) 2026-06-17 21:54:04 -04:00
jest.setup.cjs 🌱 fix: Inject Code-Tool Files Into Graph Sessions on First Call (+ read_file Sandbox Fallback) (#12831) 2026-04-27 08:56:39 +09:00
package.json 🧭 feat: Mid-Run Steering and Queued Messages for Agent Runs (#14220) 2026-07-14 10:11:10 -04:00
tsconfig-paths-bootstrap.mjs
tsconfig.build.json 🧑‍💻 refactor: Secure Field Selection for 2FA & API Build Sourcemap (#9087) 2025-08-15 18:55:49 -04:00
tsconfig.json 📦 chore: npm audit fixes and Mongoose 8.23 TypeScript follow-ups (#12996) 2026-05-07 09:47:40 -04:00
tsconfig.spec.json 📦 chore: Update TypeScript Config for TS v7 (#12794) 2026-04-23 12:51:03 -04:00
tsdown.config.mjs 🪟 fix: Cross-Platform Absolute-Path Check in tsdown neverBundle Predicates (#13700) 2026-06-13 11:04:46 -04:00