Minbook
KO
52 Tools, 23-Step Security: Inside an Agent's Tool System

52 Tools, 23-Step Security: Inside an Agent's Tool System

MJ · · 5 min read

52 built-in tools' common interface, 10-step execution pipeline, safe=parallel/unsafe=sequential concurrency model, 5-stage permission pipeline, and 888KB Tree-sitter 23-step bash security.

When you tell Claude Code to “edit this file,” tool selection, input validation, permission checks, AST security scanning, execution, and result processing all happen in strict sequence within milliseconds. 52 tools pass through a 10-step pipeline via a common interface, surviving 23 steps of security checks. This article deconstructs that system based on analysis of the Claude Code source (512,000 lines of TypeScript).


What Happens When a Tool Is Called

Here is the full journey of a single tool call in Claude Code:

  1. Tool name lookup (matching among 52 tools)
  2. Abort check (did the user press Ctrl+C?)
  3. Input schema validation (Zod)
  4. PreToolUse hook execution
  5. Permission check (5-stage pipeline)
  6. Tool execution (the actual work)
  7. Result mapping to ToolResult
  8. Result size check (overflow goes to disk)
  9. PostToolUse hook execution
  10. Telemetry dispatch

Out of these 10 steps, only step 6 is the actual work. The other 9 are infrastructure for safety and consistency. This is the reality of production agent systems.


The 52-Tool Catalog

All tools fall into 8 categories. gated = controlled by Feature Flags, disabled by default.

CategoryCountTools
File Operations6FileRead, FileEdit, FileWrite, Glob, Grep, NotebookEdit
Execution3Bash, PowerShell, REPL
Search and Fetch4WebBrowser (gated), WebFetch, WebSearch, ToolSearch
Agents and Tasks11Agent, SendMessage, TaskCreate/Get/List/Update/Stop/Output, TeamCreate, TeamDelete, ListPeers (gated)
Planning5EnterPlanMode, ExitPlanMode, EnterWorktree, ExitWorktree, VerifyPlanExecution (gated)
MCP4mcp, ListMcpResources, ReadMcpResource, McpAuth
System11AskUserQuestion, TodoWrite, Skill, Config, RemoteTrigger (gated), CronCreate/Delete/List (gated), Snip (gated), Workflow (gated), TerminalCapture (gated)
Experimental8Sleep, SendUserMessage, StructuredOutput (gated), LSP (gated), SendUserFile (gated), PushNotification (gated), Monitor (gated), SubscribePR (gated)

52 total: 41 always active, 11 feature-gated. The real insight is that all 52 share a single interface.


The Common Interface: Every Tool’s Contract

All 52 tools share this contract:

Property / MethodPurpose
nameTool identifier for LLM calls
inputSchema (Zod)Parameter validation — defends against hallucination
call()Execution logic
description()Tool description for the LLM
checkPermissions()Permission verification
validateInput()Additional input validation beyond schema
isConcurrencySafe()Whether parallel execution is safe
isReadOnly()Read-only flag for permission modes
maxResultSizeCharsSize ceiling — overflow to disk
render*()Terminal UI rendering

isConcurrencySafe() is not a simple boolean. FileRead always returns true, but FileEdit returns false only when targeting the same file. This granularity is the foundation of the concurrency model.


The Tool Assembly Pipeline

Tools pass through 5 filtering stages before reaching the LLM.

flowchart LR
    A["getAllBaseTools()\n44+ built-in tools"] --> B["Feature Gate\nFilter"]
    B --> C["Deny Rules\nFilter"]
    C --> D["Mode Filter\nplan/auto/default"]
    D --> E["Alphabetical\nSort"]
    E --> F["assembleToolPool()\n+ MCP tool merge"]

Why alphabetical sorting? Tool definitions ship as part of the system prompt for API prompt caching. If tool order changes, the cache key changes and hit rates plummet. Alphabetical sorting keeps order stable across tool additions/removals. Deny Rules let users block tools via .claude/settings.json; Mode filter excludes write tools in Plan mode, etc.


The 10-Step Execution Pipeline in Detail

StepNameWhat It Does
1Name LookupMatch tool name against assembleToolPool(). No match = immediate error.
2Abort CheckCheck AbortSignal (Ctrl+C). Skip execution if set.
3Input ValidationZod schema check on LLM-generated params. Catches hallucinated types.
4PreToolUse HookExternal script extension point. Enterprise custom security checks.
5Permission Check5-stage Permission Pipeline (detailed below). Block = no execution.
6Executioncall() invoked — the actual work happens here.
7Result MappingRaw result converted to standardized ToolResult format.
8Size CheckOverflow beyond maxResultSizeChars saved to disk; LLM gets reference only.
9PostToolUse HookPost-execution extension point. Audit logging, notifications.
10TelemetryExecution time, success/failure, tool name collected for monitoring.

Concurrency Model: Safe Tools Run Parallel, Unsafe Tools Run Sequential

Claude Code’s StreamingToolExecutor manages multi-tool execution per turn.

flowchart TB
    subgraph "Batch 1 -- Parallel"
        direction LR
        R1["FileRead\n/src/a.ts"]
        R2["FileRead\n/src/b.ts"]
        R3["Grep\npattern: error"]
    end
    subgraph "Batch 2 -- Sequential"
        E1["FileEdit\n/src/a.ts"]
    end
    subgraph "Batch 3 -- Parallel"
        R4["FileRead\n/src/c.ts"]
        R5["Glob\n**/*.test.ts"]
    end
    R1 & R2 & R3 --> E1
    E1 --> R4 & R5

Core rules:

