Tools System
Overview
The tools system is the core abstraction of claude-code-leaked. Every capability (file operations, shell execution, MCP integration, agent orchestration) is exposed as a self-contained tool.
Tool Catalog (~40 tools)
File I/O Tools
FileReadTool- Read file contentsFileWriteTool- Write new filesFileEditTool- Edit existing filesFileSearchTool- Search files by patternGlobTool- Glob pattern matching
Shell Execution Tools
BashTool- Execute bash commandsPowerShellTool- Execute PowerShell commandsREPLTool- Interactive REPL execution
Agent Orchestration Tools
AgentTool- Spawn and manage sub-agentsTeamCreateTool- Create agent teamsSendMessageTool- Send messages to agents
MCP Integration Tools
MCPTool- Model Context Protocol operationsListMcpResourcesTool- List MCP resourcesReadMcpResourceTool- Read MCP resource content
System Tools
WebSearchTool- Web search capabilitiesTaskTool- Task managementMemoryTool- Memory management
Tool Factory Pattern
All tools are created using a consistent factory function:
buildTool({
name: 'ToolName',
description: 'Description of what the tool does',
inputSchema: z.object({
// Input validation schema using Zod
param1: z.string(),
param2: z.number(),
}),
async call(args, context) {
// Tool execution logic
return result;
},
async checkPermissions(input, context) {
// Permission checks before execution
return true; // or false with error message
},
async toMessage(args, result, context) {
// Convert tool result to message format
return message;
},
});
Tool Directory Structure
Each tool lives in its own directory:
src/tools/<ToolName>/
├── <ToolName>Tool.ts - Tool implementation
├── UI.tsx - UI rendering component
├── prompt.ts - System prompt contribution
└── [optional: test files, helpers]
Example: BashTool
// src/tools/BashTool/BashTool.ts
import { buildTool } from '../../Tool.js';
import { z } from 'zod/v4';
import { executeCommand } from './executor.js';
export const BashTool = buildTool({
name: 'BashTool',
description: 'Execute bash commands in the terminal',
inputSchema: z.object({
command: z.string().describe('The bash command to execute'),
timeout: z.number().optional().describe('Timeout in milliseconds'),
workdir: z.string().optional().describe('Working directory'),
}),
async call(args, context) {
const result = await executeCommand(args.command, {
timeout: args.timeout,
workdir: args.workdir,
});
return result;
},
async checkPermissions(input, context) {
// Check if command is allowed in current working directory
return true;
},
});
Permission System
Tools can define custom permission checks:
async checkPermissions(input, context) {
// Check working directory restrictions
// Check tool-specific rules
// Return { result: true } or { result: false, message: '...', errorCode: 403 }
}
Permission modes:
- default: Standard permission checks
- plan: Planning mode (model-initiated)
- auto: Automated permission handling
- bypassPermissions: Skip permission prompts
Tool Execution Flow
1. User invokes tool via natural language or command
2. QueryEngine validates tool input schema
3. checkPermissions() runs (if enabled)
4. call() executes the tool logic
5. toMessage() formats result for display
6. Result added to message history
7. UI renders tool output
Key Tool Categories
Core Tools (Always Available)
- File operations (read, write, edit)
- Shell execution (bash, powershell)
- Search and glob patterns
Conditional Tools (Feature-Gated)
- Agent tools (COORDINATOR_MODE)
- MCP tools (MCP_ENABLED)
- Web search (WEB_SEARCH)
Special Tools
SyntheticOutputTool: Handles structured outputSkillTool: Loads and executes skillsAgentTool: Manages sub-agent spawning
Tool Best Practices
- Self-Contained: Each tool has its own logic, UI, and prompt
- Schema Validation: Zod schemas for strict input validation
- Permission Gates: Configurable access control
- Lazy Loading: Heavy tools loaded on demand
- Error Handling: Consistent error codes and messages
- UI Integration: Dedicated UI component per tool
Tool Discovery
Tools are discovered and registered via:
// src/tools.ts
export function getTools(): Tools {
const tools: Tools = [];
tools.push(BashTool);
tools.push(FileReadTool);
tools.push(FileWriteTool);
// ... all other tools
return tools;
}
MCP Tool Integration
MCP (Model Context Protocol) tools extend capabilities:
// src/services/mcp/client.ts
export async function getMcpToolsCommandsAndResources(
servers: MCPServerConnection[]
): Promise<Tools> {
// Fetch tools from connected MCP servers
// Return aggregated tool list
}
Security Considerations
- Working Directory Restrictions: Tools check if paths are within allowed directories
- Permission Modes: Different modes control tool access
- Lazy Loading: Reduces memory footprint
- Feature Flags: Conditional tool availability
- Error Handling: Graceful degradation on failures