Webmcp register tool

From OrcaQubits/agentic-commerce-skills-plugins · View interactive version

Webmcp register tool is an AI agent skill from the GitHub repository OrcaQubits/agentic-commerce-skills-plugins. Implement the WebMCP Imperative API — register tools via navigator.modelContext.registerTool() with proper schemas, execute callbacks, and lifecycle management. Use when building dynamic tool registration in JavaScript.

How to use this skill

Skill file

WebMCP Tool Registration (Imperative API)

Before writing code

Fetch live docs:

  1. Fetch https://webmachinelearning.github.io/webmcp/ for the latest registerTool method signature and parameters
  2. Web-search webmcp navigator.modelContext registerTool specification for the full API reference
  3. Web-search site:developer.chrome.com webmcp registerTool for Chrome-specific guidance
  4. Web-search site:github.com mcp-b registerTool for polyfill-compatible registration patterns

Conceptual Architecture

What registerTool Does

navigator.modelContext.registerTool() is the core imperative method for exposing a tool to AI agents. Each registered tool becomes discoverable and invocable by any agent integrated with the browser.

Tool Definition Object

A tool object passed to registerTool() has these fields:

| Field | Type | Required | Description |

|-------|------|----------|-------------|

| name | string | Yes | Unique tool identifier (e.g., "addToCart") |

| description | string | Yes | Natural-language description for the agent |

| inputSchema | object | Yes | JSON Schema defining accepted parameters |

| execute | async function | Yes | Callback (input, client) => Promise<result> |

| annotations | object | No | Safety hints (readOnlyHint, destructiveHint, idempotentHint) |

Execute Callback

The execute(input, client) callback:

Tool Lifecycle

  1. Register — navigator.modelContext.registerTool(tool) makes the tool discoverable
  2. Discovery — Agent queries available tools and reads names/descriptions/schemas
  3. Invocation — Agent calls a tool; browser validates input against schema and calls execute
  4. Execution — Tool logic runs in the page's context (can use fetch, DOM APIs, session cookies)
  5. Result — Tool returns JSON result to the agent
  6. Unregister — navigator.modelContext.unregisterTool(name) removes the tool

Registration Patterns

Simple read-only tool:

navigator.modelContext.registerTool({
  name: "getProductInfo",
  description: "Get details about a product by ID",
  inputSchema: {
    type: "object",
    properties: { productId: { type: "string" } },
    required: ["productId"]
  },
  annotations: { readOnlyHint: true },
  async execute(input) {
    const res = await fetch(`/api/products/${input.productId}`);
    return await res.json();
  }
});

Transactional tool with user confirmation:

navigator.modelContext.registerTool({
  name: "placeOrder",
  description: "Place the current order and charge the saved payment method",
  inputSchema: { type: "object", properties: {}, required: [] },
  annotations: { destructiveHint: true },
  async execute(input, client) {
    const confirmed = await client.requestUserInteraction((resolve) => {
      showConfirmDialog("Confirm order?", resolve);
    });
    if (!confirmed) return { status: "canceled" };
    const res = await fetch("/api/orders", { method: "POST" });
    return await res.json();
  }
});

Naming Best Practices

Description Best Practices

Error Handling

Tools should handle errors gracefully:

Dynamic Registration

Tools can be registered and unregistered dynamically based on page state:

Fetch the specification for exact method signatures, return types, and any new fields before implementing.

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.