246 lines
7.7 KiB
TypeScript
246 lines
7.7 KiB
TypeScript
import { Client } from "@modelcontextprotocol/sdk/client/index.js";
|
|
import { Transport } from "@modelcontextprotocol/sdk/shared/transport.js";
|
|
|
|
import { SSEClientTransport } from "@modelcontextprotocol/sdk/client/sse.js";
|
|
import { StdioClientTransport } from "@modelcontextprotocol/sdk/client/stdio.js";
|
|
import { WebSocketClientTransport } from "@modelcontextprotocol/sdk/client/websocket.js";
|
|
|
|
import {
|
|
MCPConnectionStatus,
|
|
MCPOptions,
|
|
MCPPrompt,
|
|
MCPResource,
|
|
MCPServerStatus,
|
|
MCPTool,
|
|
} from "../..";
|
|
|
|
const DEFAULT_MCP_TIMEOUT = 20_000; // 10 seconds
|
|
|
|
class MCPConnection {
|
|
public client: Client;
|
|
private transport: Transport;
|
|
|
|
private connectionPromise: Promise<unknown> | null = null;
|
|
public abortController: AbortController;
|
|
|
|
public status: MCPConnectionStatus = "not-connected";
|
|
public errors: string[] = [];
|
|
|
|
public prompts: MCPPrompt[] = [];
|
|
public tools: MCPTool[] = [];
|
|
public resources: MCPResource[] = [];
|
|
|
|
constructor(private readonly options: MCPOptions) {
|
|
this.transport = this.constructTransport(options);
|
|
|
|
this.client = new Client(
|
|
{
|
|
name: "continue-client",
|
|
version: "1.0.0",
|
|
},
|
|
{
|
|
capabilities: {},
|
|
},
|
|
);
|
|
|
|
this.abortController = new AbortController();
|
|
}
|
|
|
|
async disconnect() {
|
|
this.abortController.abort();
|
|
await this.client.close();
|
|
await this.transport.close();
|
|
}
|
|
|
|
private constructTransport(options: MCPOptions): Transport {
|
|
switch (options.transport.type) {
|
|
case "stdio":
|
|
const env: Record<string, string> = options.transport.env || {};
|
|
if (process.env.PATH !== undefined) {
|
|
env.PATH = process.env.PATH;
|
|
}
|
|
return new StdioClientTransport({
|
|
command: options.transport.command,
|
|
args: options.transport.args,
|
|
env,
|
|
});
|
|
case "websocket":
|
|
return new WebSocketClientTransport(new URL(options.transport.url));
|
|
case "sse":
|
|
return new SSEClientTransport(new URL(options.transport.url));
|
|
default:
|
|
throw new Error(
|
|
`Unsupported transport type: ${(options.transport as any).type}`,
|
|
);
|
|
}
|
|
}
|
|
|
|
getStatus(): MCPServerStatus {
|
|
return {
|
|
...this.options,
|
|
errors: this.errors,
|
|
prompts: this.prompts,
|
|
resources: this.resources,
|
|
tools: this.tools,
|
|
status: this.status,
|
|
};
|
|
}
|
|
|
|
async connectClient(forceRefresh: boolean, externalSignal: AbortSignal) {
|
|
if (!forceRefresh) {
|
|
// Already connected
|
|
if (this.status === "connected") {
|
|
return;
|
|
}
|
|
|
|
// Connection is already in progress; wait for it to complete
|
|
if (this.connectionPromise) {
|
|
await this.connectionPromise;
|
|
return;
|
|
}
|
|
}
|
|
|
|
this.status = "connecting";
|
|
this.tools = [];
|
|
this.prompts = [];
|
|
this.resources = [];
|
|
this.errors = [];
|
|
|
|
this.abortController.abort();
|
|
this.abortController = new AbortController();
|
|
|
|
this.connectionPromise = Promise.race([
|
|
// If aborted by a refresh or other, cancel and don't do anything
|
|
new Promise((resolve) => {
|
|
externalSignal.addEventListener("abort", () => {
|
|
resolve(undefined);
|
|
});
|
|
}),
|
|
new Promise((resolve) => {
|
|
this.abortController.signal.addEventListener("abort", () => {
|
|
resolve(undefined);
|
|
});
|
|
}),
|
|
(async () => {
|
|
const timeoutController = new AbortController();
|
|
const connectionTimeout = setTimeout(
|
|
() => timeoutController.abort(),
|
|
this.options.timeout ?? DEFAULT_MCP_TIMEOUT,
|
|
);
|
|
|
|
try {
|
|
await Promise.race([
|
|
new Promise((_, reject) => {
|
|
timeoutController.signal.addEventListener("abort", () => {
|
|
reject(new Error("Connection timed out"));
|
|
});
|
|
}),
|
|
(async () => {
|
|
this.transport = this.constructTransport(this.options);
|
|
try {
|
|
await this.client.connect(this.transport);
|
|
await this.client.close();
|
|
} catch (error) {
|
|
// Allow the case where for whatever reason is already connected
|
|
if (
|
|
error instanceof Error &&
|
|
error.message.startsWith(
|
|
"StdioClientTransport already started",
|
|
)
|
|
) {
|
|
await this.client.close();
|
|
await this.client.connect(this.transport);
|
|
} else {
|
|
throw error;
|
|
}
|
|
}
|
|
|
|
// TODO register server notification handlers
|
|
// this.client.transport?.onmessage(msg => console.log())
|
|
// this.client.setNotificationHandler(, notification => {
|
|
// console.log(notification)
|
|
// })
|
|
|
|
const capabilities = this.client.getServerCapabilities();
|
|
|
|
// Resources <—> Context Provider
|
|
if (capabilities?.resources) {
|
|
try {
|
|
const { resources } = await this.client.listResources(
|
|
{},
|
|
{ signal: timeoutController.signal },
|
|
);
|
|
this.resources = resources;
|
|
} catch (e) {
|
|
let errorMessage = `Error loading resources for MCP Server ${this.options.name}`;
|
|
if (e instanceof Error) {
|
|
errorMessage += `: ${e.message}`;
|
|
}
|
|
this.errors.push(errorMessage);
|
|
}
|
|
}
|
|
|
|
// Tools <—> Tools
|
|
if (capabilities?.tools) {
|
|
try {
|
|
const { tools } = await this.client.listTools(
|
|
{},
|
|
{ signal: timeoutController.signal },
|
|
);
|
|
this.tools = tools;
|
|
} catch (e) {
|
|
let errorMessage = `Error loading tools for MCP Server ${this.options.name}`;
|
|
if (e instanceof Error) {
|
|
errorMessage += `: ${e.message}`;
|
|
}
|
|
this.errors.push(errorMessage);
|
|
}
|
|
}
|
|
|
|
// Prompts <—> Slash commands
|
|
if (capabilities?.prompts) {
|
|
try {
|
|
const { prompts } = await this.client.listPrompts(
|
|
{},
|
|
{ signal: timeoutController.signal },
|
|
);
|
|
this.prompts = prompts;
|
|
} catch (e) {
|
|
let errorMessage = `Error loading prompts for MCP Server ${this.options.name}`;
|
|
if (e instanceof Error) {
|
|
errorMessage += `: ${e.message}`;
|
|
}
|
|
this.errors.push(errorMessage);
|
|
}
|
|
}
|
|
|
|
this.status = "connected";
|
|
})(),
|
|
]);
|
|
} catch (error) {
|
|
// Otherwise it's a connection error
|
|
let errorMessage = `Failed to connect to MCP server ${this.options.name}`;
|
|
if (error instanceof Error) {
|
|
const msg = error.message.toLowerCase();
|
|
if (msg.includes("spawn") && msg.includes("enoent")) {
|
|
const command = msg.split(" ")[1];
|
|
errorMessage += `command "${command}" not found. To use this MCP server, install the ${command} CLI.`;
|
|
} else {
|
|
errorMessage += ": " + error.message;
|
|
}
|
|
}
|
|
|
|
this.status = "error";
|
|
this.errors.push(errorMessage);
|
|
} finally {
|
|
this.connectionPromise = null;
|
|
clearTimeout(connectionTimeout);
|
|
}
|
|
})(),
|
|
]);
|
|
|
|
await this.connectionPromise;
|
|
}
|
|
}
|
|
|
|
export default MCPConnection;
|