Implementation:Ollama Ollama XTools Bash
| Knowledge Sources | |
|---|---|
| Domains | Agent Loop, Tool Execution |
| Last Updated | 2025-02-15 00:00 GMT |
Overview
Implements the bash shell command execution tool for the Ollama agent loop, running commands with timeout and output size limits.
Description
BashTool implements the Tool interface. Execute runs a bash command with a 60-second timeout using exec.CommandContext, capturing both stdout and stderr separately. Output is truncated to 50KB to prevent overwhelming the LLM context. Returns exit codes and timeout information as part of the output string rather than as errors, so the LLM can reason about failures. Provides a schema definition with a single required command parameter.
Usage
Registered as the "bash" tool in the agent tool registry. The primary mechanism by which the LLM agent can interact with the user's system.
Code Reference
Source Location
- Repository: Ollama
- File: x/tools/bash.go
- Lines: 1-114
Signature
const bashTimeout = 60 * time.Second
const maxOutputSize = 50000
type BashTool struct{}
func (b *BashTool) Name() string
func (b *BashTool) Description() string
func (b *BashTool) Schema() api.ToolFunction
func (b *BashTool) Execute(args map[string]any) (string, error)
Import
import "github.com/ollama/ollama/x/tools"
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| command | string | Yes | The bash command to execute |
Outputs
| Name | Type | Description |
|---|---|---|
| output | string | Combined stdout/stderr with exit code info |
| error | error | Non-nil only for execution setup failures |
Usage Examples
tool := &tools.BashTool{}
output, err := tool.Execute(map[string]any{
"command": "ls -la /tmp",
})
// output: "total 48\ndrwxrwxrwt 12 root root ..."
// Timeout handling
output, err = tool.Execute(map[string]any{
"command": "sleep 120",
})
// output: "... Error: command timed out after 60 seconds"