Skip to content

fix(mcp): fix resource leak and stale-header bug in MCP client - #890

Closed
h4sht wants to merge 7650 commits into
CodebuffAI:mainfrom
h4sht:fix/mcp-client-resource-leak
Closed

h4sht wants to merge 7650 commits into
CodebuffAI:mainfrom
h4sht:fix/mcp-client-resource-leak

Conversation

@h4sht

@h4sht h4sht commented Jul 22, 2026

Copy link
Copy Markdown

Summary

Fixes two related bugs in the MCP client module that cause resource leaks and stale connections:

1. Memory/Connection Leak — No Client Cleanup

The `runningClients` map and `listToolsCache` in `common/src/mcp/client.ts` grew without bound. Once an MCP client was connected, it was never removed from the registry, even after the transport died or the session ended. In long-running sessions with MCP tools, this accumulates stale connections and never releases resources.

Fix: Added `closeMCPClient(clientId)` — closes the underlying transport (stdio process or HTTP/SSE connection) and removes entries from both `runningClients` and `listToolsCache`.

2. Stale Headers in Config Hash

`hashConfig()` did not include `config.headers` when computing the cache key for HTTP and SSE transports. Two MCP server configs with different auth tokens but the same URL were treated as the same client, returning a cached client with stale credentials.

Fix: Added `headers: config.headers` to the hash for both `http` and `sse` config types.

3. Automatic Cleanup on Tool Call Failure

When an MCP tool call failed (broken connection, dead stdio process), the dead client remained in the cache. Subsequent calls would reuse the same broken connection and fail with the same error.

Fix: In `sdk/src/run.ts`, the MCP tool call catch block now calls `closeMCPClient()` to evict the dead client so the next call reconnects with a fresh transport.

Files changed

  • `common/src/mcp/client.ts` — Fixed `hashConfig`, added `closeMCPClient` export
  • `sdk/src/run.ts` — Imported `closeMCPClient` and integrated into MCP error handling

Known limitations

  • `closeMCPClient` only evicts clients reactively (on tool call failure). There is no proactive liveness check in `getMCPClient` for transports that die silently (e.g., stdio process exits without a pending call). This means a stale entry could serve one bad request before being evicted. A follow-up could add a health check or TTL-based eviction.

github-actions Bot added 30 commits July 13, 2026 00:39
Source: CodebuffAI/freebuff-private@5d478ef87ad24a0d8196017955acbc06287a8817
Source: CodebuffAI/freebuff-private@af0ed51e8eb87c4d757f5907e03aed3de2084588
Source: CodebuffAI/freebuff-private@b82f04601da5595e3a455d650e995c96d2a287f3
Source: CodebuffAI/freebuff-private@291b36e4c5650445be78b5b9a8a8c0139ecbd75d
Source: CodebuffAI/freebuff-private@8ef8f619c1b5c0f870b6b86fdd63f26f863fa329
Source: CodebuffAI/freebuff-private@f9aa87f919b2fe50b29c168dcb686ad20f836bc8
Source: CodebuffAI/freebuff-private@1bf96b7124195b02ba5f7e6e7b55c0320ba45ee2
Source: CodebuffAI/freebuff-private@5eaf3b400bded126723813d5ff59571c11850f68
Source: CodebuffAI/freebuff-private@cd607420f725cb9e35ddb0fdefee49106ab4c043
Source: CodebuffAI/freebuff-private@c2b9942658dda50e01bca873198b63341cbcac74
Source: CodebuffAI/freebuff-private@e47ca1533b01b76747bbf9ed25563bd059483c4e
Source: CodebuffAI/freebuff-private@e4448abe9165b7779a06131fb17325c8dd14a420
Source: CodebuffAI/freebuff-private@270ce1535ca77c83db9d7cb2b234d87644617976
Source: CodebuffAI/freebuff-private@5aaf5764ac6219d8444dab840b6881afe2289fce
Source: CodebuffAI/freebuff-private@23c28e63d23e44c4aedf2a00c03a4181c691e1ac
Source: CodebuffAI/freebuff-private@0558644001974e1658ba13e49e174a51e740c090
Source: CodebuffAI/freebuff-private@a8362648017150c963055019b750bed3ef7112fa
Source: CodebuffAI/freebuff-private@6331fdd8b6bbd43e5a64beb5dfab2acba881bac2
Source: CodebuffAI/freebuff-private@9af5112095054d2e77630742175019b51d5cd338
Source: CodebuffAI/freebuff-private@454311725dcb1af96854f2edd1b3771f27959885
Source: CodebuffAI/freebuff-private@3112d215ad60d002457858cd5b050a8347c6f4d2
Source: CodebuffAI/freebuff-private@f5a8e40a9d3e1163a7e580ee03666e6e01939785
Source: CodebuffAI/freebuff-private@4578f51695e9221b7e7c4d7b3f34f8947e0b1573
Source: CodebuffAI/freebuff-private@d82e557bf69dc2cc34003fceedc8335f8ed00cd9
Source: CodebuffAI/freebuff-private@6af8cd6605433a16000f7c2e5d48c38c9e736729
Source: CodebuffAI/freebuff-private@408c0558614175d986483d75c1c54b3e8b2e4831
Source: CodebuffAI/freebuff-private@a4891449016c82ebd1d6063ce76af0bdba4c6b49
Source: CodebuffAI/freebuff-private@db39af5155524bb76d1913686639bdfc4bb430f6
Source: CodebuffAI/freebuff-private@72534175da7eda9a6afcb5060a6d1a3789e476f7
Source: CodebuffAI/freebuff-private@a291b72dec65fe0c4d5bc2c43d7bb9a792203979
github-actions Bot and others added 22 commits July 21, 2026 18:28
Source: CodebuffAI/freebuff-private@91280276442f9909f46ab084ba9bdb5c81b440a0
Source: CodebuffAI/freebuff-private@1168f0efd7b9bdf1d8a3c18a01f06d9e5fa389a6
Source: CodebuffAI/freebuff-private@65462e322e96cb65c56dc157dd2a65b28bc3c428
Source: CodebuffAI/freebuff-private@211ed813ee8bbaab114ee483337c05d51cbe886d
Source: CodebuffAI/freebuff-private@c2d7a5b11dc21596170d56b8fbae25bae91983cc
Source: CodebuffAI/freebuff-private@61a53b84ec87f270fc39bae2e0a95c0d7115c278
Source: CodebuffAI/freebuff-private@0bad7149b8125afa0c651ebebb02ca7e1ef2d6c1
Source: CodebuffAI/freebuff-private@75571b128e5c3efa6a4a19fe1c20d6d88f6703d7
Source: CodebuffAI/freebuff-private@0c151844c0542e2811eaf53050a6e3262b20a505
Source: CodebuffAI/freebuff-private@e22b9ffae2362ecf7e8ccc8be221c1f202140c1d
Source: CodebuffAI/freebuff-private@5d879d3080ba1cc65562d15af957237c36081a2d
Source: CodebuffAI/freebuff-private@b588ab2fc0b0f1d7912d5132c8a4a836452b1c3a
Source: CodebuffAI/freebuff-private@e00966144e53d596fb4a04a84847508d8c3a682e
Source: CodebuffAI/freebuff-private@a03ee4d78c81e609fe2acfe04e803ba4ebb371d9
Source: CodebuffAI/freebuff-private@c796a36eb773ffe7ba3b66a42d7f231a057190b2
Source: CodebuffAI/freebuff-private@883518c986df67629001feec202f5c50d9651df8
Source: CodebuffAI/freebuff-private@178049c931212b83ab40e9a46b0ef78fe02f25dd
Source: CodebuffAI/freebuff-private@75e02aa5d55b001b74ed7c93e60ef058b14244cc
Source: CodebuffAI/freebuff-private@29ed3efdf266b6ba65ea88bd1912d634851d8039
Source: CodebuffAI/freebuff-private@c7fcf29452fb31cc2cf956548de173bb3ba88ec4
Source: CodebuffAI/freebuff-private@c067fa1da9e8c8293704f43973b9e01635289990
- Fix hashConfig to include headers for HTTP/SSE configs, preventing
  stale auth tokens when two MCP servers differ only by headers.

