diff --git a/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts b/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts index e1665b1bafb3e..42578a4e3e828 100644 --- a/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts +++ b/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts @@ -6736,11 +6736,12 @@ describe("ClaudeAdapterV2 background wake turns", () => { overrides?: Partial< Pick< ClaudeAdapterV2.ClaudeAgentSdkQuerySession, - "mcpServerStatus" | "readMcpResource" | "listPermissionRules" + "mcpServerStatus" | "readMcpResource" | "callMcpTool" | "listPermissionRules" > >, toolName = "mcp__probe_server__show_weather", toolUseResult: unknown = { content: '{"tempC":21}', structuredContent: { tempC: 21 } }, + runtimePolicy = CLAUDE_TEST_RUNTIME_POLICY, ) { const mcpRequests: Array = []; const harness = yield* makeWakeHarnessWithOptions({ @@ -6778,6 +6779,7 @@ describe("ClaudeAdapterV2 background wake turns", () => { attemptId: RunAttemptId.make("mcp-app"), text: "Show the weather", attachments: [], + runtimePolicy, }), ); const id = "weather-app-call"; @@ -7094,28 +7096,120 @@ describe("ClaudeAdapterV2 background wake turns", () => { called: false, readOnly: false, }, + { + case: "Claude's own check and rules for other tools", + rules: [ + { behavior: "ask", rule: "mcp__atlassian__createJiraIssue" }, + { behavior: "ask", rule: "mcp__probe_server__delete_*" }, + { behavior: "deny", rule: "mcp__buildkite__create_build" }, + { behavior: "deny", rule: "mcp__probe_server__delete_*" }, + ], + checks: "full-access", + called: true, + readOnly: true, + }, + { + case: "Claude's own check and an ask rule naming the tool", + rules: [{ behavior: "ask", rule: "mcp__probe_server__refresh_*" }], + checks: "full-access", + claudeAsks: true, + called: true, + readOnly: false, + }, + { + case: "Claude's own check and an ask wildcard that repeats a segment and misses its last one", + rules: [{ behavior: "ask", rule: "mcp__probe_server__*a*a*a*a*a*a*a*ab" }], + tool: "a".repeat(256), + checks: "full-access", + called: true, + readOnly: true, + }, + { + case: "Claude's own check and an ask rule for a server alias T3 can't list", + rules: [{ behavior: "ask", rule: "mcp__probe_alias" }], + checks: "full-access", + claudeAsks: true, + called: true, + readOnly: false, + }, + { + case: "Claude's own check and an ask T3 did not prompt for", + rules: [{ behavior: "ask", rule: "mcp__atlassian" }], + checks: "full-access", + claudeAsks: true, + called: false, + readOnly: true, + }, + { + case: "Claude's own check and permission rules that cannot be read", + rules: "unreadable", + checks: "full-access", + called: true, + readOnly: true, + }, + { + case: "Claude's own check outside Full Access", + rules: [{ behavior: "deny", rule: "mcp__buildkite__create_build" }], + checks: "approval-required", + called: false, + readOnly: false, + }, ] as const)("applies Claude's permission rules to MCP app calls with $case", (testCase) => Effect.gen(function* () { const server = "server" in testCase ? testCase.server : PROBE_SERVER_STATUS.name; const tool = "tool" in testCase ? testCase.tool : "refresh_weather"; - const { harness, mcpRequests } = yield* runClaudeMcpAppToolTurn(MCP_APP_CAPABILITIES, { - mcpServerStatus: Effect.succeed([ - { - ...PROBE_SERVER_STATUS, - name: server, - tools: [{ ...PROBE_SERVER_STATUS.tools[1], name: tool }], - }, - ]), - listPermissionRules: - testCase.rules === "unreadable" - ? Effect.fail( - new ClaudeAdapterV2.ClaudeAgentSdkQueryRunnerError({ - method: "listPermissionRules", - cause: new Error("Unsupported control request subtype"), + const calls: Array<{ readonly checkPermissions?: boolean }> = []; + const opened: { canUseTool?: ClaudeSdk.CanUseTool | undefined } = {}; + const { harness } = yield* runClaudeMcpAppToolTurn( + "checks" in testCase + ? [...MCP_APP_CAPABILITIES, "mcp_call_check_permissions_v1"] + : MCP_APP_CAPABILITIES, + { + mcpServerStatus: Effect.succeed([ + { + ...PROBE_SERVER_STATUS, + name: server, + tools: [{ ...PROBE_SERVER_STATUS.tools[1], name: tool }], + }, + { name: "atlassian", status: "connected", tools: [] }, + ]), + listPermissionRules: + testCase.rules === "unreadable" + ? Effect.fail( + new ClaudeAdapterV2.ClaudeAgentSdkQueryRunnerError({ + method: "listPermissionRules", + cause: new Error("Unsupported control request subtype"), + }), + ) + : Effect.succeed(testCase.rules), + // Shape recorded from Claude Code 2.1.330: a checked call's ask goes to canUseTool, and a refusal fails the call. + callMcpTool: Effect.fnUntraced(function* (call) { + if ("claudeAsks" in testCase && call.checkPermissions === true) { + const answer = yield* Effect.promise(() => + opened.canUseTool!(call.tool, call.arguments, { + signal: new AbortController().signal, + toolUseID: "toolu_mcp_call_a53e26e8c8a9483c8466271c506fcdcb", + requestId: "request-mcp-call", }), - ) - : Effect.succeed(testCase.rules), - }); + ); + if (answer?.behavior !== "allow") { + return yield* new ClaudeAdapterV2.ClaudeAgentSdkQueryRunnerError({ + method: "callMcpTool", + cause: new Error("The tool did not run"), + }); + } + } + calls.push(call); + return { content: [{ type: "text", text: "refreshed" }] }; + }), + }, + undefined, + undefined, + "checks" in testCase + ? { ...CLAUDE_TEST_RUNTIME_POLICY, runtimeMode: testCase.checks } + : CLAUDE_TEST_RUNTIME_POLICY, + ); + opened.canUseTool = harness.getOpenedOptions()?.canUseTool; const mcpApps = harness.runtime.mcpApps!; const target = { providerThread: harness.providerThread, server }; const [listed] = yield* mcpApps.listTools(target); @@ -7127,12 +7221,15 @@ describe("ClaudeAdapterV2 background wake turns", () => { ); const exit = yield* Effect.exit(mcpApps.callTool({ ...target, tool, arguments: {} })); assert.equal(Exit.isSuccess(exit), testCase.called); - assert.equal( - mcpRequests.some( - (request) => typeof request === "object" && request !== null && "arguments" in request, - ), - testCase.called, + assert.deepEqual( + calls.map((call) => call.checkPermissions === true), + testCase.called ? ["checks" in testCase && testCase.checks === "full-access"] : [], ); + if ("claudeAsks" in testCase && !testCase.called) { + // Nobody was prompted for the refused call, so it cannot make a later unprompted one pass. + const retry = yield* Effect.exit(mcpApps.callTool({ ...target, tool, arguments: {} })); + assert.isTrue(Exit.isFailure(retry)); + } }).pipe( Effect.scoped, Effect.provide( diff --git a/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts b/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts index b120f9feb5e5a..afb4d2a66d27b 100644 --- a/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts +++ b/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts @@ -747,6 +747,8 @@ export interface ClaudeAgentSdkQuerySession { readonly callMcpTool: (input: { readonly tool: string; readonly arguments: Record; + /** Has Claude decide the call as the model's own; an ask reaches `canUseTool`. */ + readonly checkPermissions?: boolean; }) => Effect.Effect; readonly listPermissionRules: Effect.Effect< ReadonlyArray, @@ -1074,21 +1076,104 @@ function claudeRulesMayCoverMcpTools( }); } +// Recorded from Claude Code 2.1.330: the tool-use id of the ask a checked `mcp_call` sends to `canUseTool`. +const CLAUDE_MCP_CALL_TOOL_USE_ID_PREFIX = "toolu_mcp_call_"; + /** - * Claude reports `annotations` as `readOnly`/`destructive`/`openWorld`; apps expect MCP's `*Hint` names. - * Card calls skip Claude's permission check, so `readOnlyHint`, which skips T3's prompt, needs readable - * rules (null when not) with no ask or deny rule that may cover the tool. + * Whether Claude itself decides a card's calls. Only in Full Access, where it asks for nothing but + * its ask rules and safety checks: plan and dontAsk refuse every ask, and other modes ask about calls + * T3 runs today. An older CLI ignores the flag and runs the tool unchecked. */ +function claudeChecksMcpAppCalls( + liveQuery: Pick, +): boolean { + return ( + liveQuery.capabilities.has("mcp_call_check_permissions_v1") && + liveQuery.permissionMode === "bypassPermissions" + ); +} + +/** + * Whether an ask rule may cover this tool. Claude also matches rules against per-server keys T3 can't + * list, so only two kinds of rule are ruled out: one under this server's name that spells another + * tool, and one under the name of another server Claude reports. A false match only asks first. + */ +function claudeAskRuleMayCoverMcpTool(input: { + readonly rules: ReadonlyArray; + readonly statuses: ReadonlyArray; + readonly server: string; + readonly toolName: string; +}): boolean { + // Claude reads a rule's server as the text up to the first `__`. + const serverPart = (name: string) => name.slice("mcp__".length).split("__", 1)[0] ?? ""; + const own = serverPart(claudeMcpToolPrefix(input.server)); + const others = new Set( + input.statuses.map((status) => serverPart(claudeMcpToolPrefix(status.name))), + ); + return input.rules.some((entry) => { + if (entry.behavior !== "ask" || entry.notInEffect === true) return false; + const name = entry.rule.split("(", 1)[0] ?? entry.rule; + if (!name.startsWith("mcp__")) return name.includes("*"); + const part = serverPart(name); + if (part === own) { + return ( + input.toolName === name || + input.toolName.startsWith(`${name}__`) || + claudeWildcardMatches(name, input.toolName) + ); + } + return part.includes("*") || !others.has(part); + }); +} + +// Leftmost search per segment, not a regex: a `.*` per `*` backtracks exponentially on a near miss. +function claudeWildcardMatches(pattern: string, value: string): boolean { + const [first = "", ...rest] = pattern.split("*"); + const last = rest.pop(); + if (last === undefined) return pattern === value; + if (!value.startsWith(first)) return false; + let from = first.length; + for (const part of rest) { + const at = value.indexOf(part, from); + if (at === -1) return false; + from = at + part.length; + } + return value.length - last.length >= from && value.endsWith(last); +} + +/** + * Whether a card runs this read-only tool without T3's prompt. A checked call is refused or asked + * about by Claude, so only an ask rule that may cover the tool needs the prompt, which then answers + * the ask. An unchecked call skips Claude's check, so it needs readable rules (null when not) with no + * ask or deny rule that may cover any MCP tool. + */ +function claudeReadOnlyMcpToolSkipsPrompt(input: { + readonly checked: boolean; + readonly rules: ReadonlyArray | null; + readonly statuses: ReadonlyArray; + readonly server: string; + readonly tool: string; +}): boolean { + if (input.rules === null) return input.checked; + return input.checked + ? !claudeAskRuleMayCoverMcpTool({ + rules: input.rules, + statuses: input.statuses, + server: input.server, + toolName: claudeMcpToolName(input.server, input.tool), + }) + : !claudeRulesMayCoverMcpTools(input.rules, ["ask", "deny"]); +} + +/** Claude reports `annotations` as `readOnly`/`destructive`/`openWorld`; apps expect MCP's `*Hint` names. */ function claudeMcpToolForApps( tool: NonNullable[number], - rules: ReadonlyArray | null, + readOnlySkipsPrompt: boolean, ) { const annotations = tool.annotations ?? {}; + // `readOnlyHint` is what skips T3's prompt. const readOnlyHint = - annotations.readOnly !== true || - (rules !== null && !claudeRulesMayCoverMcpTools(rules, ["ask", "deny"])) - ? annotations.readOnly - : undefined; + annotations.readOnly !== true || readOnlySkipsPrompt ? annotations.readOnly : undefined; return { name: tool.name, ...(tool.description === undefined ? {} : { description: tool.description }), @@ -1987,6 +2072,7 @@ export const layerQueryRunner: Layer.Layer< tool: call.tool, arguments: call.arguments, result_not_for_model: true, + ...(call.checkPermissions === true ? { check_permissions: true } : {}), }), catch: (cause) => queryRunnerError(cause, "callMcpTool"), }), @@ -6095,6 +6181,8 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* ( const pdfBudgets = new Map>(); // MCP app notes already in each native session's history, by key. Cleared when it compacts. const deliveredAppContext = new Map>(); + // In-flight card calls, by Claude tool name, that T3 asked the user about first. + const promptedMcpAppCalls = new Map(); const latestPlanByKind = yield* Ref.make(new Map()); // Claude 5 task tools edit one list per native session, across turns. const taskListsByNativeThread = yield* Ref.make( @@ -12551,6 +12639,21 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* ( callbackOptions: Parameters[2], options?: { readonly startToolCall?: boolean }, ) { + // A card's call belongs to no turn; T3's own prompt, shown before the call, is the user's answer. + if (callbackOptions.toolUseID.startsWith(CLAUDE_MCP_CALL_TOOL_USE_ID_PREFIX)) { + if (promptedMcpAppCalls.has(toolName)) { + return { + behavior: "allow", + updatedInput: toolInput, + toolUseID: callbackOptions.toolUseID, + } satisfies PermissionResult; + } + return { + behavior: "deny", + message: "The user was not asked about this app call.", + toolUseID: callbackOptions.toolUseID, + } satisfies PermissionResult; + } const context = yield* Ref.get(activeTurn); if (context === null) { return { @@ -14554,7 +14657,19 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* ( const rules = yield* liveQuery.query.listPermissionRules.pipe( Effect.orElseSucceed(() => null), ); - return (status?.tools ?? []).map((tool) => claudeMcpToolForApps(tool, rules)); + const checked = claudeChecksMcpAppCalls(liveQuery); + return (status?.tools ?? []).map((tool) => + claudeMcpToolForApps( + tool, + claudeReadOnlyMcpToolSkipsPrompt({ + checked, + rules, + statuses, + server, + tool: tool.name, + }), + ), + ); }), callTool: Effect.fn("ClaudeAdapterV2.callMcpTool")(function* (call) { const { liveQuery, statuses } = yield* mcpAppServerStatuses( @@ -14572,39 +14687,67 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* ( detail: "Another tool on this MCP server has the same Claude tool name.", }); } - // mcp_call skips Claude's permission check, so its deny rules are applied here. + const checked = claudeChecksMcpAppCalls(liveQuery); const rules = yield* liveQuery.query.listPermissionRules.pipe( - Effect.mapError( - (cause) => - new ProviderAdapter.ProviderAdapterProtocolError({ - driver: CLAUDE_PROVIDER, - detail: `Claude's permission rules could not be read, so ${tool} was not called.`, - cause, - }), + Effect.catch((cause) => + checked + ? Effect.succeed(null) + : Effect.fail( + new ProviderAdapter.ProviderAdapterProtocolError({ + driver: CLAUDE_PROVIDER, + detail: `Claude's permission rules could not be read, so ${tool} was not called.`, + cause, + }), + ), ), ); - if (claudeRulesMayCoverMcpTools(rules, ["deny"])) { + // An unchecked mcp_call skips Claude's permission check, so its deny rules are applied here. + if (!checked && rules !== null && claudeRulesMayCoverMcpTools(rules, ["deny"])) { return yield* new ProviderAdapter.ProviderAdapterProtocolError({ driver: CLAUDE_PROVIDER, detail: `A Claude deny rule may cover ${tool}, so it was not called.`, }); } - return claudeMcpCallToolResult( - yield* liveQuery.query - .callMcpTool({ - tool, - arguments: call.arguments, + // The client asked first unless it was told the tool is read-only, as listTools decides. + const prompted = + checked && + !( + sameName[0]?.annotations?.readOnly === true && + claudeReadOnlyMcpToolSkipsPrompt({ + checked, + rules, + statuses, + server: call.server, + tool: call.tool, }) - .pipe( - Effect.mapError( - (cause) => - new ProviderAdapter.ProviderAdapterProtocolError({ - driver: CLAUDE_PROVIDER, - detail: "Claude MCP tool call failed.", - cause, - }), - ), + ); + const countPrompted = (change: 1 | -1) => + Effect.sync(() => { + if (!prompted) return; + const count = (promptedMcpAppCalls.get(tool) ?? 0) + change; + if (count > 0) promptedMcpAppCalls.set(tool, count); + else promptedMcpAppCalls.delete(tool); + }); + return claudeMcpCallToolResult( + yield* Effect.acquireUseRelease( + countPrompted(1), + () => + liveQuery.query.callMcpTool({ + tool, + arguments: call.arguments, + ...(checked ? { checkPermissions: true } : {}), + }), + () => countPrompted(-1), + ).pipe( + Effect.mapError( + (cause) => + new ProviderAdapter.ProviderAdapterProtocolError({ + driver: CLAUDE_PROVIDER, + detail: "Claude MCP tool call failed.", + cause, + }), ), + ), ); }), readResource: Effect.fn("ClaudeAdapterV2.readMcpResource")(function* (read) {