Skip to content

fix(strands): close a failed MCP connection only after in-flight calls finish - #101

Merged
brianstrauch merged 2 commits into
temporalio:mainfrom
wankhede04:fix/strands-mcp-defer-evict-on-failure
Oct 16, 2026
Merged

brianstrauch merged 2 commits into
temporalio:mainfrom
wankhede04:fix/strands-mcp-defer-evict-on-failure

Conversation

@wankhede04

Copy link
Copy Markdown
Contributor

What was changed

When a listTools or callTool Activity fails, the pooled MCP connection is now marked broken instead of being
disconnected 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.

  • ConnectionRecord gets a broken flag.
  • markBroken removes the record from the cache. releaseConnection closes a broken record when its in-flight
    count reaches 0; otherwise it behaves as before and re-arms the idle timer.
  • _evictConnection and the new path share one best-effort closeRecord.

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 shared
connection failed with "Connection closed" and were retried, which can repeat side effects of non-idempotent
tools.

Checklist

  1. Closes [Bug] strands-agents: a failed MCP call closes the shared connection under in-flight calls #100

  2. How was this tested:
    Ran in typescript/strands-agents (Node 22.22.2, pnpm 10.27.0):

  • make sync, make lint (clean) and make test: 24 tests passed, 0 failed.
  • make build: succeeded.
  • New src/__tests__/test-mcp-connection-eviction.ts (3 tests, MockActivityEnvironment and a fake MCP client that
    rejects in-flight requests on disconnect()): a slow call and a concurrent failing call on one server (the slow
    call 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 listTools does not close the connection under an in-flight
    callTool; a failure with nothing else in flight still closes the connection and the next call reconnects.
  • Against the original temporal-mcp-client.ts the new file gives 1 passed and 2 failed (the two sibling-call
    tests fail with "Connection closed"); with the change all 3 pass.
  1. Any docs updates needed?

None.

…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 brianstrauch left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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
brianstrauch enabled auto-merge (squash) October 16, 2026 21:59
@brianstrauch
brianstrauch merged commit 7319910 into temporalio:main Oct 16, 2026
16 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] strands-agents: a failed MCP call closes the shared connection under in-flight calls

2 participants