tool

fun tool(fn: KFunction<*>): Tool

Creates a Tool instance from a handler function by validating its structure, annotations, and parameters.

The function ensures that the handler function meets the required conditions:

  • It must be annotated with ToolFunction to provide a name and description.

  • Its parameters must be annotated with ToolParameter to define their metadata.

  • If applicable, parameters can optionally use AllowedValues to constrain their valid input values.

  • Its return type must be non-nullable and serializable.

Return

A Tool instance representing the validated and structured tool, ready for execution.

Parameters

fn

The suspend function representing the tool's behavior. It must be annotated with @ToolFunction and its parameters must be described using @ToolParameter annotations.

Samples

import io.fletchly.comparator.annotation.ToolFunction
import io.fletchly.comparator.annotation.ToolParameter
import io.fletchly.comparator.tool.tool

fun main() { 
   //sampleStart 
   // Tool handler definition
@ToolFunction("adder", "Adds two numbers")
fun adderToolHandler(
    @ToolParameter("First number to be added") num1: Int,
    @ToolParameter("Second number to be added") num2: Int
): Int = num1 + num2

// Tool object creation
val adderTool = tool(::adderToolHandler) 
   //sampleEnd
}