text
stringlengths
0
59.1k
**Understanding VoltAgent Tools:** In VoltAgent, tools are the actions your agent can perform. Think of them as functions that your AI agent can call when needed. Each tool has a single responsibility, validates inputs with Zod, returns structured responses, and surfaces clear errors so runtime behaviour stays predicta...
Let's examine each tool:
### List Menu Items Tool
This tool retrieves available menu items from the database:
<details>
<summary>View code</summary>
```typescript
import { createTool } from "@voltagent/core";
import { z } from "zod";
import { supabase } from "../../lib/supabase";
export const listMenuItemsTool = createTool({
name: "listMenuItems",
description: "Lists all menu items from the Supabase database",
parameters: z.object({
limit: z.number().optional().default(100).describe("Number of items to fetch"),
offset: z.number().optional().default(0).describe("Number of items to skip"),
}),
execute: async ({ limit, offset }) => {
try {
const { data, error } = await supabase
.from("menu_items")
.select("*")
.range(offset, offset + limit - 1)
.order("id", { ascending: true });
if (error) {
throw new Error(`Failed to fetch menu items: ${error.message}`);
}
return {
success: true,
data: data || [],
count: data?.length || 0,
message: `Successfully fetched ${data?.length || 0} menu items`,
};
} catch (error) {
return {
success: false,
error: error instanceof Error ? error.message : "Unknown error occurred",
data: [],
};
}
},
});
```
</details>
![List tool](https://cdn.voltagent.dev/examples/with-whatsapp/list-tool.png)
**What this tool does:** Fetches menu items from Supabase with optional pagination, returning a structured response that keeps error handling and schema validation in one place so the agent can safely show the menu whenever a conversation starts.
## Create Order Tool
This tool processes and saves customer orders to the database:
<details>
<summary>View code</summary>
```typescript
import { createTool } from "@voltagent/core";
import { z } from "zod";
import { supabase } from "../../lib/supabase";
export const createOrderTool = createTool({
name: "createOrder",
description: "Creates a new order with the items and delivery address from working memory",
parameters: z.object({
items: z
.array(
z.object({
menuItemId: z.number().describe("ID of the menu item"),
itemName: z.string().describe("Name of the menu item"),
quantity: z.number().describe("Quantity of the item"),
price: z.number().describe("Price per item"),
})
)
.describe("List of ordered items"),
deliveryAddress: z.string().describe("Delivery address for the order"),
customerNotes: z.string().optional().describe("Optional customer notes for the order"),
}),
execute: async ({ items, deliveryAddress, customerNotes }, context) => {
try {
// Get customer phone from context userId
const customerPhone = context?.userId || "unknown";
// Calculate total amount
const totalAmount = items.reduce((sum, item) => sum + item.price * item.quantity, 0);
// Create order in orders table
const { data: orderData, error: orderError } = await supabase
.from("orders")
.insert({
customer_phone: customerPhone,