From b8ca4cb6ee91670e87ae331f5e4bab24a42adbe6 Mon Sep 17 00:00:00 2001 From: Patrick Erichsen Date: Mon, 12 May 2025 09:38:11 -0700 Subject: [PATCH 1/5] fix: use `cmd` for windows MCP connections --- core/context/mcp/MCPConnection.ts | 54 +++++++++++++++++++++++++++---- 1 file changed, 47 insertions(+), 7 deletions(-) diff --git a/core/context/mcp/MCPConnection.ts b/core/context/mcp/MCPConnection.ts index 532bb273a..74449b51d 100644 --- a/core/context/mcp/MCPConnection.ts +++ b/core/context/mcp/MCPConnection.ts @@ -16,6 +16,9 @@ import { const DEFAULT_MCP_TIMEOUT = 20_000; // 20 seconds +// Commands that are batch scripts on Windows and need cmd.exe to execute +const WINDOWS_BATCH_COMMANDS = ["npx", "npm", "uv", "uvx"]; + class MCPConnection { public client: Client; private transport: Transport; @@ -52,18 +55,53 @@ class MCPConnection { await this.transport.close(); } + /** + * Resolves the command and arguments for the current platform + * On Windows, batch script commands need to be executed via cmd.exe + * @param originalCommand The original command + * @param originalArgs The original command arguments + * @returns An object with the resolved command and arguments + */ + private resolveCommandForPlatform( + originalCommand: string, + originalArgs: string[], + ): { command: string; args: string[] } { + // If not on Windows or not a batch command, return as-is + if ( + process.platform !== "win32" || + !WINDOWS_BATCH_COMMANDS.includes(originalCommand) + ) { + return { command: originalCommand, args: originalArgs }; + } + + // On Windows, we need to execute batch commands via cmd.exe + // Format: cmd.exe /c command [args] + return { + command: "cmd.exe", + args: ["/c", originalCommand, ...originalArgs], + }; + } + private constructTransport(options: MCPOptions): Transport { switch (options.transport.type) { case "stdio": const env: Record = options.transport.env ? { ...options.transport.env } : {}; + if (process.env.PATH !== undefined) { env.PATH = process.env.PATH; } + + // Resolve the command and args for the current platform + const { command, args } = this.resolveCommandForPlatform( + options.transport.command, + options.transport.args || [], + ); + return new StdioClientTransport({ - command: options.transport.command, - args: options.transport.args, + command, + args, env, }); case "websocket": @@ -76,11 +114,13 @@ class MCPConnection { ...init, headers: { ...init?.headers, - ...(options.transport.requestOptions?.headers as Record | undefined), - } + ...(options.transport.requestOptions?.headers as + | Record + | undefined), + }, }), }, - requestInit: { headers: options.transport.requestOptions?.headers } + requestInit: { headers: options.transport.requestOptions?.headers }, }); default: throw new Error( @@ -232,12 +272,12 @@ class MCPConnection { ]); } catch (error) { // Otherwise it's a connection error - let errorMessage = `Failed to connect to MCP server ${this.options.name}`; + let errorMessage = `Failed to connect to MCP server "${this.options.name}"\n`; 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.`; + errorMessage += `: command "${command}" not found. To use this MCP server, install the ${command} CLI.`; } else { errorMessage += ": " + error.message; } From 5e184e25ff8bb1de1c1b0ade4e7471bc2b326ead Mon Sep 17 00:00:00 2001 From: Patrick Erichsen Date: Mon, 12 May 2025 09:42:27 -0700 Subject: [PATCH 2/5] Update MCPConnection.ts --- core/context/mcp/MCPConnection.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/core/context/mcp/MCPConnection.ts b/core/context/mcp/MCPConnection.ts index 74449b51d..38e998064 100644 --- a/core/context/mcp/MCPConnection.ts +++ b/core/context/mcp/MCPConnection.ts @@ -277,9 +277,9 @@ class MCPConnection { 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.`; + errorMessage += `Error: command "${command}" not found. To use this MCP server, install the ${command} CLI.`; } else { - errorMessage += ": " + error.message; + errorMessage += "Error: " + error.message; } } From 569695e650a907627496d5b7dd8cb61d9c8647a0 Mon Sep 17 00:00:00 2001 From: Patrick Erichsen Date: Mon, 12 May 2025 09:49:46 -0700 Subject: [PATCH 3/5] Update MCPConnection.ts --- core/context/mcp/MCPConnection.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/context/mcp/MCPConnection.ts b/core/context/mcp/MCPConnection.ts index 38e998064..3cab6ea04 100644 --- a/core/context/mcp/MCPConnection.ts +++ b/core/context/mcp/MCPConnection.ts @@ -17,7 +17,7 @@ import { const DEFAULT_MCP_TIMEOUT = 20_000; // 20 seconds // Commands that are batch scripts on Windows and need cmd.exe to execute -const WINDOWS_BATCH_COMMANDS = ["npx", "npm", "uv", "uvx"]; +const WINDOWS_BATCH_COMMANDS = ["npx", "uv", "uvx"]; class MCPConnection { public client: Client; From 0eb05459163807cb632e20f1f9a97eb6c2acf5a6 Mon Sep 17 00:00:00 2001 From: Patrick Erichsen Date: Mon, 12 May 2025 10:23:34 -0700 Subject: [PATCH 4/5] Update MCPConnection.ts --- core/context/mcp/MCPConnection.ts | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/core/context/mcp/MCPConnection.ts b/core/context/mcp/MCPConnection.ts index 3cab6ea04..56e90e8e7 100644 --- a/core/context/mcp/MCPConnection.ts +++ b/core/context/mcp/MCPConnection.ts @@ -17,7 +17,15 @@ import { const DEFAULT_MCP_TIMEOUT = 20_000; // 20 seconds // Commands that are batch scripts on Windows and need cmd.exe to execute -const WINDOWS_BATCH_COMMANDS = ["npx", "uv", "uvx"]; +const WINDOWS_BATCH_COMMANDS = [ + "npx", + "uv", + "uvx", + "pnpx", + "dlx", + "nx", + "bunx", +]; class MCPConnection { public client: Client; @@ -272,7 +280,7 @@ class MCPConnection { ]); } catch (error) { // Otherwise it's a connection error - let errorMessage = `Failed to connect to MCP server "${this.options.name}"\n`; + let errorMessage = `Failed to connect to "${this.options.name}"\n`; if (error instanceof Error) { const msg = error.message.toLowerCase(); if (msg.includes("spawn") && msg.includes("enoent")) { From 30cd65a205d505d1d432f683b3af2f543cd428ac Mon Sep 17 00:00:00 2001 From: Patrick Erichsen Date: Mon, 12 May 2025 10:34:39 -0700 Subject: [PATCH 5/5] Update MCPConnection.test.ts --- core/context/mcp/MCPConnection.test.ts | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/core/context/mcp/MCPConnection.test.ts b/core/context/mcp/MCPConnection.test.ts index f2b13ca10..acfc6618a 100644 --- a/core/context/mcp/MCPConnection.test.ts +++ b/core/context/mcp/MCPConnection.test.ts @@ -64,10 +64,10 @@ describe("MCPConnection", () => { url: "http://test.com/events", requestOptions: { headers: { - "Authorization": "Bearer token123", - "X-Custom-Header": "custom-value" - } - } + Authorization: "Bearer token123", + "X-Custom-Header": "custom-value", + }, + }, }, }; @@ -172,7 +172,7 @@ describe("MCPConnection", () => { expect(mockConnect).toHaveBeenCalled(); }); - it('should handle custom connection timeout', async () => { + it("should handle custom connection timeout", async () => { const conn = new MCPConnection({ ...options, timeout: 11 }); const mockConnect = jest .spyOn(Client.prototype, "connect") @@ -199,7 +199,7 @@ describe("MCPConnection", () => { await conn.connectClient(false, abortController.signal); expect(conn.status).toBe("error"); - expect(conn.errors[0]).toContain("Failed to connect to MCP server"); + expect(conn.errors[0]).toContain("Failed to connect"); expect(mockConnect).toHaveBeenCalled(); });