MCP bridge leaves upstream HTTP sessions open after calls and discovery
Problem
McpClientEngine.execute() and listTools() create a fresh Streamable HTTP client, but their finally blocks only call client.close(). The OAuth retry closes its second client the same way.
In the locked MCP client SDK, closing the client aborts the local transport; it does not send the DELETE that releases an upstream stateful HTTP session. A remote server that issues Mcp-Session-Id therefore retains the session until its own expiry policy reclaims it. Repeated bridged calls and discovery can consume the upstream session cap.
Reproduction
Reproduced against main at 6ed36dca with the real MCP SDK and an in-process HTTP server:
- The server returns
Mcp-Session-Idwith a valid initialize response and tracks that ID until DELETE. - Call
McpClientEngine.execute()orlistTools(). - The tool result/list is returned, but the server receives no DELETE and still holds the session.
- With a tool request rejected once with 401 and a successful renewed-token retry, both initialized sessions remain open.
The regression fixture uses no external service or real credentials. Before the fix, the focused engine tests have 39 passing and 10 failing checks; the failures cover the missing termination and bounded-cleanup behavior.
Proposed fix
- Best-effort
transport.terminateSession()before local close in normal execution, discovery and the OAuth retry. - Bound the termination wait to five seconds, then close locally so a server that never answers DELETE cannot hang an otherwise completed call.
- Preserve the original result/error if cleanup fails. Keep session headers/authentication attached to their own transport, and retain the existing guarded fetch.
- Cover stateful/stateless servers, failed calls, retries, refused termination and the cleanup deadline. Update the bridge documentation.
This matches the lifecycle already used by the CLI (packages/cli/src/mcp.ts, #1024). The MCP session-management specification recommends DELETE when a client no longer needs a session and allows the server to refuse it with 405.
Deployment behavior
Same backend connector engine on Cloud and self-hosted. No schema, authentication-policy or public API change; no ee/ changes.
I am working on the focused fix and regression tests.