What Are Tools?
Tools are functions your agent can call when it needs to perform an action.
They extend your agent beyond text — into the real world of files, folders, and automation.
1. Create tools.js
touch tools.js
Add this code:
import fs from "fs";
import path from "path";
export const tools = {
listFiles: {
description: "List files in a directory",
parameters: {
type: "object",
properties: {
dir: { type: "string" }
},
required: ["dir"]
},
handler: async ({ dir }) => {
return fs.readdirSync(dir);
}
},
readFile: {
description: "Read a text file",
parameters: {
type: "object",
properties: {
file: { type: "string" }
},
required: ["file"]
},
handler: async ({ file }) => {
return fs.readFileSync(file, "utf8");
}
},
writeFile: {
description: "Write text to a file",
parameters: {
type: "object",
properties: {
file: { type: "string" },
content: { type: "string" }
},
required: ["file", "content"]
},
handler: async ({ file, content }) => {
fs.writeFileSync(file, content);
return "File written successfully.";
}
}
};
2. Connect Tools to Your Agent
Open server.js and import your tools:
import { tools } from "./tools.js";
Then pass them into your agent call:
const response = await ollama.chat({
model: "llama3",
messages,
tools,
stream: false
});
Your agent can now call any tool you define — automatically — when it decides a tool is needed.
3. Test Your Tools
Ask your agent:
"List the files in the current directory."
Or:
"Create a file called notes.txt and write 'Hello from my agent!' inside it."
You’ll see your agent think, decide to use a tool, and execute it.
4. Add Your Own Tools
Try adding tools for:
- searching text
- summarizing logs
- organizing folders
- fetching URLs
- generating reports
Tools are the foundation of real agentic automation.
Next step:
Build an Autonomous Loop →