BaseTool

abstract class BaseTool<ARGS, R>(val name: String, val description: String? = null)

Base class for defining callable tools that an LLM can invoke.

Tools allow the model to interact with external systems (databases, APIs, etc.). To create a tool:

  1. Subclass BaseTool with your args type and return type.

  2. Define a @Serializable data class for your arguments.

  3. Annotate argument properties with @ToolParam to provide descriptions for the JSON schema.

  4. Override argsSerializer and execute.

Type Parameters

ARGS

The type of the tool's input arguments.

R

The return type of the tool's execution.

Inheritors

Constructors

Link copied to clipboard
constructor(name: String, description: String? = null)

Properties

Link copied to clipboard
abstract val argsSerializer: KSerializer<ARGS>

The kotlinx.serialization KSerializer for the ARGS type. Used to generate the JSON schema for the tool's parameters.

Link copied to clipboard
open val description: String?

A human-readable description of what the tool does.

Link copied to clipboard
open val name: String

The tool's name (used by the model to reference it).

Link copied to clipboard
abstract val resultSerializer: KSerializer<R>

The kotlinx.serialization KSerializer for the R type. Used to generate the JSON schema for the tool's results.

Functions

Link copied to clipboard
abstract suspend fun execute(args: ARGS): R

Executes the tool with the given parsed arguments.

Link copied to clipboard
open suspend fun executeRaw(argsJson: String, json: Json = Json { ignoreUnknownKeys = true }): String

Executes the tool from raw JSON arguments.