tambo-ai

Tools

Tools enable tambo to interact with your application's APIs, agents, and other server-side functionality.

Tools enable tambo to interact with your application's data and functionality. They provide a type-safe way to extend the AI's capabilities with custom business logic. When applicable, tambo will automatically call these tools and use their outputs to help decide how to respond to a message, what component to show, and what data to fill the component with.

interface TamboTool {
  name: string;
  description: string; // What is this tool for and when should tambo use it?
  tool: (input: z.infer<ZodTypeAny>) => any; // The function that tambo will call when using this tool
  toolSchema: z.ZodFunction<Args, Returns>; // The schema definition that tambo will use when calling the tool. This is where we describe to tambo what parameters exist, what they are for, whether they are required, etc.
}

Adding a Tool

//define the tool function. This is your own custom function and can perform any logic you want.
const getWeather = async (city: string) => {
  try {
    const weather = await fetch(
      `http://api.weatherapi.com/v1/current.json?key=${process.env.NEXT_PUBLIC_WEATHER_API_KEY}&q=${city}`,
    );
    return weather.json();
  } catch (error) {
    throw new Error(`Failed to fetch weather for ${city}`);
  }
};
 
//define the tool
export const weatherTool: TamboTool = {
  name: "get_weather",
  description: "Fetch current weather information for a specified city",
  tool: getWeather,
  toolSchema: z
    .function()
    .args(z.string().describe("The city to fetch weather for"))
    .returns(
      z.object({
        location: z.object({
          name: z.string(),
        }),
      }),
    ),
};
 
//register the tool using the @tambo-ai/react package
const { registerTool, registerTools } = useTambo();
 
registerTool(weatherTool); // register a single tool
registerTools([weatherTool, anotherTool]); // register multiple tools at once

Now tambo can fetch weather information for a city when responding to a message!

On this page