Overview
TheClientTools class handles registration and execution of client-side tools that can be called by the conversational AI agent. It supports both synchronous and asynchronous tools and runs them in a dedicated event loop to ensure non-blocking operation.
Constructor
asyncio.AbstractEventLoop
Optional custom asyncio event loop to use for tool execution. If not provided, a new event loop will be created and run in a separate thread. Using a custom loop prevents “different event loop” runtime errors and allows for better context propagation and resource management.
Methods
start
stop
register
str
required
Unique identifier for the tool. This name will be used by the agent to call the tool.
Union[Callable[[dict], Any], Callable[[dict], Awaitable[Any]]]
required
Function that implements the tool’s logic. Receives a dictionary of parameters and returns the tool result.
bool
Whether the handler is an async function. Defaults to False.
ValueErrorif the handler is not callableValueErrorif a tool with the same name is already registered
handle
str
required
The name of the tool to execute.
dict
required
Parameters to pass to the tool handler.
ValueError if the tool is not registered.
execute_tool
str
required
The name of the tool to execute.
dict
required
Parameters to pass to the tool handler.
Callable[[dict], None]
required
Callback function to send the result to.
RuntimeError if the ClientTools event loop is not running.
Examples
Synchronous Tool
Asynchronous Tool
Multiple Tools
Using Custom Event Loop
Tool Handler Requirements
Parameter Format
Tool handlers receive parameters as a dictionary. The agent will call your tool with parameters based on the tool definition configured in your agent settings.Return Values
Tools can return any JSON-serializable value:- Strings
- Numbers
- Dictionaries
- Lists
- Booleans
- None