WebMCP Tools
Card 42: WebMCP Tools
The portable agent skill for this pattern is playwright-webmcp.
What This Pattern Solves
WebMCP lets a page hand an agent a set of callable tools. Your page calls document.modelContext.registerTool(), and the browser advertises those tools to whatever agent is driving. The page stops being something an agent has to read pixel by pixel and becomes something it can call.
That creates a surface you have to test. Your tool schemas are an API contract now, and a wrong enum or a missing required sends the agent down a path your buttons never allow. Two problems make it awkward:
- Bundled Chromium has no WebMCP, so your everyday suite cannot see the API at all.
- Chrome ships it behind flags on a moving draft, so a suite pinned to today’s behaviour goes stale without telling you.
This card runs two lanes to cover both: a default lane with a document.modelContext test double on bundled Chromium, and a native lane against real Chrome with the flags on.
How It Works
- The page owns one state.
/webmcpregistersadd_to_cartandget_cartagainst the same cart its buttons drive, then feature-detects so it still works without WebMCP. - The double is installed before page scripts.
webmcp-fixture.tsusespage.addInitScript()so the page findsdocument.modelContextthe way it would in a WebMCP browser. - The native lane keeps the double honest.
.contract.tsfiles drive real Chrome throughplaywright-webmcp-native.config.tsand are skipped by the defaultpnpm test.
Drive a tool the way an agent would, then assert on what the user sees. A tool that reports success while the cart stays empty has given the agent a lie, and only the UI check catches it.
When To Use
- When a page registers WebMCP tools and you want their schemas and handlers covered.
- When the everyday suite must stay green on machines with no flagged Chrome.
- Not when you are testing an agent’s reasoning about your tools. That needs the agent, not Playwright.
Live Demo
👇 The Playwright harness is a bare agent-ready page (no card chrome). Open it to try the cart buttons and the WebMCP tools they share state with:
Run This Example
pnpm test:webmcp # default lane, no special browser
pnpm test:webmcp:native # native lane, needs a Chrome with WebMCP