Skip to content

Commit 7ea4d85

Browse files
committed
Adjusted MCPConnection logic so the cwd is resolved to current workspace folder.
1 parent 80c6797 commit 7ea4d85

File tree

1 file changed

+22
-1
lines changed

1 file changed

+22
-1
lines changed

core/context/mcp/MCPConnection.ts

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { StdioClientTransport } from "@modelcontextprotocol/sdk/client/stdio.js"
99
import { StreamableHTTPClientTransport } from "@modelcontextprotocol/sdk/client/streamableHttp.js";
1010
import { WebSocketClientTransport } from "@modelcontextprotocol/sdk/client/websocket.js";
1111

12+
import { fileURLToPath } from "node:url";
1213
import {
1314
IDE,
1415
MCPConnectionStatus,
@@ -343,6 +344,26 @@ class MCPConnection {
343344
};
344345
}
345346

347+
/**
348+
* Resolves the current working directory of the current workspace.
349+
* @param cwd The cwd parameter provided by user.
350+
* @returns Current working directory (user-provided cwd or workspace root).
351+
*/
352+
private async resolveCwd(cwd?: string) {
353+
// Return cwd if it has been explicitly set.
354+
if (cwd) {
355+
return cwd;
356+
}
357+
// Otherwise use workspace folder.
358+
const workspaceDirs = await this.extras?.ide.getWorkspaceDirs();
359+
if (workspaceDirs) {
360+
if (workspaceDirs.length > 0) {
361+
cwd = fileURLToPath(workspaceDirs[0]);
362+
}
363+
}
364+
return cwd;
365+
}
366+
346367
private async constructTransportAsync(
347368
options: MCPOptions,
348369
): Promise<Transport> {
@@ -379,7 +400,7 @@ class MCPConnection {
379400
command,
380401
args,
381402
env,
382-
cwd: options.transport.cwd,
403+
cwd: await this.resolveCwd(options.transport.cwd),
383404
stderr: "pipe",
384405
});
385406

0 commit comments

Comments
 (0)