Skip to main content
This guide teaches you three approaches to adding tools to your site, starting with the simplest method and advancing to more powerful patterns. By the end, you’ll understand which installation method fits your project.

Try It Now

Build and test your first WebMCP tool right here - no setup required:

Prerequisites

Before you begin, ensure you have:
  • Modern browser: Chrome, Edge, or Brave (latest version)
  • MCP-B Extension installed from Chrome Web Store
  • Node.js 18+ (for NPM installation) or basic HTML knowledge (for script tag method)
  • Package manager: npm, pnpm, or yarn (optional for NPM method)
Early Incubation: WebMCP is being incubated by the W3C Web Machine Learning Community Group. Not all MCP-B features may be included in the final WebMCP specification. See the introduction for details.

Installation

<script src="https://unpkg.com/@mcp-b/global@latest/dist/index.iife.js"></script>
<script>
  // Register tools using the standard API
  navigator.modelContext.registerTool({
    name: "get_page_title",
    description: "Get current page title",
    inputSchema: { type: "object", properties: {} },
    async execute() {
      return { content: [{ type: "text", text: document.title }] };
    }
  });
</script>

Real-World Example

Wrap your existing application logic as tools:
navigator.modelContext.registerTool({
  name: "add_to_cart",
  description: "Add item to shopping cart",
  inputSchema: {
    type: "object",
    properties: {
      productId: { type: "string" },
      quantity: { type: "number", minimum: 1 }
    },
    required: ["productId", "quantity"]
  },
  async execute({ productId, quantity }) {
    await fetch('/api/cart/add', {
      method: 'POST',
      credentials: 'same-origin',
      body: JSON.stringify({ productId, quantity })
    });
    return {
      content: [{ type: "text", text: `Added ${quantity} items` }]
    };
  }
});

Testing

  1. Run dev server: pnpm dev
  2. Open in Chrome with MCP-B extension
  3. Click extension icon → Tools tab
  4. Test tools via chat or inspector

Examples

Quick Tips

  • Security: Only expose tools users can already access via your UI. Tools inherit user authentication.
  • Naming: Use descriptive names like add_to_cart, search_products, update_profile
  • Validation: Always define input schemas - use Zod (React) or JSON Schema (vanilla)
  • Feedback: Update UI state in handlers so users see what’s happening
See Best Practices for comprehensive guidance on tool design and security.

Next Steps