DEV Community

Cover image for WebMCP: your HTML forms become tools for AI agents
richi.codes
richi.codes

Posted on Originally published at richi.codes

WebMCP: your HTML forms become tools for AI agents

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>
Enter fullscreen mode Exit fullscreen mode
  • toolname and tooldescription register 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 name becomes a property of the input schema, and required ends up in the schema's required list. A <select> becomes a list of allowed values, with the option text as each value's title.
  • Each parameter's description comes from toolparamdescription if it's there; if not, from the text of the associated <label>; and if there's no label, from aria-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));
});
Enter fullscreen mode Exit fullscreen mode

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.modelContext and registerTool(), 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 any id, 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.modelContext only exists on HTTPS, and the API is gated by the tools Permissions Policy, which defaults to self: a cross-origin iframe can't register tools without allow="tools". The demo runs in a sandboxed iframe, so its agent is simulated.
  • Events moved. From Chrome 156, window.ontoolactivated and window.ontoolcancel are no longer supported; listen on document.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


Originally published at richi.codes.

Top comments (1)

Collapse
 
userxnom profile image
Abdullah Naseer •

The label gotcha is the detail that would have bitten me first — the docs example itself ships a pointing at nothing, so the schema silently loses that field's description instead of failing loudly. That makes me think toolname deserves a lint rule more than a docs note: if a form is registered as a tool, an unlinked label should fail the build, because the agent just gets a worse tool and nobody notices.

On toolautosubmit, where would you draw the low-risk line in practice — is the visible fill-in plus the agent's own confirmation enough for something like the workshop booking, or would you only ever autosubmit when the form has no side effect beyond a search?