const searchTool = defineTool({
schema: {
name: 'search',
description: 'Search for information on the web.',
parameters: {
type: 'object',
properties: {
query: { type: 'string', description: 'Search query' },
},
required: ['query'],
},
},
async execute({ query }) {
return { results: [`Result for: ${query}`] }
},
})
const calculatorTool = defineTool({
schema: {
name: 'calculator',
description: 'Perform mathematical calculations.',
parameters: {
type: 'object',
properties: {
expression: { type: 'string', description: 'Math expression' },
},
required: ['expression'],
},
},
async execute({ expression }) {
// Use a safe math evaluator in production
return { result: eval(expression) }
},
})
const agent = createAgent({
name: 'multi-tool-agent',
systemPrompt: 'You are a helpful assistant. Use tools when needed.',
})
.provider(openai({ apiKey: process.env['OPENAI_API_KEY'], model: 'gpt-4o' }))
.tool(searchTool)
.tool(calculatorTool)
const result = await agent.run({
input: 'Search for the population of Tokyo and calculate 12345 * 6789',
})
// The agent will intelligently call both tools