|
| 1 | +--- |
| 2 | +id: ai-agent-friendly-cli-patterns |
| 3 | +created: 2026-03-28 |
| 4 | +modified: 2026-03-28 |
| 5 | +type: research |
| 6 | +status: active |
| 7 | +tags: [cli, ai-agents, tool-design, mcp, self-discovery] |
| 8 | +--- |
| 9 | + |
| 10 | +# Research: What Makes a CLI "AI-Agent-Friendly"? |
| 11 | + |
| 12 | +## Overview |
| 13 | + |
| 14 | +This research investigates patterns and principles that make CLI tools easily navigable by AI agents. Key sources include the GitHub CLI's `AGENTS.md`, the Model Context Protocol (MCP) specification, and established conventions for structured output and error handling. |
| 15 | + |
| 16 | +## Key Principles for AI-Agent-Friendly CLIs |
| 17 | + |
| 18 | +### 1. Provide Structured, Machine-Readable Output |
| 19 | + |
| 20 | +**JSON Output Flags** |
| 21 | +- Offer `--json` or `--format json` flags for structured data output |
| 22 | +- Include `--jq` and `--template` flags for data manipulation (as GitHub CLI does) |
| 23 | +- Output should be valid, parseable JSON that preserves type information |
| 24 | + |
| 25 | +**Structured Content Patterns (from MCP)** |
| 26 | +```json |
| 27 | +{ |
| 28 | + "type": "text", |
| 29 | + "text": "Tool result text" |
| 30 | +} |
| 31 | +// or for structured data: |
| 32 | +{ |
| 33 | + "type": "structured", |
| 34 | + "structuredContent": { ... } |
| 35 | +} |
| 36 | +``` |
| 37 | + |
| 38 | +### 2. Create an `AGENTS.md` or Developer Guide |
| 39 | + |
| 40 | +GitHub CLI pioneered this pattern with their [`AGENTS.md`](https://github.com/cli/cli/blob/trunk/AGENTS.md) file. This file should include: |
| 41 | + |
| 42 | +- **Architecture overview**: Entry points, key packages, command structure |
| 43 | +- **Build/test instructions**: Commands for development and validation |
| 44 | +- **Command patterns**: How commands are structured (`$ gh foo bar`) |
| 45 | +- **Error types**: Specific error codes and what they mean |
| 46 | +- **Testing patterns**: How to test changes |
| 47 | + |
| 48 | +**Example structure from GitHub CLI:** |
| 49 | +```markdown |
| 50 | +# AGENTS.md |
| 51 | + |
| 52 | +This is [tool-name], a command-line tool for [purpose]. |
| 53 | + |
| 54 | +## Build, Test, and Lint |
| 55 | +\`\`\`bash |
| 56 | +make # Build |
| 57 | +go test ./... # Tests |
| 58 | +\`\`\` |
| 59 | + |
| 60 | +## Architecture |
| 61 | +Entry point: `cmd/...` |
| 62 | + |
| 63 | +## Error Handling |
| 64 | +- `FlagErrorf(...)` — flag validation errors |
| 65 | +- `SilentError` — exit 1, no message |
| 66 | +- `CancelError` — user cancelled |
| 67 | +- `NoResultsError` — empty results |
| 68 | +``` |
| 69 | + |
| 70 | +### 3. Design Structured Error Messages |
| 71 | + |
| 72 | +**Error Type Hierarchy (GitHub CLI pattern):** |
| 73 | +```go |
| 74 | +// Flag errors - show usage |
| 75 | +FlagErrorf("invalid flag: %s", flag) |
| 76 | + |
| 77 | +// Silent errors - exit 1 without message |
| 78 | +var SilentError = errors.New("SilentError") |
| 79 | + |
| 80 | +// Cancellation - user-initiated |
| 81 | +var CancelError = errors.New("CancelError") |
| 82 | + |
| 83 | +// No results - graceful empty state |
| 84 | +type NoResultsError struct { message string } |
| 85 | +``` |
| 86 | + |
| 87 | +**AI-friendly error messages should:** |
| 88 | +- Be parseable and categorized |
| 89 | +- Include error codes that map to specific failure modes |
| 90 | +- Provide actionable information |
| 91 | +- Distinguish between recoverable and fatal errors |
| 92 | + |
| 93 | +### 4. Implement Consistent Exit Codes (POSIX Convention) |
| 94 | + |
| 95 | +**Standard Exit Codes:** |
| 96 | +| Code | Meaning | |
| 97 | +|------|---------| |
| 98 | +| 0 | Success | |
| 99 | +| 1 | General error | |
| 100 | +| 2 | Misuse/shell conflict | |
| 101 | +| 126 | Command not executable | |
| 102 | +| 127 | Command not found | |
| 103 | +| 128+N | Terminated by signal N | |
| 104 | + |
| 105 | +**Extended codes (BSD/sendmail convention):** |
| 106 | +| Code | Meaning | |
| 107 | +|------|---------| |
| 108 | +| 64 | Usage error | |
| 109 | +| 65 | Data format error | |
| 110 | +| 66 | Cannot open input | |
| 111 | +| 69 | Service unavailable | |
| 112 | +| 70 | Internal software error | |
| 113 | +| 78 | Configuration error | |
| 114 | + |
| 115 | +### 5. Provide Self-Discovery Mechanisms |
| 116 | + |
| 117 | +**Built-in Help That Scales** |
| 118 | +- `--help` or `-h` for basic usage |
| 119 | +- `--help --verbose` or `man` pages for detailed docs |
| 120 | +- `--examples` flag showing common usage patterns |
| 121 | +- `--tour` or interactive tutorials for complex tools |
| 122 | + |
| 123 | +**Discovery via Structured Output:** |
| 124 | +```bash |
| 125 | +# List all available commands |
| 126 | +$ tool --list --format json |
| 127 | +$ tool --list-commands --json |
| 128 | + |
| 129 | +# Show command schema |
| 130 | +$ tool <subcommand> --help --json |
| 131 | +``` |
| 132 | + |
| 133 | +### 6. Define Tool Schemas (MCP Pattern) |
| 134 | + |
| 135 | +For tools that will be used via MCP or similar protocols: |
| 136 | + |
| 137 | +```json |
| 138 | +{ |
| 139 | + "name": "get_weather", |
| 140 | + "title": "Weather Information Provider", |
| 141 | + "description": "Get current weather information for a location", |
| 142 | + "inputSchema": { |
| 143 | + "type": "object", |
| 144 | + "properties": { |
| 145 | + "location": { |
| 146 | + "type": "string", |
| 147 | + "description": "City name or zip code" |
| 148 | + } |
| 149 | + }, |
| 150 | + "required": ["location"] |
| 151 | + } |
| 152 | +} |
| 153 | +``` |
| 154 | + |
| 155 | +### 7. Support Output Annotations |
| 156 | + |
| 157 | +Include metadata that helps AI agents prioritize and route information: |
| 158 | + |
| 159 | +```json |
| 160 | +{ |
| 161 | + "annotations": { |
| 162 | + "audience": ["user", "assistant"], |
| 163 | + "priority": 0.8, |
| 164 | + "lastModified": "2025-01-12T15:00:58Z" |
| 165 | + } |
| 166 | +} |
| 167 | +``` |
| 168 | + |
| 169 | +### 8. Design for Idempotency and Safety |
| 170 | + |
| 171 | +**For AI agents that may retry commands:** |
| 172 | +- Make operations idempotent where possible |
| 173 | +- Provide `--dry-run` or `--preview` flags |
| 174 | +- Support `--force` for intentional overwrites |
| 175 | +- Use `--yes` or `-y` for non-interactive confirmation |
| 176 | + |
| 177 | +### 9. Document Command Hierarchy Clearly |
| 178 | + |
| 179 | +**Consistent Command Structure:** |
| 180 | +``` |
| 181 | +tool # Root - overview, auth |
| 182 | +tool <entity> # Entity commands |
| 183 | +tool <entity> list # List entities |
| 184 | +tool <entity> get <id> # Get specific entity |
| 185 | +tool <entity> create # Create new entity |
| 186 | +tool <entity> update <id> # Update entity |
| 187 | +tool <entity> delete <id> # Delete entity |
| 188 | +``` |
| 189 | + |
| 190 | +### 10. Support Progressive Disclosure |
| 191 | + |
| 192 | +**Verbose vs. Compact Output:** |
| 193 | +```bash |
| 194 | +$ tool --quiet # Minimal output |
| 195 | +$ tool # Normal output |
| 196 | +$ tool --verbose # Detailed output |
| 197 | +$ tool --debug # Debug information |
| 198 | +``` |
| 199 | + |
| 200 | +## Resources and References |
| 201 | + |
| 202 | +- [GitHub CLI AGENTS.md](https://github.com/cli/cli/blob/trunk/AGENTS.md) — Pioneer of AI-agent documentation |
| 203 | +- [Model Context Protocol (MCP)](https://modelcontextprotocol.io/) — Standard for AI-tool integration |
| 204 | +- [POSIX Exit Codes](https://pubs.opengroup.org/onlinepubs/9699919799/) — Unix exit status conventions |
| 205 | +- [sysexits.h](https://www.freebsd.org/cgi/man.cgi?query=sysexits) — BSD exit code conventions |
| 206 | + |
| 207 | +## Patterns to Avoid |
| 208 | + |
| 209 | +1. **Unstructured error messages** — "Something went wrong" provides no actionable info |
| 210 | +2. **Inconsistent exit codes** — Same error condition returning different codes |
| 211 | +3. **No JSON output option** — Requiring text parsing for automation |
| 212 | +4. **Hidden functionality** — Features only discoverable by reading source |
| 213 | +5. **Non-idempotent operations** — `create` creates duplicates instead of failing gracefully |
| 214 | +6. **Blocking interactive prompts** — No way to run headless in CI/automation |
| 215 | + |
| 216 | +## Implementation Checklist |
| 217 | + |
| 218 | +- [ ] Add `--json` output flag to all list/display commands |
| 219 | +- [ ] Create `AGENTS.md` with architecture and patterns |
| 220 | +- [ ] Define and document error codes |
| 221 | +- [ ] Use POSIX exit code conventions (0=success, non-zero=failure) |
| 222 | +- [ ] Add `--dry-run`/`--preview` flags for destructive operations |
| 223 | +- [ ] Include `--list` or `--help` output suitable for programmatic parsing |
| 224 | +- [ ] Add examples to all command help text |
| 225 | +- [ ] Consider MCP server implementation for AI integration |
| 226 | + |
| 227 | +--- |
| 228 | + |
| 229 | +*Research completed: 2026-03-28* |
| 230 | +*Primary sources: GitHub CLI AGENTS.md, MCP Specification, POSIX Standards* |
0 commit comments