Tools
A "tool" is a utility that can be called by an agent on behalf of an LLM. A tool can be called to perform custom actions, or retrieve extra information based on the LLM-generated input. A result from a tool call can be used by subsequent steps in a workflow, or to compute a final answer. For example, a "weather tool" could fetch some live weather information from a geographical location.
Tool Function
The tool
function is a utility provided to define a tool that can be used by an agent. It takes a function and a configuration object as arguments. The configuration object includes the tool's name, description, and parameters.
Parameters with Zod
The parameters
field in the tool configuration is defined using zod
, a TypeScript-first schema declaration and validation library. zod
allows you to specify the expected structure and types of the input parameters, ensuring that the data passed to the tool is valid.
Example:
In this example, z.object
is used to define a schema for the parameters
where question
is expected to be a string. This ensures that any input to the tool adheres to the specified structure, providing a layer of type safety and validation.
Built-in tools
You can import built-in tools from the @llamaindex/tools
package.
MCP tools
If you have a MCP server running, you can fetch tools from the server and use them in your agents.
Function tool
You can still use the FunctionTool
class to define a tool.
A FunctionTool
is constructed from a function with signature
where
input
is generated by the LLM,T
is the type defined by the toolparameters
additionalArg
is an optional extra argument, see "Binding" belowR
is the return type
Binding
An additional argument can be bound to a tool, each tool call will be passed
- the input provided by the LLM
- the additional argument (extends object)
Note: calling the bind
method will return a new FunctionTool
instance, without modifying the tool which bind
is called on.
Example to pass a userToken
as additional argument: