Adding Real Tools: Files, Web, and APIs
Give your agent useful abilities — reading and writing files, fetching web pages, and calling APIs — with safe implementations.
A clock and a calculator prove the loop works, but nobody needs an agent for that. Agents earn their keep when they can touch the real world: read your notes, write a report to disk, pull a page from the web, query a live API. That's also exactly when they become dangerous, because a model that can write files can overwrite the wrong one, and a model that can fetch URLs can be fed instructions by a stranger's web page.
In this lesson you'll give your agent five real tools — list, read, and write files, fetch web pages, and get live weather — and implement each one defensively. You'll also restructure the code into a tool registry and run independent tool calls in parallel. By the end, your agent can do genuinely useful research-and-write tasks inside a safe sandbox.
From one file to a tool registry
In the last lesson the tool descriptions lived in one array and the implementations in a separate if chain. That gets messy fast: add a tool and you have to edit two places that can drift apart. A cleaner design keeps each tool's description and implementation together.
You'll move all tools into src/tools.ts, where each tool is an object with two fields:
spec: the definition the model sees (name,description,input_schema).run: an async function that takes the model's input and returns a string.
Everything is built by one function, createTools(workspaceDir). Passing the folder in, rather than hard-coding it, will pay off in the multi-agent module, where different agents get different folders.