Skip to content
WebMCP Agents
Esc
↑↓navigate↵open⌘Jpreview
On this page

Confirmations and yielding

Test that a confirm-gated tool waits for the person's click, runs once they click, and is cancelled when they move on.

A tool with requireConfirmation parks: the cursor points at the button and waits for the person to click it. Three things are worth a test: nothing happens before the click, the tool runs after it, and the call ends cleanly when the person does something else.

A complete example

The checkout button, whose tool waits for the person:

import { useWebTool } from "@webmcp-agents/react";

import { orders } from "./orders";

export function Checkout() {
  const place = () => {
    orders.add(`FS-${orders.get().length + 1}`);
    return "Order placed.";
  };

  // The agent's call waits for the person's click on the button.
  useWebTool({
    id: "place_order",
    title: "Place order",
    description:
      "Place the order for what is in the cart. The user confirms " +
      "on the page.",
    requireConfirmation: true,
    execute: place,
  });

  return (
    <button type="button" onClick={place}>
      Place order
    </button>
  );
}

Its tests:

import { expect, test } from "@webmcp-agents/testing";
import { WebAgentsProvider } from "@webmcp-agents/react";

import { Checkout } from "./app/checkout";
import { orders } from "./app/orders";

test("waits for the person before placing the order", async ({
  tools,
  events,
  user,
  mount,
}) => {
  orders.clear();
  mount(
    <WebAgentsProvider>
      <Checkout />
    </WebAgentsProvider>,
  );
  await expect.poll(() => tools.list()).toContain("place_order");

  const call = tools.call("place_order");
  await expect.poll(() => events).toHaveParked("place_order");
  expect(orders.get()).toHaveLength(0);

  // A trusted click on the parked control.
  await user.confirm("Place order");
  expect(await call).toBeToolResult();
  expect(orders.get()).toHaveLength(1);
});

test("gives up when the person moves on", async ({
  tools,
  events,
  user,
  mount,
}) => {
  mount(
    <WebAgentsProvider>
      <Checkout />
    </WebAgentsProvider>,
  );
  await expect.poll(() => tools.list()).toContain("place_order");

  const call = tools.call("place_order");
  await expect.poll(() => events).toHaveParked();
  // A trusted click away from the control.
  await user.moveOn();
  expect(await call).toBeToolError("cancelled");
  // The cursor got out of the way of the person's pointer.
  expect(events).toHaveYielded("pointer");
});

Start the call without awaiting it, wait until it has parked, then act as the person. user.confirm("Place order") is a trusted click on the parked control (with no name, it clicks whatever control the call waits on). user.moveOn() is a trusted click somewhere else on the page.

What each matcher checks

  • toHaveParked("place_order"): the call reached its waiting state. Use it with expect.poll, since parking happens after the cursor has moved.
  • toBeToolResult() on the awaited call: it ran after the click.
  • toBeToolError("cancelled"): the person moved on, and the agent was told so. A gate nobody answers ends with needs_user after ten seconds.
  • toHaveYielded("pointer"): the cursor got out of the way of the person. The reason is pointer, keyboard, wheel or touch.

See also

Was this page helpful?