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

Test privacy and telemetry

One test that checks a secret reaches no sink (events, run events, spans, the console), and one that checks a run is one trace with the spans you expect.

Redaction only helps if it covers every place data goes. Rather than check each place in its own test, collect them all and check the secret appears in none: the cursor’s events, the run events, the spans, and the console.

A complete example

A form whose tool marks the passport as sensitive:

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

export function TravelerForm() {
  // `sensitive`: the passport is masked wherever the library shows
  // or logs the call. The field itself gets the real value.
  useWebTool({
    id: "fill_traveler",
    description: "Fill in the traveler's name and passport number.",
    inputSchema: z.object({
      fullName: z.string(),
      passport: z.string(),
    }),
    sensitive: ["passport"],
    execute: () => "Traveler saved.",
  });

  return (
    <form>
      <label>
        Full name <input name="fullName" />
      </label>
      <label>
        Passport <input name="passport" />
      </label>
      {/* The tool's control: the cursor finds it by name. */}
      <button type="button">Fill traveler</button>
    </form>
  );
}

The test:

import { expect, test, vi } from "@webmcp-agents/testing";
import {
  mockAdapter,
  text,
  toolCall,
} from "@webmcp-agents/testing/chat";
import { defineWebAgent } from "@webmcp-agents/chat/agent";
import { WebAgentsProvider } from "@webmcp-agents/react";

import { TravelerForm } from "./app/traveler-form";

const traveler = defineWebAgent({
  id: "traveler",
  instructions: "Fill in the traveler's details.",
});
const passport = "A1234567";

test("a passport number leaves the page nowhere", async ({
  tools,
  events,
  runs,
  spans,
  mount,
}) => {
  const logs = vi.spyOn(console, "error");
  mount(
    <WebAgentsProvider>
      <TravelerForm />
    </WebAgentsProvider>,
  );
  await expect.poll(() => tools.list()).toContain("fill_traveler");
  const input = { fullName: "Sara", passport };

  const adapter = mockAdapter([
    toolCall("fill_traveler", input),
    text("Done."),
  ]);
  await traveler.invoke(`Book for Sara, passport ${passport}`, {
    adapter,
  });
  await spans.flush();

  // Typed into the field, "••••" in the cursor's events.
  expect(events).toHaveFilled("passport");
  // The run events and the spans carry the tool call, redacted.
  expect(runs).toHaveCalledTools(["fill_traveler"]);
  expect(spans).toHaveSpan("execute_tool fill_traveler");

  const sinks = [events.of("fill"), runs, spans, logs];
  for (const sink of sinks) {
    expect(sink).not.toContainSensitive([passport]);
  }
});

toContainSensitive reads any value: an event log, a run log, spans, a vi.spyOn of the console, or a plain object. Use it negated. A failure names the sink and the path where the value was found, and prints nothing else of the sink’s content.

runs and spans need @webmcp-agents/chat and @webmcp-agents/telemetry installed; both are experimental in 0.5.0. spans.flush() waits for the spans still buffered, so call it before you read them.

The shape of a trace

The spans fixture observes every run of the test into memory, so you can check the span tree directly:

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

import { Store, storeAgent } from "./app/store";

test("one run is one trace", async ({ tools, spans, mount }) => {
  mount(
    <WebAgentsProvider>
      <Store />
    </WebAgentsProvider>,
  );
  await expect.poll(() => tools.list()).toContain("add_to_cart");

  const adapter = mockAdapter([
    toolCall("add_to_cart", { id: "mug-01", qty: 2 }),
    text("Added."),
  ]);
  await storeAgent.invoke("Two mugs.", { adapter });
  await spans.flush();

  expect(spans).toHaveSpan("invoke_agent store");
  expect(spans).toHaveSpan("execute_tool add_to_cart", {
    "webmcp.tool.state": "done",
  });
  expect(spans).toBeOneTrace();
});

toHaveSpan(name, attributes?) finds a span by name (or a pattern) with at least these attributes. toBeOneTrace checks every span shares one trace id under one root. To include the server’s chat spans, give a memoryExporter() to defineObservability({ exporters }) too, and pass both arrays.

See also

Was this page helpful?