Skip to content

Support AbortSignal-based tool unregistration (Chrome 148)#21

Open
kashishhora wants to merge 1 commit intomainfrom
feat/abort-signal-tool-unregistration
Open

Support AbortSignal-based tool unregistration (Chrome 148)#21
kashishhora wants to merge 1 commit intomainfrom
feat/abort-signal-tool-unregistration

Conversation

@kashishhora
Copy link
Copy Markdown
Member

Chrome 148 removes unregisterTool() from navigator.modelContext and replaces it with an AbortSignal passed to registerTool(). This aligns with the addEventListener ownership pattern — you can only unregister a tool if you hold the original AbortController.

What changed

Types (src/types.ts, src/index.ts)

  • New RegisterToolOptions interface with optional signal?: AbortSignal
  • registerTool accepts optional second arg options?: RegisterToolOptions
  • unregisterTool is now optional (?) — won't exist on Chrome 148+
  • RegisterToolOptions exported for consumers

Polyfill registry (src/polyfill/registry.ts)

  • registerTool accepts signal option
  • Pre-aborted signals skip registration entirely
  • Abort listener deletes the tool and fires change notification
  • Identity check prevents stale abort signals from removing same-name re-registrations
  • unregisterTool stays on the polyfill for backward compat

React hook (src/hooks/useMcpTool.ts)

  • Creates one AbortController per effect cycle
  • Passes { signal } to registerTool
  • Cleanup: mc.unregisterTool?.(name) (optional chaining) + controller.abort()
  • Works with both old (unregisterTool) and new (signal-only) APIs

Breaking change

navigator.modelContext.unregisterTool is now typed as optional. Downstream TS code calling it directly must add optional chaining (?.). This matches the Chrome 148 runtime behavior.

Testing

  • pnpm test — 158/158 pass
  • pnpm typecheck — clean
  • pnpm lint — clean
  • pnpm build — CJS + ESM + types build successfully

New tests:

  • Registry: abort removes tool, pre-aborted skip, abort-after-unregister no-op, stale abort safety, notification timing
  • Hook: signal-only native API mount/unmount, StrictMode, re-registration on prop change
  • Context: native API without unregisterTool reports available

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant