Skip to main content
Expose existing application functionality to AI agents while keeping your app’s own UI and state model intact. For framework-specific lifecycle patterns (React hooks, Vue composables, Svelte actions), see Integrate with your framework.

Install the runtime

If you have a build step, install @mcp-b/global from npm:
main.ts
If you do not have a build step, add a single script tag. Place it before any code that registers tools:
Both approaches auto-initialize document.modelContext on load.
If you only need the strict core API (no MCP bridge, no transports), use @mcp-b/webmcp-polyfill instead. See Choose a Runtime for guidance.

Register tools that wrap existing functions

Identify functions in your app that an AI agent would find useful (search, add to cart, submit a form, fetch data). Wrap each one as a tool descriptor and register it.

Register tools with registerTool

Call registerTool for each tool you want to expose. You can register them all at startup or add them incrementally (for example, after user login or after a feature flag check).
tools.ts

Register tools conditionally

admin-tools.ts
registerTool throws if a tool with the same name already exists. To update a tool, abort the previous registration signal, then register the new version.

Update tools when app state changes

If your available tools depend on state (logged in vs. guest, current page, feature flags), update the registry when that state changes.
auth.ts
Use registerTool and an AbortController to add or remove individual tools:

Interact with the DOM

Tools can read from and write to the DOM. This is useful for form-filling, page navigation, or surfacing visible content.
form-tools.ts

Return errors from tools

If a tool execution fails, return an error response instead of throwing. This gives the AI agent a structured signal to retry or adjust its approach.

Add annotations for AI planning

Tool annotations give the AI agent hints about side effects. They do not change execution behavior, but help the agent decide when and how to call the tool.
Available annotation fields: title, readOnlyHint, destructiveHint, idempotentHint, openWorldHint. See the @mcp-b/webmcp-types reference for the full ToolAnnotations interface.

Verify tools are registered

Use navigator.modelContextTesting to list registered tools and execute them programmatically. This API is available when the testing shim is installed (the default for both @mcp-b/global and @mcp-b/webmcp-polyfill).
If you installed @mcp-b/global, use the standard in-page consumer methods directly:

Next steps