TypeConditionExecution
Safe toolisConcurrencySafe() = trueBatched together, parallel execution (max 10)
Unsafe toolisConcurrencySafe() = falseStarts a new batch, sequential solo execution
Failure handlingOne tool in a batch failsSibling tools in the same batch are aborted

StreamingToolExecutor optimization: Execution starts as soon as tool parameters are complete while the API response is still streaming. The first tool may already be running while the LLM generates the third tool’s parameters. The parallel batch cap of 10 is a practical ceiling to limit filesystem load.


The 5-Stage Permission Pipeline

The pipeline that checks permissions before tool execution.

flowchart TB
    A["1. Rule Matching\nDeny/Allow rule lookup"] --> B["2. Mode Check\nExecution mode verification"]
    B --> C["3. ReadOnly Check\nRead-only flag check"]
    C --> D["4. AI Classifier\nAuto mode only"]
    D --> E["5. User Prompt\nFinal user confirmation"]
    E --> F["ALLOW / DENY"]

Four permission modes govern this pipeline:

ModeRead ToolsWrite ToolsNotes
DefaultAuto-allowUser confirmationSafest, default
AutoAuto-allowAI classifierLightweight rule engine, not LLM call
PlanAllowBlockRead-only mode
BypassAuto-allowAuto-allowCI/CD only

Rule priority: Local > Project > User > CLI Flags > Org Policy. Lower-level rules cannot override higher-level ones.


BashTool Security: 888KB, 23-Step AST Inspection

Bash can execute arbitrary shell commands — the most dangerous capability in an agent. Claude Code addresses this with Tree-sitter-based AST parsing.

MetricValue
Security code size888KB (18 files)
Inspection steps23
ParserTree-sitter (bash + zsh grammar)
Design principleFail-closed (block on parse failure)

Key Areas of the 23-Step Inspection

AreaWhat It Catches
Command SubstitutionNested commands like $(rm -rf /), even hidden inside strings
Variable Expansion$HOME, ${PATH} — prevents malicious path injection via env vars
RedirectionValidates every >, >> target path. Blocks writes to system files
PipesInspects post-`
Zsh-specific=curl (equals expansion), builtin bypass attempts
Unicode attacksZero-width chars, null bytes, homoglyphs that visually mimic safe characters

Fail-Closed Design

If Tree-sitter cannot parse a command — it blocks by default. “Do not execute what you cannot understand.” Occasionally blocks legitimate commands, but security takes precedence.


The Hook System: Intercepting the Execution Flow

Hooks insert external logic at specific pipeline points.

timeline
    title Hook Lifecycle
    SessionStart : On session initialization
    UserPromptSubmit : On user input submission
    PreToolUse : Just before tool execution
    PostToolUse : Just after tool execution
    Stop : On agent response completion

Hook Response Types

Response FieldEffect
continue: falseAbort the entire pipeline
decision: "block"Block only this tool execution
updatedInputModify the tool’s input parameters
additionalContextInject additional context to the LLM

updatedInput lets enterprises change directory paths or mask parameters before execution. additionalContext injects supplementary info alongside the tool result.


BashTool Special Features

15-second timeout with auto-background: Commands exceeding 15 seconds convert to background tasks, freeing the agent. 2-second progress reports capture stdout/stderr at intervals so the LLM tracks long-running processes in real time. No more synchronous stalls.


AgentTool: 5 Execution Modes

The Agent tool creates sub-agents to delegate work across five modes.

ModeProcessState IsolationPrimary Use Case
Same-process ImmediateSame processSharedSimple delegation, fast response
Background AsyncSame process, asyncSharedLong tasks in parallel
Tmux Panel (Teammate)Separate tmux panelPartial isolationConcurrent work with user
Isolated Git WorktreeSeparate processFull isolation (separate git worktree)Parallel branch work
Remote CloudRemote serverFull isolationCI/CD integration, large-scale tasks

Isolated Git Worktree stands out: independent work on a different branch without cloning, so you review code on main while a sub-agent tests on a feature branch.


Key Numbers at a Glance

ItemValue
Total tools52 (41 active + 11 gated)
Tool categories8
Execution pipeline steps10
Max parallel batch size10
Permission pipeline stages5
Permission modes4
Bash security code size888KB (18 files)
Bash security inspection steps23
Hook points5
AgentTool execution modes5
BashTool timeout15 seconds (then background switch)
Progress report interval2 seconds

Conclusion: The Tool System Determines Agent Quality

An agent’s real capability is not determined by LLM reasoning ability alone. How safely, efficiently, and extensibly you manage tools determines the quality of a production agent.

Three principles recur: consistency (one interface, one pipeline), safety first (fail-closed, 23-step AST, 5-stage permissions), and extensibility (Hooks, MCP, Feature Flags). If you are designing an agent system, invest in the Tool System before the LLM prompt.


Sources & Limitations

This series synthesizes the following publicly available analyses and does not directly contain leaked source code.

SourceURLFocus
ccunpacked.devccunpacked.devVisual architecture guide, tool/command catalog
Wikidocs Analysiswikidocs.net/338204Detailed technical analysis (execution flow, state, rendering)
PyTorch KRdiscuss.pytorch.krCommunity analysis + HN discussion synthesis
Claw Codegithub.com/ultraworkers/claw-codeClean-room reimplementation (Rust/Python), PARITY.md gap analysis

Analysis date: April 2, 2026. Anthropic issued DMCA takedowns on 8,100+ forks and discontinued npm distribution shortly after the leak, so some sources may have changed accessibility. Features behind feature flags are unreleased and may be modified or deprecated before launch.

Share

Related Posts