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.

Tambo uses existing LLM tool calling capabilities to call your tools. For more details see the tool documentation at OpenAI and Anthropic.

interface TamboTool {
  /**
   * The name of the tool function. This is the name that will be passed
   * to the LLM when calling the tool.
   */
  name: string;
  /**
   * Description of the tool and when tambo should use it this will be
   * passed to the LLM to help it decide when to use the tool.
   */
  description: string;
  /** The function that tambo will call when using this tool.   */
  tool: (input: any) => any;
  /**
   * 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.
   */
  toolSchema: z.ZodFunction<Args, Returns>;
}

Adding a Tool

Define your tool function and register it with tambo. Tools are asynchronous functions that take in a single argument and return a single value.

//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