Testing with the fake agent
Most browsers have no WebMCP host yet, so @webmcpui/core/testing gives you one: installFakeAgent() installs the W3C WebMCP polyfill on document.modelContext and returns a handle that drives it exactly as an agent would — getTools() snapshots, executeTool() calls, JSON results. It is the spec'd API, not a stand-in, so what passes here is what a real host will do.
import { installFakeAgent } from '@webmcpui/core/testing';
const agent = installFakeAgent();
// ... connect a <wmcp-input name="email" expose> to the DOM ...
const result = await agent.call('fill-email', { value: '[email protected]' });
// the live element is now filled, validated, and has fired input/change
Order matters. Install the fake agent before the element connects — controls register their tool in
connectedCallback, so the host must already be present.
On a browser that already has a native document.modelContext (the Chrome or Edge origin trial), the polyfill steps aside and the same handle drives the real host. There is no separate code path for "with an agent" and "without".
The handle
installFakeAgent() returns:
| Member | Description |
|---|---|
modelContext | The underlying document.modelContext — add toolchange / toolactivated / toolcancel listeners here, or call the spec API directly. |
polyfilled | true when the host is the polyfill, false when it's the browser's own. |
tools() | Promise of the spec Tool snapshots currently exposed to this document (name, title, description, inputSchema, outputSchema, annotations, origin, source). |
get(name) | Promise of one Tool by name, or undefined. |
call(name, args?, { signal? }) | Look the tool up, run the host's executeTool(), and return the parsed JSON result. Throws if no tool has that name; rejects with the host's DOMException (OperationError, NotFoundError, …) if the call fails. |
The host is page-level and persistent, like the real thing: a tool lives as long as the element that registered it is connected (or until the disposer returned by exposeTool is called). There is nothing to tear down between tests — disconnect the elements, as test fixtures already do.
Environments
<wmcp-*> form controls are form-associated custom elements built on ElementInternals, so run component tests in a real browser — @web/test-runner, Vitest browser mode, and Playwright component testing all work. WebMCP is a secure-context API, so the page must be served from localhost or over HTTPS, which every test runner does.
jsdom and happy-dom degrade gracefully instead of crashing: components render, validate, and expose their WebMCP tools, and a single console warning is logged. What doesn't work under jsdom/happy-dom is native <form> participation — FormData, form.checkValidity(), and requestSubmit()-driven submits are all disabled. Anything asserting on form submission needs a real browser.
| Real browser | jsdom / happy-dom | |
|---|---|---|
| Render, validate, expose tools | yes | yes |
FormData includes control values | yes | no |
form.checkValidity() | yes | no |
requestSubmit()-driven submit | yes | no |
| Console warning on load | no | yes (once) |
Listening to the host
Because the handle exposes the real ModelContext, your tests can observe what an agent would:
const agent = installFakeAgent();
agent.modelContext.addEventListener('toolchange', () => {
/* a tool was registered or unregistered */
});
agent.modelContext.addEventListener('toolactivated', (e) => {
console.log(`agent called ${e.toolName}`);
});
This is exactly what powers the live demos on the homepage — the same host, driving real elements.