handleToolCall method

Future<ToolResult> handleToolCall(
  1. String toolName,
  2. dynamic arguments,
  3. ToolInvocation invocation, {
  4. List<Tool> fallbackTools = const [],
})

Internal: handle a tool call from the CLI.

Implementation

Future<ToolResult> handleToolCall(
  String toolName,
  dynamic arguments,
  ToolInvocation invocation, {
  List<Tool> fallbackTools = const [],
}) async {
  // Check session-local tools first, then config tools, then client-level
  final tool = _tools[toolName] ??
      config.tools.cast<Tool?>().firstWhere(
            (t) => t!.name == toolName,
            orElse: () => null,
          ) ??
      fallbackTools.cast<Tool?>().firstWhere(
            (t) => t!.name == toolName,
            orElse: () => null,
          );

  if (tool == null) {
    return ToolResult.failure(
      error: 'Unknown tool: $toolName',
      textForLlm: 'Tool "$toolName" is not registered in this session.',
    );
  }

  try {
    return await tool.handler(arguments, invocation);
  } catch (e) {
    // Don't expose detailed error information to the LLM for security
    return ToolResult.failure(error: e.toString());
  }
}