Skip to content

Commit 0582134

Browse files
authored
🐛 fix: remove mcp client cache (#7776)
1 parent 35d9ac4 commit 0582134

File tree

1 file changed

+2
-35
lines changed

1 file changed

+2
-35
lines changed

src/server/services/mcp/index.ts

Lines changed: 2 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,7 @@ import { safeParseJSON } from '@/utils/safeParseJSON';
99

1010
const log = debug('lobe-mcp:service');
1111

12-
// Removed MCPConnection interface as it's no longer needed
13-
1412
class MCPService {
15-
// Store instances of the custom MCPClient, keyed by serialized MCPClientParams
16-
private clients: Map<string, MCPClient> = new Map();
17-
1813
// --- MCP Interaction ---
1914

2015
// listTools now accepts MCPClientParams
@@ -97,15 +92,6 @@ class MCPService {
9792

9893
// Private method to get or initialize a client based on parameters
9994
private async getClient(params: MCPClientParams): Promise<MCPClient> {
100-
const key = this.serializeParams(params); // Use custom serialization
101-
log(`Attempting to get client for key: ${key} (params: %O)`, params);
102-
103-
if (this.clients.has(key)) {
104-
log(`Returning cached client for key: ${key}`);
105-
return this.clients.get(key)!;
106-
}
107-
108-
log(`No cached client found for key: ${key}. Initializing new client.`);
10995
try {
11096
// Ensure stdio is only attempted in desktop/server environments within the client itself
11197
// or add a check here if MCPClient doesn't handle it.
@@ -120,11 +106,10 @@ class MCPService {
120106
log(`New client initializing... ${progress.progress}/${progress.total}`);
121107
},
122108
}); // Initialization logic should be within MCPClient
123-
this.clients.set(key, client);
124-
log(`New client initialized and cached for key: ${key}`);
109+
log(`New client initialized`);
125110
return client;
126111
} catch (error) {
127-
console.error(`Failed to initialize MCP client for key ${key}:`, error);
112+
console.error(`Failed to initialize MCP client`, error);
128113
// Do not cache failed initializations
129114
throw new TRPCError({
130115
cause: error,
@@ -134,24 +119,6 @@ class MCPService {
134119
}
135120
}
136121

137-
// Custom serialization function to ensure consistent keys
138-
private serializeParams(params: MCPClientParams): string {
139-
const sortedKeys = Object.keys(params).sort();
140-
const sortedParams: Record<string, any> = {};
141-
142-
for (const key of sortedKeys) {
143-
const value = (params as any)[key];
144-
// Sort the 'args' array if it exists
145-
if (key === 'args' && Array.isArray(value)) {
146-
sortedParams[key] = JSON.stringify(key);
147-
} else {
148-
sortedParams[key] = value;
149-
}
150-
}
151-
152-
return JSON.stringify(sortedParams);
153-
}
154-
155122
async getStreamableMcpServerManifest(
156123
identifier: string,
157124
url: string,

0 commit comments

Comments
 (0)