Skip to main content

Tools Module

The kotlin-tools module provides a framework for executing client-side tools that agents can call during conversations. Tools run locally on the client device, enabling secure access to device capabilities while maintaining privacy.

Installation

Note: The tools module is automatically included when using kotlin-client.

Core Components

ToolExecutor

Interface for implementing custom tools that agents can call.
  • Defines tool execution logic
  • Handles validation and error handling
  • Provides timeout configuration
Learn more about ToolExecutor →

ToolRegistry

Manages and executes registered tools.
  • Tool registration and discovery
  • Execution with timeout handling
  • Statistics and monitoring
  • Thread-safe concurrent access
Learn more about ToolRegistry →

Key Concepts

Client-Side Execution

Tools execute on the client device, not on the agent’s server:
  • Access to local device capabilities (location, camera, file system)
  • Enhanced privacy - sensitive data stays on device
  • Reduced server load
  • Custom business logic integration

Tool Lifecycle

  1. Registration: Tools are registered with a ToolRegistry
  2. Discovery: Agent receives tool definitions during conversation
  3. Request: Agent requests tool execution via ToolCall events
  4. Execution: Client executes tool and returns result
  5. Response: Agent receives tool result and continues conversation

Quick Start

Basic Tool Implementation

Tool Registration

Using with Agents

Tool Execution Features

Validation

Tools can validate arguments before execution:

Timeouts

Configure maximum execution time:

Error Handling

Handle different types of errors:

Statistics and Monitoring

Execution Statistics

Track tool performance:

Registry Information

Inspect registered tools:

Best Practices

Tool Design

  • Keep tools focused: One tool, one responsibility
  • Validate inputs: Always check parameters before execution
  • Handle errors gracefully: Return meaningful error messages
  • Set appropriate timeouts: Prevent hanging operations

Performance

  • Avoid blocking: Use suspend functions for I/O operations
  • Be efficient: Tools should execute quickly
  • Cache when appropriate: Store expensive computations

Security

  • Validate all inputs: Never trust tool call arguments
  • Limit access: Only expose necessary capabilities
  • Handle sensitive data: Be careful with user information

Error Handling

Resource Management

Platform Considerations

Android

  • Request appropriate permissions for device access
  • Handle runtime permission requests
  • Consider background execution limits

iOS

  • Request usage permissions (location, camera, etc.)
  • Handle app lifecycle events
  • Consider iOS privacy restrictions

JVM

  • File system access works normally
  • Network operations available
  • Consider server environment limitations

Common Tool Examples

Location Tool