Agent with custom tools for real-world tasks.
- Custom tool definitions with Zod validation
- Type-safe tool inputs/outputs
- Multi-step tool usage
- Error handling
This agent has tools for:
- Weather lookup
- Calculator
- Web search (mock)
- Email sending (mock)
npm install
npm startconst tools = new ToolRegistry();
tools.register(
tool({
name: "calculate",
description: "Perform math calculations",
schema: z.object({
expression: z.string()
}),
handler: async ({ expression }) => {
try {
return { result: eval(expression), success: true };
} catch (error) {
return { error: "Invalid expression", success: false };
}
}
})
);
const agent = createAgent({
name: "tool-agent",
client,
tools // Pass tools to agent
});
// Agent will automatically use tools when needed
await agent.run("What's 123 * 456?");tools.register(
tool({
name: "my_tool",
description: "What it does",
schema: z.object({
param1: z.string(),
param2: z.number().optional()
}),
handler: async (input) => {
// Your logic here
return { result: "..." };
}
})
);- Clear Descriptions: Help the agent understand when to use the tool
- Validate Inputs: Use Zod schemas for type safety
- Handle Errors: Return structured error objects
- Return Structured Data: Objects are better than strings
- Build a multi-agent system
- Add memory for context
- Create a web interface