Overview
Loaf’s tool system provides an extensible runtime for executing operations. Tools are functions with typed inputs and outputs that loaf can invoke to perform file operations, run commands, search the web, and more.Architecture
Tool Definition
Every tool implements theToolDefinition interface:
Tool Context
Tools receive a context object with runtime information:Tool Result
All tools return a structured result:Built-in Tools
Loaf includes several categories of built-in tools:Bash Tools
- Interactive TUI applications
- Long-running processes
- PTY-based terminal emulation
- Session management and reuse
File Operations Tools
- Indentation-aware reading - Extract code blocks by indentation level
- Large file handling - Offset/limit for reading specific sections
- Fast search - Uses ripgrep for content search
- Safe patching - Unified diff format with validation
JavaScript Tools
Web Search Tools
Integration with Exa for web search:Persistent Tool Creation
Custom Tools
Create custom tools by placing JavaScript/TypeScript files in~/.loaf/tools/:
File Structure
Tool Export Formats
Loaf supports multiple export patterns:Tool Name Requirements
Tool names must match the pattern:_, ., :, -
Loading Custom Tools
Tool Registry
TheToolRegistry manages available tools:
Registry Features
- Uniqueness enforcement - Duplicate names throw errors
- Sorted listing - Tools returned in alphabetical order
- Model manifest - Generate schema for LLM tool use
- Type safety - Full TypeScript support
Tool Runtime
TheToolRuntime executes tool calls:
Error Handling
The runtime handles:- Unknown tools - Returns
{ ok: false, error: "unknown tool: ..." } - Execution errors - Catches exceptions and returns structured error
- Cancellation - Respects AbortSignal in context
Configuration
Configure tool behavior:Source Code Reference
src/tools/types.ts- Core type definitions (src/tools/types.ts:1)src/tools/registry.ts- Tool registry implementation (src/tools/registry.ts:1)src/tools/runtime.ts- Tool execution runtime (src/tools/runtime.ts:1)src/tools/custom.ts- Custom tool discovery and loading (src/tools/custom.ts:1)src/tools/index.ts- Tool system exports and defaults (src/tools/index.ts:1)src/tools/builtin/- Built-in tool implementations
Best Practices
Writing Custom Tools
- Single responsibility - Each tool does one thing well
- Clear naming - Use descriptive, action-oriented names
- Schema validation - Define comprehensive input schemas
- Error handling - Return structured errors, don’t throw
- Idempotency - Tools should be safe to retry
- Documentation - Write clear descriptions for LLM understanding
Tool Organization
- Group related tools - Use subdirectories for tool families
- Reusable modules - Extract common logic to shared utilities
- Environment config - Use environment variables for API keys
- Testing - Test tools independently before integration
Performance
- Async by default - Use async/await for I/O operations
- Timeout handling - Respect context.signal for cancellation
- Resource cleanup - Close files, connections, processes
- Lazy loading - Load dependencies only when needed
Related
- Skills System - Reusable instruction workflows
- Custom Tools - Authoring your own tools
- Tool API Reference - Complete tool API documentation