exec tool allows agents to execute shell commands with extensive safety guards, timeout controls, and workspace restrictions.
exec
Execute a shell command and return its output.Parameters
The shell command to execute.
Optional working directory for the command. If not specified, uses the tool’s default working directory.
Returns
The combined stdout and stderr output from the command. If stderr is present, it’s appended with a “STDERR:” header.
The exit code of the command (included in output if non-zero).
Usage Example
Security Model
Deny Patterns
The exec tool blocks dangerous commands by default using pattern matching:Destructive Operations
rm -rf/rm -f- Recursive/forced file deletiondel /f/rmdir /s- Windows deletion commandsformat,mkfs,diskpart- Disk formattingdd if=- Disk imaging/wiping- Writes to block devices (
> /dev/sd*, etc.)
System Control
shutdown,reboot,poweroff- Fork bombs (
: () { ... };:)
Command Injection
- Command substitution:
$(...),`...` - Variable expansion:
${...} - Pipe to shell:
| sh,| bash - Chained dangerous commands:
; rm -rf,&& rm -rf - Heredoc:
<< EOF
Privilege Escalation
sudochmod,chownpkill,killall,kill -9
Remote Execution
curl ... | shwget ... | bashssh user@hostevalsource script.sh
Package Management
npm install -gpip install --userapt install/remove/purgeyum install/removednf install/remove
Container & Version Control
docker run,docker execgit push,git force
Custom Patterns
You can extend or override deny patterns:Allow Patterns
Explicitly allow commands that match deny patterns:Disable All Deny Patterns
Workspace Restriction
When enabled, workspace restriction prevents commands from accessing files outside the workspace:Path Traversal Protection
- Blocks
../in commands - Blocks
..\\on Windows
Absolute Path Validation
Extracts absolute paths from commands and validates them:Safe Paths
These pseudo-devices are always allowed:/dev/null/dev/zero/dev/random,/dev/urandom/dev/stdin,/dev/stdout,/dev/stderr
Working Directory Validation
Ifworking_dir is provided, it’s validated against the workspace:
Timeout Control
Commands have a default 60-second timeout. Configure custom timeouts:Timeout Behavior
- When timeout is reached, sends termination signal to process
- Waits 2 seconds for graceful shutdown
- Force kills if process doesn’t exit
- Returns partial output with timeout error
Output Handling
Combined Output
Stdout and stderr are captured separately, then combined:Exit Codes
Non-zero exit codes are appended to output:Truncation
Output is truncated at 10,000 characters:Empty Output
Platform Support
Unix/Linux/macOS
Commands execute via:Windows
Commands execute via:Process Management
Process Groups
On Unix systems, commands run in their own process group to ensure all child processes are terminated on timeout or cancellation.Termination
- Graceful: Sends termination signal to process group
- Force: If process doesn’t exit within 2 seconds, sends SIGKILL
- Cleanup: Ensures all child processes are terminated
Error Handling
Guard Errors
Execution Errors
Timeout Errors
Best Practices
1. Use Specific Commands
2. Validate Paths
3. Set Working Directory
4. Handle Errors Gracefully
Check exit codes and parse stderr for error handling.5. Use File Tools When Possible
Preferread_file over cat, write_file over echo >, etc.