Skip to content
WebMCP Agents
Esc
↑↓navigate↵open⌘Jpreview
On this page

callTool

Call a tool the way an agent would and get the result it would read.

Calls a tool and resolves to its MCP result ({ content, isError?, structuredContent? }), the value the tool matchers read. A tool’s failure is a result, never a rejection.

Import

import { callTool } from "@webmcp-agents/testing";

Example

import { callTool, expect, test } from "@webmcp-agents/testing";

import { addToCart } from "./app/add-to-cart";

// In Node: no page, no cursor. callTool runs the tool's schema and
// execute, and answers what the agent would read.
test("adds the quantity", async () => {
  const result = await callTool(addToCart, { id: "mug-01", qty: 2 });
  expect(result).toBeToolResult(/2 × Field Mug/);
});

test("answers a schema error instead of throwing", async () => {
  const result = await callTool(addToCart, { id: "mug-01", qty: 0 });
  expect(result).toBeToolError("invalid_input");
});

Options

callTool(tool, input?, options?): tool is a name (through document.modelContext) or a defineWebTool tool (through the page when it is registered there, else its schema and execute in-process).

Name Type Default Description
signal AbortSignal none Cancels the call; the promise rejects with an AbortError.

Returns

Promise<ModelContextToolResult | null>: null when the call navigated the page away.

Notes

By name it needs a page with WebMCP; with the tool object it also runs in Node, with no page and no cursor. A schema error is an invalid_input result, a WebToolError keeps its code.

See also

Was this page helpful?