{"_id":"@w3c/webmcp-polyfill@0.1.0","bugs":{"url":"https://github.com/w3c-oss/webmcp-polyfill/issues"},"dist":{"shasum":"9e6707aaed69131f327dca3e70309e649952bec9","tarball":"http://registry-npm.ywwl.com/@w3c/webmcp-polyfill/download/@w3c/webmcp-polyfill-0.1.0.tgz","fileCount":10,"integrity":"sha512-FNzmob4B72T9cHDVODskyFjNi0JaZgcD1U0eFmuc2J4o3Lr5k1xabZ5rjw1AuvzgvdxRfvNr9FHUP9yG1s/StA==","signatures":[{"sig":"MEUCIGWhc8vbsrYRWXwWjKeKCmmvba6NTSYeyPyzSqV+atPnAiEAlFH16f4qZskikNC/ZMnW6Hv+HxnSYo3MjBZdWYLgzvU=","keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U"},{"keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U","sig":"MEUCIFK8DuEpGHsXBfPyGgnIBfisjwpySB5VMBJzM1NO7T7BAiEA1X2ta1YFbAUghO/q9m0opCZBV/o6u+qSdY18PVWEfBM="}],"unpackedSize":81178,"size":23284,"noattachment":false,"key":"/@w3c/webmcp-polyfill/-/@w3c/webmcp-polyfill-0.1.0.tgz"},"name":"@w3c/webmcp-polyfill","type":"module","_from":"file:w3c-webmcp-polyfill-0.1.0.tgz","types":"./dist/index.d.ts","exports":{".":"./dist/index.js","./auto":"./dist/auto.js"},"license":"Apache-2.0","scripts":{"lint":"oxlint --deny-warnings","test":"pnpm lint && pnpm build && pnpm typecheck && playwright test","build":"tsc && esbuild src/auto.ts --bundle --format=iife --target=es2022 --minify --legal-comments=inline --outfile=dist/polyfill.js","test:wpt":"pnpm build && node wpt/run.ts","typecheck":"tsc -p tsconfig.test.json","test:package":"node tests/package.test.ts"},"version":"0.1.0","_npmUser":{"name":"tidoust","email":"fd@tidoust.net"},"homepage":"https://github.com/w3c-oss/webmcp-polyfill#readme","_resolved":"/home/tidoust/docs.w3.org/w3c-webmcp-polyfill-0.1.0.tgz","_integrity":"sha512-FNzmob4B72T9cHDVODskyFjNi0JaZgcD1U0eFmuc2J4o3Lr5k1xabZ5rjw1AuvzgvdxRfvNr9FHUP9yG1s/StA==","repository":{"url":"git+https://github.com/w3c-oss/webmcp-polyfill.git","type":"git"},"_npmVersion":"11.17.0","description":"A polyfill for WebMCP","directories":{},"maintainers":[{"name":"deniak","email":"denis@w3.org"},{"name":"dontcallmedom","email":"dom@w3.org"},{"name":"tidoust","email":"fd@tidoust.net"},{"name":"vivienlacourba","email":"vivien@w3.org"},{"name":"kfranqueiro","email":"kenneth.franqueiro@gmail.com"}],"sideEffects":["./dist/auto.js"],"_nodeVersion":"24.14.1","dependencies":{"webmcp-types":"^0.1.9"},"_hasShrinkwrap":false,"devDependencies":{"oxlint":"1.82.0","esbuild":"^0.25.0","typescript":"^5.9.0","@types/node":"^24.13.4","@playwright/test":"^1.55.0"},"_npmOperationalInternal":{"host":"s3://npm-registry-packages-npm-production","tmp":"tmp/webmcp-polyfill_0.1.0_1793693016894_0.6945288109570729"},"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":1793693016987,"_cnpm_publish_time":1793693016987,"dist-tags":{"latest":"0.1.1"}}