@@ -6,6 +6,7 @@ import * as NodePath from "node:path";
66import * as NodeStream from "node:stream";
77
88import type {
9+ ElicitationRequest,
910 Query as ClaudeQuery,
1011 SDKMessage,
1112 SDKResultMessage,
@@ -1159,6 +1160,56 @@ describe("ClaudeAdapterV2 approval cancellation", () => {
11591160 );
11601161});
11611162
1163+ describe("ClaudeAdapterV2 MCP elicitation acceptance", () => {
1164+ const form = (
1165+ properties: Record<string, unknown>,
1166+ required?: ReadonlyArray<string>,
1167+ ): ElicitationRequest => ({
1168+ serverName: "deploys",
1169+ message: "Deploy web?",
1170+ mode: "form",
1171+ requestedSchema: { type: "object", properties, ...(required ? { required } : {}) },
1172+ });
1173+
1174+ it("answers an empty-properties form with empty content", () => {
1175+ assert.deepEqual(ClaudeAdapterV2.claudeMcpElicitationAcceptance(form({})), {
1176+ action: "accept",
1177+ content: {},
1178+ });
1179+ });
1180+
1181+ it("picks the one-time choice however the server spells it", () => {
1182+ for (const choice of ["allow_once", "Allow", "accept_once", "Approve once"]) {
1183+ assert.deepEqual(
1184+ ClaudeAdapterV2.claudeMcpElicitationAcceptance(
1185+ form({ decision: { type: "string", enum: ["deny", choice, "always"] } }, ["decision"]),
1186+ ),
1187+ { action: "accept", content: { decision: choice } },
1188+ );
1189+ }
1190+ });
1191+
1192+ it("answers a persistence toggle with false", () => {
1193+ assert.deepEqual(
1194+ ClaudeAdapterV2.claudeMcpElicitationAcceptance(
1195+ form({ remember: { type: "boolean", title: "Always allow" } }, ["remember"]),
1196+ ),
1197+ { action: "accept", content: { remember: false } },
1198+ );
1199+ });
1200+
1201+ it("has no answer for URL mode or a form asking for a value", () => {
1202+ for (const request of [
1203+ { serverName: "deploys", message: "Sign in", mode: "url", url: "https://example.com" },
1204+ form({ reason: { type: "string" } }, ["reason"]),
1205+ form({ grant: { type: "string", enum: ["always", "session"] } }, ["grant"]),
1206+ { serverName: "deploys", message: "Deploy web?", mode: "form" },
1207+ ] satisfies ReadonlyArray<ElicitationRequest>) {
1208+ assert.isUndefined(ClaudeAdapterV2.claudeMcpElicitationAcceptance(request));
1209+ }
1210+ });
1211+ });
1212+
11621213const captureSdkExecutablePaths = Effect.fn("captureSdkExecutablePaths")(function* (
11631214 binaryPath: string,
11641215 environment: ProviderInstanceEnvironment = [],
@@ -2779,6 +2830,148 @@ describe("ClaudeAdapterV2 background wake turns", () => {
27792830 ),
27802831 );
27812832
2833+ describe("MCP elicitations", () => {
2834+ const testLayer = Layer.mergeAll(
2835+ IdAllocator.layer,
2836+ McpProviderSessions.layer,
2837+ NodeServices.layer,
2838+ );
2839+ const approvalOnly: ElicitationRequest = {
2840+ serverName: "deploys",
2841+ message: "Deploy web to production?",
2842+ mode: "form",
2843+ requestedSchema: { type: "object", properties: {} },
2844+ title: "Deploy",
2845+ };
2846+
2847+ const startElicitationTurn = Effect.fnUntraced(function* (
2848+ options?: Parameters<typeof makeWakeHarnessWithOptions>[0],
2849+ ) {
2850+ const harness = yield* makeWakeHarnessWithOptions(options);
2851+ yield* harness.runtime.startTurn(
2852+ makeClaudeTestTurnInput({
2853+ threadId: harness.threadId,
2854+ providerThread: harness.providerThread,
2855+ now: yield* DateTime.now,
2856+ attemptId: RunAttemptId.make("attempt-claude-elicitation"),
2857+ text: "Deploy web.",
2858+ attachments: [],
2859+ }),
2860+ );
2861+ const onElicitation = harness.getOpenedOptions()?.onElicitation;
2862+ if (onElicitation === undefined) throw new Error("The Claude query has no onElicitation.");
2863+ const runtimeRequests = () =>
2864+ harness.events.flatMap((event) =>
2865+ event.type === "runtime_request.updated" ? [event.runtimeRequest] : [],
2866+ );
2867+ const elicit = (request: ElicitationRequest, signal = new AbortController().signal) =>
2868+ Effect.promise(() =>
2869+ onElicitation(request, { signal, requestId: `sdk-${runtimeRequests().length}` }),
2870+ ).pipe(Effect.forkScoped);
2871+ const awaitCard = Effect.fnUntraced(function* (count: number) {
2872+ yield* awaitUntil(() => runtimeRequests().length === count, `elicitation card ${count}`);
2873+ return runtimeRequests()[count - 1]!;
2874+ });
2875+ return { harness, elicit, awaitCard, runtimeRequests };
2876+ });
2877+
2878+ it.effect("shows an approval-only form and accepts only after Approve", () =>
2879+ Effect.gen(function* () {
2880+ const { harness, elicit, awaitCard } = yield* startElicitationTurn();
2881+ const answer = yield* elicit(approvalOnly);
2882+ const request = yield* awaitCard(1);
2883+ assert.equal(request.kind, "mcp-elicitation");
2884+ const card = harness.events.findLast(
2885+ (event) =>
2886+ event.type === "turn_item.updated" && event.turnItem.type === "approval_request",
2887+ );
2888+ assert.deepInclude(card?.type === "turn_item.updated" ? card.turnItem : undefined, {
2889+ prompt: "Deploy web to production?",
2890+ appName: "Deploy",
2891+ options: [
2892+ { decision: "cancel", label: "Cancel" },
2893+ { decision: "decline", label: "Decline" },
2894+ { decision: "accept", label: "Approve" },
2895+ ],
2896+ });
2897+ assert.isUndefined(answer.pollUnsafe());
2898+
2899+ yield* harness.runtime.respondToRuntimeRequest({
2900+ requestId: request.id,
2901+ decision: "accept",
2902+ });
2903+ assert.deepEqual(yield* Fiber.join(answer), { action: "accept", content: {} });
2904+
2905+ // The card is gone, so a second click is an unknown request.
2906+ const late = yield* Effect.flip(
2907+ harness.runtime.respondToRuntimeRequest({ requestId: request.id, decision: "decline" }),
2908+ );
2909+ assert.include(String(late.cause), "No pending Claude runtime request");
2910+ }).pipe(Effect.scoped, Effect.provide(testLayer)),
2911+ );
2912+
2913+ it.effect.each(["decline", "cancel"] as const)("answers %s with that action", (decision) =>
2914+ Effect.gen(function* () {
2915+ const { harness, elicit, awaitCard } = yield* startElicitationTurn();
2916+ const answer = yield* elicit(approvalOnly);
2917+ const request = yield* awaitCard(1);
2918+ yield* harness.runtime.respondToRuntimeRequest({ requestId: request.id, decision });
2919+ assert.deepEqual(yield* Fiber.join(answer), { action: decision });
2920+ }).pipe(Effect.scoped, Effect.provide(testLayer)),
2921+ );
2922+
2923+ it.effect("declines URL and value forms without a card", () =>
2924+ Effect.gen(function* () {
2925+ const { elicit, runtimeRequests } = yield* startElicitationTurn();
2926+ for (const request of [
2927+ { ...approvalOnly, mode: "url", url: "https://example.com/auth" },
2928+ {
2929+ ...approvalOnly,
2930+ requestedSchema: {
2931+ type: "object",
2932+ properties: { reason: { type: "string" } },
2933+ required: ["reason"],
2934+ },
2935+ },
2936+ ] satisfies ReadonlyArray<ElicitationRequest>) {
2937+ assert.deepEqual(yield* Fiber.join(yield* elicit(request)), { action: "decline" });
2938+ }
2939+ assert.isEmpty(runtimeRequests());
2940+ }).pipe(Effect.scoped, Effect.provide(testLayer)),
2941+ );
2942+
2943+ it.effect("cancels when the SDK aborted before the card was answered", () =>
2944+ Effect.gen(function* () {
2945+ const { harness, elicit, awaitCard } = yield* startElicitationTurn();
2946+ const aborted = new AbortController();
2947+ aborted.abort();
2948+ const answer = yield* elicit(approvalOnly, aborted.signal);
2949+ const request = yield* awaitCard(1);
2950+ assert.deepEqual(yield* Fiber.join(answer), { action: "cancel" });
2951+ yield* Effect.flip(
2952+ harness.runtime.respondToRuntimeRequest({ requestId: request.id, decision: "accept" }),
2953+ );
2954+ }).pipe(Effect.scoped, Effect.provide(testLayer)),
2955+ );
2956+
2957+ it.effect("cancels an open card when the session stops", () =>
2958+ Effect.gen(function* () {
2959+ // Like the SDK, the fake query aborts in-flight callbacks when closed.
2960+ const inFlight = new AbortController();
2961+ const session = yield* Scope.make();
2962+ const { elicit, awaitCard } = yield* startElicitationTurn({
2963+ close: () => Effect.sync(() => inFlight.abort()),
2964+ }).pipe(Scope.provide(session));
2965+ const answer = yield* elicit(approvalOnly, inFlight.signal);
2966+ yield* awaitCard(1);
2967+ assert.isUndefined(answer.pollUnsafe());
2968+
2969+ yield* Scope.close(session, Exit.void);
2970+ assert.deepEqual(yield* Fiber.join(answer), { action: "cancel" });
2971+ }).pipe(Effect.scoped, Effect.provide(testLayer)),
2972+ );
2973+ });
2974+
27822975 it.effect(
27832976 "applies model, effort and Fast changes to a process running a background shell, but not thinking or a cleared effort",
27842977 () =>
0 commit comments