From d11ed7da53d1fa710263f008eb3466103d3f9a8b Mon Sep 17 00:00:00 2001 From: Dallin Romney Date: Mon, 17 Mar 2025 00:17:05 -0700 Subject: [PATCH 1/5] kill unused mcp servers --- core/config/yaml/loadYaml.ts | 5 +++++ core/context/mcp/index.ts | 7 +++++++ 2 files changed, 12 insertions(+) diff --git a/core/config/yaml/loadYaml.ts b/core/config/yaml/loadYaml.ts index 0b1565f58..0918d992f 100644 --- a/core/config/yaml/loadYaml.ts +++ b/core/config/yaml/loadYaml.ts @@ -369,6 +369,11 @@ async function configYamlToContinueConfig( // Apply MCP if specified const mcpManager = MCPManagerSingleton.getInstance(); + if (config.mcpServers) { + await mcpManager.removeUnusedConnections( + config.mcpServers.map((s) => s.name), + ); + } await Promise.allSettled( config.mcpServers?.map(async (server) => { const abortController = new AbortController(); diff --git a/core/context/mcp/index.ts b/core/context/mcp/index.ts index 19de5434a..f76624fe5 100644 --- a/core/context/mcp/index.ts +++ b/core/context/mcp/index.ts @@ -46,6 +46,13 @@ export class MCPManagerSingleton { this.connections.delete(id); } + + async removeUnusedConnections(keepIds: string[]) { + const toRemove = this.connections + .keys() + .filter((k) => !keepIds.includes(k)); + await Promise.all(toRemove.map(this.removeConnection)); + } } class MCPConnection { From 5f574be9a4616eadc2ef2b78336ea15b99fffad3 Mon Sep 17 00:00:00 2001 From: Dallin Romney Date: Mon, 17 Mar 2025 00:46:18 -0700 Subject: [PATCH 2/5] kill unused mcp connections --- core/config/load.ts | 31 +++++++++++++++---------------- core/config/yaml/loadYaml.ts | 18 ++++-------------- core/context/mcp/index.ts | 27 +++++++++++++-------------- 3 files changed, 32 insertions(+), 44 deletions(-) diff --git a/core/config/load.ts b/core/config/load.ts index ade6aa054..cf2bcf5ac 100644 --- a/core/config/load.ts +++ b/core/config/load.ts @@ -28,6 +28,7 @@ import { IdeType, ILLM, LLMOptions, + MCPOptions, ModelDescription, RerankerDescription, SerializedContinueConfig, @@ -542,40 +543,39 @@ async function intermediateToFinalConfig( // Apply MCP if specified const mcpManager = MCPManagerSingleton.getInstance(); + function getMcpId(options: MCPOptions) { + return JSON.stringify(options); + } + if (config.experimental?.modelContextProtocolServers) { + await mcpManager.removeUnusedConnections( + config.experimental.modelContextProtocolServers.map(getMcpId), + ); + } + if (config.experimental?.modelContextProtocolServers) { const abortController = new AbortController(); const mcpConnectionTimeout = setTimeout( () => abortController.abort(), - 4000, + 5000, ); await Promise.allSettled( config.experimental.modelContextProtocolServers?.map( async (server, index) => { try { - const mcpId = index.toString(); + const mcpId = getMcpId(server); const mcpConnection = mcpManager.createConnection(mcpId, server); - if (!mcpConnection) { - return; - } - const mcpError = await mcpConnection.modifyConfig( + await mcpConnection.modifyConfig( continueConfig, mcpId, abortController.signal, "MCP Server", server.faviconUrl, ); - if (mcpError) { - errors.push(mcpError); - } } catch (e) { let errorMessage = "Failed to load MCP server"; if (e instanceof Error) { - if (e.name === "AbortError") { - errorMessage += ": connection timed out"; - } else { - errorMessage += ": " + e.message; - } + errorMessage += ": " + e.message; } errors.push({ fatal: false, @@ -998,6 +998,5 @@ export { finalToBrowserConfig, intermediateToFinalConfig, loadContinueConfigFromJson, - type BrowserSerializedContinueConfig + type BrowserSerializedContinueConfig, }; - diff --git a/core/config/yaml/loadYaml.ts b/core/config/yaml/loadYaml.ts index 0918d992f..4fa892caa 100644 --- a/core/config/yaml/loadYaml.ts +++ b/core/config/yaml/loadYaml.ts @@ -374,12 +374,13 @@ async function configYamlToContinueConfig( config.mcpServers.map((s) => s.name), ); } + await Promise.allSettled( config.mcpServers?.map(async (server) => { const abortController = new AbortController(); const mcpConnectionTimeout = setTimeout( () => abortController.abort(), - 4000, + 5000, ); try { @@ -391,28 +392,17 @@ async function configYamlToContinueConfig( ...server, }, }); - if (!mcpConnection) { - return; - } - - const mcpError = await mcpConnection.modifyConfig( + await mcpConnection.modifyConfig( continueConfig, mcpId, abortController.signal, server.name, server.faviconUrl, ); - if (mcpError) { - localErrors.push(mcpError); - } } catch (e) { let errorMessage = `Failed to load MCP server ${server.name}`; if (e instanceof Error) { - if (e.name === "AbortError") { - errorMessage += ": connection timed out"; - } else { - errorMessage += ": " + e.message; - } + errorMessage += ": " + e.message; } localErrors.push({ fatal: false, diff --git a/core/context/mcp/index.ts b/core/context/mcp/index.ts index f76624fe5..77c357703 100644 --- a/core/context/mcp/index.ts +++ b/core/context/mcp/index.ts @@ -4,7 +4,6 @@ import { StdioClientTransport } from "@modelcontextprotocol/sdk/client/stdio.js" import { WebSocketClientTransport } from "@modelcontextprotocol/sdk/client/websocket.js"; import { Transport } from "@modelcontextprotocol/sdk/shared/transport.js"; -import { ConfigValidationError } from "@continuedev/config-yaml"; import { ContinueConfig, MCPOptions, SlashCommand, Tool } from "../.."; import { constructMcpSlashCommand } from "../../commands/slash/mcp"; import { encodeMCPToolUri } from "../../tools/callTool"; @@ -24,13 +23,13 @@ export class MCPManagerSingleton { return MCPManagerSingleton.instance; } - createConnection(id: string, options: MCPOptions): MCPConnection | undefined { + createConnection(id: string, options: MCPOptions): MCPConnection { if (!this.connections.has(id)) { const connection = new MCPConnection(options); this.connections.set(id, connection); return connection; } else { - return this.connections.get(id); + return this.connections.get(id)!; } } @@ -130,25 +129,25 @@ class MCPConnection { signal: AbortSignal, name: string, faviconUrl: string | undefined, - ): Promise { + ) { try { await Promise.race([ this.connectClient(), new Promise((_, reject) => { signal.addEventListener("abort", () => - reject(new Error("Connection aborted")), + reject(new Error("Connection timed out")), ); }), ]); - } catch (error: any) { - if (signal.aborted) { - throw new Error("Operation aborted"); - } - if (!error.message.startsWith("StdioClientTransport already started")) { - return { - fatal: false, - message: `Failed to connect to MCP: ${error.message}`, - }; + } catch (error) { + // don't throw error if it's just a "server already running" case + if ( + !( + error instanceof Error && + error.message.startsWith("StdioClientTransport already started") + ) + ) { + throw error; } } From 5e13ead3e7e2f72dc20c21cb2695a20b10a5a7a0 Mon Sep 17 00:00:00 2001 From: Dallin Romney Date: Mon, 17 Mar 2025 16:04:47 -0700 Subject: [PATCH 3/5] handle spawn enoent error --- core/config/yaml/loadYaml.ts | 1 + core/context/mcp/index.ts | 21 ++++++++++++++------- 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/core/config/yaml/loadYaml.ts b/core/config/yaml/loadYaml.ts index 4fa892caa..a11e1ab44 100644 --- a/core/config/yaml/loadYaml.ts +++ b/core/config/yaml/loadYaml.ts @@ -392,6 +392,7 @@ async function configYamlToContinueConfig( ...server, }, }); + await mcpConnection.modifyConfig( continueConfig, mcpId, diff --git a/core/context/mcp/index.ts b/core/context/mcp/index.ts index 77c357703..c4f29f3e6 100644 --- a/core/context/mcp/index.ts +++ b/core/context/mcp/index.ts @@ -140,13 +140,20 @@ class MCPConnection { }), ]); } catch (error) { - // don't throw error if it's just a "server already running" case - if ( - !( - error instanceof Error && - error.message.startsWith("StdioClientTransport already started") - ) - ) { + if (error instanceof Error) { + const msg = error.message.toLowerCase(); + if (msg.includes("spawn") && msg.includes("enoent")) { + const command = msg.split(" ")[1]; + throw new Error( + `command "${command}" not found. To use this MCP server, install the ${command} CLI.`, + ); + } else if ( + !error.message.startsWith("StdioClientTransport already started") + ) { + // don't throw error if it's just a "server already running" case + throw error; + } + } else { throw error; } } From 2a77f8c5100dfe45b38868eeb1c26c510169e97e Mon Sep 17 00:00:00 2001 From: Dallin Romney Date: Tue, 18 Mar 2025 12:32:27 -0700 Subject: [PATCH 4/5] filter --- core/context/mcp/index.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/core/context/mcp/index.ts b/core/context/mcp/index.ts index c4f29f3e6..a9ad4211b 100644 --- a/core/context/mcp/index.ts +++ b/core/context/mcp/index.ts @@ -49,6 +49,7 @@ export class MCPManagerSingleton { async removeUnusedConnections(keepIds: string[]) { const toRemove = this.connections .keys() + .toArray() .filter((k) => !keepIds.includes(k)); await Promise.all(toRemove.map(this.removeConnection)); } From 427ef923b94cad7d564c8297e0ec0a4215dfddca Mon Sep 17 00:00:00 2001 From: Dallin Romney Date: Tue, 18 Mar 2025 13:18:47 -0700 Subject: [PATCH 5/5] typescript shenangians --- core/context/mcp/index.ts | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/core/context/mcp/index.ts b/core/context/mcp/index.ts index a9ad4211b..af4b39a5c 100644 --- a/core/context/mcp/index.ts +++ b/core/context/mcp/index.ts @@ -47,10 +47,9 @@ export class MCPManagerSingleton { } async removeUnusedConnections(keepIds: string[]) { - const toRemove = this.connections - .keys() - .toArray() - .filter((k) => !keepIds.includes(k)); + const toRemove = Array.from(this.connections.keys()).filter( + (k) => !keepIds.includes(k), + ); await Promise.all(toRemove.map(this.removeConnection)); } }