Skip to content

FunctionToolCallback

type FunctionToolCallback = (input, toolContext) =>
| AsyncGenerator<JSONValue, JSONValue, never>
| Promise<JSONValue>
| JSONValue;

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

Callback function for FunctionTool implementations. The callback can return values in multiple ways, and FunctionTool handles the conversion to ToolResultBlock.

| Parameter | Type | Description | | ------------- | -------------------------------- | --------------------------------------------------------- | | input | unknown | The input parameters conforming to the tool’s inputSchema | | toolContext | ToolContext | The tool execution context with invocation state |

| AsyncGenerator<JSONValue, JSONValue, never> | Promise<JSONValue> | JSONValue

Can return:

  • AsyncGenerator: Each yielded value becomes a ToolStreamEvent, final value wrapped in ToolResultBlock
  • Promise: Resolved value is wrapped in ToolResultBlock
  • Synchronous value: Value is wrapped in ToolResultBlock
  • If an error is thrown, it’s handled and returned as an error ToolResultBlock
// Async generator example
async function* calculator(input: unknown, context: ToolContext) {
yield 'Calculating...'
const result = input.a + input.b
yield `Result: ${result}`
return result
}
// Promise example
async function fetchData(input: unknown, context: ToolContext) {
const response = await fetch(input.url)
return await response.json()
}
// Synchronous example
function multiply(input: unknown, context: ToolContext) {
return input.a * input.b
}