WebMCP 如何把 HTML 表單轉為 AI 智能體可呼叫的工具
WebMCP: your HTML forms become tools for AI agents
WebMCP 的宣告式做法可透過 HTML 表單上的兩個屬性,讓瀏覽器把現有表單轉成 AI 智能體可呼叫的工具。toolname 和 tooldescription 用來註冊工具,有 name 屬性的表單控制項會成為輸入欄位,required 會列入必填項目;預設仍由用戶按下提交,加入 toolautosubmit 後 Agent 可自行提交。
This note has an interactive demo. Try it in the original
Two attributes on a <form> are enough for an AI agent in the browser to stop squinting at your page and call it like a function. That's the declarative side of WebMCP, which Chrome is testing in an origin trial since Chrome 149, and it's the most down-to-earth way I've seen to make a site usable by agents.
Most of what I've written here about AI has been about being found: llms.txt, robots.txt per bot, what crawlers actually read. WebMCP is the next step: being used. Today, when an agent books something for you, it typically works by actuation, as Chrome's docs call it: taking screenshots, finding fields, clicking and typing as if it were a person, with every step open to interpretation. WebMCP lets the page declare instead: "this is a tool, this is what it does, these are its inputs".
What it is
WebMCP is a proposed web standard with two flavours. The imperative one is JavaScript: document.modelContext.registerTool() with a name, a description, a JSON Schema and an execute function. The declarative one, today's topic, needs no new JavaScript at all: you annotate a form you already have, and the browser builds the tool from it.
The demo above does exactly that. On the left there's a normal form; on the right, the tool definition an agent would receive. Toggle the attributes and watch the schema change, then press "Run the agent".
How it works
This is the form in the demo, cut down to the essentials:
<form toolname="book_workshop_seat"
tooldescription="Books a seat in an in-person workshop.">
<label for="name">Your name</label>
<input id="name" name="name" type="text" required>
<label for="workshop">Workshop</label>
<select id="workshop" name="workshop" required
toolparamdescription="Workshop topic. Each one runs for two hours.">
<option value="css">Modern CSS</option>
<option value="a11y">Accessibility</option>
</select>
<button type="submit">Book seat</button>
</form>
-
toolnameandtooldescriptionregister the form as a tool. Chrome's docs are explicit: remove either one and the tool is unregistered. Try it in the demo: the schema disappears and the agent has to fall back to guessing. - Each control with a
namebecomes a property of the input schema, andrequiredends up in the schema'srequiredlist. A<select>becomes a list of allowed values, with the option text as each value'stitle. - Each parameter's description comes from
toolparamdescriptionif it's there; if not, from the text of the associated<label>; and if there's no label, fromaria-description. Good labels, the ones you should already have for accessibility, now also document your tool.
When an agent calls the tool, the browser focuses the form and fills it in, in plain view. By default, the person still has to press submit. If you add toolautosubmit, the agent can submit it on its own. I'd keep that for searches and other low-risk actions; for anything with consequences, like a payment, that last click by the person is the whole point.
To answer the agent without navigating, the SubmitEvent gains two things: agentInvoked, which is true when an agent triggered the submit, and respondWith(), which takes a promise whose value goes back to the model as the tool's result. You must call preventDefault() first:
const form = document.querySelector('form');
form.addEventListener('submit', (event) => {
event.preventDefault();
const result = `Seat booked: ${form.elements.name.value}`;
if (event.agentInvoked) event.respondWith(Promise.resolve(result));
});
In a browser without WebMCP, agentInvoked is simply undefined and the form behaves as always. There are also two events, toolactivated and toolcancel, fired on document.modelContext, and two pseudo-classes for the moment the agent is working: :tool-form-active on the form and :tool-submit-active on the submit button. The demo mimics them with classes (the teal dashed outline and the pulsing button), because no browser applies them without the flag.
Browser support and gotchas
-
It's an experiment. Chrome offers it as an origin trial from Chrome 149, and locally behind
chrome://flags/#enable-webmcp-testing. Chrome Status lists no signal yet from Firefox or Safari. The attributes are harmless elsewhere: a browser that doesn't know them ignores them and the form keeps working. -
The declarative part isn't in the spec yet. The W3C Community Group draft defines
document.modelContextandregisterTool(), but not the form attributes. That's why the demo builds the schema in JavaScript following the example in Chrome's docs, not an official algorithm. -
Labels have to really be linked. In the docs example itself, the
<label for="firstName">doesn't point to anyid, and the generated JSON has no description for that field. Untick<label for>in the demo to see the same thing. -
Secure context and iframes.
document.modelContextonly exists on HTTPS, and the API is gated by thetoolsPermissions Policy, which defaults toself: a cross-origin iframe can't register tools withoutallow="tools". The demo runs in a sandboxed iframe, so its agent is simulated. -
Events moved. From Chrome 156,
window.ontoolactivatedandwindow.ontoolcancelare no longer supported; listen ondocument.modelContext.
What to keep an eye on
Whether Firefox and WebKit take a position, how the form-to-schema algorithm ends up specified, and whether agents beyond Chrome's own start calling these tools. In the meantime, the cost of trying it is tiny: two attributes on a form that, if nothing understands them, stays exactly the same form.
Sources
- Declarative API | WebMCP | Chrome for Developers
- WebMCP | Chrome for Developers
- WebMCP, Draft Community Group Report
- WebMCP | Chrome Platform Status
Originally published at richi.codes.
來源:DEV Community · dev.to