Adjusted the MCPConnection::resoveCwd() logic + added tests.
This commit is contained in:
parent
7ea4d85f1e
commit
a1114019ca
2 changed files with 61 additions and 8 deletions
|
|
@ -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;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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",
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue