Evan X. Merz

Programmer / Master Gardener / Doctor of Music / Curious Person

How to add agentic WebMCP search to a static site (like an 11ty blog)

In this post, I'm going to show how to add a WebMCP search feature to your static site so that agents can easily search it.

WebMCP + 11ty

What is WebMCP?

WebMCP is a web browser specification that defines a way for websites to create MCP tools using JavaScript. MCP is a standard for describing tools that can be used by AI agents. So, for example, an ecommerce site might define several tools relating to the shopping experience. A search tool might allow an agent to search for products, a product tool might allow an agent to get the details for an individual product, an addToCart tool might add a product to an agent's cart, and a checkout tool might allow an agent to finalize a purchase.

WebMCP is an alternative to forcing agents to click buttons in your user interface. WebMCP allows agents to interact with your website in a way that is defined for agent use, rather than human use.

The WebMCP spec was defined by W3C, but the GitHub page is a good source for learning more about WebMCP.

Adding WebMCP to a static website

To add WebMCP to your static site, you need to do a few things.

  1. Determine the actions that an agent could take on your website. These will become tools.
  2. Write JavaScript functions that execute those actions.
  3. Write WebMCP compliant JavaScript that describes the tools and calls the JavaScript functions for the tools.

On a static website like a blog, there aren't a lot of actions that an agent could take. If an agent wants to read an article on your blog, then the article should be listed in llms.txt so the agent can find the article and read it. The only possible action I could define for my blog is a search feature. The remainder of this article will show how I added the search feature to this blog, including WebMCP support, but hypothetically you could follow this tutorial to add any action that your website supports.

The first thing to do to add support for agentic searching is to create a search feature. Search is not native to static websites because they lack the backend that would typically support a mature search feature. However, the PageFind JavaScript library enables post-build support for searching static sites using a pre-built static search index.

To add search functionality to your static site using PageFind, I suggest following the tutorial by Robb Knight. After you do that, come back here, and we will layer in agentic search.

Enabling agentic search using PageFind is incredibly easy. All you need to do is add JavaScript to define the search tool. Any WebMCP tool is defined by its name, description, inputSchema, and what to do when an agent calls it. All you have to do is call document.modelContext.registerTool. The first argument takes a json object with the name, description, and inputSchema parameters. The second argument defines the execute function. Notice the definition of the parameters; in this case there is only a term parameter that passes the search term into the execute function. Then there is a small bit of JavaScript that says how to run the actual tool.

The code for enabling WebMCP for static sites that use pagefind should be exactly the same for every site. Pagefind generates the search index by scanning your generated html files in a post-build step, as shown in the tutorial linked above. So the code to define a search tool should work with any static pagefind site, even if you aren't using 11ty like me.


<script type="text/javascript">
  async function registerTools() {
    const pagefind = await import("/pagefind/pagefind.js");

    await document.modelContext.registerTool({
      name: "search",
      description: "Returns an array of articles that include the search term.",
      inputSchema: {
        type: "object",
        properties: {
          term: { type: "string", description: "What you are searching for." },
        }
      },
      async execute({ term }) {
        const search = await pagefind.search(term);
        return search.results;
      }
    });
  }
  registerTools();
</script>

After you create the search feature and add the code to define the search tool, you can validate that it's working using the WebMCP Inspector Chrome extension. Just install the extension in Chrome, and enable the experimental WebMCP for testing feature in Chrome by visiting chrome://flags/#enable-webmcp-testing.

That extension will allow you to see the tools defined on a page, execute them, and inspect the output.

My WebMCP enabled blog search feature can be used at https://evanxmerz.com/search/.