Agents in your app

WebMCP

Hand the calendar's tools to the agent built into a browser, with rules of their own for an agent you didn't write.

Live. Register the tools, then call them as a browser's agent would. In a browser without WebMCP a stand-in takes the tools; the bridge behind them is the real one.

WebMCP lets a page offer tools to the agent built into a browser, through document.modelContext. registerTools from @ui-sdk/react/agent registers the bridge's three tools there, and a browser's agent can then read your calendar and change it without any server of yours in between.

WebMCP is new, and most browsers don't have it yet. In one without it, registerTools does nothing, so calling it is safe everywhere. The panel above says what this browser has.

This page uses the bridge in agent.ts from Connect an agent.

Register the tools

Call registerTools once, in the browser, with the bridge and an allow:

agent-webmcp.ts
"use client";

import { registerTools } from "@ui-sdk/react/agent";

import { agent } from "./agent";

/**
 * Hands `snapshot`, `act` and `query` to the browser's own agent. Does nothing in a browser
 * without WebMCP. Returns the function that takes the tools back.
 */
export function registerCalendarTools(): () => void {
  return registerTools(agent, {
    // The browser's agent isn't one you wrote, so it gets an answer of its own for every step,
    // after the bridge's `allow`. Here it may navigate and edit, and never delete.
    allow: ({ action, component, confirm }) =>
      component === "calendar" &&
      confirm === undefined &&
      action !== "deleteEvent" &&
      action !== "setTimeZone",
  });
}

/** Whether this browser takes tools from a page. */
export function hasWebMcp(): boolean {
  const context = (globalThis.document as { modelContext?: { registerTool?: unknown } } | undefined)
    ?.modelContext;
  return typeof context?.registerTool === "function";
}

Call registerCalendarTools() when your app starts, or when the person turns the feature on. It returns the function that takes the tools back:

agent-webmcp-tools.tsx
"use client";

import { useEffect } from "react";

import { registerCalendarTools } from "./agent-webmcp";

/** Offers the tools to the browser's agent while this component is on the page. */
export function WebMcpTools() {
  // The function `registerCalendarTools` returns takes the tools back on unmount.
  useEffect(() => registerCalendarTools(), []);
  return null;
}

The browser's agent gets three tools:

ToolWhat it doesHints the browser gets
snapshotLists the page's components, your calendar among themreadOnlyHint, untrustedContentHint
queryFinds components, or the events one calendar showsreadOnlyHint, untrustedContentHint
actRuns steps, such as moveEventconsequentialHint, untrustedContentHint

The tools are the bridge's own, with the descriptions ui-sdk wrote for models. They aren't calendar-specific: the agent learns the calendar's actions from the snapshot, where the entry lists them by name.

The names are all the tools can tell it. act's definition doesn't say what moveEvent takes, and you can't add to a browser agent's prompt. The calendar says it in two places. Its describeCalendar action returns each action with its input. And an agent that guesses gets the input spelled out in the failed step, which is enough to correct the next call:

calendar-agent-invalid-arguments: moveEvent takes { eventId: ID, start: ISO date or date-time, end?: same, resourceIds?: ID[] }; it takes one object, not 2 arguments

An allow of its own

You didn't write the browser's agent, and it acts for whoever is using the browser. So registerTools takes an allow of its own, apart from the bridge's:

  1. The bridge's allow from createAgent, when it has one.
  2. The allow you pass to registerTools.
  3. Your policies, then onEventChange.

A step runs when all of them let it. The one above lets the browser's agent read, navigate and change events, and never delete or change the time zone. A refused step fails with The app refused setTimeZone on c1.

deleteEvent is on that list for the calendars that turn deleting on. On any other calendar the action doesn't exist, and the step fails before allow is asked.

confirm === undefined in the example leaves out any step on a control you marked with data-agent-confirm, which is ui-sdk's mark for a control a person must press. The calendar has no such controls of its own; the check keeps the rule for the other ui-sdk components on your page, which the same three tools reach.

Detect WebMCP

hasWebMcp() in the file above checks for the registration function, so you can show the feature only where it works:

typeof document.modelContext?.registerTool === "function";

TypeScript's DOM types don't have modelContext yet, which is why the helper reads it through a cast.

What the browser's agent reads

Everything the tools return is page content: event titles, labels, the calendar's title, and what listEvents and describeCalendar return. A title is text somebody typed, and it may say "ignore your instructions". The tools carry untrustedContentHint so the browser treats their results as data. Your part is the same as for any agent:

Try it

  1. In a browser with WebMCP, open your app and look for snapshot, query and act among the page's tools.
  2. Ask the agent to read first: "Which meetings are on the team calendar this week?"
  3. Then to change something: "Move the design review to Friday at 10."

Without such a browser, the example above registers the tools with a stand-in for document.modelContext and calls their execute functions the way a browser does. The bridge behind them is the real one, and so is the allow.

The ui-sdk documentation at ui-sdk.dev/docs has a walkthrough for a browser that supports WebMCP today.

Next, drive the calendar from a model on your own server.

On this page