- Add closeMCPClient() export to clean up client connections and
  cached tool listings, fixing the memory/connection leak where
  runningClients and listToolsCache grew without bound.

- Integrate automatic cleanup in SDK: when an MCP tool call fails,
  the dead client is evicted so the next call reconnects with a
  fresh transport instead of reusing the broken connection.
@chrismindpower369

Copy link
Copy Markdown

Thanks — this addresses two important reliability issues for projects using authenticated HTTP/SSE MCP servers.

Including config.headers in the cache key is especially important: otherwise switching tokens or credentials for the same endpoint can silently reuse the wrong client. Evicting a client after a failed tool call also gives the next request a clean reconnect path.

Would it be possible to add regression tests for:

  • same endpoint with different headers creates distinct clients
  • a failed MCP tool call removes both the client and its listToolsCache entry
  • closeMCPClient() safely handles an already-closed or missing client

The documented limitation around silently dead transports also makes sense. A small follow-up for a liveness check or TTL-based eviction would improve long-running sessions further. Overall, this looks like a valuable fix for real user projects.

@codebuff-team

Copy link
Copy Markdown
Contributor

Good catch on both bugs. The hashConfig fix (including headers for http/sse) is a clear, correct fix for a real staleness bug — two configs differing only by auth header would otherwise collide on the same cached client, which is a legitimate correctness issue worth porting as-is.

The closeMCPClient addition and its use in sdk/src/run.ts's catch block are reasonable, though worth double-checking on the maintainer's side:

  • Eviction is reactive only (as you note in "Known limitations") — a client that dies silently between calls (e.g., stdio process exits without a pending request) will still serve one failed call before eviction. That's an acceptable tradeoff for a small fix, not a blocker.
  • closeMCPClient swallows all errors from client.close(), which is probably fine given the transport may be dead, but it also means genuine close-failures (e.g., a hung process) are invisible. Consider at least a debug log if the codebase has a logger convention.
  • No tests are included. Given the size of the change, a unit test for hashConfig distinguishing two configs by headers, and for closeMCPClient no-oping on unknown ids, would make this easier for a maintainer to accept without re-deriving the fix.

Overall this is a small, well-targeted fix addressing a real caching bug with a clear root cause. Recommend porting after maintainers verify test coverage or add a couple of unit tests. Nice first PR.

@codebuff-team codebuff-team added bot:triaged Classified by the community triage bot pr:port-candidate Worth porting into the private source tree labels Aug 20, 2026
@victorxheng

Copy link
Copy Markdown

Apologies — this PR was auto-closed by GitHub when we force-pushed a history rewrite of this repository (repository maintenance; every commit SHA changed). That was not a judgment on this PR, and GitHub does not allow us to reopen it because the commits it was based on no longer exist in the new history.

If you'd like to continue with this change: rebase your branch onto the new main (or recreate it from a fresh clone) and open a new PR — feel free to link back to this one for context, and we'll pick up the review there.

Sorry for the churn, and thanks for contributing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bot:triaged Classified by the community triage bot pr:port-candidate Worth porting into the private source tree

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants