Repository navigation
fix(strands): close a failed MCP connection only after in-flight calls finish - #101
Merged
brianstrauch merged 2 commits intoOct 16, 2026
Conversation
…s finish When a listTools or callTool Activity failed, the pooled MCP connection was disconnected immediately, so other calls running on it failed with "Connection closed" and were retried. Mark the connection broken instead, stop handing it out so later calls open a fresh one, and disconnect it when the last in-flight call releases it, as typescript/vercel-ai-sdk does with refCount and broken. Adds tests for a failing call next to an in-flight call. Signed-off-by: wankhede04 <[email protected]>
brianstrauch
approved these changes
Oct 16, 2026
brianstrauch
left a comment
Member
There was a problem hiding this comment.
Both callTool and listTools now stop reusing a failed connection while allowing its in-flight calls to finish. The record identity checks protect the replacement connection, and the drained broken record is disconnected through one cleanup path.
The new tests use MockActivityEnvironment to exercise the real activity functions and cover concurrent callTool failure, listTools failure with a callTool in flight, and a lone failure followed by reconnect. I also verified that the concurrent-call tests fail against the base implementation.
Validation on the reviewed commit (Node 24.21.0, pnpm 10.27.0):
- make lint passed.
- make test passed: all 24 tests, including the three new regression tests.
No blocking findings. GitHub CI is currently awaiting workflow-run approval.
brianstrauch
enabled auto-merge (squash)
October 16, 2026 21:59
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
What was changed
When a
listToolsorcallToolActivity fails, the pooled MCP connection is now markedbrokeninstead of beingdisconnected immediately. A broken connection is no longer handed out, so the next call opens a fresh one, and it
is disconnected once the last Activity using it finishes.
ConnectionRecordgets abrokenflag.markBrokenremoves the record from the cache.releaseConnectioncloses a broken record when its in-flightcount reaches 0; otherwise it behaves as before and re-arms the idle timer.
_evictConnectionand the new path share one best-effortcloseRecord.This is the same approach as
typescript/vercel-ai-sdk(refCount/broken).Why?
The failure path used to
await _evictConnection(server)right away, so other calls running on the sharedconnection failed with "Connection closed" and were retried, which can repeat side effects of non-idempotent
tools.
Checklist
Closes [Bug] strands-agents: a failed MCP call closes the shared connection under in-flight calls #100
How was this tested:
Ran in
typescript/strands-agents(Node 22.22.2, pnpm 10.27.0):make sync,make lint(clean) andmake test: 24 tests passed, 0 failed.make build: succeeded.src/__tests__/test-mcp-connection-eviction.ts(3 tests,MockActivityEnvironmentand a fake MCP client thatrejects in-flight requests on
disconnect()): a slow call and a concurrent failing call on one server (the slowcall succeeds, one connection is opened for both, a call made after the failure opens a fresh connection, and
the broken one is closed once drained); a failing
listToolsdoes not close the connection under an in-flightcallTool; a failure with nothing else in flight still closes the connection and the next call reconnects.temporal-mcp-client.tsthe new file gives 1 passed and 2 failed (the two sibling-calltests fail with "Connection closed"); with the change all 3 pass.
None.