@@ -8,6 +8,7 @@ import * as ClaudeSdk from "@anthropic-ai/claude-agent-sdk";
88import { beforeEach, vi } from "vite-plus/test";
99
1010import type {
11+ ElicitationRequest,
1112 Query as ClaudeQuery,
1213 SDKMessage,
1314 SDKResultMessage,
@@ -1187,6 +1188,82 @@ describe("ClaudeAdapterV2 approval cancellation", () => {
11871188 );
11881189});
11891190
1191+ describe("ClaudeAdapterV2 MCP elicitation acceptance", () => {
1192+ const form = (
1193+ properties: Record<string, unknown>,
1194+ required?: ReadonlyArray<string>,
1195+ ): ElicitationRequest => ({
1196+ serverName: "deploys",
1197+ message: "Deploy web?",
1198+ mode: "form",
1199+ requestedSchema: { type: "object", properties, ...(required ? { required } : {}) },
1200+ });
1201+
1202+ it("answers an empty-properties form with empty content", () => {
1203+ assert.deepEqual(ClaudeAdapterV2.claudeMcpElicitationAcceptance(form({})), {
1204+ action: "accept",
1205+ content: {},
1206+ });
1207+ });
1208+
1209+ it("picks the one-time choice however the server spells it", () => {
1210+ for (const choice of [
1211+ "allow_once",
1212+ "Allow",
1213+ "accept_once",
1214+ "Approve once",
1215+ "allow_this_time",
1216+ "allow_once_only",
1217+ ]) {
1218+ assert.deepEqual(
1219+ ClaudeAdapterV2.claudeMcpElicitationAcceptance(
1220+ form({ decision: { type: "string", enum: ["disallow", choice, "always"] } }, [
1221+ "decision",
1222+ ]),
1223+ ),
1224+ { action: "accept", content: { decision: choice } },
1225+ );
1226+ }
1227+ });
1228+
1229+ it("answers a persistence toggle with false", () => {
1230+ assert.deepEqual(
1231+ ClaudeAdapterV2.claudeMcpElicitationAcceptance(
1232+ form({ remember: { type: "boolean", title: "Always allow" } }, ["remember"]),
1233+ ),
1234+ { action: "accept", content: { remember: false } },
1235+ );
1236+ });
1237+
1238+ it("leaves out optional fields the card does not show", () => {
1239+ assert.deepEqual(
1240+ ClaudeAdapterV2.claudeMcpElicitationAcceptance(
1241+ form({ scope: { type: "string", enum: ["once", "always"], default: "always" } }),
1242+ ),
1243+ { action: "accept", content: { scope: "once" } },
1244+ );
1245+ assert.deepEqual(
1246+ ClaudeAdapterV2.claudeMcpElicitationAcceptance(
1247+ form({ limit: { type: "number", default: 500 } }),
1248+ ),
1249+ { action: "accept", content: {} },
1250+ );
1251+ });
1252+
1253+ it("has no answer for URL mode or a form asking for a value", () => {
1254+ for (const request of [
1255+ { serverName: "deploys", message: "Sign in", mode: "url", url: "https://example.com" },
1256+ form({ reason: { type: "string" } }, ["reason"]),
1257+ form({ grant: { type: "string", enum: ["always", "session"] } }, ["grant"]),
1258+ form({ grant: { type: "string", enum: ["deny", "always"], default: "always" } }, ["grant"]),
1259+ form({ limit: { type: "number", default: 500 } }, ["limit"]),
1260+ { serverName: "deploys", message: "Deploy web?", mode: "form" },
1261+ ] satisfies ReadonlyArray<ElicitationRequest>) {
1262+ assert.isUndefined(ClaudeAdapterV2.claudeMcpElicitationAcceptance(request));
1263+ }
1264+ });
1265+ });
1266+
11901267const captureSdkExecutablePaths = Effect.fn("captureSdkExecutablePaths")(function* (
11911268 binaryPath: string,
11921269 environment: ResolvedProviderInstanceEnvironment = [],
@@ -4403,6 +4480,196 @@ describe("ClaudeAdapterV2 background wake turns", () => {
44034480 ),
44044481 );
44054482
4483+ describe("MCP elicitations", () => {
4484+ const testLayer = Layer.mergeAll(
4485+ IdAllocator.layer,
4486+ McpProviderSessions.layer,
4487+ NodeServices.layer,
4488+ );
4489+ const approvalOnly: ElicitationRequest = {
4490+ serverName: "deploys",
4491+ message: "Deploy web to production?",
4492+ mode: "form",
4493+ requestedSchema: { type: "object", properties: {} },
4494+ title: "Deploy",
4495+ };
4496+
4497+ const startElicitationTurn = Effect.fnUntraced(function* (
4498+ options?: Parameters<typeof makeWakeHarnessWithOptions>[0],
4499+ ) {
4500+ const harness = yield* makeWakeHarnessWithOptions(options);
4501+ yield* harness.runtime.startTurn(
4502+ makeClaudeTestTurnInput({
4503+ threadId: harness.threadId,
4504+ providerThread: harness.providerThread,
4505+ now: yield* DateTime.now,
4506+ attemptId: RunAttemptId.make("attempt-claude-elicitation"),
4507+ text: "Deploy web.",
4508+ attachments: [],
4509+ }),
4510+ );
4511+ const onElicitation = harness.getOpenedOptions()?.onElicitation;
4512+ if (onElicitation === undefined) throw new Error("The Claude query has no onElicitation.");
4513+ const runtimeRequests = () =>
4514+ harness.events.flatMap((event) =>
4515+ event.type === "runtime_request.updated" ? [event.runtimeRequest] : [],
4516+ );
4517+ let requestNumber = 0;
4518+ const elicit = (request: ElicitationRequest, signal = new AbortController().signal) =>
4519+ Effect.promise(() =>
4520+ onElicitation(request, { signal, requestId: `sdk-${requestNumber++}` }),
4521+ ).pipe(Effect.forkScoped);
4522+ const awaitCard = Effect.fnUntraced(function* (count: number) {
4523+ const event = yield* Queue.take(harness.runtimeRequestReceipts);
4524+ assert.equal(runtimeRequests().length, count);
4525+ return event.runtimeRequest;
4526+ });
4527+ return { harness, elicit, awaitCard, runtimeRequests };
4528+ });
4529+
4530+ it.effect("shows an approval-only form and accepts only after Approve", () =>
4531+ Effect.gen(function* () {
4532+ const { harness, elicit, awaitCard } = yield* startElicitationTurn();
4533+ const answer = yield* elicit(approvalOnly);
4534+ const request = yield* awaitCard(1);
4535+ assert.equal(request.kind, "mcp-elicitation");
4536+ assert.isTrue(request.responseCapability.type === "live");
4537+ if (request.responseCapability.type === "live") {
4538+ assert.isUndefined(request.responseCapability.supportsFeedback);
4539+ }
4540+ const card = harness.events.findLast(
4541+ (event) =>
4542+ event.type === "turn_item.updated" && event.turnItem.type === "approval_request",
4543+ );
4544+ assert.deepInclude(card?.type === "turn_item.updated" ? card.turnItem : undefined, {
4545+ prompt: "Deploy web to production?",
4546+ appName: "Deploy",
4547+ options: [
4548+ { decision: "cancel", label: "Cancel" },
4549+ { decision: "decline", label: "Decline" },
4550+ { decision: "accept", label: "Approve" },
4551+ ],
4552+ });
4553+ assert.isUndefined(answer.pollUnsafe());
4554+
4555+ yield* harness.runtime.respondToRuntimeRequest({
4556+ requestId: request.id,
4557+ decision: "accept",
4558+ });
4559+ assert.deepEqual(yield* Fiber.join(answer), { action: "accept", content: {} });
4560+
4561+ // The card is gone, so a second click is an unknown request.
4562+ const late = yield* Effect.flip(
4563+ harness.runtime.respondToRuntimeRequest({ requestId: request.id, decision: "decline" }),
4564+ );
4565+ assert.include(String(late.cause), "No pending Claude runtime request");
4566+ }).pipe(Effect.scoped, Effect.provide(testLayer)),
4567+ );
4568+
4569+ it.effect.each(["decline", "cancel"] as const)("answers %s with that action", (decision) =>
4570+ Effect.gen(function* () {
4571+ const { harness, elicit, awaitCard } = yield* startElicitationTurn();
4572+ const answer = yield* elicit(approvalOnly);
4573+ const request = yield* awaitCard(1);
4574+ yield* harness.runtime.respondToRuntimeRequest({ requestId: request.id, decision });
4575+ assert.deepEqual(yield* Fiber.join(answer), { action: decision });
4576+ }).pipe(Effect.scoped, Effect.provide(testLayer)),
4577+ );
4578+
4579+ it.effect.each(["acceptForSession", "acceptAlways"] as const)(
4580+ "declines an unsupported %s decision",
4581+ (decision) =>
4582+ Effect.gen(function* () {
4583+ const { harness, elicit, awaitCard } = yield* startElicitationTurn();
4584+ const answer = yield* elicit(approvalOnly);
4585+ const request = yield* awaitCard(1);
4586+ yield* harness.runtime.respondToRuntimeRequest({ requestId: request.id, decision });
4587+ assert.deepEqual(yield* Fiber.join(answer), { action: "decline" });
4588+ }).pipe(Effect.scoped, Effect.provide(testLayer)),
4589+ );
4590+
4591+ it.effect("declines URL and value forms without a card", () =>
4592+ Effect.gen(function* () {
4593+ const { elicit, runtimeRequests } = yield* startElicitationTurn();
4594+ for (const request of [
4595+ { ...approvalOnly, mode: "url", url: "https://example.com/auth" },
4596+ {
4597+ ...approvalOnly,
4598+ requestedSchema: {
4599+ type: "object",
4600+ properties: { reason: { type: "string" } },
4601+ required: ["reason"],
4602+ },
4603+ },
4604+ ] satisfies ReadonlyArray<ElicitationRequest>) {
4605+ assert.deepEqual(yield* Fiber.join(yield* elicit(request)), { action: "decline" });
4606+ }
4607+ assert.isEmpty(runtimeRequests());
4608+ }).pipe(Effect.scoped, Effect.provide(testLayer)),
4609+ );
4610+
4611+ it.effect("cancels an already-aborted request without a card", () =>
4612+ Effect.gen(function* () {
4613+ const { harness, elicit, awaitCard, runtimeRequests } = yield* startElicitationTurn();
4614+ const aborted = yield* Effect.sync(() => {
4615+ const controller = new AbortController();
4616+ controller.abort();
4617+ return controller.signal;
4618+ });
4619+ const answer = yield* elicit(approvalOnly, aborted);
4620+ assert.deepEqual(yield* Fiber.join(answer), { action: "cancel" });
4621+ assert.isEmpty(runtimeRequests());
4622+ const next = yield* elicit(approvalOnly);
4623+ const card = yield* awaitCard(1);
4624+ assert.equal(card.nativeRequestRef?.nativeId, "mcp-elicitation:sdk-1");
4625+ yield* harness.runtime.respondToRuntimeRequest({ requestId: card.id, decision: "accept" });
4626+ assert.deepEqual(yield* Fiber.join(next), { action: "accept", content: {} });
4627+ }).pipe(Effect.scoped, Effect.provide(testLayer)),
4628+ );
4629+
4630+ it.effect("withdraws an open card when the SDK cancels while the session continues", () =>
4631+ Effect.gen(function* () {
4632+ const { harness, elicit, awaitCard } = yield* startElicitationTurn();
4633+ const controller = yield* Effect.sync(() => new AbortController());
4634+ const answer = yield* elicit(approvalOnly, controller.signal);
4635+ const request = yield* awaitCard(1);
4636+ yield* Effect.sync(() => controller.abort());
4637+ assert.deepEqual(yield* Fiber.join(answer), { action: "cancel" });
4638+ const cancelled = yield* Queue.take(harness.runtimeRequestReceipts);
4639+ assert.equal(cancelled.runtimeRequest.id, request.id);
4640+ assert.equal(cancelled.runtimeRequest.status, "cancelled");
4641+ assert.equal(
4642+ harness.events.findLast((e) => e.type === "node.updated")?.node.status,
4643+ "cancelled",
4644+ );
4645+ assert.equal(
4646+ harness.events.findLast((e) => e.type === "turn_item.updated")?.turnItem.status,
4647+ "cancelled",
4648+ );
4649+ yield* Effect.flip(
4650+ harness.runtime.respondToRuntimeRequest({ requestId: request.id, decision: "accept" }),
4651+ );
4652+ }).pipe(Effect.scoped, Effect.provide(testLayer)),
4653+ );
4654+
4655+ it.effect("cancels an open card when the session stops", () =>
4656+ Effect.gen(function* () {
4657+ // Like the SDK, the fake query aborts in-flight callbacks when closed.
4658+ const inFlight = yield* Effect.sync(() => new AbortController());
4659+ const session = yield* Scope.make();
4660+ const { elicit, awaitCard } = yield* startElicitationTurn({
4661+ close: () => Effect.sync(() => inFlight.abort()),
4662+ }).pipe(Scope.provide(session));
4663+ const answer = yield* elicit(approvalOnly, inFlight.signal);
4664+ yield* awaitCard(1);
4665+ assert.isUndefined(answer.pollUnsafe());
4666+
4667+ yield* Scope.close(session, Exit.void);
4668+ assert.deepEqual(yield* Fiber.join(answer), { action: "cancel" });
4669+ }).pipe(Effect.scoped, Effect.provide(testLayer)),
4670+ );
4671+ });
4672+
44064673 it.effect(
44074674 "applies model, effort and Fast changes to a process running a background shell, but not thinking or a cleared effort",
44084675 () =>
0 commit comments