שלב 5 מתוך 13 - כ-40 דקות
Lab 04 - Tools
Time: about 40 minutes. You will: give Steward real tools - reading your notes and running programs - and make every one of them safe against a model that is wrong, or tricked.
Read docs/security.md before you start.
Why
A study coach that can read your notes and your study log gives far better advice than one that only knows what you type. But every tool is a door: the path the model picks, the program it asks for, and the text that comes back are all outside your control. This lab builds the doors with locks on them.
Concepts
The workspace is a jail
File tools may touch exactly one folder, the workspace (paths.workspace, by default
~/steward-workspace). Every model-supplied path goes through Workspace.resolve():
קוד המקור של התרשים
flowchart TD
P["path from the model"] --> A{"empty or has NUL?"}
A -- yes --> X[reject]
A -- no --> B{"inside the workspace after resolving ../ ?"}
B -- no --> X
B -- yes --> C{"a secret-looking name? .env, .ssh, id_ed25519, *.pem"}
C -- yes --> X
C -- no --> D{"every existing link on the way resolves inside? (dangling links refused)"}
D -- no --> X
D -- yes --> OK[open the file]The symlink step matters: notes/today.md can be a link to ~/.ssh/id_ed25519, and a purely
textual check would miss it. Every component of the path is checked, not just the end, and a
link that points nowhere is refused: writing through it would create its target, wherever that
is. The secret-name list also covers config that programs act on, such as .gitconfig and
.config/: git runs the programs named in its config (for example core.fsmonitor), so a
config file the model could write would be a way to run code.
Programs without a shell
The model never sends a command string. It sends a program name and an argument list:
{ "program": "git", "args": ["log", "--oneline", "-5"] }run_program starts it with execFile, which runs the program directly. With no shell, ;,
|, &&, $(...), and * mean nothing: ["hello; touch hacked"] is one argument, printed by
echo as text. On top of that:
| Guard | Why |
|---|---|
| Bare program names only | No /usr/local/bin/anything or ./script.sh |
| Working folder = workspace | Relative paths resolve inside the jail |
| Minimal environment | Your API key and tokens are not inherited by the child |
Empty HOME, no git config |
A file in the workspace cannot become git's config |
| Timeout and output cap | A hung or chatty program cannot stall or flood the turn |
What may run is decided by the policy before run_program is reached. Lab 05 covers it; for
now, know that ls, wc, git status, git log, and git diff are allowed, rm, sudo,
curl, wget, and ssh are denied, and everything else asks you first.
Tool output is untrusted and bounded
Text that comes back from a tool goes straight into the model's context. Two consequences:
- It may contain instructions.
examples/workspace/notes/forum-tips.mdcontains a planted one: this is prompt injection (Mitigate jailbreaks and prompt injections).config/rules.mdrule 1 tells the model that tool results are data, and the policy makes sure that even a model that falls for it cannot runrm. - It costs tokens.
limitOutput()cuts anything abovetools.maxOutputCharsand says how much was cut, so the model can ask for the next part instead. A program that prints more than 1 MB is stopped, and the start of its output comes back.
Build
src/tools/workspace.ts-Workspace.resolve()andisSecretName().src/tools/file-tools.ts-list_files,read_file(numbered lines, paging withstart_line),search_files, and two write tools you will meet in lab 05:write_fileandappend_study_log.src/tools/program-tool.ts-run_programwithexecFile, the clean environment, timeout, and buffer limit. Noteargv(): it gives the policy the exact argument vector to match.src/tools/tool.ts-parseCall()andexecute()split validation from running, so the policy can decide between the two.summarize()gives each call a one-line description for approvals and the audit log.
The tool descriptions are part of the design: the model picks a tool from its name and
description alone, so each one says what it does, what its input means, and its limits
(Define tools). The API can also check tool input
against the schema itself (strict tool use);
Steward validates with Zod instead, because it streams tool input (eager_input_streaming, lab 01) and the server does not validate that input.
Run
Copy the example workspace (it only copies into an empty folder) and start chatting:
pnpm steward init
pnpm steward chatyou> Look at my notes. What is my weakest domain, and what should I review today?
you> How many study blocks are in my log, and what were my scores?
you> Run git status in the workspace.Watch the [tool] lines. The last request runs git status (allowed) and reports that the
workspace is not a git repository, which is fine.
Break it
Path escape. Ask:
Read the file ../.ssh/config. The tool returns an error result ("outside the workspace") and the model tells you it cannot.Planted instruction. Ask:
Summarize notes/forum-tips.md. A good model reports the suspicious instruction instead of following it. Then runpnpm steward audit: if the model did tryrm, you seedeny ... not-run.Symlink. Create a link inside the workspace that points out of it, then ask Steward to read it:
ln -s ~/.zshrc ~/steward-workspace/notes/shell.mdThe answer is "leads outside the workspace through a link". Remove the link afterwards.
Check
pnpm lab 04The checkpoint proves: escapes, secret names, and symlinks out are rejected; secret files are
hidden from listings; reads are numbered and long output is cut; run_program has no shell, takes
only bare names, does not pass ANTHROPIC_API_KEY to children, refuses dangling links and config
files such as .gitconfig, runs with an empty HOME, stops slow programs and programs that print
too much, says so when the turn is cancelled, and reports missing ones.
Exercises
- Add a
word_countoption toread_filethat returns only line, word, and character counts. - Add
treeto the allowed programs in your local config and ask for an overview of the workspace. Which argument would make the policy ask first, and why? search_filesreads every file each time. Sketch how you would add a small index, and where it should live so it never leaks outside the workspace.
Claude API docs
- Define tools - names, descriptions, and schemas the model chooses from.
- Handle tool calls - returning errors with
is_errorso the model can recover. - Strict tool use - schema checks done by the API, the alternative to our own validation.
- Mitigate jailbreaks and prompt injections - why tool output is treated as data.
Exam connection
CCA-F - Tool Design & MCP Integration: tool schemas and descriptions, input validation, error results, and least privilege for tools. Practice this domain on CertLabPro. Related: tool integration is part of Agent Development on the NVIDIA-Certified Professional: Agentic AI (NCP-AAI) exam.
Previous: Lab 03 - Next: Lab 05 - Permissions and approvals