From f42054ed033efe6177af3744b62a3d7cc4bd2f48 Mon Sep 17 00:00:00 2001 From: SuYao Date: Thu, 19 Jun 2025 12:02:03 +0800 Subject: [PATCH] fix(ApiService): improve error handling when fetching tools from MCP servers (#7340) - Added error handling for tool fetching to log errors and return an empty array if a server fails to respond. - Changed from Promise.all to Promise.allSettled to ensure all tool fetching attempts are accounted for, filtering out any rejected promises. --- src/renderer/src/services/ApiService.ts | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/src/renderer/src/services/ApiService.ts b/src/renderer/src/services/ApiService.ts index e7cd409c46..b081e3dcf7 100644 --- a/src/renderer/src/services/ApiService.ts +++ b/src/renderer/src/services/ApiService.ts @@ -254,12 +254,20 @@ async function fetchExternalTool( const enabledMCPs = assistant.mcpServers if (enabledMCPs && enabledMCPs.length > 0) { try { - const toolPromises = enabledMCPs.map(async (mcpServer) => { - const tools = await window.api.mcp.listTools(mcpServer) - return tools.filter((tool: any) => !mcpServer.disabledTools?.includes(tool.name)) + const toolPromises = enabledMCPs.map>(async (mcpServer) => { + try { + const tools = await window.api.mcp.listTools(mcpServer) + return tools.filter((tool: any) => !mcpServer.disabledTools?.includes(tool.name)) + } catch (error) { + console.error(`Error fetching tools from MCP server ${mcpServer.name}:`, error) + return [] + } }) - const results = await Promise.all(toolPromises) - mcpTools = results.flat() // Flatten the array of arrays + const results = await Promise.allSettled(toolPromises) + mcpTools = results + .filter((result): result is PromiseFulfilledResult => result.status === 'fulfilled') + .map((result) => result.value) + .flat() } catch (toolError) { console.error('Error fetching MCP tools:', toolError) }