Solving Claude Code's gh Command Permission Prompts with a Readonly Wrapper
Solving Claude Code's gh Command Permission Prompts with a Readonly Wrapper
Current Situation Analysis
When integrating the GitHub CLI (gh) into Claude Code workflows, developers encounter a critical friction point: Claude Code triggers an interactive permission prompt for every single gh api invocation, regardless of the operation's intent. Even harmless, read-only queries like listing repository commits require manual approval, effectively breaking autonomous agent execution.
The fundamental failure mode stems from Claude Code's permission model, which operates at the command-execution level rather than the semantic operation level. The system cannot natively distinguish between a safe read operation (gh api "repos/.../commits") and a destructive write operation (gh pr merge or gh repo delete).
Traditional workarounds fail because:
- Blanket Allow: Granting unrestricted
allowforgheliminates prompts but exposes the repository to unintended modifications, secret leaks, or accidental destructive actions by the AI agent. - Command Filtering: Regex-based shell aliases or
PATHmanipulation are easily bypassed by LLMs that generate commands dynamically or fall back to alternative invocation methods. - Credential Sharing: Using the same
ghauthentication state for both read and write contexts makes it impossible to enforce least-privilege principles at the agent level.
WOW Moment: Key Findings
By decoupling read and write contexts using environment-based configuration isolation, we achieve a secure, frictionless workflow. The following comparison demonstrates the operational impact of the ghro wrapper approach versus standard configurations:
| Approach | Permission Prompt Frequency | Security Risk (Write Exposure) | Workflow Autonomy |
|---|---|---|---|
Default gh (Manual Prompts) |
100% | Low (Human-in-the-loop) | 0% |
Default gh (Blanket Allow) |
0% | Critical | 100% |
ghro Readonly Wrapper |
0% (Read) / 100% (Write) | Minimal | 95%+ |
Key Findings:
- Sweet Spot: The
GH_CONFIG_DIRenvironment variable enables complete credential and configuration isolation without modifying theghbinary or relying on fragile shell parsing. - Zero-Trust Read Path: Read operations execute autonomously while write operations remain gated, preserving both developer velocity and repository integrity.
- Agent-Native Design: The solution aligns with Claude Code's instruction-following architecture by explicitly routing operations through semantic command separation (
ghrovsgh).
Core Solution
The implementation relies on GitHub's Fine-grained Personal Access Tokens, environment-driven configuration isolation, and a lightweight shell wrapper. Follow these steps to deploy the readonly execution path:
1. Create a Fine-grained Personal Access Token Generate a token on GitHub with strictly read-only repository scopes. Ensure write permissions for contents, pull requests, issues, and secrets are explicitly disabled.
2. Authenticate with an Isolated Readonly Profile
Leverage GH_CONFIG_DIR to maintain separate credentials from your default gh configuration. This prevents credential leakage and enforces scope boundaries.
GH_CONFIG_DIR=~/.config/ghro gh auth login --with-token <<< "<your-token>"
3. Create the ghro Wrapper Script
Place the following script in a directory included in your system PATH. The exec directive ensures process replacement, maintaining proper signal handling and avoiding zombie processes.
#!/bin/sh
GH_CONFIG_DIR=~/.config/ghro exec gh "$@"
Make the wrapper executable:
chmod +x ghro
4. Configure Claude Code Instructions
Update your project's CLAUDE.md to explicitly route operations. This directive ensures the model respects the read/write boundary during autonomous execution.
## When fetching information or content from GitHub
- Use the ghro command for readonly access to GitHub information
- Fetch cannot retrieve file contents β use the ghro command instead
- For any write operations, use the gh command instead of ghro
With this architecture, read operations bypass permission prompts entirely, while write operations correctly trigger Claude Code's safety gates. The separation of concerns provides both guardrails and operational convenience.
Pitfall Guide
- Over-Permissive Fine-grained Token Scopes: Accidentally granting "Read and Write" or "Admin" scopes to the readonly token defeats the entire security model. Always verify repository access is strictly limited to
Contents: Read-only,Issues: Read-only, andPull Requests: Read-only. GH_CONFIG_DIRPath Resolution Failures: Using unexpanded~or relative paths in non-interactive shells can causeexec ghto fallback to the default config or fail silently. Ensure the wrapper uses absolute paths or that the execution environment properly expands tilde notation.- Claude Code Context Drift: LLMs may ignore implicit instructions over time. If
CLAUDE.mdlacks explicit routing rules, the model may default toghfor all operations. Use imperative, rule-based syntax and periodically audit agent command logs. - Bypassing via Direct HTTP Clients: Claude Code might attempt to use
curlorwgetwith environment variables or hardcoded tokens ifghrois restricted. Ensure no globalGITHUB_TOKENis exposed to the agent's execution environment, and rely solely on the isolatedghroprofile. - Shell Compatibility &
execOmission: Replacingexecwith a standard subshell call (GH_CONFIG_DIR=... gh "$@") creates a child process that breaks signal propagation (e.g.,Ctrl+Cwon't terminate the CLI properly). Always useexecto replace the shell process. - Write Operation Prompt Fatigue: While
ghrosolves read friction, write operations will still prompt. Define clear thresholds inCLAUDE.mdfor when to useghvsghroto prevent unnecessary interruptions or silent failures when write access is genuinely required.
Deliverables
- Architecture Blueprint: Visual workflow diagram showing the read/write command routing,
GH_CONFIG_DIRisolation boundaries, and Claude Code permission gate interactions. - Implementation Checklist: Step-by-step verification guide covering token scope validation,
PATHconfiguration, wrapper execution testing, andCLAUDE.mddirective validation. - Configuration Templates:
- Ready-to-deploy
ghrowrapper script with error handling and absolute path fallbacks CLAUDE.mdrouting ruleset optimized for Claude Code's instruction parsing- Fine-grained PAT scope matrix template for GitHub organization/enterprise deployment
- Ready-to-deploy
