---
title: Test a tool
description: "Run a tool's schema and execute in Node, the way an agent would, and check the answer it reads: no page, no browser."
related:
  - /build/define-a-tool
  - /testing/test-a-tool-on-the-page
---

A tool is a function with a schema. Most of what can go wrong in it (a wrong total, a missing item, a bad input
accepted) shows up without a page, so test it in Node first: it is fast, and a failure points at the tool, not at the
page around it.

## A complete example

The tool, defined with `defineWebTool`:

```ts title="tools/add-to-cart.ts"
import { defineWebTool } from "@webmcp-agents/core";
import { z } from "zod";

import { cart } from "../../shared/lib/cart";

const mugs: Record<string, { name: string; price: number }> = {
  "mug-01": { name: "Field Mug", price: 18 },
};

export const addToCart = defineWebTool({
  id: "add_to_cart",
  description:
    "Add a mug to the cart. Use when the user wants to buy one. " +
    "Answers the line added.",
  inputSchema: z.object({
    id: z.string().describe('The mug\'s id, e.g. "mug-01".'),
    qty: z.number().int().min(1).describe("How many, e.g. 2."),
  }),
  execute: async ({ id, qty }) => {
    const mug = mugs[id];
    if (!mug) throw new Error(`No mug "${id}".`);
    cart.add({ id, name: mug.name, quantity: qty, price: mug.price });
    return `Added ${qty} × ${mug.name}.`;
  },
});
```

Its test:

```ts title="add-to-cart.test.ts"
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");
});
```

`callTool` validates the input against the schema, runs `execute`, and answers what the agent would read. A schema
error comes back as an `invalid_input` result and a `WebToolError` keeps its code, the way an agent receives them.

## What to check

- **The text the agent reads.** `toBeToolResult` takes a string, a regular expression, or nothing (it only checks the
  call succeeded).
- **Failures, by code.** `toBeToolError("invalid_input")` for a schema error, `"cancelled"`, `"unavailable"` or your
  own code from [`toolError`](/docs/reference/core/tool-error). The code is what an agent can act on, so it is worth a test.
- **Structured data.** A tool that returns an object answers `structuredContent` too:
  [`toHaveStructuredContent`](/docs/reference/testing/to-have-structured-content) checks a part of it.

A tool defined inside a component with `useWebTool` needs its page: test it as on the next page.

## See also

- [`callTool`](/docs/reference/testing/call-tool), [`toBeToolResult`](/docs/reference/testing/to-be-tool-result),
  [`toBeToolError`](/docs/reference/testing/to-be-tool-error)
- [Errors](/docs/concepts/errors), the codes a tool answers
