fix(mcp): call the live client after a reconnect

Draft
KE
11d ago
+45-6

Issue for this PR

Linear REPL-35428: brand kit update_brand fails with "Not connected" while get_branding works in the same run.

Type of change

  • Bug fix
  • New feature
  • Refactor / code improvement
  • Documentation

What does this PR do?

Writes that waited for approval were sent on a closed MCP connection. A tool call captured its server's MCP client when the tool list was built, then waited for the user to approve the write. If the server reconnected during that wait, the captured client had already been closed, and the call failed with the MCP SDK's Not connected (Protocol.request throws it once close() has cleared the transport). Reads need no approval, so they ran on a fresh client and kept working.

Replo's coordinator reconnects the bedrock server on every integration change (it reloads opencode, then calls POST /mcp/bedrock/connect; storeClient swaps in the new client and closes the old one). That happens 16 to 50 times a day across the fleet.

In the reported session (2026-11-05, UTC):

TimeWhat happened
23:34:30update_brand asks for approval inside execute
23:35:14The customer's Figma connection changes in Brand Studio, so Bedrock fans out an integration refresh
23:35:15Coordinator reloads opencode; a new Bedrock MCP session opens (initialize, tools/list)
23:35:24Customer approves; the call goes out on the closed client: Not connected
23:35:29Retry asks for approval
23:35:44The coordinator's deferred second refresh reconnects Bedrock again
23:41:45Customer approves; Not connected again

The fix: each MCP.tools() entry now looks up the server's current client whenever it's read (a getter over s.clients[name]). convertTool now takes a client getter instead of a client. The code-mode child call (invokeChildTool) and the direct MCP tool (session/tools.ts) both read the client after ctx.ask, so they send through the live connection. When the server is gone entirely, the entry falls back to the captured client, which behaves exactly as before.

Not covered here: a call already sent when a reconnect closes its client still fails with Connection closed. The window is the call's own duration, not the approval wait.

How did you verify your code works?

  • New lifecycle.test.ts case: take a tools() entry, restart the server, mcp.connect() it, then call through the old entry. Before the fix it failed with error: Not connected (the customer's exact error); now it lists tools on the new client.
  • New catalog.test.ts case: convertTool calls the client current at execute time.
  • bun test test/mcp/ test/tool/code-mode.test.ts test/tool/code-mode-integration.test.ts test/session/tools.test.ts: 122 pass. bun run typecheck clean.

Screenshots / recordings

None: there's no UI change.

Checklist

  • I have tested my changes locally
  • I have not included unrelated changes in this PR

Shipping to Replo needs a fork release (v1.18.32-12) and the andytown pin bump (Daytona Dockerfile ARG, sandbox-upgrader OPENCODE_VERSION, coordinator SDK tarball).

šŸ¤– Generated with Claude Code


Summary by cubic

Fixes MCP write tools failing with Not connected after the server reconnects while the call waits for user approval.

A tool call captured its server's MCP client when the tool list was built and held it through the approval wait. Replo's coordinator reconnects the bedrock server on every integration change, closing the captured client and causing writes to fail while reads kept working. Tool entries now resolve the server's current client at call time, and convertTool takes a client getter, so calls go through the live connection. If the server is gone entirely, the entry falls back to the captured client.

  • Adds test coverage for calling through a tools() entry after a server restart and reconnect.
  • A call already in flight when a reconnect closes its client still fails with Connection closed; the window is the call's own duration, not the approval wait.