Install the runtime
If you have a build step, install@mcp-b/global from npm:
main.ts
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 returns a rejected promise if a tool with the same name already
exists. To update a tool, abort the previous registration signal, then await the
new registration.
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
registerTool and an AbortController to add or remove individual tools:
registration-lifetime.ts
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.tool-errors.ts
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.Read-only tool annotations
readOnlyHint and untrustedContentHint.
title is a top-level tool field. MCP-B runtimes also accept the MCP-specific
destructiveHint, idempotentHint, and openWorldHint annotations for MCP
clients. Annotations are planning hints, not authorization checks.
Verify tools are registered
Use standard discovery, then feature-detect Chrome’s optional execution extension:Next steps
- Use Schemas and Structured Output to add type-safe input validation and structured responses.
- Choose a Runtime if you are unsure whether
@mcp-b/globalis the right package. - Framework integration guide for framework-specific patterns.
- Debug and Troubleshoot if tools are not appearing to AI agents.
