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 0b1565f58..a11e1ab44 100644 --- a/core/config/yaml/loadYaml.ts +++ b/core/config/yaml/loadYaml.ts @@ -369,12 +369,18 @@ 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(); const mcpConnectionTimeout = setTimeout( () => abortController.abort(), - 4000, + 5000, ); try { @@ -386,28 +392,18 @@ 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 19de5434a..af4b39a5c 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)!; } } @@ -46,6 +45,13 @@ export class MCPManagerSingleton { this.connections.delete(id); } + + async removeUnusedConnections(keepIds: string[]) { + const toRemove = Array.from(this.connections.keys()).filter( + (k) => !keepIds.includes(k), + ); + await Promise.all(toRemove.map(this.removeConnection)); + } } class MCPConnection { @@ -123,25 +129,32 @@ 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) { + 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; } }