From d372571e55c920843230b49706b68b47b261b374 Mon Sep 17 00:00:00 2001 From: zhangzikuan Date: Sun, 1 Feb 2026 18:27:38 +0800 Subject: [PATCH] =?UTF-8?q?perf:=20=E4=BC=98=E5=8C=96=20tools=20=E5=91=BD?= =?UTF-8?q?=E4=BB=A4=E6=80=A7=E8=83=BD=EF=BC=8C=E7=BC=93=E5=AD=98=E5=AE=8C?= =?UTF-8?q?=E6=95=B4=E7=9A=84=E5=B7=A5=E5=85=B7=E5=88=97=E8=A1=A8=E7=BB=93?= =?UTF-8?q?=E6=9E=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 问题: - tools 命令执行缓慢 - 同一个工具列表被请求了两次 1. getClient() 建立连接后调用 listTools() 缓存工具数量 2. /list 接口又调用一次 listTools() 获取完整信息 优化: - 将 toolsCache 从 Map 改为 Map - 缓存完整的 tools 结果而不仅仅是数量 - 新增 getCachedTools() 方法获取缓存的工具列表 - /list 接口优先使用缓存,避免重复调用 性能提升: - tools 命令从两次 listTools() 调用减少到零次(使用缓存) - 显著提升 tools 命令的响应速度 Co-Authored-By: Claude Sonnet 4.5 --- src/commands/daemon.ts | 9 +++++++++ src/core/pool.ts | 18 +++++++++++------- 2 files changed, 20 insertions(+), 7 deletions(-) diff --git a/src/commands/daemon.ts b/src/commands/daemon.ts index f62f2b1..6e521db 100644 --- a/src/commands/daemon.ts +++ b/src/commands/daemon.ts @@ -384,6 +384,15 @@ const startDaemon = (port: number) => { return; } + // Try to use cached tools first + const cachedTools = connectionPool.getCachedTools(serverName); + if (cachedTools !== null) { + res.writeHead(200, { 'Content-Type': 'application/json' }); + res.end(JSON.stringify({ tools: cachedTools })); + return; + } + + // Fallback: fetch from client if not cached const client = await connectionPool.getClient(serverName); const toolsResult = await client.listTools(); diff --git a/src/core/pool.ts b/src/core/pool.ts index 7e9a2b0..69d292f 100644 --- a/src/core/pool.ts +++ b/src/core/pool.ts @@ -4,7 +4,7 @@ import { ServerConfig } from '../types/config.js'; export class ConnectionPool { private clients: Map = new Map(); - private toolsCache: Map = new Map(); + private toolsCache: Map = new Map(); // Cache full tools result private initializing = false; private initialized = false; @@ -30,18 +30,22 @@ export class ConnectionPool { } this.clients.set(serverName, client); - // Cache tools count after connection + // Cache full tools result after connection try { const result = await client.listTools(); - this.toolsCache.set(serverName, result.tools.length); + this.toolsCache.set(serverName, result.tools || []); } catch (e) { - // Connection succeeded but listTools failed, cache as 0 - this.toolsCache.set(serverName, 0); + // Connection succeeded but listTools failed, cache as empty array + this.toolsCache.set(serverName, []); } return client; } + getCachedTools(serverName: string): any[] | null { + return this.toolsCache.has(serverName) ? this.toolsCache.get(serverName)! : null; + } + async closeClient(serverName: string) { if (this.clients.has(serverName)) { console.log(`[Daemon] Closing connection to ${serverName}...`); @@ -151,13 +155,13 @@ export class ConnectionPool { if (includeTools) { // Use cached tools count instead of calling listTools again if (this.toolsCache.has(name)) { - toolsCount = this.toolsCache.get(name)!; + toolsCount = this.toolsCache.get(name)!.length; } else { // Fallback: if not cached, fetch it now try { const result = await client.listTools(); toolsCount = result.tools.length; - this.toolsCache.set(name, toolsCount); + this.toolsCache.set(name, result.tools || []); } catch (e) { status = 'error'; }