Merge branch 'main' into pe/tagToString-fix

This commit is contained in:
Patrick Erichsen 2025-05-12 12:06:24 -07:00
commit 372fd87ffb
2 changed files with 62 additions and 14 deletions

View file

@ -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();
});

View file

@ -16,6 +16,17 @@ 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",
"pnpx",
"dlx",
"nx",
"bunx",
];
class MCPConnection {
public client: Client;
private transport: Transport;
@ -52,18 +63,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<string, string> = 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 +122,13 @@ class MCPConnection {
...init,
headers: {
...init?.headers,
...(options.transport.requestOptions?.headers as Record<string, string> | undefined),
}
...(options.transport.requestOptions?.headers as
| Record<string, string>
| undefined),
},
}),
},
requestInit: { headers: options.transport.requestOptions?.headers }
requestInit: { headers: options.transport.requestOptions?.headers },
});
default:
throw new Error(
@ -232,14 +280,14 @@ 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 "${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 += `Error: command "${command}" not found. To use this MCP server, install the ${command} CLI.`;
} else {
errorMessage += ": " + error.message;
errorMessage += "Error: " + error.message;
}
}