From 31f630e816c2fbda17bb892311448d9d2388bcf6 Mon Sep 17 00:00:00 2001 From: Vaayne Date: Tue, 30 Dec 2025 13:35:20 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20fix:=20sync=20hub=20tool=20cache?= =?UTF-8?q?=20and=20map?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/mcpServers/hub/index.ts | 5 ++++- src/main/mcpServers/hub/mcp-bridge.ts | 18 ++++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/src/main/mcpServers/hub/index.ts b/src/main/mcpServers/hub/index.ts index 2c60ba49b8..c3abbd517c 100644 --- a/src/main/mcpServers/hub/index.ts +++ b/src/main/mcpServers/hub/index.ts @@ -4,7 +4,7 @@ import { Server } from '@modelcontextprotocol/sdk/server/index.js' import { CallToolRequestSchema, ErrorCode, ListToolsRequestSchema, McpError } from '@modelcontextprotocol/sdk/types.js' import { generateToolFunction } from './generator' -import { callMcpTool, listAllTools } from './mcp-bridge' +import { callMcpTool, clearToolMap, listAllTools, syncToolMapFromGeneratedTools } from './mcp-bridge' import { Runtime } from './runtime' import { searchTools } from './search' import type { ExecInput, GeneratedTool, SearchQuery } from './types' @@ -124,6 +124,7 @@ export class HubServer { const cached = CacheService.get(TOOLS_CACHE_KEY) if (cached) { logger.debug('Returning cached tools') + syncToolMapFromGeneratedTools(cached) return cached } @@ -132,11 +133,13 @@ export class HubServer { const existingNames = new Set() const tools = allTools.map((tool) => generateToolFunction(tool, existingNames, callMcpTool)) CacheService.set(TOOLS_CACHE_KEY, tools, TOOLS_CACHE_TTL) + syncToolMapFromGeneratedTools(tools) return tools } invalidateCache(): void { CacheService.remove(TOOLS_CACHE_KEY) + clearToolMap() logger.debug('Tools cache invalidated') } diff --git a/src/main/mcpServers/hub/mcp-bridge.ts b/src/main/mcpServers/hub/mcp-bridge.ts index 83549f86ac..b759d83b35 100644 --- a/src/main/mcpServers/hub/mcp-bridge.ts +++ b/src/main/mcpServers/hub/mcp-bridge.ts @@ -4,6 +4,9 @@ */ import mcpService from '@main/services/MCPService' import { generateMcpToolFunctionName } from '@shared/mcp' +import type { MCPTool } from '@types' + +import type { GeneratedTool } from './types' export const listAllTools = () => mcpService.listAllActiveServerTools() @@ -11,6 +14,10 @@ const toolFunctionNameToIdMap = new Map { const tools = await listAllTools() + syncToolMapFromTools(tools) +} + +export function syncToolMapFromTools(tools: MCPTool[]): void { toolFunctionNameToIdMap.clear() const existingNames = new Set() for (const tool of tools) { @@ -19,6 +26,17 @@ export async function refreshToolMap(): Promise { } } +export function syncToolMapFromGeneratedTools(tools: GeneratedTool[]): void { + toolFunctionNameToIdMap.clear() + for (const tool of tools) { + toolFunctionNameToIdMap.set(tool.functionName, { serverId: tool.serverId, toolName: tool.toolName }) + } +} + +export function clearToolMap(): void { + toolFunctionNameToIdMap.clear() +} + export const callMcpTool = async (functionName: string, params: unknown, callId?: string): Promise => { const toolInfo = toolFunctionNameToIdMap.get(functionName) if (!toolInfo) {