Agent tools
WebMCP lets a page register tools that a browser agent calls directly, through document.modelContext, instead of finding and clicking the DOM. registerFormAgentTool from @schema-engine/forms/agent-tool registers a form as one: the tool's input is the exact set of values the form submits, and calling it fills the form and, when you allow it, submits it the way a person would.
It uses WebMCP's imperative API (document.modelContext.registerTool(tool, { signal })), not the declarative toolname attribute on a <form>. The declarative API derives a schema from native inputs, which fits a form of plain controls but not fields that depend on each other, custom controls that do not serialize themselves, or a wizard. The engine already knows all three.
Register a form
registerFormAgentTool(runtime, options) takes the runtime of the form that is on screen and returns the function that unregisters the tool. Register while the form is in use and unregister when it goes. You do not create the runtime: a renderer creates one for each form it draws and hands it to you, as the React and Radiant examples below show.
| Option | Meaning |
|---|---|
name | The tool's name. Unique on the page: the browser rejects a duplicate. |
description | What the tool does, for the agent. |
formatIssue | Optional. The form's own issue formatter, which the bindings give you. Without it the engine's, then an issue's message, then its type and params. |
autoSubmit | Optional, false by default. See When to submit. |
In a renderer
A renderer gives you the runtime of the form it draws, and the formatter of that form. Forms React and Forms Radiant have the setup the two examples below need.
React
useFormRuntime() from @schema-engine/forms-react/hooks gives the runtime of the form the component sits in, and useFormContext().formatIssue its formatter. Render the component as a child of FormRenderer.
import { useEffect } from 'react';
import { registerFormAgentTool } from '@schema-engine/forms/agent-tool';
import { FormRenderer } from '@schema-engine/forms-react';
import { useFormContext, useFormRuntime } from '@schema-engine/forms-react/hooks';
import { engine } from './react-widgets.react';
const feedback = engine.defineForm({
id: 'feedback',
steps: [
{
id: 'main',
title: 'Feedback',
elements: [
{ $type: 'text', name: 'name', label: 'Name', rules: [{ $type: 'required' }] },
{ $type: 'rating', name: 'stars', label: 'Rating', description: 'From 1 to 5' },
{ $type: 'submit', name: 'send', label: 'Send' },
],
},
],
});
/**
* Offers the form it sits in to a browser agent while it is rendered. Without `autoSubmit` the agent fills the form
* and the user sends it.
*/
function FeedbackAgentTool() {
const runtime = useFormRuntime();
const { formatIssue } = useFormContext();
useEffect(
() =>
registerFormAgentTool(runtime, {
name: 'send_feedback',
description: 'Fill in the feedback form: the name of the sender and a rating from 1 to 5.',
formatIssue,
}),
[runtime, formatIssue],
);
return null;
}
export function FeedbackForm({ send }: { send: (name: string, stars: number | undefined) => Promise<void> }) {
return (
<FormRenderer
engine={engine}
config={feedback}
onSubmit={async (values) => send(values.name, typeof values.stars === 'number' ? values.stars : undefined)}
>
<FeedbackAgentTool />
</FormRenderer>
);
}Radiant
FormController.watchRuntime calls you with the runtime of the form the element sits in, now and each time the form replaces it (a new config starts a new runtime), so release the previous tool first.
import * as z from 'zod/mini';
import { defineStatic } from '@schema-engine/forms';
import { registerFormAgentTool } from '@schema-engine/forms/agent-tool';
import { createRadiantFormEngine, FormController, schemaFormRegistrations } from '@schema-engine/forms-radiant';
/**
* Offers the form it sits in to a browser agent while it is connected.
*
* @remarks
* The controller finds the form through the host's ancestors and hands over its runtime now and each time the form
* replaces it, so the tool is registered again for a new config. Without `autoSubmit` the agent fills the form and the
* user sends it.
*/
class FeedbackAgentTool extends HTMLElement {
readonly form = new FormController(this);
private stop?: () => void;
private unregister?: () => void;
connectedCallback() {
this.stop = this.form.watchRuntime((runtime) => {
this.unregister?.();
this.unregister = runtime
? registerFormAgentTool(runtime, {
name: 'send_feedback',
description: 'Fill in the feedback form: the name of the sender and a comment.',
formatIssue: (issue, field) => this.form.formatIssue(issue, field),
})
: undefined;
});
}
disconnectedCallback() {
this.stop?.();
this.unregister?.();
}
}
customElements.define('feedback-agent-tool', FeedbackAgentTool);
const AgentToolRegistration = defineStatic({
$type: 'agentTool',
schema: z.looseObject({ $type: z.literal('agentTool') }),
metadata: { name: 'Agent tool', category: 'content' },
render: () => <feedback-agent-tool />,
});
export const engine = createRadiantFormEngine({ elements: [...schemaFormRegistrations, AgentToolRegistration] });
export const feedback = engine.defineForm({
id: 'feedback',
steps: [
{
id: 'main',
title: 'Feedback',
elements: [
{ $type: 'input', name: 'name', label: 'Name', rules: [{ $type: 'required' }] },
{ $type: 'input', name: 'comment', label: 'Comment', description: 'What went well or badly' },
{ $type: 'agentTool', id: 'agent-tool' },
{ $type: 'submit', name: 'send', label: 'Send' },
],
},
],
});Feature detection
registerFormAgentTool does nothing and does not throw on the server and in a browser without document.modelContext: it returns a cleanup that does nothing. Call it unconditionally. When you need to know whether an agent can see the tool, for example to show a hint, check the browser yourself:
const agentsAvailable = typeof document !== 'undefined' && 'modelContext' in document;WebMCP is behind an Origin Trial in Chrome, so most browsers have no document.modelContext today. The browser may also reject a registration after the call returns (a duplicate name, or a page where the tools permission policy is denied). The rejection is logged, and the cleanup stays safe to call.
What the tool takes
The input schema is the JSON Schema of engine.submissionSchema(config), so it has the keys, types, enums and required keys the form submits, and each parameter is described by its field's label and description (its help text). A value schema with no JSON Schema makes the registration throw, naming the field.
When the agent calls the tool:
- The input is checked against the same schema, with every key optional. A value of the wrong type is refused with a list of the keys, and nothing is written.
- The values are written into the form's container, as a person's edits arrive, so the controls show them.
- With
autoSubmit, the runtime submits: the same validation, constraints, value check, actions andonSubmitas a submit by a person.
A refused submission returns its issues as text, one line per field, worded by the form's formatter, so the agent can correct them and call again. An agent never sees a rule type unless nothing else says what is wrong.
A wizard is filled across the steps the answers make active. With autoSubmit, the tool submits again after each step it moves past and the runtime validates the steps in order. Without it, the tool fills what it can and the user moves through the steps. The values of a skipped step are not submitted. If a step refuses, the form stays on it and the agent gets that step's issues.
If the agent cancels the call (its signal aborts) before the tool writes, the form is untouched. If it aborts while the form is being submitted, the values and the step the user had are put back, unless the form was already submitted. Errors that the check recorded stay.
When to submit
By default the tool only fills the form and answers that it waits for the user to review and submit it, so a person confirms. That is the setting to start with, and the right one for anything that spends money, sends a message, deletes or changes an account, or cannot be undone.
Set autoSubmit: true only for a form that reads or changes something low risk and reversible: a search, a filter, a draft. An agent that reads a page can be steered by what it reads, so do not let one submit what you would not let an arbitrary visitor submit. The submission still goes through the same validation and onSubmit, but nothing in them asks a person.
The option is plain data that you keep next to your config. It is not a key of the form config, so the config schema is unchanged.
Related
- Form state: the container the tool writes to.
- Wizard: the steps the tool fills.
- Issue text: the formatter that words what an agent reads.
- Other React state: hosting a form in a panel that holds other state.