Building Tools
Build reusable, annotated, and class-based tools that your AI agents can call during conversations.
Tools give agents the ability to call your application code during a conversation. This page covers all the ways to create custom tools — from quick inline closures to full class hierarchies.
Quick Tool via aiTool()
The fastest way to create a tool is with the aiTool() BIF:
searchTool = aiTool(
name : "searchProducts",
description : "Search the product catalog",
parameters : [
{ name: "query", type: "string", description: "Search term", required: true },
{ name: "maxResults", type: "number", description: "Max results to return", required: false }
],
callback : ( query, maxResults = 10 ) => productService.search( query, maxResults )
)
agent = aiAgent( name: "shop-assistant", tools: [ searchTool ] )See aiTool() Reference for full parameter docs.
Accessing Conversation Context (_chatRequest)
Closures receive _chatRequest injected into their arguments so you can access the full conversation context:
Register via Tool Registry
Instead of passing tools to each agent individually, register them globally:
See Tool Registry for the full registry API.
Annotated Class Tools (@AITool)
Annotate methods with @AITool and scan the class — no manual registration needed:
Annotation formats:
@AITool
Uses method name + @hint as description
@AITool( "Description" )
Explicit description string
@AITool( { name: "x", description: "y" } )
Full struct with custom name
Extending BaseTool
For maximum control — parameter validation, custom schema, fluent API — extend BaseTool:
Fluent Schema Helpers
BaseTool provides helper methods for building schemas without writing JSON manually:
Tool Lifecycle Events
Every tool invocation fires BoxLang interceptor events:
beforeAIToolExecute
Before tool executes
tool, toolName, args, chatRequest
afterAIToolExecute
After tool executes
tool, toolName, args, result, duration
Related Pages
Tools — Using tools with agents and models
Tool Registry — Register and resolve tools globally
Agents — Tools & MCP — Agent-specific tool patterns
aiTool() Reference — BIF reference
Last updated