Call Tool

Tools Function Calling Zod

Give your AI the ability to call custom functions. The AI decides when to use tools based on the conversation—like fetching weather, querying databases, or calling APIs.

How Tools Work

When you ask "What's the weather in Tokyo?", the AI recognizes it needs weather data. It calls the getWeather tool with the right parameters, gets the result, and formulates a natural response.

Demo

Try asking about weather in any city!

Define a Tool

Tools have three parts: a description (helps AI understand when to use it), an inputSchema (validates parameters), and an execute function (runs when called).

tools/get-weather.ts

Server Endpoint

Pass tools to streamText. The stopWhen: stepCountIs(5) prevents infinite tool loops.

+server.ts

Client Component

Check part.type === "tool-invocation" to display tool calls and their results in your UI.

+page.svelte