How-to Add WebMCP to Your Website

webmcp feature image

Adding WebMCP to your website enables Agentic AI systems to dynamically discover and interact with your specific services and capabilities. While a full server-side deployment offers deep integration, you can also implement a client-side script that registers your custom tools directly into the browser’s model context, bridging the gap between your on-page data and AI assistants.

What is WebMCP?

Model Context Protocol (WebMCP). The WebMCP API enables web applications to provide JavaScript-based tools to AI agents. Proposed WebMCP will replace DOM Scraping, meaning instead of an AI agent parsing HTML, reading accessibility trees, or taking screenshots to guess what buttons or forms do, the website with WebMCP will explicitly declare what actions are available and what inputs they require.

Video Tutorial: What is WebMCP? How-to Add WebMCP to Your Website

Implementing the Client-Side Registration Script

The core of this approach involves executing a JavaScript function that polls for the availability of the `modelContext` API and registers a structured tool when it becomes active. This allows you to define exactly what your website offers in a format AI understands.

  • Wrap your registration logic in an immediately invoked function expression (IIFE) to prevent scope pollution.
  • Implement a polling mechanism (using `setInterval`) to check for `window.modelContext` or `navigator.modelContext`, as the AI environment may inject this object asynchronously.
  • Set a maximum number of polling attempts to avoid infinite loops if the user agent does not support the protocol.

Defining the WebMCP Tool Schema

Once the context is detected, you must define the tool using a strict JSON schema. This schema acts as the interface contract for the AI.

  1. Provide a clear, unique `name` for your tool (e.g., “order_bistro_food”).
  2. Write a precise `description` outlining exactly what data the tool returns.
  3. Construct the `inputSchema` specifying the expected parameters. For example, if you offer various services, define an `enum` of acceptable categories so the AI knows how to filter the request.
  4. Implement the `execute` function to process the AI’s input and return the corresponding data array or object.

Option 1: The Declarative HTML API (Simplest)

<!-- Adding WebMCP attributes to a standard form --> <form toolname="order_bistro_food" tooldescription="Allows an AI agent to order food directly from our bistro menu." toolautosubmit > <label for="dish">Choose a dish:</label> <input type="text" id="dish" name="dish" toolparamtitle="Dish Name" toolparamdescription="The full name of the menu item (e.g., Croque Monsieur)" required /> <button type="submit">Place Order</button> </form>

Explanation of Declarative HTML API

  • toolname: The identifier the AI agent uses to call the tool.
  • tooldescription: A short description of what this tool is used for. This is to let the AI agent understand when to use this form.
  • toolautosubmit: This is a boolean flag that allows the agent to submit the form immediately after filling out the parameters.
  • toolparamtitle / toolparamdescription: Field-level context guiding the agent on exactly what data structure to input.

All you need to do is modify your interactable web form/s to ensure the webmcp is declared on the HTML elements itself.

Option 2: The Imperative JavaScript API (Advanced)

// Register a custom function as an agent-callable tool if (window.navigator && 'modelContext' in navigator) { navigator.modelContext.api.registerTool({ name: "update_kanban_board", title: "Move Project Card", description: "Moves a project card to a different status column on the dashboard.", inputSchema: { type: "object", properties: { cardId: { type: "string", description: "The unique ID of the project card." }, status: { type: "string", enum: ["todo", "in_progress", "done"] } }, required: ["cardId", "status"] }, readOnly: false, // Indicates that this tool modifies the page state execute: async ({ cardId, status }) => { // Call your app's existing state or API functions directly await myApp.moveCard(cardId, status); return { success: true, message: `Card ${cardId} shifted to ${status}.` }; } }); }

For using The Imperative JavaScript API, all you need to do is add javascript on your website code.

Option 3: Alternative: Zero-Code via Cloudflare

If your site is routed through Cloudflare, you can skip manual implementation entirely. Because Cloudflare offers a one-click WebMCP Developer Preview toggle in its dashboard that injects pre-packaged “tool packs” into your site automatically without changing a single line of your origin source code.

Synchronizing with JSON-LD Structured Data

To maximize Answer Engine Optimization, your WebMCP tool output should mirror the structured data already present on your page. This dual-layer approach reinforces your entity definitions.

Ensure the data returned by your `execute` function logically maps to the `ProfessionalService` and `Offer` items declared in your `< script type="application/ld+json" >` block. When an AI agent reads the JSON-LD to understand your page context and uses the WebMCP tool to fetch specific details, the consistency across both data sets establishes high trust and topical authority.

How-to Test a WebMCP Integration Functionality

Deploy the script within the `< head >` or just before the closing `< /body >` tag of your HTML document. You can verify the execution by simulating the `modelContext` object in your browser’s developer console and triggering the `registerTool` method to ensure your schema and execute functions resolve without errors.

To test: Press F12 for web developer toolbar > Application > WebMCP > Select Tool > Run Tool

Web Dev Toolbar for webMCP Application

What is the Best Way to Add WebMCP to Your Website?

Best way to implement WebMCP to your website will depend on various factors such as: the type of website you operate, your resources. Ideally, it is often better to ensure that the website is fully search engine optimized, fully structured using Schema Markup so that AI agents do not hallucinate when cross-referencing your website’s code.

Frequently Asked Questions

Do I need a dedicated server to run WebMCP?

You can run WebMCP using edge network solutions like Cloudflare Workers, which eliminates the need to manage and host a dedicated physical server.

Is WebMCP compatible with WordPress?

Yes, WebMCP can connect directly to WordPress by utilizing the built-in REST API endpoints to fetch content and execute commands securely.

What are 2 Ways I can Implement WebMCP?

WebMCP can work through server-side integration by creating and configuring the Server Worker. Or client-side integration, by using HTML and JavaScript that registers your WebMCP codes. Which you choose will depend on your website or app requirements.

Can I add WebMCP using only JavaScript?

Yes, you can implement WebMCP on the client side by writing a script that references the `modelContext` API and then registers your specific tool/s and data schemas directly so that the browser’s and AI agents can references your WebMCP tools.

How does WebMCP relate to JSON-LD?

While JSON-LD provides static, structured markup for search engines to understand page content, WebMCP allows active, dynamic data retrieval by AI agents based on specific queries users put in the AI agent chat/task.

Why is polling necessary in the WebMCP script?

Polling is required because the `modelContext` object may not be immediately available when the webpage load process begins.

When an Agentic AI browser, or extension accesses your page, it doesn’t always load its connection the exact instant your webpage does. Polling simply tells your script to keep checking for a split second until that connection is ready to use. While the connection is open, your script calls mc.registerTool(). This hands the AI agent your tool’s name (for example: book_service), description, and the inputSchema so the AI knows exactly what data it can ask for.

By Tolga Savas

Tolga Savas is an Australian web developer, technical SEO consultant, and digital content creator. As the founder of RankYa, Tolga brings over 10 years of hands-on expertise in WordPress and Shopify code-level optimization, technical search engine optimization, Agentic AI discovery, and Google Search Console remediation.

Questions? Leave a Comment!

Your email address will not be published. Required fields are marked *