11import { Client } from "@modelcontextprotocol/sdk/client/index.js" ;
22import { Transport } from "@modelcontextprotocol/sdk/shared/transport.js" ;
3+ import { fileURLToPath } from "url" ;
34
45import {
56 SSEClientTransport ,
@@ -9,7 +10,6 @@ import { StdioClientTransport } from "@modelcontextprotocol/sdk/client/stdio.js"
910import { StreamableHTTPClientTransport } from "@modelcontextprotocol/sdk/client/streamableHttp.js" ;
1011import { WebSocketClientTransport } from "@modelcontextprotocol/sdk/client/websocket.js" ;
1112
12- import { fileURLToPath } from "node:url" ;
1313import {
1414 IDE ,
1515 MCPConnectionStatus ,
@@ -20,6 +20,7 @@ import {
2020 MCPServerStatus ,
2121 MCPTool ,
2222} from "../.." ;
23+ import { resolveRelativePathInDir } from "../../util/ideUtils" ;
2324import { getEnvPathFromUserShell } from "../../util/shellPath" ;
2425import { getOauthToken } from "./MCPOauth" ;
2526
@@ -350,16 +351,34 @@ class MCPConnection {
350351 * @returns Current working directory (user-provided cwd or workspace root).
351352 */
352353 private async resolveCwd ( cwd ?: string ) {
353- // Return cwd if it has been explicitly set.
354- if ( cwd ) {
354+ if ( ! cwd ) {
355+ return this . resolveWorkspaceCwd ( undefined ) ;
356+ }
357+
358+ if ( cwd . startsWith ( "file://" ) ) {
359+ return fileURLToPath ( cwd ) ;
360+ }
361+
362+ // Return cwd if cwd is an absolute path.
363+ if ( cwd . charAt ( 0 ) === "/" ) {
355364 return cwd ;
356365 }
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 ] ) ;
366+
367+ return this . resolveWorkspaceCwd ( cwd ) ;
368+ }
369+
370+ private async resolveWorkspaceCwd ( cwd : string | undefined ) {
371+ const IDE = await this . extras ?. ide ;
372+ if ( IDE ) {
373+ const target = cwd ?? "." ;
374+ const resolved = await resolveRelativePathInDir ( target , IDE ) ;
375+ if ( resolved ) {
376+ if ( resolved . startsWith ( "file://" ) ) {
377+ return fileURLToPath ( resolved ) ;
378+ }
379+ return resolved ;
362380 }
381+ return resolved ;
363382 }
364383 return cwd ;
365384 }
0 commit comments