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:
Shipping Tools From Your Own Module
A module can offer tools to BoxLang AI without requiring it. The module registers its @AITool class when BoxLang AI is present, and does nothing when it is not. This is how the bx-audio module adds its audio tools.
Put the tools in a class, and register it from your ModuleConfig:
Do not use this.dependencies for this: it makes BoxLang AI mandatory. Keep the tools in the module's own namespace (@mymodule) so they never collide with @bxai tools, and give every @AITool method a description and a hint for each parameter, because that is all the model sees.
If your tools touch files, validate every path from the model against allowed folders, and never overwrite existing files.
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