Skip to content

Tools

Tools enable agents to interact with external systems, perform calculations, and access data. Viber uses a decorator-based approach with automatic Zod schema generation.

import { tool } from 'viber';
import { z } from 'zod';
const calculator = tool({
name: 'calculator',
description: 'Perform mathematical calculations',
parameters: z.object({
expression: z.string().describe('Mathematical expression to evaluate'),
}),
execute: async ({ expression }) => {
return { result: eval(expression) };
},
});
const agent = new Agent({
name: 'MathAssistant',
model: 'openai:gpt-4o',
systemPrompt: 'You are a math tutor.',
tools: [calculator],
});

The parameters field uses Zod schemas, which are automatically converted to JSON Schema for LLM consumption:

const searchTool = tool({
name: 'web_search',
description: 'Search the web for information',
parameters: z.object({
query: z.string().describe('Search query'),
limit: z.number().optional().default(10).describe('Max results'),
}),
execute: async ({ query, limit }) => {
// Implementation
},
});

Viber includes several built-in tools:

ToolDescription
readFileRead file contents from the workspace
writeFileWrite content to a file
shellCommandExecute shell commands (sandboxed)
webSearchSearch the web
webScrapeExtract content from URLs

Tools should handle errors gracefully:

const safeTool = tool({
name: 'safe_operation',
parameters: z.object({ input: z.string() }),
execute: async ({ input }) => {
try {
const result = await riskyOperation(input);
return { success: true, result };
} catch (error) {
return { success: false, error: error.message };
}
},
});