Install the runtime
If you have a build step, install@mcp-b/global from npm:
main.ts
navigator.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, call unregisterTool first, 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
registerTool and unregisterTool 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.title, readOnlyHint, destructiveHint, idempotentHint, openWorldHint. See the @mcp-b/webmcp-types reference for the full ToolAnnotations interface.
Verify tools are registered
Usenavigator.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).
@mcp-b/global, you can also use the extension methods directly:
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.
