Instructions for AI agents using bash-tool in projects.
- Provides
bash,readFile,writeFiletools for AI SDK agents - Runs commands in sandboxed environments (just-bash or @vercel/sandbox)
- Pre-populates sandbox with files from inline content or disk
- Generates contextual LLM instructions with working directory and file list
import { createBashTool } from "bash-tool";
import { ToolLoopAgent, stepCountIs } from "ai";
const { tools } = await createBashTool({
files: {
"src/index.ts": "export const x = 1;",
"package.json": '{"name": "test"}',
},
});
const agent = new ToolLoopAgent({
model,
tools,
// Or use just the bash tool as tools: {bash: tools.bash}
stopWhen: stepCountIs(20),
});
const result = await agent.generate({
prompt: "List files in src/",
});- Default sandbox is just-bash - Install
just-bashor provide your own sandbox - Working directory defaults to
/workspace- All files written relative todestination - Files are written before tools are returned - Sandbox is pre-populated
- Tool descriptions include file list - LLM sees available files in bash tool description
- No
stop()method - Sandbox lifecycle is managed externally
const { bash } = await createBashTool({
uploadDirectory: { source: "./my-project", include: "**/*.ts" },
});Use @vercel/sandbox for full VM
import { Sandbox } from "@vercel/sandbox";
const vm = await Sandbox.create();
const { tools } = await createBashTool({ sandbox: vm });
// Call vm.stop() when doneimport { Sandbox } from "@vercel/sandbox";
// First invocation: create and store sandboxId
const newSandbox = await Sandbox.create();
const sandboxId = newSandbox.sandboxId; // store this
// Later invocations: reconnect by ID
const existingSandbox = await Sandbox.get({ sandboxId });
const { tools } = await createBashTool({ sandbox: existingSandbox });
// Previous files and state preservedconst { tools } = await createBashTool({
onBeforeBashCall: ({ command }) => {
console.log("Running:", command);
return undefined; // Or return { command: modifiedCommand } to change it
},
onAfterBashCall: ({ command, result }) => {
console.log(`Exit: ${result.exitCode}`);
return undefined; // Or return { result: modifiedResult } to change it
},
});const { bash } = await createBashTool({
destination: "/home/user/app",
files: { "main.ts": "console.log('hi');" },
});
// Files at /home/user/app/main.ts, cwd is /home/user/app- just-bash is simulated - Cannot support python, node.js or binaries; use @vercel/sandbox for a full VM. So, createBashTool supports full VMs, it is just the default that does not
- No persistent state between calls - Each
createBashToolstarts fresh, but the tool itself has persistence and it can be achieved across serverless function invocations by passing in the same sandbox acrosscreateBashToolinvocations - Text files only -
filesoption accepts strings, not buffers - No streaming - Command output returned after completion
const { tools, sandbox } = await createBashTool();
const result = await tools.bash.execute({ command: "ls /nonexistent" });
if (result.exitCode !== 0) {
console.error("Command failed:", result.stderr);
}
// readFile throws on missing files
try {
await sandbox.readFile("/missing.txt");
} catch (e) {
// "File not found: /missing.txt" or "Failed to read file: ..."
}- Check sandbox type -
isVercelSandbox()andisJustBash()exported for detection - Inspect tool description -
bash.descriptionshows working dir and file list - Use
pwdfirst - Verify working directory matches expectations - Check
exitCode- Non-zero means command failed, checkstderr - Missing just-bash error - Install it or provide custom sandbox
TypeScript types are available in the .d.ts files:
# View main exports
cat node_modules/bash-tool/dist/index.d.ts
# View all options and types
cat node_modules/bash-tool/dist/types.d.ts
# Search for interfaces
grep -r "^export interface" node_modules/bash-tool/dist/*.d.tsKey types to explore:
CreateBashToolOptions- Options for createBashTool()BashToolkit- Return type with bash, tools, sandboxSandbox- Interface for custom sandbox implementationsCommandResult- Shape of executeCommand results