|
| 1 | +--- |
| 2 | +id: cli |
| 3 | +title: CLI Protocol |
| 4 | +sidebar_position: 4 |
| 5 | +--- |
| 6 | + |
| 7 | +# CLI Protocol |
| 8 | + |
| 9 | +The CLI protocol plugin (`utcp-cli`) enables UTCP to execute command-line tools and scripts. This is particularly useful for wrapping existing CLI applications and making them available to AI agents. |
| 10 | + |
| 11 | +## Installation |
| 12 | + |
| 13 | +```bash |
| 14 | +pip install utcp-cli |
| 15 | +``` |
| 16 | + |
| 17 | +## Call Template Structure |
| 18 | + |
| 19 | +```json |
| 20 | +{ |
| 21 | + "call_template_type": "cli", |
| 22 | + "command": "curl", |
| 23 | + "args": [ |
| 24 | + "-X", "GET", |
| 25 | + "-H", "Authorization: Bearer ${API_TOKEN}", |
| 26 | + "https://api.example.com/data" |
| 27 | + ], |
| 28 | + "working_directory": "/tmp", |
| 29 | + "environment": { |
| 30 | + "API_TOKEN": "${API_TOKEN}" |
| 31 | + }, |
| 32 | + "timeout": 30 |
| 33 | +} |
| 34 | +``` |
| 35 | + |
| 36 | +## Configuration Options |
| 37 | + |
| 38 | +### Required Fields |
| 39 | + |
| 40 | +| Field | Type | Description | |
| 41 | +|-------|------|-------------| |
| 42 | +| `call_template_type` | string | Must be `"cli"` | |
| 43 | +| `command` | string | The command to execute | |
| 44 | + |
| 45 | +### Optional Fields |
| 46 | + |
| 47 | +| Field | Type | Description | |
| 48 | +|-------|------|-------------| |
| 49 | +| `args` | array | Command arguments | |
| 50 | +| `working_directory` | string | Working directory for command execution | |
| 51 | +| `environment` | object | Environment variables | |
| 52 | +| `timeout` | number | Execution timeout in seconds (default: 30) | |
| 53 | +| `shell` | boolean | Whether to execute through shell (default: false) | |
| 54 | +| `capture_output` | boolean | Whether to capture stdout/stderr (default: true) | |
| 55 | + |
| 56 | +## Security Considerations |
| 57 | + |
| 58 | +:::danger Security Warning |
| 59 | +CLI protocol executes commands on the local system. Always validate inputs and use with caution. |
| 60 | +::: |
| 61 | + |
| 62 | +### Input Validation |
| 63 | + |
| 64 | +Always validate and sanitize inputs to prevent command injection: |
| 65 | + |
| 66 | +```json |
| 67 | +{ |
| 68 | + "name": "safe_file_read", |
| 69 | + "description": "Safely read a file", |
| 70 | + "inputs": { |
| 71 | + "type": "object", |
| 72 | + "properties": { |
| 73 | + "filename": { |
| 74 | + "type": "string", |
| 75 | + "pattern": "^[a-zA-Z0-9._-]+$" |
| 76 | + } |
| 77 | + }, |
| 78 | + "required": ["filename"] |
| 79 | + }, |
| 80 | + "tool_call_template": { |
| 81 | + "call_template_type": "cli", |
| 82 | + "command": "cat", |
| 83 | + "args": ["${filename}"], |
| 84 | + "working_directory": "/safe/directory" |
| 85 | + } |
| 86 | +} |
| 87 | +``` |
| 88 | + |
| 89 | +### Sandboxing |
| 90 | + |
| 91 | +Consider running CLI tools in sandboxed environments: |
| 92 | + |
| 93 | +```json |
| 94 | +{ |
| 95 | + "call_template_type": "cli", |
| 96 | + "command": "docker", |
| 97 | + "args": [ |
| 98 | + "run", "--rm", "--read-only", |
| 99 | + "-v", "/safe/data:/data:ro", |
| 100 | + "alpine:latest", |
| 101 | + "cat", "/data/${filename}" |
| 102 | + ] |
| 103 | +} |
| 104 | +``` |
| 105 | + |
| 106 | +## Examples |
| 107 | + |
| 108 | +### Simple Command Execution |
| 109 | + |
| 110 | +```json |
| 111 | +{ |
| 112 | + "name": "get_system_info", |
| 113 | + "description": "Get system information", |
| 114 | + "inputs": { |
| 115 | + "type": "object", |
| 116 | + "properties": {} |
| 117 | + }, |
| 118 | + "tool_call_template": { |
| 119 | + "call_template_type": "cli", |
| 120 | + "command": "uname", |
| 121 | + "args": ["-a"] |
| 122 | + } |
| 123 | +} |
| 124 | +``` |
| 125 | + |
| 126 | +### File Operations |
| 127 | + |
| 128 | +```json |
| 129 | +{ |
| 130 | + "name": "list_directory", |
| 131 | + "description": "List files in a directory", |
| 132 | + "inputs": { |
| 133 | + "type": "object", |
| 134 | + "properties": { |
| 135 | + "path": { |
| 136 | + "type": "string", |
| 137 | + "description": "Directory path to list" |
| 138 | + } |
| 139 | + }, |
| 140 | + "required": ["path"] |
| 141 | + }, |
| 142 | + "tool_call_template": { |
| 143 | + "call_template_type": "cli", |
| 144 | + "command": "ls", |
| 145 | + "args": ["-la", "${path}"], |
| 146 | + "timeout": 10 |
| 147 | + } |
| 148 | +} |
| 149 | +``` |
| 150 | + |
| 151 | +### Script Execution |
| 152 | + |
| 153 | +```json |
| 154 | +{ |
| 155 | + "name": "run_analysis", |
| 156 | + "description": "Run data analysis script", |
| 157 | + "inputs": { |
| 158 | + "type": "object", |
| 159 | + "properties": { |
| 160 | + "input_file": {"type": "string"}, |
| 161 | + "output_format": {"type": "string", "enum": ["json", "csv"]} |
| 162 | + }, |
| 163 | + "required": ["input_file"] |
| 164 | + }, |
| 165 | + "tool_call_template": { |
| 166 | + "call_template_type": "cli", |
| 167 | + "command": "python", |
| 168 | + "args": [ |
| 169 | + "/scripts/analyze.py", |
| 170 | + "--input", "${input_file}", |
| 171 | + "--format", "${output_format}" |
| 172 | + ], |
| 173 | + "working_directory": "/workspace", |
| 174 | + "environment": { |
| 175 | + "PYTHONPATH": "/workspace/lib" |
| 176 | + }, |
| 177 | + "timeout": 300 |
| 178 | + } |
| 179 | +} |
| 180 | +``` |
| 181 | + |
| 182 | +### Git Operations |
| 183 | + |
| 184 | +```json |
| 185 | +{ |
| 186 | + "name": "git_status", |
| 187 | + "description": "Get git repository status", |
| 188 | + "inputs": { |
| 189 | + "type": "object", |
| 190 | + "properties": { |
| 191 | + "repo_path": {"type": "string"} |
| 192 | + }, |
| 193 | + "required": ["repo_path"] |
| 194 | + }, |
| 195 | + "tool_call_template": { |
| 196 | + "call_template_type": "cli", |
| 197 | + "command": "git", |
| 198 | + "args": ["status", "--porcelain"], |
| 199 | + "working_directory": "${repo_path}" |
| 200 | + } |
| 201 | +} |
| 202 | +``` |
| 203 | + |
| 204 | +## Output Handling |
| 205 | + |
| 206 | +The CLI protocol captures and returns: |
| 207 | + |
| 208 | +- **stdout**: Standard output as the primary result |
| 209 | +- **stderr**: Standard error (included in error cases) |
| 210 | +- **return_code**: Process exit code |
| 211 | +- **execution_time**: Time taken to execute |
| 212 | + |
| 213 | +### Success Response |
| 214 | + |
| 215 | +```json |
| 216 | +{ |
| 217 | + "stdout": "file1.txt\nfile2.txt\n", |
| 218 | + "stderr": "", |
| 219 | + "return_code": 0, |
| 220 | + "execution_time": 0.123 |
| 221 | +} |
| 222 | +``` |
| 223 | + |
| 224 | +### Error Response |
| 225 | + |
| 226 | +```json |
| 227 | +{ |
| 228 | + "stdout": "", |
| 229 | + "stderr": "ls: cannot access '/invalid/path': No such file or directory\n", |
| 230 | + "return_code": 2, |
| 231 | + "execution_time": 0.045 |
| 232 | +} |
| 233 | +``` |
| 234 | + |
| 235 | +## Environment Variables |
| 236 | + |
| 237 | +Set environment variables for command execution: |
| 238 | + |
| 239 | +```json |
| 240 | +{ |
| 241 | + "call_template_type": "cli", |
| 242 | + "command": "node", |
| 243 | + "args": ["app.js"], |
| 244 | + "environment": { |
| 245 | + "NODE_ENV": "production", |
| 246 | + "API_KEY": "${API_KEY}", |
| 247 | + "PORT": "3000" |
| 248 | + } |
| 249 | +} |
| 250 | +``` |
| 251 | + |
| 252 | +## Working Directory |
| 253 | + |
| 254 | +Specify the working directory for command execution: |
| 255 | + |
| 256 | +```json |
| 257 | +{ |
| 258 | + "call_template_type": "cli", |
| 259 | + "command": "make", |
| 260 | + "args": ["build"], |
| 261 | + "working_directory": "/project/src" |
| 262 | +} |
| 263 | +``` |
| 264 | + |
| 265 | +## Best Practices |
| 266 | + |
| 267 | +1. **Validate Inputs**: Always validate and sanitize user inputs |
| 268 | +2. **Use Absolute Paths**: Prefer absolute paths for commands and files |
| 269 | +3. **Set Timeouts**: Configure appropriate timeouts to prevent hanging |
| 270 | +4. **Limit Permissions**: Run with minimal necessary permissions |
| 271 | +5. **Sandbox Execution**: Use containers or chroot when possible |
| 272 | +6. **Log Execution**: Log all command executions for audit trails |
| 273 | +7. **Handle Errors**: Properly handle and report command failures |
| 274 | + |
| 275 | +## Common Use Cases |
| 276 | + |
| 277 | +- **Development Tools**: Git, npm, pip, docker commands |
| 278 | +- **System Administration**: File operations, process management |
| 279 | +- **Data Processing**: Scripts for ETL, analysis, reporting |
| 280 | +- **Build Systems**: Make, gradle, webpack execution |
| 281 | +- **Testing**: Running test suites and validation scripts |
| 282 | + |
| 283 | +## Error Handling |
| 284 | + |
| 285 | +| Error Type | Description | Handling | |
| 286 | +|------------|-------------|----------| |
| 287 | +| Command Not Found | Command doesn't exist | Raise `CommandNotFoundError` | |
| 288 | +| Permission Denied | Insufficient permissions | Raise `PermissionError` | |
| 289 | +| Timeout | Command exceeded timeout | Raise `TimeoutError` | |
| 290 | +| Non-zero Exit | Command failed | Include stderr in error | |
| 291 | + |
| 292 | +## Platform Considerations |
| 293 | + |
| 294 | +### Windows |
| 295 | + |
| 296 | +```json |
| 297 | +{ |
| 298 | + "call_template_type": "cli", |
| 299 | + "command": "cmd", |
| 300 | + "args": ["/c", "dir", "${path}"], |
| 301 | + "shell": true |
| 302 | +} |
| 303 | +``` |
| 304 | + |
| 305 | +### Unix/Linux |
| 306 | + |
| 307 | +```json |
| 308 | +{ |
| 309 | + "call_template_type": "cli", |
| 310 | + "command": "ls", |
| 311 | + "args": ["-la", "${path}"] |
| 312 | +} |
| 313 | +``` |
| 314 | + |
| 315 | +### Cross-platform |
| 316 | + |
| 317 | +```json |
| 318 | +{ |
| 319 | + "call_template_type": "cli", |
| 320 | + "command": "python", |
| 321 | + "args": ["-c", "import os; print(os.listdir('${path}'))"] |
| 322 | +} |
| 323 | +``` |
0 commit comments