@ag-kit/tools API Reference
Complete API documentation for the@ag-kit/tools package. This reference covers all available tools, their interfaces, method signatures, and practical usage examples with the latest features including MultiEditTool, Transaction support, and enhanced toolkits.
API Overview
File System API
Complete API reference for file operations with MultiEditTool, Transaction support, and multiple backends
Code Execution API
Multi-language code execution with E2B sandbox and local environments
Command Line API
Shell command execution with security validation and multi-command support
Browser API
Web automation with Playwright and E2B sandbox integration
MCP Protocol API
Model Context Protocol integration for external tool ecosystems
Installation
Core Interfaces
BaseTool Interface
All tools extend the abstractBaseTool class with advanced generic support and lifecycle management:
- Generic Type Safety: Full TypeScript generics for schema, state, and output types
- Automatic Validation: Built-in input validation using Zod schemas
- Context Support: Execution context with conversation, user, and state tracking
- Approval Workflow: Optional approval requirement for sensitive operations
- Error Handling: Comprehensive error categorization and timing metrics
- Metadata Access: Tool introspection and display customization
ToolResult Interface
Standardized result format implemented as a class with comprehensive error categorization:success: Boolean indicating operation success/failuredata: Tool output data (when successful) - strongly typed based on toolerror: Detailed error message (when failed)error_type: Categorized error type for programmatic handlingexecutionTime: Execution duration in milliseconds for performance monitoring
validation: Input validation failuressecurity: Security violations (path traversal, unauthorized access)file_not_found: Missing files or directoriespermission: Access denied or insufficient permissionsexecution: Runtime execution errorsunknown: Uncategorized errors
BaseToolkit Interface
Comprehensive toolkit class for organizing and managing collections of related tools:- Generic State Support: Type-safe state management with generics
- Lifecycle Hooks: Overridable
onInitialize()andonDestroy()methods - Batch Operations: Execute multiple tools with
invokeTools() - Tool Discovery: Search and filter tools by name or description
- Event System: Listen to toolkit and tool lifecycle events
- Context Sharing: Shared execution context across all tools
- Validation: Built-in integrity checking and error reporting
- Cloning: Create toolkit copies with different configurations
DynamicTool Class
Dynamic tool implementation for creating tools from functions without subclassing:tool() Helper Function
Convenient factory function for creating DynamicTool instances:ToolkitManager Class
Centralized manager for multiple toolkits with global tool discovery and lifecycle management:ToolkitEvent System
Comprehensive event system for monitoring toolkit and tool lifecycle:API Usage Patterns
Tool Creation
Create tools using factory functions, thetool() helper, or by extending BaseTool:
Tool Invocation
All tools follow the same async invocation pattern with comprehensive error handling:Error Handling
Handle different error types with specific recovery strategies:API Features
Type Safety
- Full TypeScript Support: Compile-time type checking for all tool inputs and outputs
- Runtime Validation: Zod schema validation with detailed error messages
- Strongly Typed Results: Tool-specific return types with IntelliSense support
- Generic Interfaces: Flexible typing for custom tool development
Async Operations
- Promise-based API: All operations return promises for async/await support
- Timeout Control: Configurable timeouts for long-running operations
- Resource Management: Automatic cleanup and resource disposal
- Cancellation Support: AbortController integration for operation cancellation
Error Handling
- Structured Error Types: Categorized errors for programmatic handling
- Detailed Context: Rich error information with stack traces and metadata
- Consistent Format: Uniform error structure across all tools
- Performance Monitoring: Execution time tracking and performance metrics
Advanced Features
- Transaction Support: Atomic operations with rollback capability
- Multi-Backend Support: Local, In-Memory, and Sandbox execution environments
- Batch Operations: MultiEditTool for efficient bulk operations
- Toolkit Management: Organized tool collections with lifecycle management
- Event System: Tool and toolkit lifecycle events for monitoring
- Security Features: Path validation, sandboxing, and permission controls
Tool Categories
File System Tools
API for file and directory operations with multiple backend support, including:- Individual Tools: Read, Write, Edit, MultiEdit, Glob, Grep, Ls operations
- Transaction Support: Atomic operations with rollback capability
- Multiple Backends: Local, In-Memory, and Sandbox file operators
- Enhanced Toolkit: Configurable modes for different use cases
Code Execution Tools
Secure multi-language code execution with sandbox isolation: Executors:- BuiltInCodeExecutor: E2B sandbox execution with full isolation
- UnsafeLocalCodeExecutor: Local execution for trusted environments
- Python, JavaScript/Node.js, TypeScript, Bash, R, Java, Go, C++, and more
- Timeout control, resource limits, file system integration
- Real-time output streaming and error handling
Command Line Tools
Shell command execution with security validation and multi-command support: Tools:- BashTool: Single command execution with environment control
- MultiCommandTool: Sequential command execution with state persistence
- Command validation, path restrictions, environment isolation
- Built-in command builders for common operations
Browser Automation Tools
Web automation with Playwright and E2B sandbox integration: Features:- Secure browser automation in isolated environments
- Screenshot capture, element interaction, JavaScript execution
- VNC access for visual debugging
MCP Integration
Model Context Protocol integration for external tool ecosystems: Components:- MCPToolkit: Manage tools from multiple MCP servers
- MCPClientManager: Connection management and tool discovery
- Transport Support: stdio, SSE, HTTP streaming
API Reference
File System API
Complete API reference with MultiEditTool, Transaction support, and multiple backends
Code Execution API
Multi-language code execution with E2B sandbox and local environments
Command Line API
Shell command execution with security validation and multi-command support
Browser API
Web automation with Playwright and E2B sandbox integration
MCP Protocol API
Model Context Protocol integration for external tool ecosystems
Latest Features & Examples
New in This Version
MultiEditTool
Perform multiple sequential edits on a single file with atomic operations:Transaction Support
Group multiple file operations with automatic rollback on failure:Enhanced FilesystemToolkit
Choose the optimal tool configuration for your use case:Toolkit Management
Organize and manage multiple toolkits:Related Documentation
- Tools Framework Guide - Comprehensive tools framework documentation
- Built-in Tools - Pre-built tool capabilities
- Custom Tool Development - Build your own tools
- Agent Integration - Using tools with AG-Kit agents