AgentContextAwareTool

abstract class AgentContextAwareTool<TArgs, TResult>(argsType: TypeToken, resultType: TypeToken, descriptor: ToolDescriptor, metadata: Map<String, String> = emptyMap()) : ToolBase<TArgs, TResult> (source)

A tool whose execute receives the live AIAgentContext driving the current call.

The framework injects the context under AgentContextKey in ToolCallMetadata before dispatch (see ai.koog.agents.core.environment.ContextualAgentEnvironment). This subclass reads that entry and forwards it to the user-facing overload, so implementors get a typed AIAgentContext parameter rather than a Map lookup.

Use this when a tool needs the agent's full state (LLM context, run id, configuration, storage, ...). For tools that only need the typed arguments, extend Tool instead; for tools that read raw ToolCallMetadata entries contributed by features (for example a trace span id), extend ToolBase directly.

Invoking an AgentContextAwareTool outside an agent run is a programming error: with no AgentContextKey entry in the metadata, execute throws IllegalStateException naming the tool. Tests that need to exercise such a tool can construct metadata via ToolCallMetadata.of(AgentContextAwareTool.AgentContextKey to context) or call execute(args, context) directly.

Type Parameters

TArgs

The type of arguments the tool accepts.

TResult

The type of result the tool returns.

Constructors

Link copied to clipboard
constructor(argsType: TypeToken, resultType: TypeToken, descriptor: ToolDescriptor, metadata: Map<String, String> = emptyMap())
constructor(argsType: TypeToken, resultType: TypeToken, name: String, description: String, jsonSchemaConfig: JsonSchemaConfig = defaultJsonSchemaConfig)

Convenience constructor that generates ToolDescriptor from the provided name, description and argsType.

Types

Link copied to clipboard
object Companion

Properties

Link copied to clipboard
val argsType: TypeToken
Link copied to clipboard
Link copied to clipboard
Link copied to clipboard
Link copied to clipboard
val resultType: TypeToken

Functions

Link copied to clipboard
open fun decodeArgs(rawArgs: JSONObject, serializer: JSONSerializer): TArgs
Link copied to clipboard
open fun decodeResult(rawResult: JSONElement, serializer: JSONSerializer): TResult
Link copied to clipboard
open fun encodeArgs(args: TArgs, serializer: JSONSerializer): JSONObject
Link copied to clipboard
fun encodeArgsToString(args: TArgs, serializer: JSONSerializer): String
Link copied to clipboard
fun encodeArgsToStringUnsafe(args: Any?, serializer: JSONSerializer): String
Link copied to clipboard
fun encodeArgsUnsafe(args: Any?, serializer: JSONSerializer): JSONObject
Link copied to clipboard
open fun encodeResult(result: TResult, serializer: JSONSerializer): JSONElement
Link copied to clipboard
open fun encodeResultToString(result: TResult, serializer: JSONSerializer): String
Link copied to clipboard
fun encodeResultToStringUnsafe(result: Any?, serializer: JSONSerializer): String
Link copied to clipboard
fun encodeResultUnsafe(result: Any?, serializer: JSONSerializer): JSONElement
Link copied to clipboard
abstract suspend fun execute(args: TArgs, context: AIAgentContext): TResult

Executes the tool's logic with the provided arguments and the live AIAgentContext driving the current call.

suspend override fun execute(args: TArgs, metadata: ToolCallMetadata): TResult
Link copied to clipboard
suspend fun executeUnsafe(args: Any?): TResult
suspend fun executeUnsafe(args: Any?, metadata: ToolCallMetadata): TResult