kill unused mcp connections

This commit is contained in:
Dallin Romney 2025-03-17 00:46:18 -07:00
parent d11ed7da53
commit 5f574be9a4
3 changed files with 32 additions and 44 deletions

View file

@ -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,
};

View file

@ -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,

View file

@ -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<ConfigValidationError | undefined> {
) {
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;
}
}