Core Solution
WebMCP provides two integration patterns: a declarative API for standard HTML forms and an imperative API for complex JavaScript interactions. Both patterns allow the website to define the tool schema, parameters, and execution logic, which the agent consumes directly.
1. Declarative API: Annotating HTML Forms
For standard form interactions, WebMCP allows developers to annotate existing HTML elements. This requires no JavaScript and enables immediate tool discovery by agents.
Implementation Strategy
Add data-mcp-tool and data-mcp-description attributes to the form element. The agent reads these attributes to understand the form's purpose and structure.
Code Example: Inventory Adjustment Form
<form
id="inventory-form"
data-mcp-tool="adjust_inventory"
data-mcp-description="Update stock levels for a specific SKU. Supports positive and negative deltas."
>
<label for="sku-input">SKU:</label>
<input type="text" id="sku-input" name="sku" required />
<label for="delta-input">Quantity Delta:</label>
<input type="number" id="delta-input" name="delta" step="1" required />
<button type="submit">Update Stock</button>
</form>
Rationale
- Progressive Enhancement: The form remains fully functional for human users. The annotations are additive and do not alter existing behavior.
- Zero Runtime Overhead: No additional JavaScript is loaded. The metadata is parsed directly from the DOM by the agent.
- Schema Inference: Agents can infer parameter types from input attributes (e.g.,
type="number" implies a numeric parameter).
2. Imperative API: Registering JavaScript Tools
For interactions that require complex logic, state management, or non-form-based triggers, developers can register tools programmatically using the navigator.mcp API.
Implementation Strategy
Use navigator.mcp.registerTool to define a tool with a name, description, parameter schema, and an asynchronous handler function. The handler executes in the browser context and returns a structured result.
Code Example: Maintenance Scheduling Tool
interface MaintenanceParams {
deviceId: string;
startTime: string;
durationMinutes: number;
}
navigator.mcp.registerTool({
name: "schedule_device_maintenance",
description: "Schedule a maintenance window for a specific IoT device. Returns the confirmation ID.",
parameters: {
deviceId: {
type: "string",
description: "Unique identifier of the device.",
pattern: "^DEV-[A-Z0-9]{6}$"
},
startTime: {
type: "string",
description: "ISO 8601 formatted start time.",
format: "date-time"
},
durationMinutes: {
type: "number",
description: "Duration of the maintenance window in minutes.",
minimum: 15,
maximum: 480
}
},
handler: async ({ deviceId, startTime, durationMinutes }: MaintenanceParams) => {
try {
const validation = await deviceService.validateMaintenanceWindow(deviceId, startTime, durationMinutes);
if (!validation.isAvailable) {
return {
success: false,
error: "CONFLICT",
message: validation.conflictReason
};
}
const confirmation = await deviceService.createMaintenanceTicket({
deviceId,
startTime,
durationMinutes
});
return {
success: true,
ticketId: confirmation.id,
scheduledAt: confirmation.startTime
};
} catch (err) {
return {
success: false,
error: "INTERNAL_ERROR",
message: "Failed to process maintenance request."
};
}
}
});
Rationale
- Type Safety: The parameter schema enforces validation before the handler executes, reducing error rates.
- Structured Responses: The handler returns a consistent JSON object, allowing agents to parse results reliably without text extraction.
- Encapsulation: Business logic remains within the application codebase. The agent interacts only with the defined interface.
Architecture Decisions
- Tool Granularity: Register tools at the action level rather than the page level. This allows agents to compose workflows by calling multiple tools in sequence.
- Error Contracts: Define explicit error codes in the response schema. Agents should not rely on exception messages; they should handle structured error objects.
- Idempotency: Design handlers to be idempotent where possible. Agents may retry calls due to network issues; the tool should handle duplicate requests safely.
Pitfall Guide
Implementing WebMCP introduces new responsibilities. The following pitfalls are common in early adoption phases.
| Pitfall | Explanation | Fix |
|---|
| Headless Assumption | WebMCP requires an active browser tab. It does not support headless execution. Agents running in headless environments cannot access registered tools. | Use WebMCP for in-browser agent interactions. For server-side automation, implement a backend MCP server or REST API. |
| Browser Fragmentation | Firefox and Safari have not committed to WebMCP. Relying solely on WebMCP excludes users on these browsers. | Implement feature detection. Check for navigator.mcp availability and provide fallbacks or degrade gracefully. |
| Spec Volatility | WebMCP is in the W3C Community Group incubation phase. The API may change before reaching the official standards track. | Abstract the integration layer. Avoid hardcoding dependencies on specific attribute names or API signatures. Monitor origin trial updates. |
| Over-Exposure | Registering too many tools increases the attack surface and confuses agents. Exposing internal utilities can lead to unintended actions. | Apply the principle of least privilege. Only expose tools that are necessary for agent workflows. Review tool descriptions for sensitive information leakage. |
| Validation Neglect | Agents may send malformed or unexpected parameters. Relying solely on the schema for validation is insufficient. | Implement runtime validation in the handler. Sanitize inputs and enforce business rules before executing actions. |
| State Inconsistency | Agents may call tools out of sequence or without proper context, leading to inconsistent application state. | Design tools to be stateless or require explicit context tokens. Validate preconditions in the handler and return errors if state is invalid. |
| Error Ambiguity | Returning generic errors prevents agents from recovering. Agents need actionable feedback to retry or adjust workflows. | Return structured error objects with codes and messages. Include recovery suggestions where appropriate. |
Production Bundle
Action Checklist
Decision Matrix
| Scenario | Recommended Approach | Why | Cost Impact |
|---|
| In-Browser Agent Interaction | WebMCP | Provides low-latency, reliable tool access for agents running in the browser. | Low (Implementation effort) |
| Server-Side Automation | Backend MCP / REST API | WebMCP requires a browser tab. Backend APIs support headless execution. | Medium (Infrastructure) |
| Legacy Site with No JS | Declarative WebMCP | HTML annotations work without JavaScript. Enables tool exposure for static sites. | Low (Markup changes) |
| Multi-Browser Support Required | Feature Detection + Fallback | WebMCP is Chrome-only currently. Fallbacks ensure functionality across browsers. | Medium (Dev effort) |
Configuration Template
Use this template to bootstrap a WebMCP integration. It includes feature detection, declarative form annotation, and a sample imperative tool registration.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>WebMCP Integration Example</title>
</head>
<body>
<!-- Declarative Tool: Search -->
<form
data-mcp-tool="site_search"
data-mcp-description="Search the knowledge base for relevant articles."
>
<input type="text" name="query" placeholder="Enter search term" />
<button type="submit">Search</button>
</form>
<script>
// Feature Detection
if ('mcp' in navigator) {
console.log('WebMCP is supported.');
// Imperative Tool: Submit Feedback
navigator.mcp.registerTool({
name: "submit_feedback",
description: "Submit user feedback with a rating and comment.",
parameters: {
rating: { type: "number", minimum: 1, maximum: 5 },
comment: { type: "string", maxLength: 500 }
},
handler: async ({ rating, comment }) => {
const response = await fetch('/api/feedback', {
method: 'POST',
body: JSON.stringify({ rating, comment })
});
const data = await response.json();
return { success: response.ok, id: data.id };
}
});
} else {
console.warn('WebMCP is not supported in this browser.');
}
</script>
</body>
</html>
Quick Start Guide
- Enable the Flag: Open Chrome and navigate to
chrome://flags. Search for "WebMCP" and set the flag to Enabled. Relaunch the browser.
- Register for Origin Trial: Visit the Chrome Origin Trial dashboard and register your domain to obtain a trial token. Add the token to your HTML
<head> via a meta tag.
- Annotate a Form: Add
data-mcp-tool and data-mcp-description to an existing form on your site.
- Test with Gemini: Open your site in Chrome and invoke Gemini. Ask Gemini to perform the action defined by your tool. Verify that the agent calls the tool directly without screenshot parsing.
- Review Console Logs: Open DevTools and check the console for WebMCP-related messages. Ensure schema validation passes and handlers execute correctly.