Skip to content

FunctionTool

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

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:148

Creates a new FunctionTool instance.

| Parameter | Type | Description | | --------- | ---------------------------------------------- | --------------------------------- | | config | FunctionToolConfig | Configuration 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:104

The unique name of the tool.

InvokableTool.name

Tool.name


readonly description: string;

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

Human-readable description of what the tool does.

InvokableTool.description

Tool.description


readonly toolSpec: ToolSpec;

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

OpenAPI JSON specification for the tool.

InvokableTool.toolSpec

Tool.toolSpec

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

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

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

| Parameter | Type | Description | | ------------- | -------------------------------- | ----------------------------------------------------------------------- | | toolContext | ToolContext | Context information including the tool use request and invocation state |

AsyncGenerator<ToolStreamEvent, ToolResultBlock, unknown>

Async generator that yields ToolStreamEvents and returns a ToolResultBlock

InvokableTool.stream

Tool.stream


invoke(input, context?): Promise<JSONValue>;

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

Invokes the tool directly with raw input and returns the unwrapped result.

Unlike stream(), this method:

  • Returns the raw result (not wrapped in ToolResult)
  • Consumes async generators and returns the generator’s return value
  • Lets errors throw naturally (not wrapped in error ToolResult)

| Parameter | Type | Description | | ---------- | -------------------------------- | --------------------------------- | | input | unknown | The input parameters for the tool | | context? | ToolContext | Optional tool execution context |

Promise<JSONValue>

The unwrapped result

InvokableTool.invoke