Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions src/commands/daemon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
18 changes: 11 additions & 7 deletions src/core/pool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { ServerConfig } from '../types/config.js';

export class ConnectionPool {
private clients: Map<string, McpClientService> = new Map();
private toolsCache: Map<string, number> = new Map();
private toolsCache: Map<string, any[]> = new Map(); // Cache full tools result
private initializing = false;
private initialized = false;

Expand All @@ -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}...`);
Expand Down Expand Up @@ -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';
}
Expand Down