From 55d07a139c064371d65726d13266bc99efd91af4 Mon Sep 17 00:00:00 2001 From: Aiden Cline <63023139+rekram1-node@users.noreply.github.com> Date: Mon, 3 Nov 2025 15:04:53 -0600 Subject: [PATCH] fix: mcp error (#3847) --- packages/opencode/src/mcp/index.ts | 32 ++++++++++++++++++++---------- 1 file changed, 22 insertions(+), 10 deletions(-) diff --git a/packages/opencode/src/mcp/index.ts b/packages/opencode/src/mcp/index.ts index d80d3433..6b153e39 100644 --- a/packages/opencode/src/mcp/index.ts +++ b/packages/opencode/src/mcp/index.ts @@ -100,7 +100,7 @@ export namespace MCP { } log.info("found", { key, type: mcp.type }) let mcpClient: MCPClient | undefined - let status: Status | undefined + let status: Status | undefined = undefined if (mcp.type === "remote") { const transports = [ @@ -142,7 +142,7 @@ export namespace MCP { error: lastError.message, }) status = { - status: "failed", + status: "failed" as const, error: lastError.message, } return false @@ -179,7 +179,7 @@ export namespace MCP { error: error instanceof Error ? error.message : String(error), }) status = { - status: "failed", + status: "failed" as const, error: error instanceof Error ? error.message : String(error), } }) @@ -187,7 +187,7 @@ export namespace MCP { if (!status) { status = { - status: "failed", + status: "failed" as const, error: "Unknown error", } } @@ -202,13 +202,12 @@ export namespace MCP { const result = await withTimeout(mcpClient.tools(), mcp.timeout ?? 5000).catch(() => {}) if (!result) { await mcpClient.close() - status = { - status: "failed", - error: "Failed to get tools", - } return { mcpClient: undefined, - status, + status: { + status: "failed" as const, + error: "Failed to get tools", + }, } } @@ -228,8 +227,21 @@ export namespace MCP { export async function tools() { const result: Record = {} + const s = await state() for (const [clientName, client] of Object.entries(await clients())) { - for (const [toolName, tool] of Object.entries(await client.tools())) { + const tools = await client.tools().catch((e) => { + log.error("failed to get tools", { clientName, error: e.message }) + const failedStatus = { + status: "failed" as const, + error: e instanceof Error ? e.message : String(e), + } + s.status[clientName] = failedStatus + delete s.clients[clientName] + }) + if (!tools) { + continue + } + for (const [toolName, tool] of Object.entries(tools)) { const sanitizedClientName = clientName.replace(/\s+/g, "_") const sanitizedToolName = toolName.replace(/[-\s]+/g, "_") result[sanitizedClientName + "_" + sanitizedToolName] = tool