Part I · Foundations: Atoms, State, and Contracts
Chapter 02
The Tool Contract: Defining What an Agent Can Do
Layer: Layer 1 (Core Capability Contracts)
Prerequisites: Chapter 01
Primary Source Files:
Tool.ts—Tool,ToolDef,buildTool(),ToolUseContext,ToolPermissionContext,ToolResult(792 lines)tools/GlobTool/GlobTool.ts— clean, complete real-world tool implementation (198 lines)tools.ts— tool catalog, DCE conditional loading, deny-rule pre-filtering, and prompt-cache-aware sorting (389 lines)
Why This Layer Exists
An LLM by itself can only predict text. What turns a language model into a coding agent is a tool contract—a uniform interface that lets the runtime:
- Advertise a tool's name, prompt instructions, and JSON Schema to the model.
- Validate malformed model arguments before bothering the user with a permission prompt.
- Decide whether two tool calls can safely run in parallel or must execute serially.
- Enforce permissions, hooks, sandboxing, and security classifiers uniformly across built-in and MCP tools.
- Stream progress and render rich terminal UI without polluting what gets sent back to the model.
- Assemble and order the final tool list without busting the LLM prompt cache.
In Claude Code, every one of those responsibilities converges in a single contract: Tool<Input, Output, P> in Tool.ts.
1. The Four Faces of Tool<Input, Output, P> (Tool.ts)
If you read Tool (Tool.ts:362-695) from top to bottom, its ~35 properties and methods fall cleanly into four orthogonal faces:
Tool<Input, Output, P>
┌──────────────────────┬───────────────────────┐
│ 1. IDENTITY & SCHEMA │ 2. SAFETY & SCHEDULE │
│ name, aliases │ isConcurrencySafe() │
│ inputSchema (Zod) │ isReadOnly() │
│ inputJSONSchema │ isDestructive() │
│ outputSchema │ interruptBehavior() │
│ prompt(), searchHint│ isEnabled() │
│ shouldDefer │ maxResultSizeChars │
├──────────────────────┼───────────────────────┤
│ 3. LIFECYCLE & EXEC │ 4. UI & PROJECTION │
│ validateInput() │ userFacingName() │
│ checkPermissions() │ renderToolUseMessage │
│ preparePermission- │ renderToolResult... │
│ Matcher() │ renderToolUseProgress│
│ toAutoClassifier- │ renderGroupedToolUse │
│ Input() │ extractSearchText() │
│ call() -> ToolResult│ mapToolResultToTool- │
│ │ ResultBlockParam() │
└──────────────────────┴───────────────────────┘
Let's examine the subtle, high-leverage design choices hidden inside each face:
Face 1: Identity, Schema, and Deferred Loading
aliases?: string[]&findToolByName()(Tool.ts:348-360): Lets tools be renamed without breaking older transcripts or prompts.inputSchemavs.inputJSONSchema: Built-in tools define Zod v4 schemas (inputSchema); MCP tools pass raw JSON Schema directly viainputJSONSchemawithout round-tripping through Zod.shouldDefer/alwaysLoad/searchHint(Tool.ts:378, 442-449): When dozens of MCP servers are connected, dumping every tool schema into turn 1 wastes tens of thousands of tokens. Deferred tools are sent withdefer_loading: trueand discovered on demand byToolSearchToolusingsearchHint(a 3–10 word keyword phrase). Tools markedalwaysLoad: truebypass deferral so the model always sees them on turn 1.maxResultSizeChars(Tool.ts:466): Controls whenutils/toolResultStorage.tsspills a huge tool output to disk as<persisted-output>. Crucially,FileReadToolsets this toInfinity: persistingRead's output to a file that the model mustReadback would create an infinite Read→file→Read loop!
Face 2: Safety & Concurrency Metadata
isConcurrencySafe(input)andisReadOnly(input)takeinput! Why aren't these static booleans? BecauseBashTooldepends on the command!git statusorls -lacan be classified as read-only/concurrency-safe, whereasrm -rfornpm installis neither. Passing parsedinputmakes scheduling and permissions input-sensitive.interruptBehavior?(): 'cancel' | 'block'(Tool.ts:416): Declares what happens if the user submits a new message while this tool is mid-flight—either cancel and discard, or block the new message until the tool finishes (default:'block').
Face 3: The Execution & Validation Lifecycle
Notice the strict ordering documented right in Tool.ts:483-503:
backfillObservableInput?(input)— Runs on a copy oftool_useinput before observers (SDK, hooks, transcripts) see it; the original API-bound input is never mutated so the prompt cache stays intact!validateInput?(input, context)— Runs before permission prompts. Returns{ result: true }or{ result: false, message, errorCode }so invalid paths or bad arguments bounce straight back to the model without nagging the human.checkPermissions(input, context)— Tool-specific permission logic (e.g., filesystem path rules forGlobTool, AST command rules forBashTool), layered with the general permission engine inutils/permissions/permissions.ts.call(args, context, canUseTool, parentMessage, onProgress)— Executes the tool and returnsPromise<ToolResult<Output>>.
Look at ToolResult<T> (Tool.ts:321-336):
export type ToolResult<T> = {
data: T
newMessages?: (UserMessage | AssistantMessage | AttachmentMessage | SystemMessage)[]
// contextModifier is only honored for tools that aren't concurrency safe.
contextModifier?: (context: ToolUseContext) => ToolUseContext
mcpMeta?: {
_meta?: Record<string, unknown>
structuredContent?: Record<string, unknown>
}
}
Notice contextModifier: a tool can return a pure function (context) => nextContext to update ToolUseContext for subsequent tools in the turn (for example, mutating session flags or caches), and the orchestrator controls when modifiers apply so concurrent tools never race on context mutation!
Face 4: Separating Model Serialization from Human UI
A tool never returns a raw string directly to both the LLM and the screen. Instead, call() returns typed data: Output, and three separate projections read that Output:
mapToolResultToToolResultBlockParam(output, toolUseID)→ Produces the Anthropic APIToolResultBlockParamthe model sees.renderToolResultMessage(output, progress, options)→ Produces the React/Ink terminal UI the human sees (withrenderToolUseMessageacceptingPartial<Input>so the UI can render while the JSON arguments are still streaming in!).extractSearchText?(output)→ Flattens the exact text visible in transcript mode (excluding<system-reminder>wrappers and chrome like"Found 3 files in 12ms") so in-terminal transcript search counts match highlighted screen cells 1-to-1 (Tool.ts:582-599).
2. Fail-Closed Defaults: buildTool() (Tool.ts:703-792)
The Problem
With 60+ tools in tools/, making every tool implement boilerplate stubs like isDestructive: () => false or isConcurrencySafe: () => false causes two failures: repetitive noise, and worse, an engineer forgetting a safety check or callers littering the engine with tool.isConcurrencySafe?.(input) ?? false.
The Implementation
Every tool is constructed through buildTool(def) (Tool.ts:757-792), which spreads a fail-closed TOOL_DEFAULTS object underneath the tool's definition:
// Tool.ts:757-792
const TOOL_DEFAULTS = {
isEnabled: () => true,
isConcurrencySafe: (_input?: unknown) => false,
isReadOnly: (_input?: unknown) => false,
isDestructive: (_input?: unknown) => false,
checkPermissions: (
input: { [key: string]: unknown },
_ctx?: ToolUseContext,
): Promise<PermissionResult> =>
Promise.resolve({ behavior: 'allow', updatedInput: input }),
toAutoClassifierInput: (_input?: unknown) => '',
userFacingName: (_input?: unknown) => '',
}
export function buildTool<D extends AnyToolDef>(def: D): BuiltTool<D> {
return {
...TOOL_DEFAULTS,
userFacingName: () => def.name,
...def,
} as BuiltTool<D>
}
Look closely at the fail-closed defaults:
isConcurrencySafedefaults tofalse— a new tool runs serially until explicitly audited and marked safe.isReadOnlydefaults tofalse— assume a tool mutates state unless proven otherwise.checkPermissionsreturns{ behavior: 'allow', updatedInput: input }, which does not bypass security—it defers to the general permission engine (permissions.ts), where modes, deny rules, ask rules, and classifiers still apply.- At the TypeScript level,
BuiltTool<D>(Tool.ts:735-741) mirrors the runtime{ ...TOOL_DEFAULTS, ...def }spread so callers retain each tool's exact literalname,Input, andOutputtypes with zero?.()optional checks needed at runtime.
3. The Capability Bag: ToolUseContext & ToolPermissionContext
Every tool's call() and checkPermissions() receives a ToolUseContext (Tool.ts:158-300). Reading its fields is like looking at an X-ray of the entire agent runtime—every Layer 0 atom we studied in Chapter 1 shows up here:
- Static turn options (
options):tools,commands,mainLoopModel,thinkingConfig,mcpClients,mcpResources,agentDefinitions,isNonInteractiveSession,refreshTools. - Execution & cancellation:
abortController,messages,queryTracking: { chainId, depth },toolUseId. - State bridges (from Chapter 1):
readFileState: FileStateCachegetAppState()/setAppState()setAppStateForTasks?(Tool.ts:184-192) — A classic production scar! For async background subagents,setAppStateis replaced with a no-op so a subagent cannot clobber the main UI. Yet background subagents still need to register or clean up session-wide background tasks and hooks.setAppStateForTaskspunches a narrow, intentional channel straight through to the root store.localDenialTracking?(Tool.ts:279-283) — Same root cause! Because an async subagent'ssetAppStateis a no-op, denial counts written viasetAppStatewould never accumulate;localDenialTrackinggives the subagent a mutable local counter so fallback-to-prompting still triggers.
- Prompt-cache & budget state:
contentReplacementState?: ContentReplacementState— tracks which tool outputs were spilled to disk so forked subagents make identical replacement decisions.renderedSystemPrompt?: SystemPrompt— freezes the parent's rendered system prompt bytes at turn start so forked subagents share the parent's prompt cache instead of re-evaluating GrowthBook flags and busting it.
- UI & notification hooks (optional—undefined in headless SDK mode!):
setToolJSX,addNotification,appendSystemMessage,sendOSNotification,requestPrompt,setStreamMode,setHasInterruptibleToolInProgress.
Alongside it sits ToolPermissionContext (Tool.ts:123-138), wrapped in DeepImmutable<...>:
mode: PermissionMode(default,plan,acceptEdits,bypassPermissions,dontAsk, or internalauto/bubble)additionalWorkingDirectories: Map<string, AdditionalWorkingDirectory>alwaysAllowRules,alwaysDenyRules,alwaysAskRules(indexed byPermissionRuleSource)shouldAvoidPermissionPrompts(auto-deny prompts for headless background agents) andprePlanMode(remembers which mode to restore when exiting Plan Mode).
4. Concrete Walkthrough: tools/GlobTool/GlobTool.ts
Abstract interfaces come alive when you read a real 198-line tool end to end. GlobTool is one of the cleanest tools in the repo, and it exercises nearly every concept in this chapter:
- Lazy Zod schemas behind getters (
GlobTool.ts:26-75): BothinputSchema(z.strictObject({ pattern, path? })) andoutputSchema({ durationMs, numFiles, filenames, truncated }) are wrapped inlazySchema(...)and exposed viaget inputSchema()/get outputSchema(). - Explicit opt-in to concurrency & read-only (
GlobTool.ts:76-87): OverridesbuildTool's fail-closed defaults withisConcurrencySafe() { return true },isReadOnly() { return true }, andisSearchOrReadCommand() { return { isSearch: true, isRead: false } }(allowing the UI to collapse consecutive search calls). - Result size cap (
GlobTool.ts:60):maxResultSizeChars: 100_000bounds inline output beforetoolResultStorage.tsspills it to disk. - Defensive pre-permission validation (
GlobTool.ts:94-134): Look at lines 100–103 insidevalidateInput:On Windows, calling// SECURITY: Skip filesystem operations for UNC paths to prevent NTLM credential leaks. if (absolutePath.startsWith('\\\\') || absolutePath.startsWith('//')) { return { result: true } }fs.stat('//attacker/share')triggers an outbound SMB handshake that leaks NTLM hashes before the user has even been asked for permission. Skippingfs.staton UNC paths invalidateInputpushes the decision safely intocheckPermissions! For normal local paths,validateInputverifies the directory exists and even suggests a corrected path undercwd(suggestPathUnderCwd) whenENOENToccurs—giving the model actionable self-correction feedback without a permission prompt. - Tool-specific permission check (
GlobTool.ts:135-142): Delegates tocheckReadPermissionForTool(GlobTool, input, appState.toolPermissionContext). - Execution & token-saving relativization (
GlobTool.ts:154-176): Runsglob(...)withabortController.signalandlimit = globLimits?.maxResults ?? 100, then maps results throughtoRelativePathspecifically to save model tokens. - Dual projection (
GlobTool.ts:151-153, 177-197):extractSearchTextreturnsfilenames.join('\n')(matching the rendered list).mapToolResultToToolResultBlockParamemits'No files found'when empty, or newline-joined paths plus a truncation hint for the LLM.
5. Assembling the Tool Catalog (tools.ts)
Having 60+ Tool objects on disk is only half the story. Before any turn begins, tools.ts decides which tools exist in this build, which are permitted right now, and in what exact order they are handed to the API.
Step 1: Build-Time Filtering & Cycle Breaking (tools.ts:16-156, 193-251)
getAllBaseTools() builds the master array using three gating mechanisms:
- Compile-time DCE:
feature('PROACTIVE'),feature('AGENT_TRIGGERS'),feature('COORDINATOR_MODE'),process.env.USER_TYPE === 'ant'. - Circular-dependency thunks:
getSendMessageTool(),getTeamCreateTool(),getTeamDeleteTool(), andgetPowerShellTool()use lazyrequire()calls to breaktools.ts → Team*Tool → ... → tools.ts. - Environment adaptation: On internal native builds with embedded
bfs/ugrep(hasEmbeddedSearchTools()),GlobToolandGrepToolare omitted because the shell'sfind/grepare already aliased to fast embedded binaries.
Step 2: Mode & Blanket-Deny Filtering (tools.ts:262-327)
getTools(permissionContext) applies runtime filtering:
CLAUDE_CODE_SIMPLEmode: Collapses the entire catalog down to just[BashTool, FileReadTool, FileEditTool](plus coordinator tools if active).- Blanket deny filtering (
filterToolsByDenyRules): Removes any tool (or entire MCP server prefix likemcp__server) matched by a blanket deny rule before the model ever sees the tool schema—saving tokens and eliminating futile tool calls. isReplModeEnabled(): When the ant-only VMREPLToolis active, hides raw primitive tools (REPL_ONLY_TOOLS) so the model routes through the VM.tool.isEnabled(): Final runtime enablement filter.
Step 3: Partitioned Sorting for Prompt-Cache Stability (tools.ts:345-367)
Finally, assembleToolPool(permissionContext, mcpTools) merges built-in tools and dynamic MCP tools. Read lines 354–366 of tools.ts carefully—this is one of the best systems-engineering gems in the entire repository:
// tools.ts:354-366
// Sort each partition for prompt-cache stability, keeping built-ins as a
// contiguous prefix. The server's claude_code_system_cache_policy places a
// global cache breakpoint after the last prefix-matched built-in tool; a flat
// sort would interleave MCP tools into built-ins and invalidate all downstream
// cache keys whenever an MCP tool sorts between existing built-ins. uniqBy
// preserves insertion order, so built-ins win on name conflict.
const byName = (a: Tool, b: Tool) => a.name.localeCompare(b.name)
return uniqBy(
[...builtInTools].sort(byName).concat(allowedMcpTools.sort(byName)),
'name',
)
Think about what would happen if you naively ran [...builtInTools, ...allowedMcpTools].sort(byName):
- Connecting a single MCP server whose tool is named
mcp__alpha__querywould insert it betweenEditandRead—invalidating the global prompt cache prefix for every built-in tool after it and the entire system prompt below it! - By sorting
[builtInTools]and[allowedMcpTools]as two separate contiguous partitions, built-in tools stay byte-identical across users and MCP configurations, ANDuniqBy(..., 'name')guarantees a malicious or colliding MCP tool can never shadow a built-in tool.
Putting Layer 1 Together
Individual Tool Modules (GlobTool, FileReadTool, BashTool, ...)
│ wrapped by buildTool() (fail-closed defaults + BuiltTool<D> types)
▼
getAllBaseTools() ── compile-time feature() DCE + lazy cycle requires
│
▼
getTools(permissionContext) ── CLAUDE_CODE_SIMPLE + filterToolsByDenyRules + isEnabled()
│
▼
assembleToolPool(ctx, mcpTools)── [sorted built-ins] ++ [sorted allowed MCPs] + uniqBy
│
▼
Ready for API serialization (Ch 4-5) & Tool Execution / StreamingToolExecutor (Ch 6)
How This Snaps Into the Next Layer
We now have the complete contract for what the model can invoke (Tool + assembleToolPool). Next, we need the other half of Layer 1: what the user can invoke and how conversation messages flow—the three-variant Command contract (types/command.ts), utils/messages.ts, and utils/processUserInput/processUserInput.ts (Chapter 3).
Then, in Part II, we will feed these tools and messages into context.ts, services/api/claude.ts, services/tools/toolOrchestration.ts, StreamingToolExecutor.ts, and query.ts to bring the entire agent loop to life.
Reading Exercises & Code Checkpoints
- Compare a read-only tool with a mutating tool:
Read
tools/FileEditTool/FileEditTool.tsalongsidetools/GlobTool/GlobTool.ts. How doesFileEditTooluseToolUseContext.readFileState(from Chapter 1) insidevalidateInput? - Find every
maxResultSizeChars: Infinityopt-out: Runrg "maxResultSizeChars" tools/and observe which tools opt out of disk persistence and why. - Trace
assembleToolPoolcallers: Runrg "assembleToolPool"to see how both interactive REPL (useMergedTools) and subagents (runAgent.ts) share the exact same partitioned-sort pipeline.
Manuscript chapters/02-the-tool-contract.md from AtlantisPleb/cc at 5aa286a, synced Nov 24, 2026.