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 {