Skip to content

Repository files navigation

WebMCP polyfill

A polyfill for WebMCP, with types from webmcp-types and no runtime JavaScript dependencies.

Build

This package is under review. To try it locally, build this checkout with Node.js 24 and pnpm:

pnpm install
pnpm build

Then install the checkout in your app with pnpm add /path/to/webmcp-polyfill. For a classic script, serve the built dist/polyfill.js.

Usage

Serve your page over HTTPS with the Origin-Agent-Cluster: ?1 response header. Localhost HTTP also works for development.

Load the polyfill before registering tools:

import "webmcp-polyfill/auto";

const context = document.modelContext;
if (!context) throw new Error("WebMCP requires a secure browser context");

const registration = new AbortController();
await context.registerTool(
  {
    name: "page-title",
    description: "Get the title of this page",
    execute() {
      return { title: document.title };
    },
  },
  { signal: registration.signal },
);

console.log(await context.getTools());

// Remove the tool when it is no longer needed.
registration.abort();

To install explicitly, import and call installWebMCP from webmcp-polyfill. Installation requires a secure browser context and leaves an existing document.modelContext unchanged, including partial native implementations. It affects only the realm that calls it, so each frame installs separately.

Scope

Tools stay in the current document. Cross-document tools, declarative forms, lifecycle window events, and browser agent integration aren't implemented. Nonempty exposedTo and fromOrigins options reject.

This version supports registration, discovery, and toolchange. It does not invoke registered callbacks; executeTool() follows alongside the types update.

The implementation follows draft source cc45efc and passes all 27 selected assertions of the upstream WPT at the pin, with no expected failures. TESTING.md records the selection and what it excludes.

The API tracks the draft. Breaking changes ship with notes: in minor releases while the version is 0.x, in majors after 1.0.

Development

See TESTING.md for browser setup, test commands, draft alignment, and known limitations.

License

MIT.

About

WebMCP polyfill

Resources

Stars

3 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages