fix(core): expand ~ in a local MCP server's cwd (#61496)

This commit is contained in:
Aiden Cline
2026-11-03 17:52:09 -06:00
committed by GitHub
Unverified
parent f73e2847ba
commit 922994ac1e
3 changed files with 23 additions and 2 deletions
+1 -2
View File
@@ -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,
+9
View File
@@ -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<string, string>
}
/**
* 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
+13
View File
@@ -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))