Merge pull request #6446 from continuedev/nate/async-exec-shell-path

run shell path async to avoid blocking config load
This commit is contained in:
Dallin Romney 2025-07-10 16:01:34 -07:00 • committed by GitHub
commit 1a87532cdc
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 42 additions and 14 deletions

View file

@ -47,7 +47,8 @@ class MCPConnection {
};
constructor(public options: MCPOptions) {
this.transport = this.constructTransport(options);
// Don't construct transport in constructor to avoid blocking
this.transport = {} as Transport; // Will be set in connectClient
this.client = new Client(
{
@ -132,7 +133,8 @@ class MCPConnection {
});
}),
(async () => {
this.transport = this.constructTransport(this.options);
this.transport = await this.constructTransportAsync(this.options);
try {
await this.client.connect(this.transport);
} catch (error) {
@ -295,7 +297,9 @@ class MCPConnection {
};
}
private constructTransport(options: MCPOptions): Transport {
private async constructTransportAsync(
options: MCPOptions,
): Promise<Transport> {
switch (options.transport.type) {
case "stdio":
const env: Record<string, string> = options.transport.env
@ -309,7 +313,7 @@ class MCPConnection {
// For non-Windows platforms, try to get the PATH from user shell
if (process.platform !== "win32") {
try {
const shellEnvPath = getEnvPathFromUserShell();
const shellEnvPath = await getEnvPathFromUserShell();
if (shellEnvPath && shellEnvPath !== process.env.PATH) {
env.PATH = shellEnvPath;
}

View file

@ -3,6 +3,13 @@ import { beforeEach, describe, expect, it, vi } from "vitest";
import MCPConnection from "./MCPConnection";
import { StdioOptions } from "../../index.d.js";
// Mock the shell path utility
vi.mock("../../util/shellPath", () => ({
getEnvPathFromUserShell: vi
.fn()
.mockResolvedValue("/usr/local/bin:/usr/bin:/bin"),
}));
describe("MCPConnection", () => {
beforeEach(() => {
vi.restoreAllMocks();
@ -98,7 +105,7 @@ describe("MCPConnection", () => {
expect(conn.status).toBe("not-connected");
});
it("should throw on invalid transport type", () => {
it("should throw on invalid transport type", async () => {
const options = {
name: "test-mcp",
id: "test-id",
@ -107,9 +114,14 @@ describe("MCPConnection", () => {
} as any,
};
expect(() => new MCPConnection(options)).toThrow(
"Unsupported transport type: invalid",
);
const conn = new MCPConnection(options);
const abortController = new AbortController();
// The validation now happens during connectClient, not constructor
await conn.connectClient(false, abortController.signal);
expect(conn.status).toBe("error");
expect(conn.errors[0]).toContain("Unsupported transport type: invalid");
});
});
@ -203,6 +215,15 @@ describe("MCPConnection", () => {
() => new Promise((resolve) => setTimeout(resolve, 1000)),
);
// Mock the required methods for successful connection
const mockGetServerCapabilities = vi
.spyOn(Client.prototype, "getServerCapabilities")
.mockReturnValue({
resources: {},
tools: {},
prompts: {},
});
const abortController = new AbortController();
await conn.connectClient(false, abortController.signal);
@ -211,7 +232,7 @@ describe("MCPConnection", () => {
});
it("should handle connection timeout", async () => {
const conn = new MCPConnection({ ...options, timeout: 1 });
const conn = new MCPConnection({ ...options, timeout: 50 });
const mockConnect = vi
.spyOn(Client.prototype, "connect")
.mockImplementation(
@ -223,7 +244,9 @@ describe("MCPConnection", () => {
expect(conn.status).toBe("error");
expect(conn.errors[0]).toContain("Failed to connect");
expect(mockConnect).toHaveBeenCalled();
// The connection should timeout before connect is called due to transport construction
// Since transport construction happens first, and we're not mocking that,
// the timeout will happen during transport construction or connect attempt
});
it("should handle already connected state", async () => {

View file

@ -1,7 +1,8 @@
import { execSync } from "child_process";
import { homedir } from "os";
import { exec } from "child_process";
import { promisify } from "util";
export function getEnvPathFromUserShell(): string | undefined {
const execAsync = promisify(exec);
export async function getEnvPathFromUserShell(): Promise<string | undefined> {
if (process.platform === "win32") {
console.warn(`${getEnvPathFromUserShell.name} not implemented for Windows`);
return undefined;
@ -15,7 +16,7 @@ export function getEnvPathFromUserShell(): string | undefined {
// Source common profile files
const command = `${process.env.SHELL} -l -c 'for f in ~/.zprofile ~/.zshrc ~/.bash_profile ~/.bashrc; do [ -f "$f" ] && source "$f" 2>/dev/null; done; echo $PATH'`;
const stdout = execSync(command, {
const { stdout } = await execAsync(command, {
encoding: "utf8",
});