Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 68 additions & 3 deletions apps/server/src/fork/claudeMcpProfile.adapter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -188,18 +188,20 @@ const resultFrame = (ordinal: number) =>
/** What the session's PreToolUse hooks answer for a call to this tool. */
const preToolUse = (
options: ClaudeAdapterV2.ClaudeAgentSdkQueryOptions | undefined,
tool: string,
tool: unknown,
/** Replaces the hook input, for a malformed one. */
rawInput?: object,
) =>
Effect.promise(async () => {
const input = {
const input = (rawInput ?? {
hook_event_name: "PreToolUse",
tool_name: tool,
tool_input: {},
tool_use_id: "toolu_profile",
session_id: NATIVE_THREAD,
transcript_path: "/tmp/transcript.jsonl",
cwd: "/workspace",
} as unknown as HookInput;
}) as unknown as HookInput;
const answers = [];
for (const matcher of options?.hooks?.PreToolUse ?? []) {
for (const hook of matcher.hooks) {
Expand Down Expand Up @@ -272,6 +274,69 @@ describe("ClaudeAdapterV2 MCP profile", () => {
}),
);

it.effect(
'builtinTools "none" opens Claude with no built-in tools and refuses any at the call',
() =>
Effect.gen(function* () {
const bare = { servers: "all", t3Tools: "all", builtinTools: "none" } as const;
// Narrowing a running thread to it opens a new process: the old one still has its tools.
const { opened, refusal } = yield* runTurns(
{},
[],
[{ mcpProfile: "bare", mcpProfiles: { bare } }],
);
assert.isUndefined(refusal);
assert.lengthOf(opened, 2);
assert.notDeepEqual(opened[0]?.tools, []);
assert.deepStrictEqual(opened[1]?.tools, []);
assert.includeMembers(opened[1]?.disallowedTools ?? [], [
"Bash",
"Read",
"Write",
"WebFetch",
]);
const [refused] = yield* preToolUse(opened[1], "Bash");
assert.deepNestedInclude(refused, {
"hookSpecificOutput.permissionDecision": "deny",
});
assert.include(
(refused as { hookSpecificOutput: { permissionDecisionReason: string } })
.hookSpecificOutput.permissionDecisionReason,
'MCP profile "bare" has builtinTools "none", so T3 refused the built-in tool "Bash"',
);
assert.deepStrictEqual(yield* preToolUse(opened[1], "mcp__t3-code__t3_thread_list"), [{}]);
}),
);

it.effect('the builtinTools "none" guard refuses a call it cannot read (fails closed)', () =>
Effect.gen(function* () {
const bare = { servers: "all", t3Tools: "all", builtinTools: "none" } as const;
const { opened } = yield* runTurns({ mcpProfile: "bare", mcpProfiles: { bare } }, []);
const decision = (answer: unknown) =>
(answer as { hookSpecificOutput?: { permissionDecision?: string } } | undefined)
?.hookSpecificOutput?.permissionDecision;
// A tool name that is not a string, or none at all.
for (const tool of [undefined, 42, null, { name: "mcp__x" }]) {
const [answer] = yield* preToolUse(opened[0], tool);
assert.strictEqual(decision(answer), "deny", `tool_name ${String(tool)}`);
}
// An input that throws while it is read: a hook that threw would let the call through.
const throwing = {
hook_event_name: "PreToolUse",
get tool_name(): string {
throw new Error("malformed hook input");
},
};
const [answer] = yield* preToolUse(opened[0], undefined, throwing);
assert.strictEqual(decision(answer), "deny");
assert.include(
(answer as { hookSpecificOutput: { permissionDecisionReason: string } }).hookSpecificOutput
.permissionDecisionReason,
"could not check this tool call, so it refused it",
);
}),
);

it.effect("refuses the turn, without opening Claude, when the profile is missing", () =>
Effect.gen(function* () {
const { opened, refusal } = yield* runTurns({ mcpProfile: "lean" }, []);
Expand Down
85 changes: 76 additions & 9 deletions apps/server/src/fork/claudeMcpProfile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,17 @@
* the groups fixed as each query opens. A profile that cannot be applied refuses the turn instead
* of starting it with every tool: the owner chose refusing over warning.
*/
import type {
HookCallback,
HookCallbackMatcher,
HookEvent,
McpServerStatus,
Settings as ClaudeSettings,
import {
BUILTIN_TOOL_NAMES,
type HookCallback,
type HookCallbackMatcher,
type HookEvent,
type McpServerStatus,
type Settings as ClaudeSettings,
} from "@anthropic-ai/claude-agent-sdk";
import {
DEFAULT_SERVER_SETTINGS,
MCP_BUILTIN_TOOLS_NONE,
MCP_CARRIED_PROFILES_REFUSE,
MCP_PROFILE_ALL,
type OrchestrationV2AppThread,
Expand Down Expand Up @@ -69,10 +71,14 @@ export interface ClaudeMcpProfilePlan {
readonly strictMcpConfig?: true;
readonly managedSettings?: ClaudeSettings;
readonly hooks?: Partial<Record<HookEvent, Array<HookCallbackMatcher>>>;
/** `builtinTools: "none"`: `[]`, after (so over) the adapter's own tool set. */
readonly tools?: Array<string>;
readonly disallowedTools?: Array<string>;
};
readonly servers: McpProfileSet["servers"];
/** T3's own tool groups; the T3 MCP server applies them (mcpT3ToolFilter.ts). */
readonly t3Tools: McpProfileSet["t3Tools"];
readonly builtinTools?: McpProfileSet["builtinTools"];
}

export type LineageThread = Pick<
Expand All @@ -91,11 +97,71 @@ const FIX = `pick "${MCP_PROFILE_ALL}" for this thread, or fix the profile in Se
const START_LEAKS =
"A managed Claude policy on this machine or --mcp-config in the Claude launch arguments can add servers T3's list does not drop";

/**
* `builtinTools: "none"`: Claude starts with no built-in tool (`tools: []`), every one the SDK
* names is also disallowed (so an `allowedTools` entry cannot bring one back), and a call to any
* tool but an MCP server's is refused as it is made (what a launch argument adds). With no
* `ToolSearch`, Claude lists MCP tools directly.
*/
export function claudeMcpProfilePlan(
name: string,
profile: McpProfileSet,
t3ServerNames: ReadonlyArray<string>,
): ClaudeMcpProfilePlan | ClaudeMcpProfileError {
const plan = claudeMcpServersPlan(name, profile, t3ServerNames);
if (isRefusal(plan) || profile.builtinTools !== MCP_BUILTIN_TOOLS_NONE) return plan;
const preToolUse = [
...(plan.options.hooks?.PreToolUse ?? []),
{ hooks: [claudeBuiltinToolsGuard(name)] },
];
return {
...plan,
builtinTools: MCP_BUILTIN_TOOLS_NONE,
options: {
...plan.options,
tools: [],
disallowedTools: [...BUILTIN_TOOL_NAMES],
hooks: { ...plan.options.hooks, PreToolUse: preToolUse },
},
};
}

/**
* Refuses a call to any tool but an MCP server's; hooks run in every mode and in sub-agents. Fails
* closed: a tool name that is not a string, or any error while checking, refuses the call (a hook
* that throws would count as a non-blocking error and let the call through).
*/
const claudeBuiltinToolsGuard =
(name: string): HookCallback =>
async (input) => {
try {
if (input.hook_event_name !== "PreToolUse") return {};
const toolName: unknown = input.tool_name;
if (typeof toolName === "string" && toolName.startsWith("mcp__")) return {};
const shown = typeof toolName === "string" ? `the built-in tool "${toolName}"` : "a tool";
return builtinToolDenial(
`MCP profile "${name}" has builtinTools "${MCP_BUILTIN_TOOLS_NONE}", so T3 refused ${shown}: this thread has only MCP tools. To use it, ${FIX}.`,
);
} catch {
return builtinToolDenial(
`MCP profile "${name}" has builtinTools "${MCP_BUILTIN_TOOLS_NONE}", and T3 could not check this tool call, so it refused it.`,
);
}
};

const builtinToolDenial = (reason: string) => ({
hookSpecificOutput: {
hookEventName: "PreToolUse" as const,
permissionDecision: "deny" as const,
permissionDecisionReason: reason,
},
});

/**
* `t3-only` and an empty list ignore every server Claude would load itself; a list keeps those named.
* `t3Tools` adds no option: T3's own server hides and refuses the tools it leaves out.
*/
export function claudeMcpProfilePlan(
function claudeMcpServersPlan(
name: string,
profile: McpProfileSet,
t3ServerNames: ReadonlyArray<string>,
Expand Down Expand Up @@ -335,15 +401,16 @@ export const planKeyFor = (mcpOverrides: object): string | undefined =>

/**
* The plan's key part, or `undefined` for `all`. NUL keeps the joined names apart. A change of T3
* tool groups changes it too, so the next turn's fresh process lists T3's tools again.
* tool groups or built-in tools changes it too, so the next turn's fresh process lists them again.
*/
const planKey = (plan: ClaudeMcpProfilePlan): string | undefined =>
plan.servers === "all" && plan.t3Tools === "all"
plan.servers === "all" && plan.t3Tools === "all" && plan.builtinTools === undefined
? undefined
: [
"mcp",
...(plan.servers === "all" ? ["*"] : plan.servers),
...(plan.t3Tools === "all" ? [] : ["\u0001t3", ...plan.t3Tools]),
...(plan.builtinTools === undefined ? [] : ["\u0002builtin", plan.builtinTools]),
].join("\u0000");

/** settings.json as this read found it. */
Expand Down
22 changes: 22 additions & 0 deletions apps/server/src/fork/codexMcpProfile.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -799,6 +799,28 @@ describe("withCodexMcpProfiles", () => {
}),
);

it.effect('builtinTools "none" refuses its turns, also one Codex starts itself for a goal', () =>
Effect.gen(function* () {
const fake = makeFakeCodex({ servers: ["alpha"] });
const { profiles, profileFor } = makeProfiles();
const client = yield* withCodexMcpProfiles(fake.client, profileFor);
const seen = yield* completions(client);
const id = (yield* start(client, "t")).thread.id;
profiles.set("t", {
...resolved("bare", "all"),
profile: { servers: "all", t3Tools: "all", builtinTools: "none" },
});
const message = yield* outcome(turn(client, id));
assert.include(message, 'MCP profile "bare" has builtinTools "none"');
assert.include(message, "codex cannot run a thread without its built-in tools");
assert.deepStrictEqual(fake.sent("turn/start"), []);
yield* fake.emit("turn/started", turnStarted(id, "goal-2"));
yield* fake.reached("turn/interrupt", 1);
yield* fake.emit("turn/completed", turnCompleted(id, "goal-2"));
assert.include((seen[0]!.error as { readonly message: string }).message, "builtinTools");
}),
);

it.effect("an unchanged profile: a turn Codex starts for the goal runs untouched", () =>
Effect.gen(function* () {
const fake = makeFakeCodex({ servers: ["alpha", "beta"] });
Expand Down
13 changes: 11 additions & 2 deletions apps/server/src/fork/codexMcpProfile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* checks the thread's live servers, refuses on a mismatch, and fixes its T3 tool groups (cut on
* T3's MCP server, mcpT3ToolFilter.ts), also on a turn Codex starts itself.
*/
import { MCP_PROFILE_ALL, ThreadId } from "@t3tools/contracts";
import { MCP_BUILTIN_TOOLS_NONE, MCP_PROFILE_ALL, ThreadId } from "@t3tools/contracts";
import type * as CodexClient from "effect-codex-app-server/client";
import * as CodexError from "effect-codex-app-server/errors";
import type * as CodexSchema from "effect-codex-app-server/schema";
Expand All @@ -14,7 +14,11 @@ import * as Schema from "effect/Schema";
import type { CodexAppServerClientFactoryShape } from "../orchestration-v2/Adapters/CodexAdapterV2.ts";
import { AGENT_THREAD_ENV } from "../preview/PortScanner.ts";
import { ClaudeMcpProfileError, makeThreadMcpProfilesById } from "./claudeMcpProfile.ts";
import type { McpProfileResolved, McpProfileSet } from "./mcpProfile.ts";
import {
type McpProfileResolved,
type McpProfileSet,
builtinToolsNoneRefusal,
} from "./mcpProfile.ts";
import { fixT3ToolSet, refusedT3ToolSet, t3ToolSetOf } from "./mcpT3ToolSets.ts";

type Client = CodexClient.CodexAppServerClient["Service"];
Expand Down Expand Up @@ -287,6 +291,11 @@ export const withCodexMcpProfiles = Effect.fn("CodexMcpProfile.wrap")(function*
Effect.mapError((cause) => refusal(method, cause)),
);
fixT3ToolSet(threadId, t3ToolSetOf(resolved));
// Codex cannot run a thread without its shell and file tools; also a turn it starts itself.
if (resolved.profile.builtinTools === MCP_BUILTIN_TOOLS_NONE) {
const reason = builtinToolsNoneRefusal(resolved.name, "codex");
return yield* refusal(method, new ClaudeMcpProfileError({ reason }));
}
// A thread loaded narrower than its profile is now stays narrower until Codex reloads it.
if (resolved.profile.servers === "all") return;
const statuses: Array<{ readonly name: string; readonly runtimeStatus?: string | null }> = [];
Expand Down
19 changes: 19 additions & 0 deletions apps/server/src/fork/launchMcpProfile.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,25 @@ it.effect("t3-only on the launcher refuses a launch into a project left on all (
}),
);

it.effect("a refusal names what the new thread keeps: only built-in tools here", () =>
Effect.gen(function* () {
// Every MCP server and T3 tool, but no built-in tool: a launch into a project on all is
// wider only in its built-in tools, and the refusal says so.
const bare = { servers: "all", t3Tools: "all", builtinTools: "none" };
const { launched, launch } = harness({
mcpProfiles: { bare },
...pickFor("bare"),
} as Settings);
const message = refusal(yield* launch({}));
expect(message).toContain('would get "all", which keeps built-in tools "bare" does not');
expect(message).not.toContain("MCP servers");
expect(launched).toHaveLength(0);
// A launcher on docs: the thread on all keeps more MCP servers.
const docs = harness({ ...DOCS_ONLY, ...pickFor("docs") } as Settings);
expect(refusal(yield* docs.launch({}))).toContain('which keeps MCP servers "docs" does not');
}),
);

it.effect("a launch the launcher's profile covers starts, also a narrower one", () =>
Effect.gen(function* () {
const settings = {
Expand Down
11 changes: 8 additions & 3 deletions apps/server/src/fork/launchMcpProfile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,12 @@ import {
makeThreadMcpProfiles,
} from "./claudeMcpProfile.ts";
import * as ModelPolicyCheck from "./modelPolicyCheck.ts";
import { isEveryMcpTool, isWithinMcpProfile, type McpProfileResolved } from "./mcpProfile.ts";
import {
isEveryMcpTool,
isWithinMcpProfile,
mcpProfileWiderParts,
type McpProfileResolved,
} from "./mcpProfile.ts";
import { boundToWiderRefusal } from "./scheduledTaskMcpProfile.ts";

type Refusal = { readonly reason: string };
Expand Down Expand Up @@ -91,7 +96,7 @@ export const makeLaunchMcpProfileCheck = Effect.gen(function* () {
: `Use delegate_task (its sub-agent keeps this thread's profile), start the thread in a project whose MCP profile is within ${named(own)}, or ask the owner to start it.`;
return yield* new OrchestratorMcpFailure({
code: "capability_denied",
message: `This thread's MCP profile is ${named(own)}, and a thread it starts in that project would get ${named(theirs)}, which keeps MCP servers or T3 tools ${named(own)} does not, so it was not started. ${instead}`,
message: `This thread's MCP profile is ${named(own)}, and a thread it starts in that project would get ${named(theirs)}, which keeps ${mcpProfileWiderParts(theirs.profile, own.profile)} ${named(own)} does not, so it was not started. ${instead}`,
});
});
});
Expand Down Expand Up @@ -128,7 +133,7 @@ const checkStarted = Effect.fn("LaunchMcpProfile.checkStarted")(function* (
if (isWithinMcpProfile(theirs.profile, own.profile)) return;
return yield* new OrchestratorMcpFailure({
code: "capability_denied",
message: `This thread's MCP profile is ${named(own)}, and ${what} would get ${named(theirs)}, which keeps MCP servers or T3 tools ${named(own)} does not, so it was not started. ${instead(named(own))}`,
message: `This thread's MCP profile is ${named(own)}, and ${what} would get ${named(theirs)}, which keeps ${mcpProfileWiderParts(theirs.profile, own.profile)} ${named(own)} does not, so it was not started. ${instead(named(own))}`,
});
});

Expand Down
Loading
Loading