# Tool calls

Script tool calls, their input and output, across multi-turn agent loops.

`toolCall(name, input, options?)` streams a tool call with the input you give. Names follow the AI SDK: a tool call has an `input`, and a tool result has an `output`.

```ts
reply: toolCall("lookupOrder", { id: "A-1042" });
```

## Two kinds of tool flows [#two-kinds-of-tool-flows]

**Client-executed tools.** This is how the OpenAI and Anthropic APIs work, and how `streamText` runs tools. The mock ends its response with the tool call: `finish_reason: "tool_calls"` on Chat Completions, a `function_call` output item on Responses, `stop_reason: "tool_use"` on Anthropic. Your code runs the tool and sends the result back in the next request. A second scenario matches that result:

```ts
scenarios: [
  { when: { prompt: /refund/i }, reply: toolCall("lookupOrder", { id: "A-1042" }) },
  {
    when: { toolResult: "lookupOrder" },
    reply: ({ toolResults }) =>
      toolCall("refund", { id: "A-1042", amount: toolResults.lookupOrder.total }),
  },
  { when: { toolResult: "refund" }, reply: text("Done. $48 is on its way back.") },
];
```

This tests your real tool code. The mock only decides what the model asks for.

**Resolved tools.** Pass an `output` and the mock streams the call and its result together. Use this for UI work, where you want a tool part to render without running anything:

```ts
reply: [
  toolCall("getWeather", { city: "Lisbon" }, { output: { tempC: 21, sky: "clear" } }),
  text("It's 21°C and clear in Lisbon."),
];
```

On the AI SDK UI stream this sends `tool-input-available` then `tool-output-available`, so `useChat` renders a tool part in the `output-available` state. Provider endpoints have no way to carry a result from the model, so on those `output` is ignored and the flow is client-executed.

## Tool errors [#tool-errors]

```ts
toolCall("lookupOrder", { id: "A-9999" }, { error: "Order not found" });
```

Streams `tool-output-error`, and the tool part ends in the `output-error` state.

## Parallel calls [#parallel-calls]

Several tool calls in one reply stream as parallel calls in one message:

```ts
reply: [toolCall("getWeather", { city: "Lisbon" }), toolCall("getWeather", { city: "Porto" })];
```

## Streaming input [#streaming-input]

Tool input streams as partial JSON (`tool-input-delta` on the AI SDK, `function_call_arguments.delta` on OpenAI, `input_json_delta` on Anthropic), so UIs that render input while it arrives see it grow.

## MCP [#mcp]

The same tools can be served as a fake MCP server. See [MCP endpoint](/docs/endpoints/mcp) and [@loremllm/mcp](/docs/packages/mcp).