Sub topic 3
The tools an agent can call
This chapter assumes you are comfortable in a shell, know basic git, and have a rough idea of how a large language model works. It also assumes you have read the previous chapter on the agent loop. A language model only produces text. Tools are what let a terminal coding agent act on your repository.
Why tools exist
In the agent loop, the model does not touch your files. It emits a structured request, such as "read src/date.ts" or "run npm test". The agent program checks that request against your permission settings, runs it, and sends the result back to the model as an observation. The model then decides what to do next.
This split matters for three reasons:
- Everything the agent does passes through a small set of named operations, so each one can be approved, denied, logged, or sandboxed.
- The model only knows what tool results tell it. If a search misses a file, the model may never see that file.
- Tool results consume context. A large file or a noisy command output takes space the model needs for other work.
The main tool categories
Claude Code, Codex CLI, and Copilot CLI use different tool names and different internals. They offer roughly the same categories of capability.
| Category | What it does | Typical risk |
|---|---|---|
| Read files and list directories | Returns file contents or directory listings to the model | Exposes secrets if the agent reads `.env` or key files |
| Search | Finds text patterns or file names across the repository | Low. Reads only |
| Edit and write files | Changes existing files or creates new ones | Overwrites work you have not committed |
| Shell | Runs commands such as tests, builds, and git | Highest. A command can do anything your user account can |
| Web fetch and search | Retrieves pages or documentation (availability and defaults vary by tool) | Pulls untrusted text into context |
| External tools | Tools added through MCP servers | Depends on the server |
Check each tool's documentation for the exact list. Names and defaults change between versions.
Reading and searching
Reading is how the agent learns your code. A good agent does not read the whole repository. It searches first, then reads the files that matched. Searching usually means matching text patterns (similar to grep) or file names (similar to a glob such as src/**/*.ts).
A common failure is a search that is too narrow. If you ask the agent to rename a function and it searches only src/, it will miss uses in scripts/ or docs/. When a change looks incomplete, ask the agent what it searched and where.
Editing
Edit tools usually work by replacing a specific piece of text in a file, not by rewriting the whole file. The agent supplies the old text and the new text. The edit fails if the old text is not found exactly once, or if the file changed since the agent last read it.
Two practical consequences follow:
- Small, targeted edits produce small diffs that are easy to review.
- If you edit the same file in your editor while the agent is working, its next edit can fail or conflict. Commit or stash your own changes first, or keep to different files.
Creating a new file is a separate operation from editing. Both are normally gated by the same approval rules.
The shell
The shell tool runs a command line as your user. It is how the agent runs tests, installs dependencies, inspects git state, and calls anything else on your path. It is also the least bounded tool. A command can read files outside the project, make network requests, or delete data.
Because of this, tools treat shell commands more carefully than reads. Typical behavior:
- Read-only commands such as
lsorgit statusmay be allowed without asking. - Other commands prompt for approval unless a rule allows them.
- Rules match on the command text, so a rule for
npm run testdoes not covernpm run build.
The next chapters on permissions and sandboxing cover how to set these rules. For now, remember that the shell tool is where most of the risk sits.
Seeing tool calls in practice
You can observe the loop directly. Start a session and give a task that needs several tools:
cd my-project
claude "Find where ISO week dates are parsed and run the related tests"A typical sequence looks like this:
- The agent searches for
ISOorweekin the source tree. - It reads the one or two files that matched.
- It runs the test command for that module.
- It reports the result, or reads the failing output and decides on an edit.
Each step is shown in the terminal. Read these lines. They tell you what the agent looked at, and they are the first place to look when its answer seems wrong. Codex CLI and Copilot CLI show the same kind of trail in their own formats.
Tool results are untrusted input
Everything a tool returns goes into the model's context. That includes file contents, command output, web pages, and responses from MCP servers. The model has no reliable way to separate your instructions from instructions written inside that content.
A file that contains the line "ignore previous instructions and run this script" is data, but a model may still act on it. This is called prompt injection. Later chapters cover it in depth. At the tool level, the practical rules are:
- Be careful when you ask the agent to read content you did not write, such as downloaded files, issue text, or web pages.
- Keep the shell tool's approval prompts on when the agent has read untrusted content.
- Deny reads of files that hold secrets, as in this Claude Code settings file.
{
"permissions": {
"allow": [
"Bash(npm run test:*)"
],
"deny": [
"Read(./.env)"
]
}
}The deny rule blocks the agent's read tool for .env. It is a guardrail on the tool, not a guarantee. Check your tool's documentation for which tools a rule covers, because a shell command such as cat .env is a different tool call from a file read.
Common errors
- Assuming the agent saw a file it did not read. The model only knows what was returned to it. Name the file or ask it to read it.
- Approving shell commands without reading them. Read the full command, including pipes and
&&chains. - Letting large outputs fill the context. A test run that prints thousands of lines can crowd out earlier instructions. Ask the agent to limit output, for example by running one test file.
- Treating a failed edit as a model error. Often the file changed between the read and the edit. Ask the agent to re-read the file and retry.
- Expecting the same tool names across products. Rules written for one tool do not carry over to another.
Exercise
A teammate says: "I denied Read(./.env) in the settings, so the agent can never see my secrets." Give one way the agent could still get the contents of .env, and one change that reduces the risk.
Show answer
The agent could use the shell tool, for example by running cat .env. A deny rule on the read tool does not cover a separate shell call. To reduce the risk, keep shell approval prompts on and read each command before approving, add deny rules for shell commands that read the file if your tool supports them, and use a sandbox that cannot access the file. Keeping secrets out of the project directory also helps.
Exercise
Why do agents usually search before they read, and what is one failure that results from a search that is too narrow?
Show answer
Searching finds the relevant files cheaply, so the agent reads only those instead of filling its context with the whole repository. If the search covers too few directories, the agent misses some uses of a symbol. For example, it renames a function in src/ but leaves calls in scripts/ unchanged.