Skip to content
Open
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 17 additions & 21 deletions core/context/mcp/MCPConnection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,13 @@ class MCPConnection {
// Don't construct transport in constructor to avoid blocking
this.transport = {} as Transport; // Will be set in connectClient

this.client = new Client(
this.client = this.createClient();

this.abortController = new AbortController();
}

private createClient(): Client {
return new Client(
{
name: "continue-client",
version: "1.0.0",
Expand All @@ -94,8 +100,6 @@ class MCPConnection {
capabilities: {},
},
);

this.abortController = new AbortController();
}

async disconnect(disable = false) {
Expand Down Expand Up @@ -213,29 +217,20 @@ Org-level secrets can only be used for MCP by Background Agents (https://docs.co
});
}),
(async () => {
try {
await this.client.close();
} catch {
// Best-effort cleanup; may fail if never connected
}
this.client = this.createClient();

if ("command" in this.options) {
// STDIO: no need to check type, just if command is present
const transport = await this.constructStdioTransport(
this.options,
);
try {
await this.client.connect(transport, {});
this.transport = transport;
} 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(transport);
this.transport = transport;
} else {
throw error;
}
}
await this.client.connect(transport, {});
this.transport = transport;
} else {
// SSE/HTTP: if type isn't explicit: try http and fall back to sse
if (this.options.type === "sse") {
Expand Down Expand Up @@ -265,6 +260,7 @@ Org-level secrets can only be used for MCP by Background Agents (https://docs.co
await this.client.connect(transport, {});
this.transport = transport;
} catch (e) {
this.client = this.createClient();
try {
const transport = this.constructSseTransport({
...this.options,
Expand Down
Loading