or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

Files

docs

agents.mdchat-engines.mddocument-processing.mdembeddings.mdindex.mdllm-integration.mdquery-engines.mdresponse-synthesis.mdsettings.mdstorage.mdtools.mdvector-indexing.md
tile.json

tools.mddocs/

0

# Tools and Utilities

1

2

Tool integration system for extending agent capabilities with external functions and APIs in LlamaIndex.TS.

3

4

## Import

5

6

```typescript

7

import { QueryEngineTool, FunctionTool } from "llamaindex/tools";

8

// Or from main package

9

import { QueryEngineTool, FunctionTool } from "llamaindex";

10

```

11

12

## Overview

13

14

The tools system allows agents and workflows to interact with external systems, perform calculations, access APIs, and execute custom functions through a standardized interface.

15

16

## Base Tool Interface

17

18

```typescript { .api }

19

interface BaseTool {

20

metadata: ToolMetadata;

21

call(input: string): Promise<ToolOutput>;

22

}

23

24

interface ToolMetadata {

25

name: string;

26

description: string;

27

parameters?: {

28

type: "object";

29

properties: Record<string, any>;

30

required?: string[];

31

};

32

}

33

34

interface ToolOutput {

35

content: string;

36

tool: BaseTool;

37

rawInput: any;

38

rawOutput?: any;

39

isError?: boolean;

40

}

41

```

42

43

## QueryEngineTool

44

45

Wraps query engines as tools for use with agents.

46

47

```typescript { .api }

48

class QueryEngineTool implements BaseTool {

49

constructor(options: {

50

queryEngine: BaseQueryEngine;

51

metadata: ToolMetadata;

52

});

53

54

call(input: string): Promise<ToolOutput>;

55

56

metadata: ToolMetadata;

57

queryEngine: BaseQueryEngine;

58

}

59

```

60

61

## FunctionTool

62

63

Creates tools from JavaScript functions.

64

65

```typescript { .api }

66

class FunctionTool implements BaseTool {

67

static from<T extends (...args: any[]) => any>(

68

fn: T,

69

metadata: ToolMetadata

70

): FunctionTool;

71

72

constructor(options: {

73

fn: Function;

74

metadata: ToolMetadata;

75

});

76

77

call(input: string): Promise<ToolOutput>;

78

79

metadata: ToolMetadata;

80

}

81

```

82

83

## Basic Usage

84

85

### Query Engine Tool

86

87

```typescript

88

import { VectorStoreIndex, QueryEngineTool } from "llamaindex";

89

90

// Create knowledge base

91

const index = await VectorStoreIndex.fromDocuments(documents);

92

93

// Create query engine tool

94

const queryTool = new QueryEngineTool({

95

queryEngine: index.asQueryEngine(),

96

metadata: {

97

name: "knowledge_search",

98

description: "Search the company knowledge base for information",

99

},

100

});

101

102

// Use with agent

103

const agent = new ReActAgent({

104

tools: [queryTool],

105

llm: /* your LLM */,

106

});

107

```

108

109

### Function Tool

110

111

```typescript

112

import { FunctionTool } from "llamaindex/tools";

113

114

// Create calculator tool

115

const calculatorTool = FunctionTool.from(

116

({ a, b, operation }: { a: number; b: number; operation: string }) => {

117

switch (operation) {

118

case "add": return a + b;

119

case "subtract": return a - b;

120

case "multiply": return a * b;

121

case "divide": return a / b;

122

default: throw new Error("Unknown operation");

123

}

124

},

125

{

126

name: "calculator",

127

description: "Perform basic arithmetic operations",

128

parameters: {

129

type: "object",

130

properties: {

131

a: { type: "number", description: "First number" },

132

b: { type: "number", description: "Second number" },

133

operation: {

134

type: "string",

135

description: "Operation to perform",

136

enum: ["add", "subtract", "multiply", "divide"]

137

},

138

},

139

required: ["a", "b", "operation"],

140

},

141

}

142

);

143

```

144

145

## Advanced Tool Creation

146

147

### API Tool

148

149

```typescript

150

const apiTool = FunctionTool.from(

151

async ({ endpoint, method = "GET" }: { endpoint: string; method?: string }) => {

152

const response = await fetch(endpoint, { method });

153

const data = await response.json();

154

return JSON.stringify(data, null, 2);

155

},

156

{

157

name: "api_call",

158

description: "Make HTTP requests to APIs",

159

parameters: {

160

type: "object",

161

properties: {

162

endpoint: { type: "string", description: "API endpoint URL" },

163

method: { type: "string", enum: ["GET", "POST"], description: "HTTP method" },

164

},

165

required: ["endpoint"],

166

},

167

}

168

);

169

```

170

171

## Best Practices

172

173

```typescript

174

// Error handling in tools

175

const robustTool = FunctionTool.from(

176

async (params: any) => {

177

try {

178

// Tool logic here

179

return "Success";

180

} catch (error) {

181

throw new Error(`Tool failed: ${error.message}`);

182

}

183

},

184

{

185

name: "robust_tool",

186

description: "A tool with proper error handling",

187

}

188

);

189

```