Skip to content

FunctionTool

Defined in: src/tools/function-tool.ts:91

A Tool implementation that wraps a callback function and handles all ToolResultBlock conversion.

FunctionTool allows creating tools from existing functions without needing to manually handle ToolResultBlock formatting or error handling. It supports multiple callback patterns:

  • Async generators for streaming responses
  • Promises for async operations
  • Synchronous functions for immediate results

All return values are automatically wrapped in ToolResultBlock, and errors are caught and returned as error ToolResultBlocks.

// Create a tool with streaming
const streamingTool = new FunctionTool({
name: 'processor',
description: 'Processes data with progress updates',
inputSchema: { type: 'object', properties: { data: { type: 'string' } } },
callback: async function* (input: any) {
yield 'Starting processing...'
// Do some work
yield 'Halfway done...'
// More work
return 'Processing complete!'
}
})
new FunctionTool(config): FunctionTool;

Defined in: src/tools/function-tool.ts:139

Creates a new FunctionTool instance.

ParameterTypeDescription
configFunctionToolConfigConfiguration object for the tool

FunctionTool

// Tool with input schema
const greetTool = new FunctionTool({
name: 'greeter',
description: 'Greets a person by name',
inputSchema: {
type: 'object',
properties: { name: { type: 'string' } },
required: ['name']
},
callback: (input: any) => `Hello, ${input.name}!`
})
// Tool without input (no parameters)
const statusTool = new FunctionTool({
name: 'getStatus',
description: 'Gets system status',
callback: () => ({ status: 'operational' })
})

Tool.constructor

readonly name: string;

Defined in: src/tools/function-tool.ts:95

The unique name of the tool.

Tool.name


readonly description: string;

Defined in: src/tools/function-tool.ts:100

Human-readable description of what the tool does.

Tool.description


readonly toolSpec: ToolSpec;

Defined in: src/tools/function-tool.ts:105

OpenAPI JSON specification for the tool.

Tool.toolSpec

stream(toolContext): AsyncGenerator<ToolStreamEvent, ToolResultBlock, unknown>;

Defined in: src/tools/function-tool.ts:166

Executes the tool with streaming support. Handles all callback patterns (async generator, promise, sync) and converts results to ToolResultBlock.

ParameterTypeDescription
toolContextToolContextContext information including the tool use request and invocation state

AsyncGenerator<ToolStreamEvent, ToolResultBlock, unknown>

Async generator that yields ToolStreamEvents and returns a ToolResultBlock

Tool.stream