GitHub CLI for AI agents
The GitHub CLI (gh) for coding agents: list PRs, read failed CI logs, open draft pull requests, and query the API as JSON, with safe-use rules.
What GitHub CLI does for an agent
The GitHub CLI is GitHub's official command-line tool. It brings pull requests, issues, Actions workflow runs, releases, and the full REST and GraphQL API to the terminal.
Most commands accept --json with a field list, so an agent gets structured data instead of scraping tables. Combined with jq, that covers most repository chores without a browser.
Official site: cli.github.com
Install GitHub CLI
The install commands from the official docs. Check it worked with gh --version.
brew install gh
winget install --id GitHub.cli
Signing in GitHub CLI for an agent
A human signs in once with gh auth login. In CI and sandboxes, gh reads a token from GH_TOKEN or GITHUB_TOKEN. Use a fine-grained token limited to the repositories and permissions the agent needs.
gh auth status
gh auth login
Environment variables: GH_TOKEN, GITHUB_TOKEN. Set them in the agent's environment; never paste values into prompts or commit them.
What agents use GitHub CLI for
Example commands with placeholder names. Swap in your own project, app, or file names.
List open pull requests as JSON
gh pr list --state open --json number,title,author
Check CI status on a pull request
gh pr checks 123
Read the logs of failed CI steps
gh run view 123456789 --log-failed
Open a draft pull request from the current branch
gh pr create --draft --fill
Call the API directly
gh api repos/{owner}/{repo}/issues --jq '.[].title'
Safety rules for agents
Safe to run without asking
gh auth statusgh pr list / view / diff / checksgh issue list / viewgh run list / viewgh api (GET requests)
Ask a human first
gh pr mergegh pr closegh issue close / deletegh release create / deletegh repo deletegh api -X POST / PATCH / DELETE
- Open pull requests as drafts and leave merging to a human.
- Use --json and --jq for output the agent parses instead of human-formatted tables.
- Never run gh auth token or print GH_TOKEN; keep tokens out of commits and chat.
- Scope tokens to the repositories the agent works in, with read-only permissions where possible.
AGENTS.md or CLAUDE.md snippet
Paste this into the instruction file your agent reads at the start of every session, then adjust the rules to your setup.
## GitHub CLI (gh) Use `gh` for working with GitHub pull requests, issues, Actions runs, and the GitHub API. - Check it is installed with `gh --version`. If it is missing, ask before installing it. - Auth: a human signs gh in. Check with `gh auth status`; if it fails, stop and ask. Never run the login flow yourself, and never print tokens (GH_TOKEN, GITHUB_TOKEN). - Run without asking (read-only): `gh auth status`, `gh pr list / view / diff / checks`, `gh issue list / view`, `gh run list / view`, `gh api (GET requests)`. - Ask a human first (changes or deletes things): `gh pr merge`, `gh pr close`, `gh issue close / delete`, `gh release create / delete`, `gh repo delete`, `gh api -X POST / PATCH / DELETE`. - Open pull requests as drafts and leave merging to a human. - Use --json and --jq for output the agent parses instead of human-formatted tables. - Never run gh auth token or print GH_TOKEN; keep tokens out of commits and chat. - Scope tokens to the repositories the agent works in, with read-only permissions where possible. - Prefer JSON output where the command supports it, and keep output short.
GitHub CLI or an MCP server?
An official MCP option exists. GitHub publishes an official GitHub MCP server, available as a remote server and for local use. Use it in chat clients and IDEs without a shell; use gh in terminal agents, where it is usually faster and cheaper in tokens.
Rule of thumb: in a terminal agent that a human has signed in, the CLI is usually faster and cheaper in context. In a chat client without a shell, or when you want a fixed set of typed tools, use an MCP server.
Other Dev & Git tools
GitHub CLI questions
How do I install GitHub CLI?
Homebrew: brew install gh. Other official options: Windows (WinGet). Check the install with gh --version, and see the GitHub docs for other platforms.
How does an AI agent authenticate GitHub CLI?
A human signs in once with gh auth login. In CI and sandboxes, gh reads a token from GH_TOKEN or GITHUB_TOKEN. Use a fine-grained token limited to the repositories and permissions the agent needs. The agent can check the sign-in with gh auth status and should stop and ask if it fails.
Which GitHub CLI commands are safe for an agent to run?
Commands that only read state are safe to run without asking: gh auth status; gh pr list / view / diff / checks; gh issue list / view; gh run list / view; gh api (GET requests). Ask a human before anything that changes or deletes something: gh pr merge; gh pr close; gh issue close / delete; gh release create / delete; gh repo delete; gh api -X POST / PATCH / DELETE.
Is there an MCP server for GitHub CLI?
GitHub publishes an official GitHub MCP server, available as a remote server and for local use. Use it in chat clients and IDEs without a shell; use gh in terminal agents, where it is usually faster and cheaper in tokens.