From a225c3ed2b9475a0f90a5d89a43fdece902a8758 Mon Sep 17 00:00:00 2001 From: Anthony Ettinger Date: Wed, 11 Nov 2026 10:14:30 +0000 Subject: [PATCH] fix: never register a tool twice, even across library copies (0.2.1) Page-wide registry keyed by Symbol.for on the ModelContext, shared by every copy of the module (site tag + crawlproof stats.js plugin, @0 + pinned). Abort during a pending registerTool now unregisters (React StrictMode). Co-Authored-By: Claude Opus 5.5 --- package.json | 2 +- src/core.js | 25 ++++++++++++++++++++++--- test/webmcp.test.js | 36 ++++++++++++++++++++++++++++++++++-- 3 files changed, 57 insertions(+), 6 deletions(-) diff --git a/package.json b/package.json index 9f8f7a0..047af64 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@profullstack/webmcp", - "version": "0.2.0", + "version": "0.2.1", "type": "module", "description": "WebMCP for every site: register page tools for in-browser AI agents (document.modelContext), bridge an existing MCP server, and turn forms into tools. One script tag or one import; no-op where unsupported.", "keywords": ["webmcp", "mcp", "model-context-protocol", "modelContext", "ai-agents", "agentic-web", "browser", "tools"], diff --git a/src/core.js b/src/core.js index 1eea42f..89e7789 100644 --- a/src/core.js +++ b/src/core.js @@ -11,9 +11,11 @@ // module hides those differences, validates tool definitions, and is a silent no-op // where WebMCP does not exist, so a site can ship it unconditionally. -export const VERSION = '0.2.0'; +export const VERSION = '0.2.1'; const NAME_RE = /^[A-Za-z0-9_.-]{1,64}$/; +// Symbol.for, not Symbol(): every copy of this module must find the same key. +const REGISTRY = Symbol.for('@profullstack/webmcp.registered'); /** The page's ModelContext, or null when the browser has no WebMCP. */ export function getModelContext(doc = globalThis.document, nav = globalThis.navigator) { @@ -115,16 +117,31 @@ export async function registerTool(tool, options = {}) { if (!mc) return () => {}; if (options.signal?.aborted) return () => {}; + // One registration per tool name per ModelContext, page-wide. The registry + // lives on the context itself, so two copies of this library (a site tag and + // the crawlproof stats.js plugin, or @0 and a pinned version) share it and the + // second copy skips instead of registering a duplicate. + const names = (mc[REGISTRY] ??= new Set()); + if (names.has(def.name)) return () => {}; + names.add(def.name); + const controller = new AbortController(); const regOptions = { signal: controller.signal }; if (options.exposedTo) regOptions.exposedTo = options.exposedTo; - const registered = await mc.registerTool({ ...def, execute: wrapExecute(def, options.onError, options.onCall) }, regOptions); + let registered; + try { + registered = await mc.registerTool({ ...def, execute: wrapExecute(def, options.onError, options.onCall) }, regOptions); + } catch (err) { + names.delete(def.name); + throw err; + } let done = false; const unregister = () => { if (done) return; done = true; + names.delete(def.name); controller.abort(); // Pre-signal builds: an object with unregister(), or unregisterTool(name). try { @@ -134,7 +151,9 @@ export async function registerTool(tool, options = {}) { // Already gone. } }; - options.signal?.addEventListener('abort', unregister, { once: true }); + // Aborted while registerTool was pending (React StrictMode mount/unmount). + if (options.signal?.aborted) unregister(); + else options.signal?.addEventListener('abort', unregister, { once: true }); return unregister; } diff --git a/test/webmcp.test.js b/test/webmcp.test.js index fcc7d6a..c5259dd 100644 --- a/test/webmcp.test.js +++ b/test/webmcp.test.js @@ -106,7 +106,7 @@ test('no WebMCP: registerTool is a no-op', async () => { assert.equal(typeof off, 'function'); }); -test('registerTools skips a duplicate and keeps the rest', async () => { +test('registerTools skips a duplicate name and keeps the rest', async () => { const mc = fakeContext(); const errors = []; const off = await registerTools( @@ -118,11 +118,43 @@ test('registerTools skips a duplicate and keeps the rest', async () => { { context: mc, onError: (e) => errors.push(e) }, ); assert.deepEqual([...mc.tools.keys()], ['a', 'b']); - assert.equal(errors.length, 1); + assert.equal(mc.tools.get('a').description, 'd'); + assert.equal(errors.length, 0); off(); assert.equal(mc.tools.size, 0); }); +test('a second copy of the library (separate module instance) never double-registers', async () => { + const mc = fakeContext(); + const copy = await import('../src/core.js?copy=2'); // a distinct module instance + await registerTool({ name: 'site_info', description: 'first', execute: () => 1 }, { context: mc }); + const off2 = await copy.registerTool({ name: 'site_info', description: 'second', execute: () => 2 }, { context: mc }); + assert.equal(mc.tools.size, 1); + assert.equal(mc.tools.get('site_info').description, 'first'); + off2(); // the skipped copy's unregister must not remove the first copy's tool + assert.equal(mc.tools.size, 1); +}); + +test('unregister frees the name for re-registration; abort during a pending register cleans up', async () => { + const mc = fakeContext(); + const off = await registerTool({ name: 'x', description: 'd', execute: () => 1 }, { context: mc }); + off(); + await registerTool({ name: 'x', description: 'again', execute: () => 1 }, { context: mc }); + assert.equal(mc.tools.get('x').description, 'again'); + + const slow = { ...fakeContext() }; + const inner = fakeContext(); + slow.tools = inner.tools; + slow.registerTool = async (t, o) => { await new Promise((r) => setTimeout(r, 10)); return inner.registerTool(t, o); }; + const ac = new AbortController(); + const p = registerTool({ name: 'y', description: 'd', execute: () => 1 }, { context: slow, signal: ac.signal }); + ac.abort(); + await p; + assert.equal(inner.tools.size, 0); + await registerTool({ name: 'y', description: 'd', execute: () => 1 }, { context: slow }); + assert.equal(inner.tools.size, 1); +}); + test('mapAnnotations: unknown tools are consequential, read-only ones are not', () => { assert.deepEqual(mapAnnotations({}), { readOnlyHint: false, consequentialHint: true, untrustedContentHint: false }); assert.deepEqual(mapAnnotations({ readOnlyHint: true, openWorldHint: true }), {