WebMCP is a browser API that lets websites expose JavaScript functions as structured tools for AI agents. Pages register tools via window.navigator.modelContext, each with a name, description, JSON Schema input, and an execute callback. Think of it as an MCP server running inside the web page itself.
Spec: https://github.com/webmachinelearning/webmcp
Before interacting with tools, check whether the current page supports WebMCP:
const supported = "modelContext" in window.navigator;
If false, the page does not expose WebMCP tools — fall back to DOM interaction or actuation.
Tools are registered by the page via provideContext() or registerTool(). The browser mediates access. To list available tools from an agent's perspective, evaluate:
// Browser-specific — the exact discovery API depends on the agent runtime.
// Typically the browser exposes registered tools to connected agents automatically.
// From page-script perspective, tools are registered like this:
window.navigator.modelContext.provideContext({
tools: [
{
name: "tool-name",
description: "What this tool does",
inputSchema: { type: "object", properties: { /* ... */ }, required: [] },
execute: (params, agent) => { /* ... */ }
}
]
});
Key points:
name, description, inputSchema (JSON Schema), and execute.provideContext() replaces all previously registered tools (useful for SPA state changes).registerTool() / unregisterTool() add/remove individual tools without resetting.Tool input schemas follow JSON Schema (aligned with MCP SDK and Prompt API tool use):
{
name: "add-stamp",
description: "Add a new stamp to the collection",
inputSchema: {
type: "object",
properties: {
name: { type: "string", description: "The name of the stamp" },
year: { type: "number", description: "Year the stamp was issued" },
imageUrl: { type: "string", description: "Optional image URL" }
},
required: ["name", "year"]
},
execute({ name, year, imageUrl }, agent) {
// Implementation — updates UI and app state
return {
content: [{ type: "text", text: `Stamp "${name}" added.` }]
};
}
}
When connected as an agent, send a tool call by name with parameters matching inputSchema. The execute callback runs on the page's main thread, can update the UI, and returns a structured response:
// Response format from execute():
{
content: [
{ type: "text", text: "Result description" }
]
}
execute may be async (returns a Promise).agent provides agent.requestUserInteraction() for user confirmation flows.Tools can request user confirmation before sensitive actions:
async function buyProduct({ product_id }, agent) {
const confirmed = await agent.requestUserInteraction(async () => {
return confirm(`Buy product ${product_id}?`);
});
if (!confirmed) throw new Error("Cancelled by user.");
executePurchase(product_id);
return { content: [{ type: "text", text: `Product ${product_id} purchased.` }] };
}
Always respect user denials — do not retry cancelled tool calls.
"modelContext" in window.navigator to confirm WebMCP support.inputSchema.共 1 个版本