diff --git a/apps/server/src/fork/claudeMcpProfile.adapter.test.ts b/apps/server/src/fork/claudeMcpProfile.adapter.test.ts index 4fe2bf668c7a1..23f7b63fa6efb 100644 --- a/apps/server/src/fork/claudeMcpProfile.adapter.test.ts +++ b/apps/server/src/fork/claudeMcpProfile.adapter.test.ts @@ -188,10 +188,12 @@ 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: {}, @@ -199,7 +201,7 @@ const preToolUse = ( 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) { @@ -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" }, []); diff --git a/apps/server/src/fork/claudeMcpProfile.ts b/apps/server/src/fork/claudeMcpProfile.ts index fa1e7fb1cdffc..757a5337e885b 100644 --- a/apps/server/src/fork/claudeMcpProfile.ts +++ b/apps/server/src/fork/claudeMcpProfile.ts @@ -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, @@ -69,10 +71,14 @@ export interface ClaudeMcpProfilePlan { readonly strictMcpConfig?: true; readonly managedSettings?: ClaudeSettings; readonly hooks?: Partial>>; + /** `builtinTools: "none"`: `[]`, after (so over) the adapter's own tool set. */ + readonly tools?: Array; + readonly disallowedTools?: Array; }; 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< @@ -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, +): 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, @@ -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. */ diff --git a/apps/server/src/fork/codexMcpProfile.test.ts b/apps/server/src/fork/codexMcpProfile.test.ts index ebc062f320b1d..43b022ce73357 100644 --- a/apps/server/src/fork/codexMcpProfile.test.ts +++ b/apps/server/src/fork/codexMcpProfile.test.ts @@ -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"] }); diff --git a/apps/server/src/fork/codexMcpProfile.ts b/apps/server/src/fork/codexMcpProfile.ts index 0bc7faa9df248..0cba7e1da5518 100644 --- a/apps/server/src/fork/codexMcpProfile.ts +++ b/apps/server/src/fork/codexMcpProfile.ts @@ -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"; @@ -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"]; @@ -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 }> = []; diff --git a/apps/server/src/fork/launchMcpProfile.test.ts b/apps/server/src/fork/launchMcpProfile.test.ts index 6e3a147543760..eba3e71c9f89f 100644 --- a/apps/server/src/fork/launchMcpProfile.test.ts +++ b/apps/server/src/fork/launchMcpProfile.test.ts @@ -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 = { diff --git a/apps/server/src/fork/launchMcpProfile.ts b/apps/server/src/fork/launchMcpProfile.ts index e3070c7d3d0a0..ce26521016546 100644 --- a/apps/server/src/fork/launchMcpProfile.ts +++ b/apps/server/src/fork/launchMcpProfile.ts @@ -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 }; @@ -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}`, }); }); }); @@ -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))}`, }); }); diff --git a/apps/server/src/fork/mcpProfile.test.ts b/apps/server/src/fork/mcpProfile.test.ts index 7901ce4567975..b6c41f96d0252 100644 --- a/apps/server/src/fork/mcpProfile.test.ts +++ b/apps/server/src/fork/mcpProfile.test.ts @@ -7,6 +7,7 @@ import { type McpProfileResolution, type McpProfileSettings, type McpProfileThread, + carryMcpProfile, isEveryMcpTool, resolveMcpProfile, isWithinMcpProfile, @@ -267,10 +268,7 @@ describe("MCP profile resolver: never falls back to all", () => { }); it.each([ - [ - "an unknown field (written by a newer build)", - { servers: [], t3Tools: [], builtinTools: "none" }, - ], + ["an unknown field (written by a newer build)", { servers: [], t3Tools: [], hosts: ["a"] }], ["a missing field", { servers: [] }], ["a wrong servers value", { servers: "docs", t3Tools: "all" }], ["a blank server name", { servers: [" "], t3Tools: "all" }], @@ -625,3 +623,54 @@ describe("isWithinMcpProfile (#681)", () => { expect(isWithinMcpProfile(set(["docs"], ["threads"]), set(["docs"], []))).toBe(false); }); }); + +describe('builtinTools "none" (assistant)', () => { + const settings = fromFile({ + mcpProfiles: { bare: { servers: "all", t3Tools: "all", builtinTools: "none" }, ...PROFILES }, + mcpThreadProfiles: { assistant: "bare", "lean-parent": "lean" }, + }); + + it("reads an old profile without the field as before: built-in tools kept", () => { + const result = resolved(resolveMcpProfile(settings, [top("lean-parent")])); + expect(result.profile).toEqual(PROFILES.lean); + expect(result.profile).not.toHaveProperty("builtinTools"); + expect(isEveryMcpTool(resolved(resolveMcpProfile(settings, [top("other")])).profile)).toBe( + true, + ); + }); + + it("is narrower than all, and a child keeps it: never wider than its parent", () => { + const own = resolved(resolveMcpProfile(settings, [top("assistant")])); + expect(own.profile).toEqual({ servers: "all", t3Tools: "all", builtinTools: "none" }); + expect(isEveryMcpTool(own.profile)).toBe(false); + expect(isWithinMcpProfile(own.profile, { servers: "all", t3Tools: "all" })).toBe(true); + expect(isWithinMcpProfile({ servers: "all", t3Tools: "all" }, own.profile)).toBe(false); + const sub = resolved(resolveMcpProfile(settings, [child("c1"), top("assistant")])); + expect(sub.profile.builtinTools).toBe("none"); + expect(sub.narrowedByParent).toBe(true); + }); + + it("travels with a delegated task and comes back the same on the other machine", () => { + const own = resolved(resolveMcpProfile(settings, [top("assistant")])); + const carried = carryMcpProfile(own); + expect(carried).toEqual({ name: "bare", servers: "all", t3Tools: "all", builtinTools: "none" }); + const there = resolved(resolveMcpProfile(fromFile({}), [child("task")], carried)); + expect(there.profile.builtinTools).toBe("none"); + // A task without it carries no field, so an older receiver reads it as before. + expect(carryMcpProfile(resolved(resolveMcpProfile(settings, [top("x")])))).not.toHaveProperty( + "builtinTools", + ); + }); + + it.each([["some"], [false], [["Bash"]]])("refuses, never widens, builtinTools %j", (value) => { + const broken = fromFile({ + mcpProfiles: { odd: { servers: "all", t3Tools: "all", builtinTools: value } }, + mcpProfile: "odd", + }); + expect(refused(resolveMcpProfile(broken, [top("t1")])).message).toContain("builtinTools"); + const carried = { name: "odd", servers: "all", t3Tools: "all", builtinTools: value }; + expect(refused(resolveMcpProfile(fromFile({}), [child("task")], carried)).message).toContain( + "builtinTools", + ); + }); +}); diff --git a/apps/server/src/fork/mcpProfile.ts b/apps/server/src/fork/mcpProfile.ts index c140cc066854f..8f2e2a993f83c 100644 --- a/apps/server/src/fork/mcpProfile.ts +++ b/apps/server/src/fork/mcpProfile.ts @@ -5,6 +5,8 @@ * The adapters, the turn check and the T3 tool filter (mcpT3ToolFilter.ts) use it. */ import { + MCP_BUILTIN_TOOLS_NONE, + MCP_PROFILE_ALL, MCP_T3_UNGROUPED_TOOLS, type McpT3ToolGroup, ProviderDriverKind, @@ -15,6 +17,7 @@ export { carryMcpProfile, isEveryMcpTool, isWithinMcpProfile, + mcpProfileWiderParts, readCarried, resolveMcpProfile, type CarriedLookup, @@ -117,6 +120,14 @@ export const MCP_PROFILE_DRIVERS: ReadonlySet = new Set([ ProviderDriverKind.make("codex"), ]); +/** + * The refusal of a turn whose profile has `builtinTools: "none"` on a driver that cannot drop its + * built-in tools: only Claude does (claudeMcpProfile.ts), so every other driver refuses, never + * runs the thread with its shell, file and web tools. + */ +export const builtinToolsNoneRefusal = (profileName: string, driver: string): string => + `This thread's MCP profile "${profileName}" has builtinTools "${MCP_BUILTIN_TOOLS_NONE}" (no shell, file or web tools), and ${driver} cannot run a thread without its built-in tools (only Claude can), so this turn was not started. Switch the thread to a Claude instance, or pick a profile without builtinTools "${MCP_BUILTIN_TOOLS_NONE}" (such as "${MCP_PROFILE_ALL}") for this thread in Settings.`; + /** * The drivers a delegation sends a narrower profile to on another machine * (fork/remoteMcpProfile.ts): Claude only, since a machine that carries profiles may run a T3 diff --git a/apps/server/src/fork/mcpProfileTurnCheck.test.ts b/apps/server/src/fork/mcpProfileTurnCheck.test.ts index 319dc6f518290..deda7cd259c9f 100644 --- a/apps/server/src/fork/mcpProfileTurnCheck.test.ts +++ b/apps/server/src/fork/mcpProfileTurnCheck.test.ts @@ -391,6 +391,28 @@ describe("mcpProfileTurnCheck", () => { ), ); + it.effect('builtinTools "none" runs only on Claude; Codex and OpenCode refuse the turn', () => + withCheck( + { + mcpProfiles: { bare: { servers: "all", t3Tools: "all", builtinTools: "none" } }, + mcpProfile: "bare", + } as Mcp, + [], + ({ calls, start }) => + Effect.gen(function* () { + const own = appThread("own"); + assert.strictEqual(yield* start(own, CLAUDE), "ok"); + for (const id of [CODEX, OPENCODE]) { + const message = yield* start(own, id); + assert.include(message, 'MCP profile "bare" has builtinTools "none"'); + assert.include(message, "Switch the thread to a Claude instance"); + assert.include(yield* start(own, id, "resume"), "builtinTools"); + } + assert.deepStrictEqual(calls, ["start:claudeAgent:own"]); + }), + ), + ); + it.effect("a profile change applies from the next turn: the running one keeps its set", () => withCheck( { diff --git a/apps/server/src/fork/mcpProfileTurnCheck.ts b/apps/server/src/fork/mcpProfileTurnCheck.ts index 4985e063498d9..850c133c3b71c 100644 --- a/apps/server/src/fork/mcpProfileTurnCheck.ts +++ b/apps/server/src/fork/mcpProfileTurnCheck.ts @@ -1,10 +1,9 @@ /** - * Fork: every turn start (also resume, compaction) fixes its thread's T3 tool groups for - * mcpT3ToolFilter.ts, on every driver but Claude (claudeMcpProfile.ts fixes its own). Outside - * `MCP_PROFILE_DRIVERS` the turn also refuses when its profile leaves out an outside server. - * A profile that cannot be worked out refuses on every wrapped driver. + * Fork: each turn start fixes the thread's T3 tool groups (mcpT3ToolFilter.ts) on every driver but + * Claude. It refuses outside `MCP_PROFILE_DRIVERS` when the profile leaves out an outside server, and + * on every wrapped driver for `builtinTools: "none"` or a profile that cannot be worked out. */ -import { MCP_PROFILE_ALL, ProviderDriverKind } from "@t3tools/contracts"; +import { MCP_BUILTIN_TOOLS_NONE, MCP_PROFILE_ALL, ProviderDriverKind } from "@t3tools/contracts"; import * as ProviderAdapter from "@t3tools/provider-core/server/ProviderAdapter"; import type { ProviderInstance } from "@t3tools/provider-core/server/driver"; import * as Effect from "effect/Effect"; @@ -18,7 +17,11 @@ import { type LineageThread, makeThreadMcpProfiles, } from "./claudeMcpProfile.ts"; -import { MCP_PROFILE_DRIVERS, type McpProfileResolved } from "./mcpProfile.ts"; +import { + MCP_PROFILE_DRIVERS, + type McpProfileResolved, + builtinToolsNoneRefusal, +} from "./mcpProfile.ts"; import { fixT3ToolSet, forgetT3ToolSet, refusedT3ToolSet, t3ToolSetOf } from "./mcpT3ToolSets.ts"; type ProfileFor = Effect.Success; @@ -91,6 +94,11 @@ const checkTurnProfile = Effect.fn("McpProfileTurnCheck.check")(function* ( Effect.onInterrupt(() => Effect.sync(() => forgetT3ToolSet(thread.id))), ); fixT3ToolSet(thread.id, t3ToolSetOf(resolved)); + if (resolved.profile.builtinTools === MCP_BUILTIN_TOOLS_NONE) { + return yield* new ClaudeMcpProfileError({ + reason: builtinToolsNoneRefusal(resolved.name, instance.driverKind), + }); + } if (appliesMcpProfiles(instance.driverKind) || resolved.profile.servers === "all") return; return yield* new ClaudeMcpProfileError({ reason: refusalReason(resolved, instance) }); }); diff --git a/apps/server/src/fork/scheduleForkMcpProfile.test.ts b/apps/server/src/fork/scheduleForkMcpProfile.test.ts index ea38e99f8b4eb..131099fd8b33b 100644 --- a/apps/server/src/fork/scheduleForkMcpProfile.test.ts +++ b/apps/server/src/fork/scheduleForkMcpProfile.test.ts @@ -414,7 +414,7 @@ it.effect("a project with no profile of its own gives the server's to the task's yield* withToolkits({ ...server, ...pickFor("t3-only") }, ({ recorded, schedule }) => Effect.gen(function* () { expect(yield* schedule({ projectId: BARE_PROJECT })).toContain( - 'would get "docs", which keeps MCP servers or T3 tools "t3-only" does not', + 'would get "docs", which keeps MCP servers "t3-only" does not', ); expect(recorded.scheduled).toHaveLength(0); }), @@ -422,6 +422,26 @@ it.effect("a project with no profile of its own gives the server's to the task's }), ); +it.effect("a schedule or fork refusal names built-in tools when only they are wider", () => + // The caller keeps every MCP server and T3 tool but no built-in tool; PROJECT is on all. + withToolkits( + { + mcpProfiles: { bare: { servers: "all", t3Tools: "all", builtinTools: "none" } }, + ...pickFor("bare"), + }, + ({ recorded, schedule, fork }) => + Effect.gen(function* () { + const scheduled = yield* schedule({ projectId: PROJECT, bindToCurrentThread: false }); + expect(scheduled).toContain('would get "all", which keeps built-in tools "bare" does not'); + expect(scheduled).not.toContain("MCP servers"); + const forked = yield* fork(WIDE); + expect(forked).toContain('would get "all", which keeps built-in tools "bare" does not'); + expect(recorded.scheduled).toHaveLength(0); + expect(recorded.dispatched).toHaveLength(0); + }), + ), +); + it.effect("a caller whose profile cannot be worked out is refused, never taken as all", () => // A pick of a profile no setting defines (an old or empty state): it cannot apply. withToolkits(pickFor("deleted-profile"), ({ recorded, schedule, fork }) => diff --git a/apps/server/src/fork/scheduledTaskMcpProfile.test.ts b/apps/server/src/fork/scheduledTaskMcpProfile.test.ts index a528c49538f10..2c620bc096a19 100644 --- a/apps/server/src/fork/scheduledTaskMcpProfile.test.ts +++ b/apps/server/src/fork/scheduledTaskMcpProfile.test.ts @@ -461,7 +461,7 @@ it.effect("a narrowed thread cannot change what a task bound to a wider thread r Effect.flip, ); expect(messageOf(refused)).toContain( - `this task runs in thread ${WIDE}, whose MCP profile "all" keeps MCP servers or T3 tools "docs" does not`, + `this task runs in thread ${WIDE}, whose MCP profile "all" keeps MCP servers "docs" does not`, ); } // Reading it, renaming it, pausing it or changing its schedule stays allowed. @@ -1828,7 +1828,7 @@ it.effect( const last = yield* scheduleTask({ scheduledTaskId: "task:bound-wide", ...change }); expect(last.isFailure).toBe(true); expect(last.result.message).toContain( - `this task runs in thread ${WIDE}, whose MCP profile "all" keeps MCP servers or T3 tools "docs" does not`, + `this task runs in thread ${WIDE}, whose MCP profile "all" keeps MCP servers "docs" does not`, ); } expect(reached).toHaveLength(0); diff --git a/apps/server/src/fork/scheduledTaskMcpProfile.ts b/apps/server/src/fork/scheduledTaskMcpProfile.ts index 4481829d8b4ea..3e9dcf6975d73 100644 --- a/apps/server/src/fork/scheduledTaskMcpProfile.ts +++ b/apps/server/src/fork/scheduledTaskMcpProfile.ts @@ -31,6 +31,7 @@ import { carryMcpProfile, isEveryMcpTool, isWithinMcpProfile, + mcpProfileWiderParts, type McpProfileCarried, type McpProfileResolved, readCarried, @@ -188,7 +189,7 @@ export const boundToWiderRefusal = Effect.fnUntraced(function* (input: { const what = theirs === null ? `thread ${boundTo}, whose MCP profile cannot be worked out` - : `thread ${boundTo}, whose MCP profile ${named(theirs.name)} keeps MCP servers or T3 tools ${named(own.name)} does not`; + : `thread ${boundTo}, whose MCP profile ${named(theirs.name)} keeps ${mcpProfileWiderParts(theirs.profile, own.profile)} ${named(own.name)} does not`; return `This thread's MCP profile is ${named(own.name)}, and this task runs in ${what}, so its prompt and where it runs cannot be changed from here (reading it, pausing it or changing its schedule can). Ask the owner to change it, or delete_scheduled_task removes it.`; }); @@ -405,7 +406,7 @@ export const makeScheduledTaskMcpProfiles = Effect.fnUntraced(function* ( if (!isWithinMcpProfile(theirs.profile, own.profile)) { return yield* refuse( task, - `This thread's MCP profile is ${named(own.name)}, and a thread this task's runs start in its project would get ${named(theirs.name)}, which keeps MCP servers or T3 tools ${named(own.name)} does not, so the task was not saved. Bind the task to this thread (bindToCurrentThread: true), schedule it in a project whose MCP profile is within ${named(own.name)}, use delegate_task, or ask the owner to schedule it.`, + `This thread's MCP profile is ${named(own.name)}, and a thread this task's runs start in its project would get ${named(theirs.name)}, which keeps ${mcpProfileWiderParts(theirs.profile, own.profile)} ${named(own.name)} does not, so the task was not saved. Bind the task to this thread (bindToCurrentThread: true), schedule it in a project whose MCP profile is within ${named(own.name)}, use delegate_task, or ask the owner to schedule it.`, ); } } @@ -465,7 +466,7 @@ export const makeScheduledTaskMcpProfiles = Effect.fnUntraced(function* ( return `Not run: the MCP profile of the thread this run would start cannot be worked out. ${theirs.failure.reason}${afterSave(task, "once that is fixed")}`; } if (isWithinMcpProfile(theirs.success.profile, recorded)) return null; - return `Not run: this task was saved by a thread whose MCP profile is ${named(recorded.name)}, and the thread this run would start in its project gets ${named(theirs.success.name)}, which keeps MCP servers or T3 tools ${named(recorded.name)} does not. Bind the task to a thread, move it to a project whose MCP profile is within ${named(recorded.name)}, or save it in the T3 app to run it as the owner's.${afterSave(task)}`; + return `Not run: this task was saved by a thread whose MCP profile is ${named(recorded.name)}, and the thread this run would start in its project gets ${named(theirs.success.name)}, which keeps ${mcpProfileWiderParts(theirs.success.profile, recorded)} ${named(recorded.name)} does not. Bind the task to a thread, move it to a project whose MCP profile is within ${named(recorded.name)}, or save it in the T3 app to run it as the owner's.${afterSave(task)}`; }); return { saving, runRefusal }; diff --git a/apps/web/src/components/settings/fork/McpProfileSettings.tsx b/apps/web/src/components/settings/fork/McpProfileSettings.tsx index 556f721dd3319..0c1cc97512786 100644 --- a/apps/web/src/components/settings/fork/McpProfileSettings.tsx +++ b/apps/web/src/components/settings/fork/McpProfileSettings.tsx @@ -228,6 +228,15 @@ function ProfileEditor({ onChange={(next) => setDraft({ ...draft, t3Tools: next })} />

{MCP_ALWAYS_KEPT_NOTE}

+ {notes.map((note) => (

{note} diff --git a/apps/web/src/components/settings/fork/mcpProfileSettings.logic.test.ts b/apps/web/src/components/settings/fork/mcpProfileSettings.logic.test.ts index 4eeb717c9f9be..d070433cc7fdb 100644 --- a/apps/web/src/components/settings/fork/mcpProfileSettings.logic.test.ts +++ b/apps/web/src/components/settings/fork/mcpProfileSettings.logic.test.ts @@ -7,6 +7,8 @@ import { describe, expect, it } from "vite-plus/test"; import { clearThreadPicksPatch, deleteMcpProfilePatch, + describeMcpProfileSet, + draftOf, mcpProfileChoices, mcpProfileRows, mcpProfileUsers, @@ -122,6 +124,23 @@ describe("saving and deleting a profile", () => { expect(Object.keys(settings.mcpProfiles)).toEqual(["other"]); }); + it('keeps builtinTools "none" through an edit and shows it; others save without it', () => { + const bare = { servers: "all", t3Tools: "all", builtinTools: "none" } as const; + const settings = fromFile({ mcpProfiles: { bare, lean: LEAN } }); + const row = mcpProfileRows(settings, null).find((entry) => entry.name === "bare")!; + expect(row.state).toBe("ok"); + expect(describeMcpProfileSet(bare)).toContain("Built-in tools: none"); + const edit = saveMcpProfilePatch(settings, "bare", draftOf("bare", bare)); + expect(edit).toEqual({ patch: { mcpProfiles: { bare } } }); + serverSave(settings, "patch" in edit ? edit.patch : null); + const lean = saveMcpProfilePatch( + settings, + "lean", + draftOf("lean", settings.mcpProfiles.lean as never), + ); + expect(JSON.stringify(lean)).not.toContain("builtinTools"); + }); + it("refuses built-in names, inherit, bad names and a name taken", () => { const settings = fromFile({ mcpProfiles: { lean: LEAN } }); const draft = { servers: [] as string[], t3Tools: "all" as const }; diff --git a/apps/web/src/components/settings/fork/mcpProfileSettings.logic.ts b/apps/web/src/components/settings/fork/mcpProfileSettings.logic.ts index 724e06482679d..8926de4803abe 100644 --- a/apps/web/src/components/settings/fork/mcpProfileSettings.logic.ts +++ b/apps/web/src/components/settings/fork/mcpProfileSettings.logic.ts @@ -5,6 +5,7 @@ */ /* oxlint-disable t3code/no-untranslated-copy -- Fork: the fork's own settings are English only; upstream's translation catalogs hold no fork keys. */ import { + MCP_BUILTIN_TOOLS_NONE, MCP_PROFILE_ALL, MCP_PROFILE_T3_ONLY, MCP_SUBAGENT_INHERIT, @@ -177,27 +178,33 @@ export function mcpServerSuggestions( const describeList = (list: "all" | ReadonlyArray, all: string) => list === "all" ? all : list.length === 0 ? "none" : list.join(", "); -/** "Servers: all · T3 tools: threads, preview". */ +/** "Servers: all · T3 tools: threads, preview", and " · Built-in tools: none" when it drops them. */ export const describeMcpProfileSet = (profile: McpProfileSet): string => - `Servers: ${describeList(profile.servers, "all")} · T3 tools: ${describeList(profile.t3Tools, "all")}`; + `Servers: ${describeList(profile.servers, "all")} · T3 tools: ${describeList(profile.t3Tools, "all")}${ + profile.builtinTools === MCP_BUILTIN_TOOLS_NONE ? " · Built-in tools: none" : "" + }`; /** What the profile editor holds. */ export interface McpProfileDraft { readonly name: string; readonly servers: "all" | ReadonlyArray; readonly t3Tools: "all" | ReadonlyArray; + /** True for `builtinTools: "none"`; an edit keeps it unless the person clears it. */ + readonly noBuiltinTools?: boolean; } export const emptyMcpProfileDraft = (): McpProfileDraft => ({ name: "", servers: [], t3Tools: "all", + noBuiltinTools: false, }); export const draftOf = (name: string, profile: McpProfileSet): McpProfileDraft => ({ name, servers: profile.servers, t3Tools: profile.t3Tools, + noBuiltinTools: profile.builtinTools === MCP_BUILTIN_TOOLS_NONE, }); /** @@ -241,7 +248,9 @@ export function saveMcpProfilePatch( : MCP_T3_TOOL_GROUPS.filter((group) => (draft.t3Tools as ReadonlyArray).includes(group), ); - return { patch: { mcpProfiles: { [name]: { servers, t3Tools } } } }; + const builtinTools = + draft.noBuiltinTools === true ? { builtinTools: MCP_BUILTIN_TOOLS_NONE } : {}; + return { patch: { mcpProfiles: { [name]: { servers, t3Tools, ...builtinTools } } } }; } /** Labels for the panel's messages: project and thread titles this client knows. */ diff --git a/docs/fork/README.md b/docs/fork/README.md index 0120d9e6c22da..3b100bc4b3ca9 100644 --- a/docs/fork/README.md +++ b/docs/fork/README.md @@ -999,6 +999,32 @@ A thread with `all` delegates to any machine and any instance there, without ask arrives from a machine on an older T3 carries nothing, so it gets this machine's profile for its role as before, and the server log says so. +A profile may also say `"builtinTools": "none"` (optional; absent, or `"all"`, keeps the provider's +built-in tools as before). Such a thread gets no shell, file, web or other built-in tool, only MCP +tools (T3's own, and the outside servers its `servers` keeps): a base for an assistant that reads +the owner's data without changing files. Only Claude applies it: its query opens with no built-in +tool (`tools: []`, and every built-in Claude names in `disallowedTools`), and a call to any tool +that is not an MCP tool is refused at the call, with the reason. A thread whose profile changes to +it starts a fresh Claude process for its next turn, so a process that still has its tools is not +reused. Every other provider (Codex too) cannot run without its built-in tools, so a turn there is +not started and says why (switch the thread to a Claude instance, or pick another profile); Codex +also stops a turn it starts on its own for a goal, but only soon after that turn starts, so its first +tool calls may already run. A child keeps `none` from its parent (never more +than its parent), a delegated task carries it to the other machine, and a delegation refuses, before +sending, a non-Claude instance there, as for any profile narrower than `all`. A T3 from before this +field reads a profile or carried task with it as one it cannot read and refuses it, never giving it +the tools; a machine on such a T3 that takes delegated tasks accepts the task, then refuses its +turn, so a thread that does nothing is left there (never one with more tools). Any other value makes +the profile broken. + +`builtinTools` removes only the provider's own tools. For a really tool-less (read-only) thread, +also give `t3Tools` a list of only the groups it needs (leave out `orchestration`, `playtest`, +`worktree`, `preview` and other groups that act: they can ask another thread to act, run build +steps, change worktrees or run page script), and `servers` a +list of read-only servers: an outside MCP server may wrap a shell or write files. Not a sandbox: a +Claude setting that turns hooks off, or launch arguments that add tools, can weaken it on a machine +the owner set up so. + ```json { "mcpProfiles": { "lean": { "servers": [], "t3Tools": ["orchestration", "threads"] } } } ``` @@ -1006,7 +1032,7 @@ role as before, and the server log says so. In the app: **Settings → Fork**, section MCP profiles. It lists `all` and `t3-only` (read-only) and each named profile, and adds, edits and deletes named ones: `servers` is all or a list of names (the editor offers the names other profiles use and takes any other; no list of the -machine's servers reaches the app yet), `t3Tools` all or a list of groups. `request_secret`, +machine's servers reaches the app yet), `t3Tools` all or a list of groups, and a "No built-in tools" box sets `builtinTools: "none"`. `request_secret`, `send_to_user` and `tool_search` always stay. A built-in name cannot be taken, and a profile that the default, the sub-agent rule, a project or a thread pick names cannot be deleted: the panel says which, and offers to clear the thread picks that name it; the server refuses such a delete diff --git a/packages/contracts/src/mcpProfile.test.ts b/packages/contracts/src/mcpProfile.test.ts index 188d68faad746..5f588f36e78b5 100644 --- a/packages/contracts/src/mcpProfile.test.ts +++ b/packages/contracts/src/mcpProfile.test.ts @@ -2,6 +2,7 @@ import * as Schema from "effect/Schema"; import { describe, expect, it } from "vite-plus/test"; import { ProjectId } from "./baseSchemas.ts"; +import { mcpProfileWiderParts } from "./mcpProfileResolve.ts"; import { ProjectSettingsOverrides, ServerSettings, ServerSettingsPatch } from "./settings.ts"; const decodeFile = Schema.decodeUnknownSync(ServerSettings); @@ -29,7 +30,7 @@ describe("MCP profile settings file", () => { it("loads a file whose profile this build cannot read, and keeps it on save", () => { // Written by a newer build: an unknown field, an unknown group, a missing field. const profiles = { - future: { servers: [], t3Tools: ["mail"], builtinTools: "none" }, + future: { servers: [], t3Tools: ["mail"], hosts: ["a"] }, half: { servers: ["docs"] }, odd: "not a profile", }; @@ -121,6 +122,25 @@ describe("MCP profile settings patch", () => { } }); + it('takes builtinTools "none" or "all"; a profile without it is as before', () => { + const patch = decodePatch({ + mcpProfiles: { + bare: { servers: "all", t3Tools: "all", builtinTools: "none" }, + full: { servers: "all", t3Tools: "all", builtinTools: "all" }, + }, + }); + expect(patch.mcpProfiles?.bare).toEqual({ + servers: "all", + t3Tools: "all", + builtinTools: "none", + }); + expect(patch.mcpProfiles?.full).toEqual({ + servers: "all", + t3Tools: "all", + builtinTools: "all", + }); + }); + it("takes null under a built-in name, so a hand-edited entry with that name can be removed", () => { expect(decodePatch({ mcpProfiles: { all: null, "t3-only": null } }).mcpProfiles).toEqual({ all: null, @@ -135,6 +155,10 @@ describe("MCP profile settings patch", () => { ["the name `inherit`", { mcpProfiles: { inherit: { servers: [], t3Tools: [] } } }], ["a bad name", { mcpProfiles: { "Bad Name": { servers: [], t3Tools: [] } } }], ["an unknown field", { mcpProfiles: { lean: { servers: [], t3Tools: [], extra: 1 } } }], + [ + "a bad builtinTools", + { mcpProfiles: { lean: { servers: [], t3Tools: [], builtinTools: "x" } } }, + ], ["a missing field", { mcpProfiles: { lean: { servers: [] } } }], ["an unknown group", { mcpProfiles: { lean: { servers: [], t3Tools: ["mail"] } } }], ["a blank server", { mcpProfiles: { lean: { servers: [" "], t3Tools: [] } } }], @@ -187,3 +211,19 @@ describe("mcpCarriedProfiles setting (#640)", () => { expect(() => decodePatch({ mcpCarriedProfiles: "ignore" })).toThrow(); }); }); + +describe("mcpProfileWiderParts", () => { + const all = { servers: "all", t3Tools: "all" } as const; + const bare = { ...all, builtinTools: "none" } as const; + it("names only what the wider profile really keeps, for a refusal", () => { + expect(mcpProfileWiderParts(all, all)).toBe(""); + expect(mcpProfileWiderParts(bare, all)).toBe(""); + expect(mcpProfileWiderParts(all, bare)).toBe("built-in tools"); + expect(mcpProfileWiderParts(all, { servers: ["docs"], t3Tools: "all" })).toBe("MCP servers"); + expect(mcpProfileWiderParts(all, { servers: "all", t3Tools: ["threads"] })).toBe("T3 tools"); + expect(mcpProfileWiderParts(all, { servers: [], t3Tools: [] })).toBe("MCP servers or T3 tools"); + expect(mcpProfileWiderParts(all, { servers: [], t3Tools: [], builtinTools: "none" })).toBe( + "MCP servers, T3 tools or built-in tools", + ); + }); +}); diff --git a/packages/contracts/src/mcpProfile.ts b/packages/contracts/src/mcpProfile.ts index b584ff72515c4..9d2eef040ca5a 100644 --- a/packages/contracts/src/mcpProfile.ts +++ b/packages/contracts/src/mcpProfile.ts @@ -15,6 +15,13 @@ export const MCP_PROFILE_ALL = "all"; export const MCP_PROFILE_T3_ONLY = "t3-only"; /** The two profiles every build knows; `mcpProfiles` cannot redefine them. */ const MCP_BUILT_IN_PROFILE_NAMES: ReadonlyArray = [MCP_PROFILE_ALL, MCP_PROFILE_T3_ONLY]; +/** + * A profile's `builtinTools`: `none` starts the thread with no built-in tools of the provider (no + * shell, files or web; only MCP tools). Only Claude applies it; a turn on any other driver is + * refused. Absent or `all` is today's behaviour. + */ +export const MCP_BUILTIN_TOOLS_NONE = "none"; +const MCP_BUILTIN_TOOLS_VALUES: ReadonlyArray = ["all", MCP_BUILTIN_TOOLS_NONE]; /** `mcpSubagentProfile` value: a delegated child follows `mcpProfile` like any other thread. */ export const MCP_SUBAGENT_INHERIT = "inherit"; @@ -97,20 +104,27 @@ export const McpProfilesFromSettingsFile = Schema.Unknown.pipe( ); const MCP_PROFILE_FIELDS: ReadonlyArray = ["servers", "t3Tools"]; +const MCP_PROFILE_OPTIONAL_FIELDS: ReadonlyArray = ["builtinTools"]; const KNOWN_GROUPS: ReadonlyArray = MCP_T3_TOOL_GROUPS; const isAllOrListOf = (value: unknown, isEntry: (entry: unknown) => boolean): boolean => value === "all" || (Array.isArray(value) && value.every(isEntry)); /** - * A profile a save may store: exactly `servers` (non-blank names) and `t3Tools` (known groups), each - * `"all"` or a keep-list; a keep-list leaves out anything not named. An unknown field is refused, not - * dropped, so a save never stores a profile cut down from what the person wrote. + * A profile a save may store: `servers` (non-blank names) and `t3Tools` (known groups), each + * `"all"` or a keep-list; a keep-list leaves out anything not named; and optionally `builtinTools` + * (`"all"` or `"none"`). An unknown field is refused, not dropped, so a save never stores a profile + * cut down from what the person wrote. */ const isSavableMcpProfile = (entry: unknown): boolean => isRecord(entry) && - Object.keys(entry).length === MCP_PROFILE_FIELDS.length && - Object.keys(entry).every((field) => MCP_PROFILE_FIELDS.includes(field)) && + MCP_PROFILE_FIELDS.every((field) => Object.hasOwn(entry, field)) && + Object.keys(entry).every( + (field) => MCP_PROFILE_FIELDS.includes(field) || MCP_PROFILE_OPTIONAL_FIELDS.includes(field), + ) && + (!Object.hasOwn(entry, "builtinTools") || + (typeof entry.builtinTools === "string" && + MCP_BUILTIN_TOOLS_VALUES.includes(entry.builtinTools))) && isAllOrListOf(entry.servers, (name) => typeof name === "string" && name.trim() !== "") && isAllOrListOf( entry.t3Tools, @@ -141,7 +155,7 @@ export const McpProfilesPatch = Schema.Record(Schema.String, Schema.Unknown) (profiles: { readonly [name: string]: unknown }) => Object.values(profiles).every((entry) => entry === null || isSavableMcpProfile(entry)), { - expected: `profiles with only ${MCP_PROFILE_FIELDS.join(" and ")}, each "all" or a list (servers: non-blank names; t3Tools: ${MCP_T3_TOOL_GROUPS.join(", ")}), or null to remove one`, + expected: `profiles with only ${MCP_PROFILE_FIELDS.join(" and ")}, each "all" or a list (servers: non-blank names; t3Tools: ${MCP_T3_TOOL_GROUPS.join(", ")}), and optionally builtinTools ("all" or "none"), or null to remove one`, }, ), ); diff --git a/packages/contracts/src/mcpProfileResolve.ts b/packages/contracts/src/mcpProfileResolve.ts index a2233567cf53b..17f6336416a00 100644 --- a/packages/contracts/src/mcpProfileResolve.ts +++ b/packages/contracts/src/mcpProfileResolve.ts @@ -5,6 +5,7 @@ * thread's result in its menu and reads profile entries by the same rules in Settings. */ import { + MCP_BUILTIN_TOOLS_NONE, MCP_CARRIED_PROFILES_REFUSE, MCP_PROFILE_ALL, MCP_PROFILE_T3_ONLY, @@ -37,10 +38,14 @@ export interface McpProfileThread { readonly delegated: boolean; } -/** A profile this build can apply: `"all"` or a keep-list, for servers and for T3 tool groups. */ +/** + * A profile this build can apply: `"all"` or a keep-list, for servers and for T3 tool groups; and + * `builtinTools: "none"` for a thread with no built-in provider tools (absent means all of them). + */ export interface McpProfileSet { readonly servers: "all" | ReadonlyArray; readonly t3Tools: "all" | ReadonlyArray; + readonly builtinTools?: typeof MCP_BUILTIN_TOOLS_NONE; } /** Where a thread's profile came from. */ @@ -109,12 +114,12 @@ const BUILT_IN_PROFILES: Readonly> = { }; const KNOWN_GROUPS: ReadonlySet = new Set(MCP_T3_TOOL_GROUPS); -const PROFILE_FIELDS: ReadonlySet = new Set(["servers", "t3Tools"]); +const PROFILE_FIELDS: ReadonlySet = new Set(["servers", "t3Tools", "builtinTools"]); const CARRIED_FIELDS: ReadonlySet = new Set(["name", ...PROFILE_FIELDS]); -/** True when the profile is today's behaviour: every outside server and every T3 tool. */ +/** True when the profile is today's behaviour: every outside server, T3 tool and built-in tool. */ export const isEveryMcpTool = (profile: McpProfileSet): boolean => - profile.servers === "all" && profile.t3Tools === "all"; + profile.servers === "all" && profile.t3Tools === "all" && profile.builtinTools === undefined; const isRecord = (value: unknown): value is Readonly> => typeof value === "object" && value !== null && !Array.isArray(value); @@ -153,6 +158,22 @@ function parseT3Tools( return { ok: true, value: { groups: unique(groups), unknown: unique(unknown) } }; } +/** + * `builtinTools`: absent or `"all"` keeps them; `"none"` drops them. Any other value (a typo, or a + * newer build's) refuses the profile, so it never reads as more than was meant. + */ +function parseBuiltinTools(value: unknown): Parsed { + if (value === undefined || value === "all") return { ok: true, value: undefined }; + if (value === MCP_BUILTIN_TOOLS_NONE) return { ok: true, value }; + return { ok: false, issue: '`builtinTools` must be "all" or "none"' }; +} + +/** `servers` and `t3Tools` with `builtinTools` added only when it drops them. */ +const withBuiltinTools = ( + set: Omit, + builtinTools: McpProfileSet["builtinTools"], +): McpProfileSet => (builtinTools === undefined ? set : { ...set, builtinTools }); + type Lookup = | { readonly ok: true; readonly profile: McpProfileSet; readonly unknown: ReadonlyArray } | { readonly ok: false; readonly problem: "missing" | "broken"; readonly issue: string }; @@ -200,28 +221,35 @@ export function readMcpProfileEntry(entry: unknown): McpProfileEntryRead { if (!servers.ok) return { ok: false, issue: servers.issue }; const t3Tools = parseT3Tools(entry.t3Tools); if (!t3Tools.ok) return { ok: false, issue: t3Tools.issue }; + const builtinTools = parseBuiltinTools(entry.builtinTools); + if (!builtinTools.ok) return { ok: false, issue: builtinTools.issue }; return { ok: true, - profile: { servers: servers.value, t3Tools: t3Tools.value.groups }, + profile: withBuiltinTools( + { servers: servers.value, t3Tools: t3Tools.value.groups }, + builtinTools.value, + ), unknown: t3Tools.value.unknown, }; } /** - * A delegating thread's resolved profile as another machine sends it with a task: its name and - * its `servers` and `t3Tools`. {@link resolveMcpProfile} cuts the task's own profile down to it. + * A delegating thread's resolved profile as another machine sends it with a task: its name, its + * `servers` and `t3Tools`, and `builtinTools` only when `"none"` (a machine from before that field + * refuses such a task, never widens it). {@link resolveMcpProfile} cuts the task's own profile down + * to it. */ export type McpProfileCarried = { readonly name: string; readonly servers: McpProfileSet["servers"]; readonly t3Tools: McpProfileSet["t3Tools"]; + readonly builtinTools?: typeof MCP_BUILTIN_TOOLS_NONE; }; /** A resolved profile as it is carried to another machine with a delegated task. */ export const carryMcpProfile = (resolved: McpProfileResolved): McpProfileCarried => ({ name: resolved.name, - servers: resolved.profile.servers, - t3Tools: resolved.profile.t3Tools, + ...resolved.profile, }); export type CarriedLookup = @@ -250,7 +278,16 @@ export function readCarried(value: unknown): CarriedLookup { if (!servers.ok) return { ok: false, name, issue: servers.issue }; const t3Tools = parseT3Tools(value.t3Tools); if (!t3Tools.ok) return { ok: false, name, issue: t3Tools.issue }; - return { ok: true, name, profile: { servers: servers.value, t3Tools: t3Tools.value.groups } }; + const builtinTools = parseBuiltinTools(value.builtinTools); + if (!builtinTools.ok) return { ok: false, name, issue: builtinTools.issue }; + return { + ok: true, + name, + profile: withBuiltinTools( + { servers: servers.value, t3Tools: t3Tools.value.groups }, + builtinTools.value, + ), + }; } /** A set value; empty or absent means "not set". */ @@ -296,7 +333,8 @@ function cutTo(child: McpProfileSet, parent: McpProfileSet): McpProfileSet { : child.t3Tools === "all" ? parent.t3Tools : child.t3Tools.filter((group) => parent.t3Tools.includes(group)); - return { servers, t3Tools }; + // Either one without built-in tools leaves the thread without them. + return withBuiltinTools({ servers, t3Tools }, child.builtinTools ?? parent.builtinTools); } const sameList = (a: "all" | ReadonlyArray, b: "all" | ReadonlyArray) => @@ -307,7 +345,27 @@ const sameList = (a: "all" | ReadonlyArray, b: "all" | ReadonlyArray