Skip to content
CLI tool · Dev & Git

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.

ghby GitHubOfficial MCP option
What it is

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

Install GitHub CLI

The install commands from the official docs. Check it worked with gh --version.

Homebrew
brew install gh
Windows (WinGet)
winget install --id GitHub.cli
Auth

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.

Check the sign-in (read-only)
gh auth status
Interactive sign-in (human)
gh auth login

Environment variables: GH_TOKEN, GITHUB_TOKEN. Set them in the agent's environment; never paste values into prompts or commit them.

Common agent tasks

What agents use GitHub CLI for

Example commands with placeholder names. Swap in your own project, app, or file names.

  1. List open pull requests as JSON

    gh
    gh pr list --state open --json number,title,author
  2. Check CI status on a pull request

    gh
    gh pr checks 123
  3. Read the logs of failed CI steps

    gh
    gh run view 123456789 --log-failed
  4. Open a draft pull request from the current branch

    gh
    gh pr create --draft --fill
  5. Call the API directly

    gh
    gh api repos/{owner}/{repo}/issues --jq '.[].title'
Safety

Safety rules for agents

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 first

  • 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.
Agent instructions

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.

AGENTS.md
## 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.
CLI or MCP server?

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.

Same category
All Dev & Git tools
FAQ

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.