From 9c5154aa268e0927a9b6c1358ab84a4339b6059b Mon Sep 17 00:00:00 2001 From: Kevin O'Connell Date: Wed, 11 Nov 2026 12:01:45 -0800 Subject: [PATCH] fix(mcp): call the live client after a reconnect A tool call captured its server's client when the tool list was built, then waited for approval. If the server reconnected meanwhile (Replo's coordinator reconnects Bedrock on every integration change), the captured client was already closed and the call failed with the SDK's "Not connected". MCP.tools() entries now resolve the server's current client on each read, and convertTool takes a client getter, so both code-mode child calls and direct MCP tools send through the live connection. REPL-35428 Co-Authored-By: Claude Opus 5.5 --- packages/opencode/src/mcp/catalog.ts | 5 +++-- packages/opencode/src/mcp/index.ts | 10 +++++++++- packages/opencode/src/session/tools.ts | 2 +- packages/opencode/test/mcp/catalog.test.ts | 17 +++++++++++++++-- packages/opencode/test/mcp/lifecycle.test.ts | 17 +++++++++++++++++ 5 files changed, 45 insertions(+), 6 deletions(-) diff --git a/packages/opencode/src/mcp/catalog.ts b/packages/opencode/src/mcp/catalog.ts index db43f1d0bc41..ff57c5f9bb79 100644 --- a/packages/opencode/src/mcp/catalog.ts +++ b/packages/opencode/src/mcp/catalog.ts @@ -42,7 +42,8 @@ export function defs(clientName: string, client: Client, timeout?: number) { ) } -export function convertTool(mcpTool: MCPToolDef, client: Client, timeout?: number): Tool { +// `client` is resolved per call so a server reconnect between build and call uses the live client. +export function convertTool(mcpTool: MCPToolDef, client: () => Client, timeout?: number): Tool { const inputSchema: JSONSchema7 = { ...(mcpTool.inputSchema as JSONSchema7), type: "object", @@ -54,7 +55,7 @@ export function convertTool(mcpTool: MCPToolDef, client: Client, timeout?: numbe description: mcpTool.description ?? "", inputSchema: jsonSchema(inputSchema), execute: async (args: unknown, options) => { - const result = await client.callTool( + const result = await client().callTool( { name: mcpTool.name, arguments: (args || {}) as Record, diff --git a/packages/opencode/src/mcp/index.ts b/packages/opencode/src/mcp/index.ts index 3e08ec5954a9..cd9eaef7474e 100644 --- a/packages/opencode/src/mcp/index.ts +++ b/packages/opencode/src/mcp/index.ts @@ -167,6 +167,7 @@ export interface ServerInstructions { export interface McpTool { /** Shared cached definition; consumers must copy rather than mutate it. */ readonly def: MCPToolDef + /** The server's current client, resolved on each read; capture it only at the moment of the call. */ readonly client: MCPClient readonly timeout?: number } @@ -736,7 +737,14 @@ const layer = Layer.effect( } const timeout = requestTimeout(s, clientName, mcpConfig, defaultTimeout) for (const def of listed) { - result[McpCatalog.toolName(clientName, def.name)] = { def, client, timeout } + result[McpCatalog.toolName(clientName, def.name)] = { + def, + // Read at call time: a reconnect while the call awaits approval closes the client captured here. + get client() { + return s.clients[clientName] ?? client + }, + timeout, + } } } return result diff --git a/packages/opencode/src/session/tools.ts b/packages/opencode/src/session/tools.ts index 39bfd7a70c05..73cb93494054 100644 --- a/packages/opencode/src/session/tools.ts +++ b/packages/opencode/src/session/tools.ts @@ -393,7 +393,7 @@ export const resolve = Effect.fn("SessionTools.resolve")(function* (input: { if (flags.experimentalCodeMode) return tools for (const [key, entry] of Object.entries(yield* mcp.tools())) { - const item = McpCatalog.convertTool(entry.def, entry.client, entry.timeout) + const item = McpCatalog.convertTool(entry.def, () => entry.client, entry.timeout) const execute = item.execute if (!execute) continue diff --git a/packages/opencode/test/mcp/catalog.test.ts b/packages/opencode/test/mcp/catalog.test.ts index d8192729dcc5..65f1fb7635ba 100644 --- a/packages/opencode/test/mcp/catalog.test.ts +++ b/packages/opencode/test/mcp/catalog.test.ts @@ -30,7 +30,7 @@ describe("McpCatalog.convertTool", () => { test("preserves content when structuredContent is also present", async () => { const content = [{ type: "image" as const, mimeType: "image/png", data: "AAAA" }] const structuredContent = { image: { mimeType: "image/png", data: "AAAA" } } - const converted = McpCatalog.convertTool(mcpTool(), clientReturning({ content, structuredContent })) + const converted = McpCatalog.convertTool(mcpTool(), () => clientReturning({ content, structuredContent })) const output = await converted.execute?.({}, options) @@ -39,7 +39,7 @@ describe("McpCatalog.convertTool", () => { test("falls back to structuredContent only when content is absent", async () => { const structuredContent = { results: [{ title: "one" }] } - const converted = McpCatalog.convertTool(mcpTool(), clientReturning({ content: [], structuredContent })) + const converted = McpCatalog.convertTool(mcpTool(), () => clientReturning({ content: [], structuredContent })) const output = await converted.execute?.({}, options) @@ -48,6 +48,19 @@ describe("McpCatalog.convertTool", () => { content: [{ type: "text", text: JSON.stringify(structuredContent) }], }) }) + + test("calls the client current at execute time, not the one at build time", async () => { + const content = [{ type: "text" as const, text: "live" }] + let client = { + callTool: async () => { + throw new Error("Not connected") + }, + } as unknown as Client + const converted = McpCatalog.convertTool(mcpTool(), () => client) + client = clientReturning({ content }) + + expect(await converted.execute?.({}, options)).toMatchObject({ content }) + }) }) test("preserves output schema validation across paginated tool discovery", async () => { diff --git a/packages/opencode/test/mcp/lifecycle.test.ts b/packages/opencode/test/mcp/lifecycle.test.ts index d74edd0220b1..d263c25a2730 100644 --- a/packages/opencode/test/mcp/lifecycle.test.ts +++ b/packages/opencode/test/mcp/lifecycle.test.ts @@ -327,6 +327,23 @@ it.instance("disconnect removes protocol data and reconnect establishes a new se }), ) +it.instance("a tools() entry taken before a reconnect calls through the new client", () => + Effect.gen(function* () { + const server = yield* lifecycleServer() + const mcp = yield* MCP.Service + yield* mcp.add("swap-server", remote(server.url)) + // A tool call holds this entry while it waits for approval; a reconnect closes the old client meanwhile. + const entry = (yield* mcp.tools())["swap-server_test_tool"]! + + yield* Effect.promise(server.restart) + yield* mcp.connect("swap-server") + + const listed = yield* Effect.promise(() => entry.client.listTools()) + expect(listed.tools.map((tool) => tool.name)).toEqual(["test_tool"]) + expect(entry.client).toBe((yield* mcp.clients())["swap-server"]!) + }), +) + it.instance("add() closes the old protocol session when replacing a server", () => Effect.gen(function* () { const first = yield* lifecycleServer()