Skip to content

InvokableTool

Defined in: src/tools/tool.ts:165

Extended tool interface that supports direct invocation with type-safe input and output. This interface is useful for testing and standalone tool execution.

Type ParameterDescription
TInputType for the tool’s input parameters
TReturnType for the tool’s return value
abstract name: string;

Defined in: src/tools/tool.ts:108

The unique name of the tool. This MUST match the name in the toolSpec.

Tool.name


abstract description: string;

Defined in: src/tools/tool.ts:115

Human-readable description of what the tool does. This helps the model understand when to use the tool.

This MUST match the description in the toolSpec.description.

Tool.description


abstract toolSpec: ToolSpec;

Defined in: src/tools/tool.ts:120

OpenAPI JSON specification for the tool. Defines the tool’s name, description, and input schema.

Tool.toolSpec

abstract stream(toolContext): ToolStreamGenerator;

Defined in: src/tools/tool.ts:155

Executes the tool with streaming support. Yields zero or more ToolStreamEvents during execution, then returns exactly one ToolResultBlock as the final value.

ParameterTypeDescription
toolContextToolContextContext information including the tool use request and invocation state

ToolStreamGenerator

Async generator that yields ToolStreamEvents and returns a ToolResultBlock

const context = {
toolUse: {
name: 'calculator',
toolUseId: 'calc-123',
input: { operation: 'add', a: 5, b: 3 }
},
}
// The return value is only accessible via explicit .next() calls
const generator = tool.stream(context)
for await (const event of generator) {
// Only yields are captured here
console.log('Progress:', event.data)
}
// Or manually handle the return value:
let result = await generator.next()
while (!result.done) {
console.log('Progress:', result.value.data)
result = await generator.next()
}
console.log('Final result:', result.value.status)

Tool.stream


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

Defined in: src/tools/tool.ts:178

Invokes the tool directly with type-safe input and returns the unwrapped result.

Unlike stream(), this method:

  • Returns the raw result (not wrapped in ToolResult)
  • Consumes async generators and returns only the final value
  • Lets errors throw naturally (not wrapped in error ToolResult)
ParameterTypeDescription
inputTInputThe input parameters for the tool
context?ToolContextOptional tool execution context

Promise<TReturn>

The unwrapped result