Declarative WebMCP Is Easy Until React Owns the Form State

declarative-webmcp-is-easy-until-react-owns-the-form-state

Read the current workflow through application logic before letting an agent update it.

HTML annotations can make a form discoverable, but a production React workflow rarely lives in HTML alone. Derived state, validation, unsaved buffers, permissions, and route ownership still decide what the operation means.

React state and browser-managed form values do not always have the same owner. That becomes especially visible when an agent fills a declarative WebMCP form and the application later reads an older React value.

A common response is to add more synchronization effects. For a review-oriented form, there is a simpler design: let the DOM hold the editable fields, take a FormData snapshot at submission, and let React own the reviewed result.

The companion example applies that design to an event title and date. It demonstrates a specific state boundary, not a claim that every controlled React form is incompatible with WebMCP.

Decide which state needs to be authoritative

A controlled React input receives its current value from React and updates that value through an event handler. An uncontrolled input uses defaultValue for its initial value, after which the DOM holds the current edit. React’s input reference documents this distinction.

For a form that continuously derives pricing or validation from each keystroke, controlled state can be appropriate. For this example, the important transition is the user’s decision to review the completed fields. There is no need to copy every intermediate edit into React state.

The form therefore has two clearly named stages: current field values and a reviewed snapshot. A snapshot is historical; changing a field later does not silently rewrite what was previously reviewed.

Expose a bounded form action

Chrome’s declarative WebMCP API lets a form describe a tool using HTML attributes. The fixture names the action prepare_event_fields and describes its limited effect:

  {...{
toolname: "prepare_event_fields",
tooldescription:
"Fill an event title and date for human review. " +
"Does not save, submit, or book.",
}}
onSubmit={handleSubmit}
>



The field names are part of the interface. Labels and constraints also matter to both people and tooling. The example does not add toolautosubmit, and its handler does not persist an event. It prepares a review result.

Keeping that scope small makes the experiment easier to reason about. There is no ambiguity between filling a form, accepting its values, and creating a booking.

Read the values that are actually in the form

The handler begins with preventDefault() and captures the current form element synchronously. It then constructs a FormData snapshot:

const data = new FormData(form);
const title = String(data.get("title") ?? "").trim();
const date = String(data.get("date") ?? "");
if (!title || title.length > 120) {
throw new Error("Use a title between 1 and 120 characters");
}

The complete helper also validates the date’s shape and calendar meaning. Browser constraints improve the editing experience, but the application still validates at the boundary where values become a reviewed result.

Only after validation does the handler call setReview(snapshot). A parent rerender therefore cannot replace the current editable field with an outdated controlled value, because this form does not supply one.

This design also avoids reading from an event object after an asynchronous boundary. Capture the element and values first; perform later work on the resulting plain object.

Return an agent result at the same boundary

The native submit event can identify agent invocation and provide respondWith(). In the fixture, the ordinary React submit handler accesses that native event and returns the same result that drives the review UI:

const native = event.nativeEvent as NativeAgentSubmit;
if (native.agentInvoked && native.respondWith) {
native.respondWith(Promise.resolve(result));
}

The handler invokes this synchronously after preventing default behavior. The result states whether field validation succeeded and always records submitted: false for a valid review candidate.

That shared result is important. If the UI shows validation errors while the tool response says success, the agent and user are no longer observing the same application state.

Understand activation without depending on it unnecessarily

The current declarative documentation includes activation and cancellation events. Activation occurs after prefilling, and current event handling belongs to document.modelContext; older examples may attach handlers elsewhere.

Those events can support an application that needs to synchronize controlled state after agent prefilling. This fixture does not require that synchronization because it reads the current DOM at submission. The smaller event surface is a deliberate consequence of the ownership model.

It is still necessary to test the real browser path. A synthetic submit in a DOM test does not reproduce trusted browser events, native agent activation, or the browser’s complete declarative workflow.

Reproduce the state regression without a model

The companion test renders the real React component in JSDOM, changes the field’s DOM value, triggers an unrelated parent rerender, and submits the form. It verifies that the edited value survives and that the reviewed snapshot contains it.

That test passed locally. It establishes the form-buffer behavior under React and JSDOM. Native Chrome agent prefilling remains a separate manual check, and the package does not label the DOM test as native WebMCP coverage.

For that check, expose the form in an eligible Chrome profile, invoke the declarative tool, inspect the prefilled fields, trigger the parent-rerender button, and complete review. Compare the visible fields, tool response, and rendered snapshot. Also test reset and invalid input.

Make the review state explicit in the product

An event form can preserve a reviewed snapshot while allowing more edits, but the interface must communicate that distinction. The demo labels it as a snapshot. A production application might instead clear review when editing resumes or require a fresh review before submission.

Choose that policy intentionally. Do not let a previous successful review become an implicit approval for later changes.

This pattern fits import panels, agent-assisted forms, and other workflows where several actors can propose values but the user makes a discrete review decision. DOM ownership handles editing; React ownership handles the accepted snapshot. The boundary is small enough to test and clear enough to explain.

Practical takeaway

Use declarative tools for the parts the DOM can describe honestly. Route stateful reads and writes through the same application services used by the human interface, then test both paths against the same invariants.


Declarative WebMCP Is Easy Until React Owns the Form State was originally published in Google Developer Experts on Medium, where people are continuing the conversation by highlighting and responding to this story.

Total
0
Shares
Leave a Reply

Your email address will not be published. Required fields are marked *

Previous Post
openai-echoes-anthropic’s-call-to-pace-ai,-minus-the-details

OpenAI echoes Anthropic’s call to pace AI, minus the details

Next Post
prepare-the-decision,-not-the-submission:-a-safer-webmcp-pattern

Prepare the Decision, Not the Submission: A Safer WebMCP Pattern

Related Posts