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.
Example
Section titled “Example”// Create a tool with streamingconst 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!' }})Extends
Section titled “Extends”Constructors
Section titled “Constructors”Constructor
Section titled “Constructor”new FunctionTool(config): FunctionTool;Defined in: src/tools/function-tool.ts:139
Creates a new FunctionTool instance.
Parameters
Section titled “Parameters”| Parameter | Type | Description |
|---|---|---|
config | FunctionToolConfig | Configuration object for the tool |
Returns
Section titled “Returns”FunctionTool
Example
Section titled “Example”// Tool with input schemaconst 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' })})Overrides
Section titled “Overrides”Properties
Section titled “Properties”readonly name: string;Defined in: src/tools/function-tool.ts:95
The unique name of the tool.
Overrides
Section titled “Overrides”description
Section titled “description”readonly description: string;Defined in: src/tools/function-tool.ts:100
Human-readable description of what the tool does.
Overrides
Section titled “Overrides”toolSpec
Section titled “toolSpec”readonly toolSpec: ToolSpec;Defined in: src/tools/function-tool.ts:105
OpenAPI JSON specification for the tool.
Overrides
Section titled “Overrides”Methods
Section titled “Methods”stream()
Section titled “stream()”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.
Parameters
Section titled “Parameters”| Parameter | Type | Description |
|---|---|---|
toolContext | ToolContext | Context information including the tool use request and invocation state |
Returns
Section titled “Returns”AsyncGenerator<ToolStreamEvent, ToolResultBlock, unknown>
Async generator that yields ToolStreamEvents and returns a ToolResultBlock