From 7ea4d85f1e5df675a8046328d491eee739e8afa9 Mon Sep 17 00:00:00 2001 From: Mattias Andersson Date: Mon, 1 Sep 2025 09:07:52 +0200 Subject: [PATCH] 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", });