From 922994ac1e3a67c66d72951732483af63ae45c5e Mon Sep 17 00:00:00 2001 From: Aiden Cline <63023139+rekram1-node@users.noreply.github.com> Date: Tue, 3 Nov 2026 17:52:09 -0600 Subject: [PATCH] fix(core): expand ~ in a local MCP server's cwd (#61496) --- packages/core/src/mcp/client.ts | 3 +-- packages/core/src/mcp/stdio.ts | 9 +++++++++ packages/core/test/mcp-stdio.test.ts | 13 +++++++++++++ 3 files changed, 23 insertions(+), 2 deletions(-) diff --git a/packages/core/src/mcp/client.ts b/packages/core/src/mcp/client.ts index 950804b8727..b32128dc29e 100644 --- a/packages/core/src/mcp/client.ts +++ b/packages/core/src/mcp/client.ts @@ -1,6 +1,5 @@ export * as McpClient from "./client.js" -import path from "node:path" import { pathToFileURL } from "node:url" import { Client, @@ -290,7 +289,7 @@ export const connect = Effect.fnUntraced(function* ( server, command, args, - cwd: config.cwd ? path.resolve(directory, config.cwd) : directory, + cwd: McpStdio.resolveCwd(directory, config.cwd), environment: { ...(command === "opencode" ? { BUN_BE_BUN: "1" } : {}), ...config.environment, diff --git a/packages/core/src/mcp/stdio.ts b/packages/core/src/mcp/stdio.ts index b94eba6bf90..569f78f1aaf 100644 --- a/packages/core/src/mcp/stdio.ts +++ b/packages/core/src/mcp/stdio.ts @@ -5,6 +5,7 @@ import { Cause, Duration, Effect, Fiber, Option, Queue, Result, Scope, Stream } import { ChildProcess } from "effect/process" import type { ChildProcessHandle } from "effect/process/ChildProcessSpawner" import { Environment } from "../environment/index.js" +import { FileAccess } from "../file-access.js" /** Mirrors StdioClientTransport: wait this long for a graceful exit after stdin closes. */ const CLOSE_GRACE = Duration.seconds(2) @@ -34,6 +35,14 @@ export interface Options { readonly environment: Record } +/** + * Resolves a local server's configured `cwd` against the workspace directory, expanding `~` to the + * opencode host's home. Environments do not expose their own home, so a workspace-backed location + * gets the host's home too. + */ +export const resolveCwd = (directory: string, cwd: string | undefined) => + cwd ? FileAccess.resolvePath(directory, cwd) : directory + /** * MCP stdio transport that spawns its server through the location's `Environment` instead of the * SDK's host-bound `StdioClientTransport`, so a workspace-backed location runs its MCP servers diff --git a/packages/core/test/mcp-stdio.test.ts b/packages/core/test/mcp-stdio.test.ts index 7e6d7ea308f..d12bcb9aa5c 100644 --- a/packages/core/test/mcp-stdio.test.ts +++ b/packages/core/test/mcp-stdio.test.ts @@ -1,7 +1,9 @@ import { expect, test } from "bun:test" +import path from "node:path" import type { JSONRPCMessage } from "@modelcontextprotocol/client" import { Environment } from "@opencode/core/environment/index" import { McpStdio } from "@opencode/core/mcp/stdio" +import { Global } from "@opencode/util/global" import { Deferred, Effect, Layer, Sink, Stream } from "effect" import { ChildProcessSpawner } from "effect/process" import { ExitCode, makeHandle, ProcessId } from "effect/process/ChildProcessSpawner" @@ -10,6 +12,17 @@ const encoder = new TextEncoder() const MiB = 1024 * 1024 const after = encoder.encode('{"jsonrpc":"2.0","method":"after"}\n') +test("resolves cwd from the workspace and expands ~ to the home directory", () => { + const directory = path.resolve("project") + const home = Global.Path.home + + expect(McpStdio.resolveCwd(directory, undefined)).toBe(directory) + expect(McpStdio.resolveCwd(directory, "~")).toBe(home) + expect(McpStdio.resolveCwd(directory, "~/tools")).toBe(path.join(home, "tools")) + expect(McpStdio.resolveCwd(directory, "tools/server")).toBe(path.join(directory, "tools", "server")) + expect(McpStdio.resolveCwd(directory, path.resolve("elsewhere"))).toBe(path.resolve("elsewhere")) +}) + test("receives a frame between 10 and 16 MiB split into pipe-sized chunks", async () => { // 12 MiB sits between the SDK read buffer's 10 MiB default and our 16 MiB cap. const output = await read(chunks(frame(12 * MiB), 64 * 1024))