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

mockModel

A scripted AI SDK model for createChatHandler: no provider key, no network.

A model (MockLanguageModelV4 from ai/test) that plays steps, one per call, for createChatHandler({ model }). model.calls[n] is what call n was offered (tools) and sent (prompt).

Import

import { mockModel, text, toolCall } from "@webmcp-agents/testing/server";

Example

import { createChatHandler } from "@webmcp-agents/chat/server";
import { expect, test } from "@webmcp-agents/testing";
import {
  chatRequest,
  mockModel,
  readChatStream,
  toolCall,
  withSession,
} from "@webmcp-agents/testing/server";

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

const model = mockModel([
  toolCall("add_to_cart", { id: "mug-01", qty: 1 }),
]);
const handler = createChatHandler({
  model,
  tools: [addToCart],
  session: true,
});
const hi = { messages: ["hi"] };

test("needs a session", async () => {
  expect((await handler(chatRequest(hi))).status).toBe(401);
  expect(model.calls).toHaveLength(0);
});

test("refuses another site", async () => {
  const evil = { origin: "https://evil.example" };
  expect((await handler(chatRequest(hi, evil))).status).toBe(403);
  expect(model.calls).toHaveLength(0);
});

test("drops tools off the allowlist", async () => {
  const send = await withSession(handler);
  const tools = ["add_to_cart", "delete_everything"];
  const res = await send(chatRequest({ ...hi, tools }));
  const parts = await readChatStream(res);

  expect(parts).toContainEqual(
    expect.objectContaining({ type: "tool-call", name: "add_to_cart" }),
  );
  const offered = model.calls[0]?.tools.map((tool) => tool.name);
  expect(offered).toEqual(["add_to_cart"]);
});

Options

mockModel(steps).

Name Type Default Description
steps StepInput[] required The steps, one per model call; the last repeats. []: a model that must never be called.

Returns

A MockModel: the model, with calls.

Notes

It takes the same steps as mockAdapter. mockModel([]) is a model that must never be called: a call fails with a sentence. The ./server entry needs ai: importing it without ai fails with a sentence naming it.

MockModelCall

What the model was sent on one call (model.calls): the tools it was offered and the prompt. StepCall and StepError are the same as mockAdapter’s.

See also

Was this page helpful?