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'; }