Hosts

AI SDK

toAiTool turns a definition into an AI SDK tool. The TypeBox schema goes in as it is and nobody writes it again in Zod
IDai04 / 04ai
Host / AI SDK

AI SDK tool

An AI SDK tool whose output is the details plus the text.

title
Text Slug
inputSchema
jsonSchema()
output
details + text
failure
thrown

Access

Importimport { toAiTool } from "@agntn/tools/ai"
Peerai >=7.0.0 <8
Failsthrown, tool error
tsagent.ts
import { toAiTool, toAiTools } from "@agntn/tools/ai";
import { slugTool, tools } from "./tools.ts";

const one = toAiTool(slugTool);
const all = toAiTools(tools); // keyed and typed by tool name

The schema goes through the AI SDK's jsonSchema() with the core's validator attached. So the model sees the same JSON Schema every other host sees, and a bad input gets refused with the same lines. No second schema in Zod. That used to be the fourth copy.

The output

execute returns the details with the text added: { ...details, text }. When details isn't an object, you get { details, text }. That's the object your code finds in toolResults.

Image blocks in the result add an images array to that object. Each entry has data and mimeType strings, like an MCP image block. The data is base64. Without images, there's no images key.

The model gets less. With only text, toModelOutput gives it text and nothing else. It's the same text MCP, Pi and OMP show their models.

Why not the whole object? The text already says it. A books_text slice used to land twice. Once as text, once escaped as JSON inside details. That came to 2.1 times the characters for one passage. Paying twice to read the same book is a strange hobby.

With images, it answers with content: the text first, when there is any, then a file part per image. Each part uses the image's MIME type as mediaType. The details stay out of view either way.

So a screenshot tool shows the model the screenshot, not a summary line to take on faith. Good. A caption without the photo is just describing a picture over the phone.

Anthropic, Google and openai(...) on the Responses API show the model a real image. But openai.chat(...) and @ai-sdk/openai-compatible without supportsMultiPartToolContent send JSON with the base64 inside. That's a model squinting at letters instead of pixels. When tools return pictures, pick a provider that accepts images in tool results.

An image without text gives the model just the image. Without text or images, the model gets the whole output as JSON.

Images live in the output object. Store and load the chat, and the model still sees them. toModelOutput skips image entries without string data and mimeType. A mangled stored output falls back to the text.

A details object with its own text or images field would lose it under the spread. The adapter throws a ToolDefinitionError instead of guessing. Rename the field.

Failures

A bad input is refused by the input schema before execute. A result with isError throws its text, and the AI SDK turns any throw into a tool error the model can read.

A thrown error with a cause brings its caused by: lines along, the same scrubbed ones MCP shows. Your catch can still reach the original through cause, and an AbortError keeps its name.

Progress

There's nowhere to put it. progress is undefined in execute, and a tool that calls it with ?. just keeps going.

Questions

Same story. The AI SDK has no person on the line, so ask is undefined. Who'd answer? A tool that checks first does its best without one.

Types

toAiTool keeps the input and details types. When you export the tool from a package, annotate it with your own types, Tool<SearchParams, AiToolOutput<SearchDetails>>. The inferred type points into the bundled TypeBox, and your declaration build can't name it.

For a set, use the same list MCP, Pi and OMP get. No second list to keep in sync. A reviewer won't spot an O where a zero goes. tsc will.

ts
import { toAiTools, type AiTools } from "@agntn/tools/ai";
import type { ToolDefinition } from "@agntn/tools";
import { bip340Tool, sr25519Tool } from "./tools.ts";

export const tools = [bip340Tool, sr25519Tool] satisfies readonly ToolDefinition[];
export const aiTools: AiTools<typeof tools> = toAiTools(tools);
// @ts-expect-error that's a letter O
aiTools.curves_bip34O_compute;

defineTool keeps each literal name. Each value has the same input and output types as toAiTool gives it. Annotating the list as readonly ToolDefinition[] loses the names, so the keys become string. A .map() list with a union of literal names gets one key per name in the union, without a cast.