@benchme/site-kit 0.6.0 → 0.6.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -9,6 +9,6 @@ export type JsonSchemaObject = {
9
9
  /**
10
10
  * One catalog, two surfaces: the MCP SDK converts `ToolDef.input` itself; the
11
11
  * page-side WebMCP registration needs the same schema as data. Strip `$schema`
12
- * so the object is exactly what `navigator.modelContext.registerTool` expects.
12
+ * so the object is exactly what a WebMCP `modelContext.registerTool` expects.
13
13
  */
14
14
  export declare function toolInputJsonSchema(def: Pick<ToolDef<unknown>, "input">): JsonSchemaObject;
@@ -3,7 +3,7 @@ import { zodToJsonSchema } from "zod-to-json-schema";
3
3
  /**
4
4
  * One catalog, two surfaces: the MCP SDK converts `ToolDef.input` itself; the
5
5
  * page-side WebMCP registration needs the same schema as data. Strip `$schema`
6
- * so the object is exactly what `navigator.modelContext.registerTool` expects.
6
+ * so the object is exactly what a WebMCP `modelContext.registerTool` expects.
7
7
  */
8
8
  export function toolInputJsonSchema(def) {
9
9
  const { $schema: _drop, ...rest } = zodToJsonSchema(z.object(def.input).strict(), { target: "jsonSchema7" });
package/dist/webmcp.d.ts CHANGED
@@ -1,20 +1,32 @@
1
1
  import type { ToolRegistry } from "./tool-registry.js";
2
2
  export declare const WEBMCP_SCRIPT_MARKER = "data-webmcp=\"bridge\"";
3
- /** The global a late-injecting agent sidecar can call once it has installed `navigator.modelContext`. */
3
+ /** The global a late-injecting agent sidecar can call once it has installed a modelContext. */
4
4
  export declare const WEBMCP_REGISTER_GLOBAL = "__benchmeWebmcpRegister";
5
5
  /**
6
- * WebMCP (`navigator.modelContext`) tools are closures inside the page. This
7
- * script registers one per catalog entry and bridges `execute` to the app's
8
- * own `/mcp` (JSON-RPC `tools/call`), so the page surface and the MCP surface
9
- * can never disagree: both are rendered from `ToolRegistry.list()`.
6
+ * WebMCP tools are closures inside the page. This script registers one per
7
+ * catalog entry and bridges `execute` to the app's own `/mcp` (JSON-RPC
8
+ * `tools/call`), so the page surface and the MCP surface can never disagree:
9
+ * both are rendered from `ToolRegistry.list()`.
10
+ *
11
+ * Two globals carry the API today, and a page cannot know which browser it is
12
+ * in, so it registers on EVERY distinct one present:
13
+ * - `document.modelContext` — the WebMCP Community Group draft; Cloudflare's
14
+ * Kitesurf browser implements this one (2026-09-28);
15
+ * - `navigator.modelContext` — Chromium behind its WebMCP testing flag, and
16
+ * the injected shims agent harnesses install.
17
+ * A browser that exposes one object under both names registers once; a
18
+ * registry that refuses a name it already holds (two names over one backing
19
+ * store) costs that tool a console warning, never the rest of the tools.
10
20
  *
11
21
  * Registration is a FUNCTION, not a parse-time side effect, because the agent
12
22
  * that provides `navigator.modelContext` usually injects it AFTER the page
13
23
  * parses: a one-shot attempt at parse time silently registered nothing and
14
24
  * never tried again. So it runs immediately, again on `DOMContentLoaded`, and
15
- * on demand via `window.__benchmeWebmcpRegister()` — guarded by a flag so the
16
- * three paths together still register each tool exactly once. `navigator`
17
- * itself is read defensively: a non-browser runtime must be a no-op, never a
25
+ * on demand via `window.__benchmeWebmcpRegister()` — remembering each context
26
+ * it already registered on, so the three paths together still register each
27
+ * tool exactly once per context, and a context that appears late (a shim
28
+ * injected after parse) still gets its tools. `document` and `navigator` are
29
+ * read defensively: a non-browser runtime must be a no-op, never a
18
30
  * ReferenceError that kills the rest of the page's scripts.
19
31
  */
20
32
  export declare function webmcpScript<C>(tools: ToolRegistry<C>, prefix: string): string;
package/dist/webmcp.js CHANGED
@@ -1,29 +1,42 @@
1
1
  import { toolInputJsonSchema } from "./tool-schema.js";
2
2
  export const WEBMCP_SCRIPT_MARKER = 'data-webmcp="bridge"';
3
- /** The global a late-injecting agent sidecar can call once it has installed `navigator.modelContext`. */
3
+ /** The global a late-injecting agent sidecar can call once it has installed a modelContext. */
4
4
  export const WEBMCP_REGISTER_GLOBAL = "__benchmeWebmcpRegister";
5
5
  /**
6
- * WebMCP (`navigator.modelContext`) tools are closures inside the page. This
7
- * script registers one per catalog entry and bridges `execute` to the app's
8
- * own `/mcp` (JSON-RPC `tools/call`), so the page surface and the MCP surface
9
- * can never disagree: both are rendered from `ToolRegistry.list()`.
6
+ * WebMCP tools are closures inside the page. This script registers one per
7
+ * catalog entry and bridges `execute` to the app's own `/mcp` (JSON-RPC
8
+ * `tools/call`), so the page surface and the MCP surface can never disagree:
9
+ * both are rendered from `ToolRegistry.list()`.
10
+ *
11
+ * Two globals carry the API today, and a page cannot know which browser it is
12
+ * in, so it registers on EVERY distinct one present:
13
+ * - `document.modelContext` — the WebMCP Community Group draft; Cloudflare's
14
+ * Kitesurf browser implements this one (2026-09-28);
15
+ * - `navigator.modelContext` — Chromium behind its WebMCP testing flag, and
16
+ * the injected shims agent harnesses install.
17
+ * A browser that exposes one object under both names registers once; a
18
+ * registry that refuses a name it already holds (two names over one backing
19
+ * store) costs that tool a console warning, never the rest of the tools.
10
20
  *
11
21
  * Registration is a FUNCTION, not a parse-time side effect, because the agent
12
22
  * that provides `navigator.modelContext` usually injects it AFTER the page
13
23
  * parses: a one-shot attempt at parse time silently registered nothing and
14
24
  * never tried again. So it runs immediately, again on `DOMContentLoaded`, and
15
- * on demand via `window.__benchmeWebmcpRegister()` — guarded by a flag so the
16
- * three paths together still register each tool exactly once. `navigator`
17
- * itself is read defensively: a non-browser runtime must be a no-op, never a
25
+ * on demand via `window.__benchmeWebmcpRegister()` — remembering each context
26
+ * it already registered on, so the three paths together still register each
27
+ * tool exactly once per context, and a context that appears late (a shim
28
+ * injected after parse) still gets its tools. `document` and `navigator` are
29
+ * read defensively: a non-browser runtime must be a no-op, never a
18
30
  * ReferenceError that kills the rest of the page's scripts.
19
31
  */
20
32
  export function webmcpScript(tools, prefix) {
21
33
  const catalog = tools.list().map((t) => ({ name: t.name, description: t.description, inputSchema: toolInputJsonSchema(t) }));
22
34
  const json = JSON.stringify({ endpoint: `${prefix}/mcp`, tools: catalog }).replace(/<\//g, "<\\/");
23
- return `<script ${WEBMCP_SCRIPT_MARKER}>(function(){var cfg=${json};var seq=0;var registered=false;
35
+ return `<script ${WEBMCP_SCRIPT_MARKER}>(function(){var cfg=${json};var seq=0;var done=[];
24
36
  function call(name,args){return fetch(cfg.endpoint,{method:"POST",headers:{"content-type":"application/json","accept":"application/json, text/event-stream"},body:JSON.stringify({jsonrpc:"2.0",id:++seq,method:"tools/call",params:{name:name,arguments:args||{}}})}).then(function(r){return r.text()}).then(function(t){var line=t.split("\\n").filter(function(l){return l.indexOf("data:")===0}).pop();var body=JSON.parse(line?line.slice(5):t);var c=body.result&&body.result.content;return c&&c[0]&&c[0].text?c[0].text:JSON.stringify(body.result||body.error)})}
25
- function register(){if(registered)return true;if(typeof navigator==="undefined")return false;var mc=navigator.modelContext;if(!mc||typeof mc.registerTool!=="function")return false;
26
- cfg.tools.forEach(function(t){mc.registerTool({name:t.name,description:t.description,inputSchema:t.inputSchema,execute:function(a){return call(t.name,a)}})});registered=true;return true}
37
+ function contexts(){var out=[];if(typeof document!=="undefined"&&document&&document.modelContext)out.push(document.modelContext);if(typeof navigator!=="undefined"&&navigator&&navigator.modelContext&&out.indexOf(navigator.modelContext)<0)out.push(navigator.modelContext);return out.filter(function(mc){return typeof mc.registerTool==="function"})}
38
+ function register(){contexts().forEach(function(mc){if(done.indexOf(mc)>=0)return;done.push(mc);
39
+ cfg.tools.forEach(function(t){try{mc.registerTool({name:t.name,description:t.description,inputSchema:t.inputSchema,execute:function(a){return call(t.name,a)}})}catch(e){if(typeof console!=="undefined")console.warn("webmcp: "+t.name+" not registered: "+e)}})});return done.length>0}
27
40
  if(typeof window!=="undefined")window.${WEBMCP_REGISTER_GLOBAL}=register;
28
41
  register();
29
42
  if(typeof document!=="undefined"&&document.addEventListener)document.addEventListener("DOMContentLoaded",register);})();</script>`;
@@ -1 +1 @@
1
- {"version":3,"file":"webmcp.js","sourceRoot":"","sources":["../src/webmcp.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,mBAAmB,EAAE,MAAM,kBAAkB,CAAC;AAEvD,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mB,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC;IAC7H,MAAM,IAAI,GAAG,IAAI,CAAC,SAAS,CAAC,EAAE,QAAQ,EAAE,GAAG,MAAM,MAAM,EAAE,KAAK,EAAE,OAAO,EAAE,CAAC,CAAC,OAAO,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IACnG,OAAO,WAAW,oBAAoB,wBAAwB,IAAI;;;;;wCAK5B,sBAAsB;;kIAEoE,CAAC;AACnI,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@benchme/site-kit",
3
- "version": "0.6.0",
3
+ "version": "0.6.1",
4
4
  "type": "module",
5
5
  "license": "MIT",
6
6
  "main": "./dist/index.js",
@@ -16,7 +16,7 @@
16
16
  "test": "vitest run"
17
17
  },
18
18
  "dependencies": {
19
- "@benchme/core": "0.6.0",
19
+ "@benchme/core": "0.6.1",
20
20
  "@modelcontextprotocol/sdk": "^1.12.0",
21
21
  "fastify": "^5.2.0",
22
22
  "zod": "^3.24.0",