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
Section titled “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
Section titled “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:
import { tool } from "llamaindex";import { agent } from "@llamaindex/workflow";import { z } from "zod";
// first arg is LLM input, second is bound argconst queryKnowledgeBase = async ({ question }, { userToken }) => { const response = await fetch(`https://knowledge-base.com?token=${userToken}&query=${question}`); // ...};
// define tool with zod validationconst kbTool = tool(queryKnowledgeBase, { name: 'queryKnowledgeBase', description: 'Query knowledge base', parameters: z.object({ question: z.string({ description: 'The user question', }), }),});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
Section titled “Built-in tools”You can import built-in tools from the @llamaindex/tools package.
import { agent } from "@llamaindex/workflow";import { wiki } from "@llamaindex/tools";
const researchAgent = agent({ name: "WikiAgent", description: "Gathering information from the internet", systemPrompt: `You are a research agent. Your role is to gather information from the internet using the provided tools.`, tools: [wiki()],});MCP tools
Section titled “MCP tools”If you have a MCP server running, you can fetch tools from the server and use them in your agents.
// 1. Import MCP tools adapterimport { mcp } from "@llamaindex/tools";import { agent } from "@llamaindex/workflow";
// 2. Initialize a MCP client// by npxconst server = mcp({ command: "npx", args: ["-y", "@modelcontextprotocol/server-filesystem", "."], verbose: true,});// or by StreamableHTTP transportconst server = mcp({ url: "http://localhost:8000/mcp", verbose: true,});
// if your MCP server is not using StreamableHTTP transport, you can also use SSE transport// by setting useSSETransport to true.// See: https://modelcontextprotocol.io/docs/concepts/transports#server-sent-events-sse-deprecatedconst server = mcp({ url: "http://localhost:8000/mcp", useSSETransport: true, verbose: true,});
// 3. Get tools from MCP serverconst tools = await server.tools();
// Now you can create an agent with the toolsconst agent = agent({ name: "My Agent", systemPrompt: "You are a helpful assistant that can use the provided tools to answer questions.", llm: openai({ model: "gpt-4o" }), tools: tools,});You can also use MCP Toolbox for Databases to interact with MCP tools.
Function tool
Section titled “Function tool”You can still use the FunctionTool class to define a tool.
A FunctionTool is constructed from a function with signature
(input: T, additionalArg?: AdditionalToolArgument) => Rwhere
inputis generated by the LLM,Tis the type defined by the toolparametersadditionalArgis an optional extra argument, see “Binding” belowRis the return type
Binding
Section titled “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:
import { tool } from "llamaindex";import { agent } from "@llamaindex/workflow";
// first arg is LLM input, second is bound argconst queryKnowledgeBase = async ({ question }, { userToken }) => { const response = await fetch(`https://knowledge-base.com?token=${userToken}&query=${question}`); // ...};
// define tool as usualconst kbTool = tool(queryKnowledgeBase, { name: 'queryKnowledgeBase', description: 'Query knowledge base', parameters: z.object({ question: z.string({ description: 'The user question', }), }),});
// create an agentconst additionalArg = { userToken: 'abcd1234' };const workflow = agent({ tools: [kbTool.bind(additionalArg)], // llm, systemPrompt etc})