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()