diff options
| author | Aiden Cline <[email protected]> | 2025-11-03 15:04:53 -0600 |
|---|---|---|
| committer | GitHub <[email protected]> | 2025-11-03 15:04:53 -0600 |
| commit | 55d07a139c064371d65726d13266bc99efd91af4 (patch) | |
| tree | 8be5834f38e3563a9c0c9d85a0715f6748814593 | |
| parent | 05232ead938b7cc7dcf75afa9470effef0ed4251 (diff) | |
| download | opencode-55d07a139c064371d65726d13266bc99efd91af4.tar.gz opencode-55d07a139c064371d65726d13266bc99efd91af4.zip | |
fix: mcp error (#3847)
| -rw-r--r-- | packages/opencode/src/mcp/index.ts | 32 |
1 files changed, 22 insertions, 10 deletions
diff --git a/packages/opencode/src/mcp/index.ts b/packages/opencode/src/mcp/index.ts index d80d34330..6b153e39d 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<string, Tool> = {} + 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 |
