Claude Code. Any Model. The most powerful AI coding agent now speaks every language.
fix(mcp): reject a setup_from that is not an existing directory
Jack Rudenko174743feff09
diff --git a/ai-docs/architecture/mcp-sessions.md b/ai-docs/architecture/mcp-sessions.mdindex 5cc80a5d..b6df0805 100644--- a/ai-docs/architecture/mcp-sessions.md+++ b/ai-docs/architecture/mcp-sessions.md@@ -356,7 +356,9 @@ What `createSession` does now (`mcp-session/inherited-settings.ts`, `session-man- `setup_from` defaults to the server's cwd (the caller's project) and is skipped when it resolvesto the child's own cwd, whose files Claude Code already reads. `setup_from: null` opts out: the- child's argv and files are byte-for-byte what they were.+ child's argv and files are byte-for-byte what they were. An explicit `setup_from` that is not an+ existing directory, or a value that is neither a string nor null, is refused as `invalid_args`;+ the implicit default is not checked.- The layer is `settings.json`, then `settings.local.json`, then every caller `--settings` value(both `--settings v` and `--settings=v`), merged in that order: objects deep, arraysconcatenated without duplicates, scalars later-wins.diff --git a/packages/cli/src/mcp-server.ts b/packages/cli/src/mcp-server.tsindex c4fe6564..d3f02990 100644--- a/packages/cli/src/mcp-server.ts+++ b/packages/cli/src/mcp-server.ts@@ -1991,7 +1991,8 @@ function defineTools(sessionManager: SessionManager, nested: boolean): ToolDefindescription:"Directory whose .claude/settings.json and settings.local.json the session " +"inherits (enabled plugins, permissions, env), so a session in another work_dir " +- "keeps the caller's plugins and agents. Default: the caller's project directory, " ++ "keeps the caller's plugins and agents; an explicit path must be an existing " ++ "directory. Default: the caller's project directory, " +"when work_dir differs from it. null keeps the session on work_dir's own settings only.",},},diff --git a/packages/cli/src/mcp-session/inherited-settings.test.ts b/packages/cli/src/mcp-session/inherited-settings.test.tsindex 6ee702ef..9275eae4 100644--- a/packages/cli/src/mcp-session/inherited-settings.test.ts+++ b/packages/cli/src/mcp-session/inherited-settings.test.ts@@ -1,7 +1,7 @@import { afterEach, beforeEach, describe, expect, test } from "bun:test";import { mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs";import { tmpdir } from "node:os";-import { join, resolve } from "node:path";+import { join, relative, resolve } from "node:path";import {buildInheritedSettingsLayer,@@ -313,14 +313,19 @@ describe("resolveSetupFrom", () => {test("resolves a relative setup directory against the process cwd", () => {// REGRESSION: MTL-204/MTL-185 sandbox child lost the caller's plugins — /dev:fix session dev-fix-20261022-023522-f75563b5- expect(resolveSetupFrom("rel/x", join(tmp, "work"), join(tmp, "server"))).toBe(- resolve("rel/x")+ const setupFrom = join(tmp, "rel", "x");+ mkdirSync(setupFrom, { recursive: true });+ const relativeSetupFrom = relative(process.cwd(), setupFrom);++ expect(resolveSetupFrom(relativeSetupFrom, join(tmp, "work"), join(tmp, "server"))).toBe(+ resolve(relativeSetupFrom));});test("omits inheritance when work_dir is absent and setup resolves to serverCwd", () => {// REGRESSION: MTL-204/MTL-185 sandbox child lost the caller's plugins — /dev:fix session dev-fix-20261022-023522-f75563b5const serverCwd = join(tmp, "caller");+ mkdirSync(serverCwd);expect(resolveSetupFrom(undefined, undefined, serverCwd)).toBeUndefined();expect(resolveSetupFrom(serverCwd, undefined, serverCwd)).toBeUndefined();@@ -329,8 +334,81 @@ describe("resolveSetupFrom", () => {test("omits inheritance when the resolved setup directory equals work_dir", () => {// REGRESSION: MTL-204/MTL-185 sandbox child lost the caller's plugins — /dev:fix session dev-fix-20261022-023522-f75563b5const workDir = join(tmp, "caller");+ mkdirSync(workDir);+ const relativeWorkDir = relative(process.cwd(), workDir);expect(resolveSetupFrom(`${workDir}/nested/..`, workDir, join(tmp, "server"))).toBeUndefined();- expect(resolveSetupFrom(resolve("rel/x"), "rel/x", join(tmp, "server"))).toBeUndefined();+ expect(+ resolveSetupFrom(resolve(relativeWorkDir), relativeWorkDir, join(tmp, "server"))+ ).toBeUndefined();+ });++ test("rejects an explicit nonexistent setup directory with its resolved path", () => {+ // REGRESSION: a mistyped setup_from silently disabled inheritance — /dev:fix session dev-fix-20261022-023522-f75563b5+ const setupFrom = relative(process.cwd(), join(tmp, "missing"));+ const action = () => resolveSetupFrom(setupFrom, join(tmp, "work"), join(tmp, "server"));++ expectInvalidArgs(action, "setup_from");+ expect(action).toThrow(resolve(setupFrom));+ });++ test("rejects an explicit setup path that is a file", () => {+ // REGRESSION: a mistyped setup_from silently disabled inheritance — /dev:fix session dev-fix-20261022-023522-f75563b5+ const setupFrom = join(tmp, "setup.txt");+ writeFileSync(setupFrom, "settings");++ expectInvalidArgs(+ () => resolveSetupFrom(setupFrom, join(tmp, "work"), join(tmp, "server")),+ "setup_from"+ );+ });++ test("rejects false as setup_from", () => {+ // REGRESSION: a mistyped setup_from silently disabled inheritance — /dev:fix session dev-fix-20261022-023522-f75563b5+ expectInvalidArgs(+ () => resolveSetupFrom(false, join(tmp, "work"), join(tmp, "server")),+ "setup_from"+ );+ });++ test("rejects 0 as setup_from", () => {+ // REGRESSION: a mistyped setup_from silently disabled inheritance — /dev:fix session dev-fix-20261022-023522-f75563b5+ expectInvalidArgs(+ () => resolveSetupFrom(0, join(tmp, "work"), join(tmp, "server")),+ "setup_from"+ );+ });++ test("rejects an object as setup_from", () => {+ // REGRESSION: a mistyped setup_from silently disabled inheritance — /dev:fix session dev-fix-20261022-023522-f75563b5+ expectInvalidArgs(+ () => resolveSetupFrom({}, join(tmp, "work"), join(tmp, "server")),+ "setup_from"+ );+ });++ test("rejects an array as setup_from", () => {+ // REGRESSION: a mistyped setup_from silently disabled inheritance — /dev:fix session dev-fix-20261022-023522-f75563b5+ expectInvalidArgs(+ () => resolveSetupFrom([], join(tmp, "work"), join(tmp, "server")),+ "setup_from"+ );+ });++ test("returns the resolved path of an explicit existing setup directory", () => {+ // REGRESSION: a mistyped setup_from silently disabled inheritance — /dev:fix session dev-fix-20261022-023522-f75563b5+ const setupFrom = join(tmp, "setup");+ mkdirSync(setupFrom);++ expect(resolveSetupFrom(setupFrom, join(tmp, "work"), join(tmp, "server"))).toBe(+ resolve(setupFrom)+ );+ });++ test("returns a nonexistent serverCwd for the implicit default", () => {+ // REGRESSION: a mistyped setup_from silently disabled inheritance — /dev:fix session dev-fix-20261022-023522-f75563b5+ const serverCwd = join(tmp, "missing-server");++ expect(resolveSetupFrom(undefined, join(tmp, "work"), serverCwd)).toBe(serverCwd);});});diff --git a/packages/cli/src/mcp-session/inherited-settings.ts b/packages/cli/src/mcp-session/inherited-settings.tsindex 8f1c6848..5173d307 100644--- a/packages/cli/src/mcp-session/inherited-settings.ts+++ b/packages/cli/src/mcp-session/inherited-settings.ts@@ -11,7 +11,7 @@* settings layer.*/-import { existsSync, readFileSync } from "node:fs";+import { existsSync, readFileSync, statSync } from "node:fs";import { join, resolve } from "node:path";type SettingsLayer = Record<string, unknown>;@@ -129,6 +129,10 @@ export function buildInheritedSettingsLayer(* The setup directory a create_session call uses: `null` opts out, a string is* resolved, `undefined` is the server's cwd (the caller's project). Undefined when* that is the child's own directory, whose files Claude Code already reads natively.+ *+ * An explicit string must name an existing directory and any other non-null value is+ * refused, both as `invalid_args:`: a mistyped path would otherwise inherit nothing+ * and look like success. The implicit default is not checked.*/export function resolveSetupFrom(arg: unknown,@@ -136,6 +140,14 @@ export function resolveSetupFrom(serverCwd: string): string | undefined {if (arg === null) return undefined;- const setupFrom = typeof arg === "string" ? resolve(arg) : serverCwd;+ let setupFrom = serverCwd;+ if (typeof arg === "string") {+ setupFrom = resolve(arg);+ if (!statSync(setupFrom, { throwIfNoEntry: false })?.isDirectory()) {+ throw new Error(`invalid_args: setup_from ${setupFrom} is not an existing directory`);+ }+ } else if (arg !== undefined) {+ throw new Error("invalid_args: setup_from must be a directory path or null");+ }return setupFrom === resolve(workDir ?? serverCwd) ? undefined : setupFrom;}