liveBook · Manning

Custom tool

As explained in Build AI-Enhanced Web Apps

A custom tool is an application-defined operation that an AI model can invoke with specified parameters to obtain structured results.

A custom tool is an application-defined operation that an AI model can invoke with specified parameters to obtain structured results. It can call an API, query a database, or run local application logic while execution remains under server control. section 4.4.1 section 4.4.2

Implement a custom tool with these moving parts:

  • Define a function that accepts explicit parameters and returns structured data validated with a Zod schema. section 4.4.2
  • Register the function in the Vercel AI SDK tools object with a name and description that guide model selection. section 4.4.2
  • Let the model request the tool, execute it on the application server, and send its result back for the final response. section 4.4.1
  • Render tool output as React components, including loading states and the completed result. section 4.4.2

Weather assistant

Register getWeather with a description such as “Get the current weather for a specific city” and a parameter schema containing city: z.string(). The generator can yield a loading spinner, fetch weather data, and return a WeatherCard. A system instruction tells the model to use getWeather for city-specific requests and interpret temperatures in Celsius. section 4.4.2

Figure 4.14 The process of tool calling in an AI-powered application using the Vercel AI SDK. This shows how a user action initiates a prompt, which is formatted by the SDK and sent to the AI provider. The AI model may request a tool call, which is then executed by the SDK. The tool’s response is fed back to the AI model through the SDK, and finally, the result is used to update the UI for the user.

Trace the request from user prompt through the AI provider, tool execution, returned tool result, and UI update. section 4.4.1

How Build AI-Enhanced Web Apps covers it

How deep it goes
Explains registration, parameter validation, server execution, response rendering, and loading states, but not tool-selection internals.
Examples
Demonstrates a getWeather tool that retrieves city weather and renders a loading state followed by a WeatherCard.
What it assumes
Assumes application-server and React familiarity while introducing tool registration, parameter schemas, and model-to-tool execution.
What you can do after
Build it from scratch with the Vercel AI SDK, Zod, an asynchronous tool function, and React response rendering.
How it connects
Connects function calling with the Vercel AI SDK, Zod schemas, streaming, React components, and external APIs.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Build AI-Enhanced Web Apps. The section links point to the parts of the book it draws on.