Changesets · Branches · Tags · Compare
| Changeset | a183d270e16c0e527939f9fdc17c29b95a7b49ac0b43b8967149eff66a14bec0 |
|---|---|
| Author | Dallin Romney <dallinromney@gmail.com> · 2025-07-10 16:01 -07:00 |
| Committer | GitHub <noreply@github.com> · 2025-07-10 16:01 -07:00 |
| Parents | 3860b50887c8, b7ffac0e7aac |
3 files changed
modified core/context/mcp/MCPConnection.ts +8 −4
| @@ -47,7 +47,8 @@ | ||
| 47 | 47 | }; |
| 48 | 48 | |
| 49 | 49 | constructor(public options: MCPOptions) { |
| 50 | - this.transport = this.constructTransport(options); | |
| 50 | + // Don't construct transport in constructor to avoid blocking | |
| 51 | + this.transport = {} as Transport; // Will be set in connectClient | |
| 51 | 52 | |
| 52 | 53 | this.client = new Client( |
| 53 | 54 | { |
| @@ -132,7 +133,8 @@ | ||
| 132 | 133 | }); |
| 133 | 134 | }), |
| 134 | 135 | (async () => { |
| 135 | - this.transport = this.constructTransport(this.options); | |
| 136 | + this.transport = await this.constructTransportAsync(this.options); | |
| 137 | + | |
| 136 | 138 | try { |
| 137 | 139 | await this.client.connect(this.transport); |
| 138 | 140 | } catch (error) { |
| @@ -295,7 +297,9 @@ | ||
| 295 | 297 | }; |
| 296 | 298 | } |
| 297 | 299 | |
| 298 | - private constructTransport(options: MCPOptions): Transport { | |
| 300 | + private async constructTransportAsync( | |
| 301 | + options: MCPOptions, | |
| 302 | + ): Promise<Transport> { | |
| 299 | 303 | switch (options.transport.type) { |
| 300 | 304 | case "stdio": |
| 301 | 305 | const env: Record<string, string> = options.transport.env |
| @@ -309,7 +313,7 @@ | ||
| 309 | 313 | // For non-Windows platforms, try to get the PATH from user shell |
| 310 | 314 | if (process.platform !== "win32") { |
| 311 | 315 | try { |
| 312 | - const shellEnvPath = getEnvPathFromUserShell(); | |
| 316 | + const shellEnvPath = await getEnvPathFromUserShell(); | |
| 313 | 317 | if (shellEnvPath && shellEnvPath !== process.env.PATH) { |
| 314 | 318 | env.PATH = shellEnvPath; |
| 315 | 319 | } |
modified core/context/mcp/MCPConnection.vitest.ts +29 −6
| @@ -3,6 +3,13 @@ | ||
| 3 | 3 | import MCPConnection from "./MCPConnection"; |
| 4 | 4 | import { StdioOptions } from "../../index.d.js"; |
| 5 | 5 | |
| 6 | +// Mock the shell path utility | |
| 7 | +vi.mock("../../util/shellPath", () => ({ | |
| 8 | + getEnvPathFromUserShell: vi | |
| 9 | + .fn() | |
| 10 | + .mockResolvedValue("/usr/local/bin:/usr/bin:/bin"), | |
| 11 | +})); | |
| 12 | + | |
| 6 | 13 | describe("MCPConnection", () => { |
| 7 | 14 | beforeEach(() => { |
| 8 | 15 | vi.restoreAllMocks(); |
| @@ -98,7 +105,7 @@ | ||
| 98 | 105 | expect(conn.status).toBe("not-connected"); |
| 99 | 106 | }); |
| 100 | 107 | |
| 101 | - it("should throw on invalid transport type", () => { | |
| 108 | + it("should throw on invalid transport type", async () => { | |
| 102 | 109 | const options = { |
| 103 | 110 | name: "test-mcp", |
| 104 | 111 | id: "test-id", |
| @@ -107,9 +114,14 @@ | ||
| 107 | 114 | } as any, |
| 108 | 115 | }; |
| 109 | 116 | |
| 110 | - expect(() => new MCPConnection(options)).toThrow( | |
| 111 | - "Unsupported transport type: invalid", | |
| 112 | - ); | |
| 117 | + const conn = new MCPConnection(options); | |
| 118 | + const abortController = new AbortController(); | |
| 119 | + | |
| 120 | + // The validation now happens during connectClient, not constructor | |
| 121 | + await conn.connectClient(false, abortController.signal); | |
| 122 | + | |
| 123 | + expect(conn.status).toBe("error"); | |
| 124 | + expect(conn.errors[0]).toContain("Unsupported transport type: invalid"); | |
| 113 | 125 | }); |
| 114 | 126 | }); |
| 115 | 127 | |
| @@ -203,6 +215,15 @@ | ||
| 203 | 215 | () => new Promise((resolve) => setTimeout(resolve, 1000)), |
| 204 | 216 | ); |
| 205 | 217 | |
| 218 | + // Mock the required methods for successful connection | |
| 219 | + const mockGetServerCapabilities = vi | |
| 220 | + .spyOn(Client.prototype, "getServerCapabilities") | |
| 221 | + .mockReturnValue({ | |
| 222 | + resources: {}, | |
| 223 | + tools: {}, | |
| 224 | + prompts: {}, | |
| 225 | + }); | |
| 226 | + | |
| 206 | 227 | const abortController = new AbortController(); |
| 207 | 228 | await conn.connectClient(false, abortController.signal); |
| 208 | 229 | |
| @@ -211,7 +232,7 @@ | ||
| 211 | 232 | }); |
| 212 | 233 | |
| 213 | 234 | it("should handle connection timeout", async () => { |
| 214 | - const conn = new MCPConnection({ ...options, timeout: 1 }); | |
| 235 | + const conn = new MCPConnection({ ...options, timeout: 50 }); | |
| 215 | 236 | const mockConnect = vi |
| 216 | 237 | .spyOn(Client.prototype, "connect") |
| 217 | 238 | .mockImplementation( |
| @@ -223,7 +244,9 @@ | ||
| 223 | 244 | |
| 224 | 245 | expect(conn.status).toBe("error"); |
| 225 | 246 | expect(conn.errors[0]).toContain("Failed to connect"); |
| 226 | - expect(mockConnect).toHaveBeenCalled(); | |
| 247 | + // The connection should timeout before connect is called due to transport construction | |
| 248 | + // Since transport construction happens first, and we're not mocking that, | |
| 249 | + // the timeout will happen during transport construction or connect attempt | |
| 227 | 250 | }); |
| 228 | 251 | |
| 229 | 252 | it("should handle already connected state", async () => { |
modified core/util/shellPath.ts +6 −5
| @@ -1,7 +1,8 @@ | ||
| 1 | -import { execSync } from "child_process"; | |
| 2 | -import { homedir } from "os"; | |
| 3 | - | |
| 4 | -export function getEnvPathFromUserShell(): string | undefined { | |
| 1 | +import { exec } from "child_process"; | |
| 2 | +import { promisify } from "util"; | |
| 3 | + | |
| 4 | +const execAsync = promisify(exec); | |
| 5 | +export async function getEnvPathFromUserShell(): Promise<string | undefined> { | |
| 5 | 6 | if (process.platform === "win32") { |
| 6 | 7 | console.warn(`${getEnvPathFromUserShell.name} not implemented for Windows`); |
| 7 | 8 | return undefined; |
| @@ -15,7 +16,7 @@ | ||
| 15 | 16 | // Source common profile files |
| 16 | 17 | 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'`; |
| 17 | 18 | |
| 18 | - const stdout = execSync(command, { | |
| 19 | + const { stdout } = await execAsync(command, { | |
| 19 | 20 | encoding: "utf8", |
| 20 | 21 | }); |
| 21 | 22 |