By
The Declarative API: Turning Forms into Tools
Most of what users do on a website already goes through forms: search, sign up, book, contact support. The declarative API lets you expose those forms as tools by adding a few attributes. The browser reads the form’s fields and builds the tool’s input schema for you.
The declarative API is described in the WebMCP explainer and Chrome’s documentation, but its spec text is not written yet. Expect attribute names and behavior to change more than in the imperative API.
The Minimum: Two Attributes
<form action="/search" toolname="search_products"
tooldescription="Search the store catalogue by keyword and show matching products.">
<label for="q">Search</label>
<input id="q" name="q" type="search" required />
<label for="category">Category</label>
<select id="category" name="category">
<option value="">Any</option>
<option value="dairy">Dairy & alternatives</option>
<option value="bakery">Bakery</option>
</select>
<button type="submit">Search</button>
</form>toolname— the tool’s name (same naming rules as the imperative API).tooldescription— what the tool does, written for the agent.
Removing either attribute unregisters the tool. Because the tool is the form, it exists only while the form is in the DOM — client-side routing that removes the form also removes the tool.
The browser derives the input schema from the form’s controls: name becomes the property name, required marks it as required, input types and <select> options constrain the values.
Describing Fields
Each property needs a description. The browser uses, in order:
- The
toolparamdescriptionattribute, if present - The field’s
<label>text - Its
aria-description
Labels written for humans are often too short for an agent. Add toolparamdescription where the label leaves things out:
<label for="when">Delivery slot</label>
<input id="when" name="slot" type="datetime-local" required
toolparamdescription="Delivery start time in the store's local time. Slots begin on the hour, 08:00–20:00." />If your forms already have proper labels, you get reasonable descriptions for free. That is one more reason to keep forms accessible.
What Happens When an Agent Calls the Tool
By default, an agent call fills the form but does not submit it:
- The agent calls
search_productswith{ "q": "oat milk", "category": "dairy" }. - The browser fills the fields and fires
toolactivatedondocument.modelContext. - The form is highlighted (see CSS hooks below), and the user reviews and clicks Submit.
This keeps the human in control, which is the right default for anything that has side effects.
For safe, read-only forms like search, add toolautosubmit so the agent’s call submits straight away:
<form action="/search" toolname="search_products" toolautosubmit
tooldescription="Search the store catalogue by keyword and show matching products.">
...
</form>Don’t put toolautosubmit on forms that place orders, send messages or change account data.
Returning a Result to the Agent
A plain form submission navigates to a new page; the agent then has to read that page to learn what happened. You can return a result directly instead, from the submit event:
const form = document.querySelector('form[toolname="search_products"]')
form.addEventListener('submit', (event) => {
if (!event.agentInvoked) return // a human submitted: keep normal behavior
event.preventDefault()
event.respondWith(
(async () => {
const data = new FormData(form)
const results = await api.searchProducts({
query: data.get('q'),
category: data.get('category') || undefined,
})
renderResults(results) // update the UI the user is looking at
return results.slice(0, 10).map(({ id, name, price }) => ({ id, name, price }))
})(),
)
})event.agentInvokedistruewhen the submission came from an agent.event.respondWith(promise)sends the promise’s value back to the agent as the tool result.- You must call
preventDefault()beforerespondWith().
This is also where you add agent-specific handling — for example, logging which submissions came from agents, or applying stricter validation.
Note that respondWith works the same whether the user clicked Submit after reviewing or the form was auto-submitted.
Events
Two events on document.modelContext let you react to agent activity:
document.modelContext.addEventListener('toolactivated', ({ toolName }) => {
// Agent filled a declarative form; e.g. scroll it into view or open its panel
document.querySelector(`form[toolname="${toolName}"]`)?.scrollIntoView({ block: 'center' })
})
document.modelContext.addEventListener('toolcancel', ({ toolName }) => {
// User cancelled the agent action, or form.reset() was called
clearPendingState(toolName)
})Both events cannot be cancelled and carry a toolName. Listen on document.modelContext; the old window.ontoolactivated / window.ontoolcancel handlers are not supported from Chrome 156.
Styling Agent-Driven Forms
When an agent fills a form, the user should see it. Two pseudo-classes apply while a tool call is in progress:
:tool-form-active— on the<form>:tool-submit-active— on the form’s submit button
Chrome adds a default outline. Customize it to match your design:
form:tool-form-active {
outline: 2px solid var(--accent);
outline-offset: 4px;
border-radius: 8px;
}
button:tool-submit-active {
box-shadow: 0 0 0 3px color-mix(in srgb, var(--accent) 40%, transparent);
animation: pulse 1.2s ease-in-out infinite;
}
@media (prefers-reduced-motion: reduce) {
button:tool-submit-active { animation: none; }
}Both stop applying when the form is submitted, cancelled or reset.
Declarative or Imperative?
| Use declarative when | Use imperative when |
|---|---|
| A form already performs the action | The action lives in JavaScript (cart store, editor, map) |
| You want the user to review before submitting | You need to return structured data without a form |
| You want minimal code | The tool should exist based on app state, not DOM |
| Server-rendered pages, little JavaScript | You need full control over schema, annotations and output |
They mix freely on the same page. A common setup: declarative search and contact forms, imperative tools for the cart and anything stateful.
Summary
toolname+tooldescriptionturn a form into a tool; the browser builds the schema from its fields.- Field descriptions come from
toolparamdescription, then<label>, thenaria-description. - By default the agent fills the form and the user submits;
toolautosubmitis for safe, read-only forms. event.agentInvokedandevent.respondWith()let you return a result instead of navigating.toolactivated,toolcancel,:tool-form-activeand:tool-submit-activekeep the user aware of what the agent is doing.
Next, Part 4 covers what it takes to ship WebMCP: tool design, security and testing.
— 3 —