From 7ea4d85f1e5df675a8046328d491eee739e8afa9 Mon Sep 17 00:00:00 2001 From: Mattias Andersson Date: Mon, 1 Sep 2025 09:07:52 +0200 Subject: [PATCH 1/2] Adjusted MCPConnection logic so the cwd is resolved to current workspace folder. --- core/context/mcp/MCPConnection.ts | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/core/context/mcp/MCPConnection.ts b/core/context/mcp/MCPConnection.ts index c5a975473..9714ef60a 100644 --- a/core/context/mcp/MCPConnection.ts +++ b/core/context/mcp/MCPConnection.ts @@ -9,6 +9,7 @@ import { StdioClientTransport } from "@modelcontextprotocol/sdk/client/stdio.js" import { StreamableHTTPClientTransport } from "@modelcontextprotocol/sdk/client/streamableHttp.js"; import { WebSocketClientTransport } from "@modelcontextprotocol/sdk/client/websocket.js"; +import { fileURLToPath } from "node:url"; import { IDE, MCPConnectionStatus, @@ -343,6 +344,26 @@ class MCPConnection { }; } + /** + * Resolves the current working directory of the current workspace. + * @param cwd The cwd parameter provided by user. + * @returns Current working directory (user-provided cwd or workspace root). + */ + private async resolveCwd(cwd?: string) { + // Return cwd if it has been explicitly set. + if (cwd) { + return cwd; + } + // Otherwise use workspace folder. + const workspaceDirs = await this.extras?.ide.getWorkspaceDirs(); + if (workspaceDirs) { + if (workspaceDirs.length > 0) { + cwd = fileURLToPath(workspaceDirs[0]); + } + } + return cwd; + } + private async constructTransportAsync( options: MCPOptions, ): Promise { @@ -379,7 +400,7 @@ class MCPConnection { command, args, env, - cwd: options.transport.cwd, + cwd: await this.resolveCwd(options.transport.cwd), stderr: "pipe", }); From a1114019ca464979fea87fc7c6f518faf9fadaec Mon Sep 17 00:00:00 2001 From: Mattias Andersson Date: Tue, 23 Sep 2025 23:11:51 +0200 Subject: [PATCH 2/2] Adjusted the MCPConnection::resoveCwd() logic + added tests. --- core/context/mcp/MCPConnection.ts | 35 ++++++++++++++++++------ core/context/mcp/MCPConnection.vitest.ts | 34 +++++++++++++++++++++++ 2 files changed, 61 insertions(+), 8 deletions(-) diff --git a/core/context/mcp/MCPConnection.ts b/core/context/mcp/MCPConnection.ts index 9714ef60a..2ea7b9498 100644 --- a/core/context/mcp/MCPConnection.ts +++ b/core/context/mcp/MCPConnection.ts @@ -1,5 +1,6 @@ import { Client } from "@modelcontextprotocol/sdk/client/index.js"; import { Transport } from "@modelcontextprotocol/sdk/shared/transport.js"; +import { fileURLToPath } from "url"; import { SSEClientTransport, @@ -9,7 +10,6 @@ import { StdioClientTransport } from "@modelcontextprotocol/sdk/client/stdio.js" import { StreamableHTTPClientTransport } from "@modelcontextprotocol/sdk/client/streamableHttp.js"; import { WebSocketClientTransport } from "@modelcontextprotocol/sdk/client/websocket.js"; -import { fileURLToPath } from "node:url"; import { IDE, MCPConnectionStatus, @@ -20,6 +20,7 @@ import { MCPServerStatus, MCPTool, } from "../.."; +import { resolveRelativePathInDir } from "../../util/ideUtils"; import { getEnvPathFromUserShell } from "../../util/shellPath"; import { getOauthToken } from "./MCPOauth"; @@ -350,16 +351,34 @@ class MCPConnection { * @returns Current working directory (user-provided cwd or workspace root). */ private async resolveCwd(cwd?: string) { - // Return cwd if it has been explicitly set. - if (cwd) { + if (!cwd) { + return this.resolveWorkspaceCwd(undefined); + } + + if (cwd.startsWith("file://")) { + return fileURLToPath(cwd); + } + + // Return cwd if cwd is an absolute path. + if (cwd.charAt(0) === "/") { return cwd; } - // Otherwise use workspace folder. - const workspaceDirs = await this.extras?.ide.getWorkspaceDirs(); - if (workspaceDirs) { - if (workspaceDirs.length > 0) { - cwd = fileURLToPath(workspaceDirs[0]); + + return this.resolveWorkspaceCwd(cwd); + } + + private async resolveWorkspaceCwd(cwd: string | undefined) { + const IDE = await this.extras?.ide; + if (IDE) { + const target = cwd ?? "."; + const resolved = await resolveRelativePathInDir(target, IDE); + if (resolved) { + if (resolved.startsWith("file://")) { + return fileURLToPath(resolved); + } + return resolved; } + return resolved; } return cwd; } diff --git a/core/context/mcp/MCPConnection.vitest.ts b/core/context/mcp/MCPConnection.vitest.ts index 3f69d8f23..36d957241 100644 --- a/core/context/mcp/MCPConnection.vitest.ts +++ b/core/context/mcp/MCPConnection.vitest.ts @@ -1,5 +1,6 @@ import { Client } from "@modelcontextprotocol/sdk/client/index.js"; import { beforeEach, describe, expect, it, vi } from "vitest"; +import * as ideUtils from "../../util/ideUtils"; import MCPConnection from "./MCPConnection"; // Mock the shell path utility @@ -152,6 +153,39 @@ describe("MCPConnection", () => { }); }); + describe("resolveCwd", () => { + const baseOptions = { + name: "test-mcp", + id: "test-id", + transport: { + type: "stdio" as const, + command: "test-cmd", + args: [], + }, + }; + + it("should return absolute cwd unchanged", async () => { + const conn = new MCPConnection(baseOptions); + + await expect((conn as any).resolveCwd("/tmp/project")).resolves.toBe( + "/tmp/project", + ); + }); + + it("should resolve relative cwd using IDE workspace", async () => { + const ide = {} as any; + const mockResolve = vi + .spyOn(ideUtils, "resolveRelativePathInDir") + .mockResolvedValue("file:///workspace/src"); + const conn = new MCPConnection(baseOptions, { ide }); + + await expect((conn as any).resolveCwd("src")).resolves.toBe( + "/workspace/src", + ); + expect(mockResolve).toHaveBeenCalledWith("src", ide); + }); + }); + describe("connectClient", () => { const options = { name: "test-mcp",