{"name":"@w3c/webmcp-polyfill","version":"0.0.0-stage","_id":"@w3c/webmcp-polyfill@0.0.0-stage","stub":true,"maintainers":[{"name":"tidoust","email":"fd@tidoust.net"}],"dist":{"shasum":"89ce7c467992a311f532ab1ad566af234564b85c","tarball":"http://registry-npm.ywwl.com/@w3c/webmcp-polyfill/download/@w3c/webmcp-polyfill-0.0.0-stage.tgz","fileCount":2,"integrity":"sha512-nKmijD0633YmRniB9tjPnkg1rt+z9Dy0kDG9JWM9n4nFNMmDeqjLg8Ees9yQTuHkdnFEzdnaiwlM3nJ1mDfYuA==","signatures":[{"sig":"MEUCIQCx3H+b02dVuKEVEeFW7sfJnRnL+k0A85roC0dGkuIhTQIgQ6gOHv//iuC93w7DeZwpDdG3iGMFLQH57J6gGddIIVo=","keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U"}],"unpackedSize":342,"size":372,"noattachment":false,"key":"/@w3c/webmcp-polyfill/-/@w3c/webmcp-polyfill-0.0.0-stage.tgz"},"_npmUser":{"name":"tidoust","email":"fd@tidoust.net"},"description":"Temporary package placeholder for staged publishing","directories":{},"readme":"# WebMCP polyfill\n\nA polyfill for [WebMCP](https://webmachinelearning.github.io/webmcp/), the API that lets web pages expose tools to browser agents through `document.modelContext`. It follows the [WebMCP draft](https://webmachinelearning.github.io/webmcp/) from the [Web Machine Learning Community Group](https://www.w3.org/groups/cg/webmachinelearning/), uses types from [webmcp-types](https://github.com/webmachinelearning/webmcp-types), and has no runtime JavaScript dependencies.\n\nWebMCP is a Community Group draft, not a W3C Standard, and it is still changing. Expect this package to change with it.\n\nThis project is part of the [W3C Open Source Program](https://github.com/w3c-oss/open-source-program) pilot. See its [project page](https://github.com/w3c-oss/open-source-program/blob/main/projects/webmcp-polyfill/README.md) for what that means.\n\n## Install\n\n```sh\nnpm install @w3c/webmcp-polyfill\n```\n\n## Usage\n\nThe API only exists in secure contexts, so serve your page over HTTPS or from localhost.\n\nLoad the polyfill before you register tools:\n\n```ts\nimport \"@w3c/webmcp-polyfill/auto\";\n\nconst context = document.modelContext;\nif (!context) {\n  throw new Error(\"WebMCP requires a secure browser context\");\n}\n\nconst registration = new AbortController();\nawait context.registerTool(\n  {\n    name: \"page-title\",\n    description: \"Get the title of this page\",\n    execute() {\n      return { title: document.title };\n    },\n  },\n  { signal: registration.signal },\n);\n\nconst tools = await context.getTools();\nconst pageTitleTool = tools.find((tool) => tool.name === \"page-title\");\nif (!pageTitleTool) {\n  throw new Error(\"The page-title tool is unavailable\");\n}\n\nconst result = await context.executeTool(pageTitleTool);\nconsole.log(result);\n\n// Remove the tool when it is no longer needed.\nregistration.abort();\n```\n\nIf you'd rather install it yourself, call `installWebMCP()` from `@w3c/webmcp-polyfill`. Calling it more than once is fine. Neither entry point replaces a native `document.modelContext`, even a partial one.\n\n### Script tag\n\nThe package also ships a standalone build at `dist/polyfill.js`. Load it before your app:\n\n```html\n<script src=\"/assets/polyfill.js\"></script>\n<script src=\"/assets/app.js\"></script>\n```\n\nIt installs `document.modelContext` as soon as it runs.\n\n### Next.js\n\nIn Next.js 15.3+, add this to `instrumentation-client.ts` beside `app` (or `src/instrumentation-client.ts` beside `src/app`):\n\n```ts\nimport \"@w3c/webmcp-polyfill/auto\";\n```\n\nNext.js runs [client instrumentation](https://nextjs.org/docs/app/api-reference/file-conventions/instrumentation-client) before hydration. A Server Component import alone won't install the polyfill in the browser.\n\nBoth entry points are SSR-safe: installation does nothing without a document. Your pages can stay server-rendered; WebMCP runs in the browser.\n\nThe package needs no `\"use client\"` directive. Use it on your [Client Components](https://nextjs.org/docs/app/api-reference/directives/use-client) and access `document.modelContext` in effects or event handlers. Pass an `AbortSignal` when registering in an effect, then abort it during cleanup.\n\n## Frames\n\nLoad the polyfill in each frame. Same-origin tools are visible by default, including those in parents and siblings. Pass the discovered descriptor to `executeTool()` so names shared by different frames reach the correct owner.\n\nFor cross-origin tools, delegate the `tools` permission on the iframe, register the tool with `exposedTo: [callerOrigin]`, and discover it with `getTools({ fromOrigins: [toolOrigin] })`. Same-origin tools are still included in that result.\n\n```html\n<iframe src=\"https://tools.example/app\" allow=\"tools https://tools.example\"></iframe>\n```\n\nInitial discovery waits up to 500 ms for existing frames. Requests use `MessageChannel` after checking the peer's source and origin; callbacks run in their owning frame. Cancellation preserves the caller's reason and sends the callback a default `AbortError`.\n\n## Implementation status\n\nThe polyfill targets `webmcp-types@0.1.9` and covers registration, discovery, execution, cancellation and `toolchange`, including frame exposure and origin filtering. Declarative forms and the `toolactivated`/`toolcancel` events are not implemented yet. Talking to an actual browser agent needs support from the browser itself.\n\nNative contexts do not join the polyfill's channels. [TESTING.md](https://github.com/w3c-oss/webmcp-polyfill/blob/main/TESTING.md) lists the known differences from the draft, the test commands, and the spec and WPT revisions we test against.\n\n`executeTool()` accepts an object and returns a JSON-serialized result. Omitted or `undefined` input defaults to a fresh empty object. Callbacks must validate their inputs; schema inference provides TypeScript checks only.\n\nBreaking API changes ship with notes: in minor releases while the version is 0.x, in majors after 1.0.\n\n## Contributing\n\n`src/` holds the polyfill, `tests/` the browser and package checks, and `wpt/` the [web-platform-tests](https://github.com/web-platform-tests/wpt/tree/master/webmcp) runner, pin, and expectations. To work on it, use Node.js 24 and pnpm:\n\n```sh\npnpm install\npnpm test\n```\n\nSpec questions belong in the [WebMCP repository](https://github.com/webmachinelearning/webmcp/issues). See [CONTRIBUTING.md](CONTRIBUTING.md) for the contributor agreement and [GOVERNANCE.md](GOVERNANCE.md) for how the project is run.\n\n## License\n\n[Apache-2.0](LICENSE) © 2026 [World Wide Web Consortium](https://www.w3.org/)\n","publish_time":1793692961471,"_cnpm_publish_time":1793692961471,"dist-tags":{"latest":"0.1.1"}}