A simple tutorial on exposing tools to AI agents
WebMCP is a proposed web standard that lets websites expose structured tools to AI agents. Instead of AI guessing how to interact with your UI, you tell it exactly what actions are available.
You might be wondering: why WebMCP when we already have MCP?
| MCP | WebMCP | |
|---|---|---|
| Where it runs | Server-side | Client-side (browser) |
| Who builds it | You deploy & maintain a server | Website adds some JS/HTML |
| Access | Need API keys, auth, infrastructure | Just visit the website |
| Updates | You update your server | Website owner updates their site |
With MCP, if you want AI to book a flight on United.com, you have to build a server that scrapes/automates their site, maintain it when their site changes, and handle auth and rate limits.
With WebMCP, United builds it once and every AI agent can use it.
MCP: AI → Your MCP Server → Website
WebMCP: AI → Website (directly)
Who knows better how to book a flight on United.com - you, or United? WebMCP lets the website author define exactly what's possible.
Imagine if every website had WebMCP tools:
You wouldn't need to build MCP servers for each one.
WebMCP only works in Chrome 146+ with the flag enabled. Always check first:
if (window.navigator.modelContext) {
// WebMCP is available, register tools here
} else {
console.log("WebMCP not available");
}
This prevents errors on browsers that don't support WebMCP yet.
Use registerTool() to expose a function to AI agents:
window.navigator.modelContext.registerTool({
name: "set_background_color",
description: "Changes the background color...",
inputSchema: { ... },
execute: ({ color }) => { ... }
});
Each tool has 4 required parts:
The schema tells AI what parameters your tool accepts:
inputSchema: {
type: "object",
properties: {
color: {
type: "string",
description: "The color to set. Can be hex (#ff5733),
RGB, or named color (coral, navy)"
}
},
required: ["color"]
}
This is where the actual work happens:
execute: ({ color }) => {
// 1. Do the actual work
document.body.style.backgroundColor = color;
// 2. Update any UI elements
document.getElementById("colorName").textContent = color;
// 3. Return a result for the AI
return {
content: [{
type: "text",
text: `Background color changed to ${color}`
}]
};
}
The return value tells AI what happened - important for multi-step tasks!
Here's the WebMCP registration part of webmcp.js:
if (window.navigator.modelContext) {
window.navigator.modelContext.registerTool({
name: "set_background_color",
description: "Changes the background color of the page. Accepts any valid CSS color (hex, rgb, named colors like 'coral', 'forestgreen', etc.)",
inputSchema: {
type: "object",
properties: {
color: {
type: "string",
description: "The color to set. Can be a hex code (#ff5733), RGB (rgb(255,87,51)), or named color (coral, navy, forestgreen)"
}
},
required: ["color"]
},
execute: ({ color }) => {
setBackgroundColor(color, true); // true = called by AI
return {
content: [{
type: "text",
text: `Background color changed to ${color}`
}]
};
}
});
console.log("✅ WebMCP tool registered!");
} else {
console.log("⚠️ WebMCP not available");
}
With Chrome Canary (146+) and the WebMCP flag enabled, visit the demo and ask an AI: "Change the background to coral"
View the Demo →Download from google.com/chrome/canary
Go to chrome://flags/#enable-webmcp-testing and set to Enabled
Click the relaunch button after enabling the flag