Skip to content

Commit 5435732

Browse files
committed
app: Replace mcp config if server config changes
1 parent fdbec0f commit 5435732

File tree

3 files changed

+58
-20
lines changed

3 files changed

+58
-20
lines changed

app/electron/main.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1474,10 +1474,7 @@ function startElecron() {
14741474
if (mainWindow) {
14751475
mainWindow.removeAllListeners('close');
14761476
}
1477-
// Cleanup MCP client
1478-
if (mcpClient) {
1479-
mcpClient.cleanup().catch(console.error);
1480-
}
1477+
14811478
// Cleanup MCP client
14821479
if (mcpClient) {
14831480
mcpClient.cleanup().catch(console.error);

app/electron/mcp-client.ts

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,13 @@ class ElectronMCPClient {
5757

5858
/**
5959
* Initialize tools configuration for all available tools
60+
* This completely replaces the existing config with current tools
6061
*/
6162
private initializeToolsConfiguration(): void {
6263
if (!this.tools || this.tools.length === 0) {
6364
console.log('No tools available for configuration initialization');
65+
// Clear the config if no tools are available
66+
this.configManager.replaceConfig({});
6467
return;
6568
}
6669

@@ -75,16 +78,16 @@ class ElectronMCPClient {
7578
> = {};
7679

7780
for (const tool of this.tools) {
78-
console.log('Initializing tools configuration...', tool);
7981
// Extract server name from tool name (format: "serverName__toolName")
8082
const toolName = tool.name;
8183
const parts = toolName.split('__');
8284

8385
// Extract schema from the tool (LangChain tools use .schema property)
8486
const toolSchema = (tool as any).schema || tool.inputSchema || null;
85-
console.log('tool schema is ', toolSchema);
8687
console.log(
87-
`Processing tool: ${toolName}, has inputSchema: ${toolSchema}, description: "${tool.description}"`
88+
`Processing tool: ${toolName}, has inputSchema: ${!!toolSchema}, description: "${
89+
tool.description
90+
}"`
8891
);
8992

9093
if (parts.length >= 2) {
@@ -114,11 +117,8 @@ class ElectronMCPClient {
114117

115118
console.log('Tools grouped by server:', Object.keys(toolsByServer));
116119

117-
// Initialize configuration for each server's tools
118-
for (const [serverName, toolsInfo] of Object.entries(toolsByServer)) {
119-
console.log(`Initializing ${toolsInfo.length} tools for server: ${serverName}`);
120-
this.configManager.initializeToolsConfig(serverName, toolsInfo);
121-
}
120+
// Replace the entire configuration with current tools
121+
this.configManager.replaceToolsConfig(toolsByServer);
122122
}
123123

124124
/**
@@ -608,6 +608,7 @@ class ElectronMCPClient {
608608
additionalToolNamePrefix: '',
609609
useStandardContentBlocks: true,
610610
mcpServers,
611+
defaultToolTimeout: 2 * 60 * 1000, // 2 minutes
611612
});
612613

613614
// Get and cache the tools

app/electron/mcp-config.ts

Lines changed: 48 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,6 @@ export class MCPConfigManager {
191191
description?: string;
192192
}>
193193
): void {
194-
console.log('MCP config called ', this.config, serverName, toolsInfo);
195194
if (!this.config[serverName]) {
196195
this.config[serverName] = {};
197196
}
@@ -203,7 +202,6 @@ export class MCPConfigManager {
203202
const toolName = toolInfo.name;
204203

205204
if (!serverConfig[toolName]) {
206-
console.log(`Creating new tool config for: ${toolName}`);
207205
serverConfig[toolName] = {
208206
enabled: true,
209207
usageCount: 0,
@@ -212,15 +210,13 @@ export class MCPConfigManager {
212210
};
213211
hasChanges = true;
214212
} else {
215-
console.log(`Updating existing tool config for: ${toolName}`);
216213
// Always update schema and description for existing tools
217214
let toolChanged = false;
218215

219216
// Update schema if it's different or missing
220217
const currentSchema = JSON.stringify(serverConfig[toolName].inputSchema || null);
221218
const newSchema = JSON.stringify(toolInfo.inputSchema || null);
222219
if (currentSchema !== newSchema) {
223-
console.log(`Updating schema for tool: ${toolName}`);
224220
serverConfig[toolName].inputSchema = toolInfo.inputSchema || null;
225221
toolChanged = true;
226222
}
@@ -229,7 +225,6 @@ export class MCPConfigManager {
229225
const currentDescription = serverConfig[toolName].description || '';
230226
const newDescription = toolInfo.description || '';
231227
if (currentDescription !== newDescription) {
232-
console.log(`Updating description for tool: ${toolName}`);
233228
serverConfig[toolName].description = newDescription;
234229
toolChanged = true;
235230
}
@@ -241,10 +236,7 @@ export class MCPConfigManager {
241236
}
242237

243238
if (hasChanges) {
244-
console.log(`Saving configuration changes for server: ${serverName}`);
245239
this.saveConfig();
246-
} else {
247-
console.log(`No changes needed for server: ${serverName}`);
248240
}
249241
}
250242

@@ -259,4 +251,52 @@ export class MCPConfigManager {
259251

260252
return { ...serverConfig[toolName] };
261253
}
254+
255+
/**
256+
* Replace the entire tools configuration with a new set of tools
257+
* This overwrites all existing tools with only the current ones
258+
*/
259+
replaceToolsConfig(
260+
toolsByServer: Record<
261+
string,
262+
Array<{
263+
name: string;
264+
inputSchema?: any;
265+
description?: string;
266+
}>
267+
>
268+
): void {
269+
// Create a new config object
270+
const newConfig: MCPToolsConfig = {};
271+
272+
for (const [serverName, toolsInfo] of Object.entries(toolsByServer)) {
273+
newConfig[serverName] = {};
274+
275+
for (const toolInfo of toolsInfo) {
276+
const toolName = toolInfo.name;
277+
278+
// Check if this tool existed in the old config to preserve enabled state and usage count
279+
const oldToolState = this.config[serverName]?.[toolName];
280+
281+
newConfig[serverName][toolName] = {
282+
enabled: oldToolState?.enabled ?? true, // Preserve enabled state or default to true
283+
usageCount: oldToolState?.usageCount ?? 0, // Preserve usage count or default to 0
284+
inputSchema: toolInfo.inputSchema || null,
285+
description: toolInfo.description || '',
286+
};
287+
}
288+
}
289+
290+
// Replace the entire config
291+
this.config = newConfig;
292+
this.saveConfig();
293+
}
294+
295+
/**
296+
* Replace the entire configuration with a new config object
297+
*/
298+
replaceConfig(newConfig: MCPToolsConfig): void {
299+
this.config = newConfig;
300+
this.saveConfig();
301+
}
262302
}

0 commit comments

Comments
 (0)