Agent tools
A cart that the person and a browser agent both add to, through the same method, with the agent's tool present exactly while the cart is on the page.
Experimental. WebMCP is an origin trial and may change, and the helpers here follow it.
Goal
A page that lets an agent do what a person does: add a product to the cart, close a dialog, filter a list. The agent calls a tool the page registered, instead of finding and clicking the DOM. The tool belongs to the component that owns the state, so it appears when the component connects and goes when it is removed, and the form and the tool call the same method.
Primitives
@agentToolturns a method into a tool. The method stays an ordinary method, so the form submit callsaddItemwith a typed argument and the agent calls the same method. The schema guards only the agent's path, since the form passes a typed value straight in, so the field carriesrequired.registerAgentTool, from@ecopages/radiant/helpers/register-agent-tool, registers a tool without a host and returns the function that removes it. The plain form uses it.createAgentTool(host, config), from@ecopages/radiant/helpers/create-agent-tool, ties a tool to an element's connect and disconnect. The decorator is built from it.- Static JSON Schema describes the input.
inputSchemais a plain object the browser shows the agent, so no schema library loads on the page. Here it isaddItemSchemainagent-tools.data.ts, kept in step with a Zod schema by a test. An app with a build step writes it withz.toJSONSchema, and validates on the server the tool calls.
Left to the recipe: the schema, what the method does, and what it returns. The tool name is unique on the page, so a component that can appear twice builds the name from an instance value such as its id. Type the host parameter, because the decorator is written before TypeScript knows the class:
@agentTool({
name: (host: AgentToolsElement) => `add_to_cart_${host.id}`,
description: 'Adds a product to the cart by its SKU.',
})
addItem(item: { sku: string }) {}Example
Add a product with the form, or call the tool from the inspector below the example, which stands in for an agent. The inspector lists the page's tools and calls one with the JSON in its box. A call that fails parse is rejected before the method runs, so the cart does not change and the agent receives an error. This page uses a small stand-in for document.modelContext where the browser has none, and reads the tools back from the browser's WebMCP testing surface where it has its own and offers one; the recipe code never touches either.
Pick an authoring pattern below; they behave the same, and the one you pick is remembered on the other recipes.
The same behavior as a RadiantElement. The markup is authored inside the element, and @agentTool on addItem registers the tool when the element connects and removes it when the element disconnects.
0 in cart
import { RadiantElement, customElement, onEvent, query } from '@ecopages/radiant';
import { agentTool } from '@ecopages/radiant/decorators/agent-tool';
import { addItemSchema, parseAddItem, type AddItem } from './agent-tools.data';
@customElement('agent-tools-element')
export class AgentToolsElement extends RadiantElement {
@query('input') private input!: HTMLInputElement;
@query('ul') private list!: HTMLElement;
@query('[data-status]') private status!: HTMLElement;
@onEvent({ selector: 'form', type: 'submit' })
submit(event: SubmitEvent): void {
event.preventDefault();
this.addItem({ sku: this.input.value });
}
@agentTool({
name: 'add_to_cart',
description: 'Adds a product to the cart by its SKU.',
annotations: { consequentialHint: false },
inputSchema: addItemSchema,
parse: parseAddItem,
})
addItem({ sku }: AddItem) {
const item = document.createElement('li');
item.textContent = sku;
this.list.append(item);
const count = this.list.children.length;
this.status.textContent = `${count} in cart`;
return { count };
}
}/** What the `add_to_cart` tool takes. */
export type AddItem = { sku: string };
/**
* The input schema the browser shows the agent: static JSON Schema, so no schema library loads on the page.
*
* @remarks
* `agent-tools-data.test.ts` fails when it drifts from the Zod schema it mirrors. In an app with a build step, write
* this file from `z.toJSONSchema` instead of by hand.
*/
export const addItemSchema = {
type: 'object',
properties: { sku: { type: 'string', minLength: 1, description: 'The product to add, by its SKU' } },
required: ['sku'],
additionalProperties: false,
};
/**
* Rejects an input that is not an `AddItem`, before the method runs.
*
* @remarks
* The recipe has no server to validate on, so the browser does. An app with a server validates there and leaves
* `parse` out.
*/
export function parseAddItem(input: unknown): AddItem {
if (
typeof input !== 'object' ||
input === null ||
!('sku' in input) ||
typeof input.sku !== 'string' ||
!input.sku
) {
throw new TypeError('sku must be a non-empty string');
}
return { sku: input.sku };
}<agent-tools-element>
<p data-status aria-live="polite">0 in cart</p>
<form>
<label for="cart-sku">Product</label>
<input id="cart-sku" name="sku" value="apple" required />
<button type="submit">Add to cart</button>
</form>
<ul aria-label="Cart"></ul>
</agent-tools-element>:is(agent-tools-element, div[data-example='agent-tools']:not(.primitive-example)) {
display: flex;
flex-direction: column;
align-items: stretch;
gap: var(--space-stack);
box-sizing: border-box;
width: min(100%, 24rem);
padding: var(--space-inset);
border: 1px solid var(--border);
border-radius: var(--radius-container);
background: var(--surface);
color: var(--on-surface);
box-shadow: var(--shadow-overlay);
}
:is(agent-tools-element, div[data-example='agent-tools']:not(.primitive-example)) [data-status] {
align-self: flex-start;
margin: 0;
padding: 0.25rem 0.75rem;
border-radius: 999px;
background: var(--primary-container);
color: var(--on-primary-container);
font-size: 0.8125rem;
font-weight: 600;
font-variant-numeric: tabular-nums;
}
:is(agent-tools-element, div[data-example='agent-tools']:not(.primitive-example)) form {
display: grid;
grid-template-columns: minmax(0, 1fr) auto;
column-gap: var(--space-stack);
row-gap: 0.375rem;
align-items: end;
}
:is(agent-tools-element, div[data-example='agent-tools']:not(.primitive-example)) :where(label) {
grid-column: 1 / -1;
font-size: 0.75rem;
font-weight: 600;
letter-spacing: 0.04em;
text-transform: uppercase;
opacity: 0.7;
}
:is(agent-tools-element, div[data-example='agent-tools']:not(.primitive-example)) input {
box-sizing: border-box;
width: 100%;
min-height: var(--size-control-lg);
padding: var(--space-control-y) var(--space-control-x);
border: 1px solid var(--border);
border-radius: var(--radius-control);
background: var(--surface-container-low);
color: inherit;
font: inherit;
}
:is(agent-tools-element, div[data-example='agent-tools']:not(.primitive-example)) button {
min-height: var(--size-control-lg);
padding: var(--space-control-y) var(--space-control-x);
border: 1px solid transparent;
border-radius: var(--radius-control);
background: var(--primary);
color: var(--on-primary);
font: inherit;
font-weight: 600;
cursor: pointer;
}
:is(agent-tools-element, div[data-example='agent-tools']:not(.primitive-example)) :where(input, button):focus-visible {
outline: 2px solid var(--focus-ring);
outline-offset: 1px;
}
:is(agent-tools-element, div[data-example='agent-tools']:not(.primitive-example)) ul {
display: grid;
gap: 0.5rem;
margin: 0;
padding: 0;
list-style: none;
}
:is(agent-tools-element, div[data-example='agent-tools']:not(.primitive-example)) li {
padding: 0.5rem 0.75rem;
border: 1px solid var(--border);
border-radius: var(--radius-control);
background: var(--surface-container-low);
font-size: 0.875rem;
font-weight: 500;
}
:is(agent-tools-element, div[data-example='agent-tools']:not(.primitive-example)) ul:empty::before {
content: 'Nothing in the cart yet';
display: block;
padding: 0.75rem;
border: 1px dashed var(--border);
border-radius: var(--radius-control);
font-size: 0.875rem;
text-align: center;
opacity: 0.6;
}
tool-inspector {
display: grid;
gap: var(--space-stack);
box-sizing: border-box;
width: min(100%, 24rem);
margin-block-start: var(--space-stack);
padding: var(--space-inset);
border: 1px dashed var(--border);
border-radius: var(--radius-container);
}
tool-inspector::before {
content: 'What an agent sees';
font-size: 0.75rem;
font-weight: 600;
letter-spacing: 0.04em;
text-transform: uppercase;
opacity: 0.7;
}
tool-inspector :where(ul) {
display: grid;
gap: 0.5rem;
margin: 0;
padding: 0;
list-style: none;
}
tool-inspector :where(li.empty) {
padding: 0.75rem;
border: 1px dashed var(--border);
border-radius: var(--radius-control);
font-size: 0.875rem;
text-align: center;
opacity: 0.6;
}
tool-inspector :where(button) {
box-sizing: border-box;
width: 100%;
min-height: var(--size-control-lg);
padding: var(--space-control-y) var(--space-control-x);
border: 1px solid var(--border);
border-radius: var(--radius-control);
background: var(--surface-container);
color: inherit;
font: inherit;
font-family: var(--font-mono, monospace);
font-size: 0.8125rem;
font-weight: 600;
text-align: start;
cursor: pointer;
}
tool-inspector :where(textarea) {
box-sizing: border-box;
width: 100%;
padding: var(--space-control-y) var(--space-control-x);
border: 1px solid var(--border);
border-radius: var(--radius-control);
background: var(--surface-container-low);
color: inherit;
font: inherit;
font-family: var(--font-mono, monospace);
font-size: 0.8125rem;
resize: vertical;
}
tool-inspector :where(button, textarea):focus-visible {
outline: 2px solid var(--focus-ring);
outline-offset: 1px;
}
tool-inspector [data-result] {
margin: 0;
padding: 0.5rem 0.75rem;
border-radius: var(--radius-control);
background: var(--surface-container-low);
font-family: var(--font-mono, monospace);
font-size: 0.8125rem;
overflow-wrap: anywhere;
}
tool-inspector [data-result]:empty {
display: none;
}Remove the element and the tool is gone, move it and the tool is registered again, and a client-side navigation that adds a copy starts it without any call. addItemSchema is the inputSchema the agent reads, parseAddItem runs on the agent's input before addItem, and AddItem types the method's argument for the form too. The recipe has no server, so a small hand-written parse does the checking in the browser; with a server, the endpoint validates and parse goes.
The same behavior as a RadiantElement. The markup is authored inside the element, and @agentTool on addItem registers the tool when the element connects and removes it when the element disconnects.
0 in cart
import { RadiantElement, customElement, onEvent, query } from '@ecopages/radiant';
import { agentTool } from '@ecopages/radiant/decorators/agent-tool';
import { addItemSchema, parseAddItem, type AddItem } from './agent-tools.data';
@customElement('agent-tools-element')
export class AgentToolsElement extends RadiantElement {
@query('input') private input!: HTMLInputElement;
@query('ul') private list!: HTMLElement;
@query('[data-status]') private status!: HTMLElement;
@onEvent({ selector: 'form', type: 'submit' })
submit(event: SubmitEvent): void {
event.preventDefault();
this.addItem({ sku: this.input.value });
}
@agentTool({
name: 'add_to_cart',
description: 'Adds a product to the cart by its SKU.',
annotations: { consequentialHint: false },
inputSchema: addItemSchema,
parse: parseAddItem,
})
addItem({ sku }: AddItem) {
const item = document.createElement('li');
item.textContent = sku;
this.list.append(item);
const count = this.list.children.length;
this.status.textContent = `${count} in cart`;
return { count };
}
}/** What the `add_to_cart` tool takes. */
export type AddItem = { sku: string };
/**
* The input schema the browser shows the agent: static JSON Schema, so no schema library loads on the page.
*
* @remarks
* `agent-tools-data.test.ts` fails when it drifts from the Zod schema it mirrors. In an app with a build step, write
* this file from `z.toJSONSchema` instead of by hand.
*/
export const addItemSchema = {
type: 'object',
properties: { sku: { type: 'string', minLength: 1, description: 'The product to add, by its SKU' } },
required: ['sku'],
additionalProperties: false,
};
/**
* Rejects an input that is not an `AddItem`, before the method runs.
*
* @remarks
* The recipe has no server to validate on, so the browser does. An app with a server validates there and leaves
* `parse` out.
*/
export function parseAddItem(input: unknown): AddItem {
if (
typeof input !== 'object' ||
input === null ||
!('sku' in input) ||
typeof input.sku !== 'string' ||
!input.sku
) {
throw new TypeError('sku must be a non-empty string');
}
return { sku: input.sku };
}<agent-tools-element>
<p data-status aria-live="polite">0 in cart</p>
<form>
<label for="cart-sku">Product</label>
<input id="cart-sku" name="sku" value="apple" required />
<button type="submit">Add to cart</button>
</form>
<ul aria-label="Cart"></ul>
</agent-tools-element>:is(agent-tools-element, div[data-example='agent-tools']:not(.primitive-example)) {
display: flex;
flex-direction: column;
align-items: stretch;
gap: var(--space-stack);
box-sizing: border-box;
width: min(100%, 24rem);
padding: var(--space-inset);
border: 1px solid var(--border);
border-radius: var(--radius-container);
background: var(--surface);
color: var(--on-surface);
box-shadow: var(--shadow-overlay);
}
:is(agent-tools-element, div[data-example='agent-tools']:not(.primitive-example)) [data-status] {
align-self: flex-start;
margin: 0;
padding: 0.25rem 0.75rem;
border-radius: 999px;
background: var(--primary-container);
color: var(--on-primary-container);
font-size: 0.8125rem;
font-weight: 600;
font-variant-numeric: tabular-nums;
}
:is(agent-tools-element, div[data-example='agent-tools']:not(.primitive-example)) form {
display: grid;
grid-template-columns: minmax(0, 1fr) auto;
column-gap: var(--space-stack);
row-gap: 0.375rem;
align-items: end;
}
:is(agent-tools-element, div[data-example='agent-tools']:not(.primitive-example)) :where(label) {
grid-column: 1 / -1;
font-size: 0.75rem;
font-weight: 600;
letter-spacing: 0.04em;
text-transform: uppercase;
opacity: 0.7;
}
:is(agent-tools-element, div[data-example='agent-tools']:not(.primitive-example)) input {
box-sizing: border-box;
width: 100%;
min-height: var(--size-control-lg);
padding: var(--space-control-y) var(--space-control-x);
border: 1px solid var(--border);
border-radius: var(--radius-control);
background: var(--surface-container-low);
color: inherit;
font: inherit;
}
:is(agent-tools-element, div[data-example='agent-tools']:not(.primitive-example)) button {
min-height: var(--size-control-lg);
padding: var(--space-control-y) var(--space-control-x);
border: 1px solid transparent;
border-radius: var(--radius-control);
background: var(--primary);
color: var(--on-primary);
font: inherit;
font-weight: 600;
cursor: pointer;
}
:is(agent-tools-element, div[data-example='agent-tools']:not(.primitive-example)) :where(input, button):focus-visible {
outline: 2px solid var(--focus-ring);
outline-offset: 1px;
}
:is(agent-tools-element, div[data-example='agent-tools']:not(.primitive-example)) ul {
display: grid;
gap: 0.5rem;
margin: 0;
padding: 0;
list-style: none;
}
:is(agent-tools-element, div[data-example='agent-tools']:not(.primitive-example)) li {
padding: 0.5rem 0.75rem;
border: 1px solid var(--border);
border-radius: var(--radius-control);
background: var(--surface-container-low);
font-size: 0.875rem;
font-weight: 500;
}
:is(agent-tools-element, div[data-example='agent-tools']:not(.primitive-example)) ul:empty::before {
content: 'Nothing in the cart yet';
display: block;
padding: 0.75rem;
border: 1px dashed var(--border);
border-radius: var(--radius-control);
font-size: 0.875rem;
text-align: center;
opacity: 0.6;
}
tool-inspector {
display: grid;
gap: var(--space-stack);
box-sizing: border-box;
width: min(100%, 24rem);
margin-block-start: var(--space-stack);
padding: var(--space-inset);
border: 1px dashed var(--border);
border-radius: var(--radius-container);
}
tool-inspector::before {
content: 'What an agent sees';
font-size: 0.75rem;
font-weight: 600;
letter-spacing: 0.04em;
text-transform: uppercase;
opacity: 0.7;
}
tool-inspector :where(ul) {
display: grid;
gap: 0.5rem;
margin: 0;
padding: 0;
list-style: none;
}
tool-inspector :where(li.empty) {
padding: 0.75rem;
border: 1px dashed var(--border);
border-radius: var(--radius-control);
font-size: 0.875rem;
text-align: center;
opacity: 0.6;
}
tool-inspector :where(button) {
box-sizing: border-box;
width: 100%;
min-height: var(--size-control-lg);
padding: var(--space-control-y) var(--space-control-x);
border: 1px solid var(--border);
border-radius: var(--radius-control);
background: var(--surface-container);
color: inherit;
font: inherit;
font-family: var(--font-mono, monospace);
font-size: 0.8125rem;
font-weight: 600;
text-align: start;
cursor: pointer;
}
tool-inspector :where(textarea) {
box-sizing: border-box;
width: 100%;
padding: var(--space-control-y) var(--space-control-x);
border: 1px solid var(--border);
border-radius: var(--radius-control);
background: var(--surface-container-low);
color: inherit;
font: inherit;
font-family: var(--font-mono, monospace);
font-size: 0.8125rem;
resize: vertical;
}
tool-inspector :where(button, textarea):focus-visible {
outline: 2px solid var(--focus-ring);
outline-offset: 1px;
}
tool-inspector [data-result] {
margin: 0;
padding: 0.5rem 0.75rem;
border-radius: var(--radius-control);
background: var(--surface-container-low);
font-family: var(--font-mono, monospace);
font-size: 0.8125rem;
overflow-wrap: anywhere;
}
tool-inspector [data-result]:empty {
display: none;
}Remove the element and the tool is gone, move it and the tool is registered again, and a client-side navigation that adds a copy starts it without any call. addItemSchema is the inputSchema the agent reads, parseAddItem runs on the agent's input before addItem, and AddItem types the method's argument for the form too. The recipe has no server, so a small hand-written parse does the checking in the browser; with a server, the endpoint validates and parse goes.
A mount function registers the tool and returns the cleanup that removes it.
0 in cart
import { registerAgentTool } from '@ecopages/radiant/helpers/register-agent-tool';
import { addItemSchema, parseAddItem, type AddItem } from './agent-tools.data';
export function mount(root: HTMLElement): () => void {
const form = root.querySelector('form')!;
const input = root.querySelector('input')!;
const list = root.querySelector('ul')!;
const status = root.querySelector('[data-status]')!;
const addItem = ({ sku }: AddItem) => {
const item = document.createElement('li');
item.textContent = sku;
list.append(item);
const count = list.children.length;
status.textContent = `${count} in cart`;
return { count };
};
const unregister = registerAgentTool({
name: 'add_to_cart',
description: 'Adds a product to the cart by its SKU.',
annotations: { consequentialHint: false },
inputSchema: addItemSchema,
parse: parseAddItem,
execute: addItem,
});
const submit = (event: SubmitEvent) => {
event.preventDefault();
addItem({ sku: input.value });
};
form.addEventListener('submit', submit);
return () => {
form.removeEventListener('submit', submit);
unregister();
};
}/** What the `add_to_cart` tool takes. */
export type AddItem = { sku: string };
/**
* The input schema the browser shows the agent: static JSON Schema, so no schema library loads on the page.
*
* @remarks
* `agent-tools-data.test.ts` fails when it drifts from the Zod schema it mirrors. In an app with a build step, write
* this file from `z.toJSONSchema` instead of by hand.
*/
export const addItemSchema = {
type: 'object',
properties: { sku: { type: 'string', minLength: 1, description: 'The product to add, by its SKU' } },
required: ['sku'],
additionalProperties: false,
};
/**
* Rejects an input that is not an `AddItem`, before the method runs.
*
* @remarks
* The recipe has no server to validate on, so the browser does. An app with a server validates there and leaves
* `parse` out.
*/
export function parseAddItem(input: unknown): AddItem {
if (
typeof input !== 'object' ||
input === null ||
!('sku' in input) ||
typeof input.sku !== 'string' ||
!input.sku
) {
throw new TypeError('sku must be a non-empty string');
}
return { sku: input.sku };
}<div data-example="agent-tools">
<p data-status aria-live="polite">0 in cart</p>
<form>
<label for="cart-sku">Product</label>
<input id="cart-sku" name="sku" value="apple" required />
<button type="submit">Add to cart</button>
</form>
<ul aria-label="Cart"></ul>
</div>:is(agent-tools-element, div[data-example='agent-tools']:not(.primitive-example)) {
display: flex;
flex-direction: column;
align-items: stretch;
gap: var(--space-stack);
box-sizing: border-box;
width: min(100%, 24rem);
padding: var(--space-inset);
border: 1px solid var(--border);
border-radius: var(--radius-container);
background: var(--surface);
color: var(--on-surface);
box-shadow: var(--shadow-overlay);
}
:is(agent-tools-element, div[data-example='agent-tools']:not(.primitive-example)) [data-status] {
align-self: flex-start;
margin: 0;
padding: 0.25rem 0.75rem;
border-radius: 999px;
background: var(--primary-container);
color: var(--on-primary-container);
font-size: 0.8125rem;
font-weight: 600;
font-variant-numeric: tabular-nums;
}
:is(agent-tools-element, div[data-example='agent-tools']:not(.primitive-example)) form {
display: grid;
grid-template-columns: minmax(0, 1fr) auto;
column-gap: var(--space-stack);
row-gap: 0.375rem;
align-items: end;
}
:is(agent-tools-element, div[data-example='agent-tools']:not(.primitive-example)) :where(label) {
grid-column: 1 / -1;
font-size: 0.75rem;
font-weight: 600;
letter-spacing: 0.04em;
text-transform: uppercase;
opacity: 0.7;
}
:is(agent-tools-element, div[data-example='agent-tools']:not(.primitive-example)) input {
box-sizing: border-box;
width: 100%;
min-height: var(--size-control-lg);
padding: var(--space-control-y) var(--space-control-x);
border: 1px solid var(--border);
border-radius: var(--radius-control);
background: var(--surface-container-low);
color: inherit;
font: inherit;
}
:is(agent-tools-element, div[data-example='agent-tools']:not(.primitive-example)) button {
min-height: var(--size-control-lg);
padding: var(--space-control-y) var(--space-control-x);
border: 1px solid transparent;
border-radius: var(--radius-control);
background: var(--primary);
color: var(--on-primary);
font: inherit;
font-weight: 600;
cursor: pointer;
}
:is(agent-tools-element, div[data-example='agent-tools']:not(.primitive-example)) :where(input, button):focus-visible {
outline: 2px solid var(--focus-ring);
outline-offset: 1px;
}
:is(agent-tools-element, div[data-example='agent-tools']:not(.primitive-example)) ul {
display: grid;
gap: 0.5rem;
margin: 0;
padding: 0;
list-style: none;
}
:is(agent-tools-element, div[data-example='agent-tools']:not(.primitive-example)) li {
padding: 0.5rem 0.75rem;
border: 1px solid var(--border);
border-radius: var(--radius-control);
background: var(--surface-container-low);
font-size: 0.875rem;
font-weight: 500;
}
:is(agent-tools-element, div[data-example='agent-tools']:not(.primitive-example)) ul:empty::before {
content: 'Nothing in the cart yet';
display: block;
padding: 0.75rem;
border: 1px dashed var(--border);
border-radius: var(--radius-control);
font-size: 0.875rem;
text-align: center;
opacity: 0.6;
}
tool-inspector {
display: grid;
gap: var(--space-stack);
box-sizing: border-box;
width: min(100%, 24rem);
margin-block-start: var(--space-stack);
padding: var(--space-inset);
border: 1px dashed var(--border);
border-radius: var(--radius-container);
}
tool-inspector::before {
content: 'What an agent sees';
font-size: 0.75rem;
font-weight: 600;
letter-spacing: 0.04em;
text-transform: uppercase;
opacity: 0.7;
}
tool-inspector :where(ul) {
display: grid;
gap: 0.5rem;
margin: 0;
padding: 0;
list-style: none;
}
tool-inspector :where(li.empty) {
padding: 0.75rem;
border: 1px dashed var(--border);
border-radius: var(--radius-control);
font-size: 0.875rem;
text-align: center;
opacity: 0.6;
}
tool-inspector :where(button) {
box-sizing: border-box;
width: 100%;
min-height: var(--size-control-lg);
padding: var(--space-control-y) var(--space-control-x);
border: 1px solid var(--border);
border-radius: var(--radius-control);
background: var(--surface-container);
color: inherit;
font: inherit;
font-family: var(--font-mono, monospace);
font-size: 0.8125rem;
font-weight: 600;
text-align: start;
cursor: pointer;
}
tool-inspector :where(textarea) {
box-sizing: border-box;
width: 100%;
padding: var(--space-control-y) var(--space-control-x);
border: 1px solid var(--border);
border-radius: var(--radius-control);
background: var(--surface-container-low);
color: inherit;
font: inherit;
font-family: var(--font-mono, monospace);
font-size: 0.8125rem;
resize: vertical;
}
tool-inspector :where(button, textarea):focus-visible {
outline: 2px solid var(--focus-ring);
outline-offset: 1px;
}
tool-inspector [data-result] {
margin: 0;
padding: 0.5rem 0.75rem;
border-radius: var(--radius-control);
background: var(--surface-container-low);
font-family: var(--font-mono, monospace);
font-size: 0.8125rem;
overflow-wrap: anywhere;
}
tool-inspector [data-result]:empty {
display: none;
}registerAgentTool returns the function that removes the tool, so the cleanup mount returns calls it with the form's listener removal. mount runs once for each example element on the page; see how the examples run.
Styles
The stylesheet is the agent-tools.css tab in the files above, and styles the element and the plain div alike, each as the one root its frame holds. The inspector is docs-only and is not part of the recipe.
What to watch for
- Unique names. A second registration with the same name is rejected by the browser, and Radiant reports it with the tool and the element. Two UIs that do the same thing use two names, or one owner registers the tool once.
- Validate in code.
inputSchemais advisory, so an agent can send anything. Check the input where the tool executes: on the server when there is one, or inparsewhen the browser must reject a call before the method runs. A method with neither receives the input as sent. - Return when the page is true. Update the UI before the method returns, and return a value that says what happened, because the agent reads it.
- Embedded pages. A tool registered inside a cross-origin iframe needs
allow="tools"on the frame. - Server-rendered elements. An element whose children come from its parent's view registers its tool only after it hydrates, and never when the parent does not render on the client.
Keeping data safe
A tool runs page code for an agent, and what it returns can leave the device. Radiant has no tool that reads the page, so a tool exposes only what you return from it. The full list is in the package README; the ones this recipe shows:
- Say what it changes. The tool sets
annotations: { consequentialHint: false }: adding to a cart is easy to undo. An action that is hard to undo setsconsequentialHint: true, and one that changes nothing setsreadOnlyHint: true. In development a tool with neither hint logs a warning. - Return the least.
{ count }is all the agent needs. Never return tokens, secrets or the contents of password, card or one-time-code fields, and setuntrustedContentHint: trueon output that carries text a user or another site wrote, because an agent may treat tool output as trusted. - Ask for the least. The schema asks for a SKU and nothing about the person.
- Confirm in your own UI. The current WebMCP spec has no confirmation API. A consequential tool awaits the app's own dialog inside
executeand closes it when thesignalaborts. - Authorize on the server. The agent can skip the checks the UI does, so a real cart checks permission on the server and binds a consequential call to an id or version, such as a cart revision that the server rejects when it is out of date.
- Keep it on this origin. Tools are visible only to the page's own origin.
exposedTolets other origins call them with arguments you cannot trust, and logs a warning in development.