Working with Private Repositories¶
Use GitHub to MCP with private repositories using authentication.
GitHub Token Setup¶
To access private repositories, you need a GitHub Personal Access Token (PAT).
Creating a Token¶
- Go to GitHub Settings → Developer Settings → Personal Access Tokens
- Click "Generate new token (classic)"
- Give it a descriptive name (e.g., "GitHub to MCP")
- Select scopes:
repo- Full control of private repositories- Or
read:packagesfor read-only access - Click "Generate token"
- Copy the token immediately (you won't see it again!)
Token Scopes¶
| Scope | Description | Use Case |
|---|---|---|
repo | Full access to private repos | Read/write operations |
public_repo | Access to public repos only | Higher rate limits |
read:packages | Read packages | Package registries |
Using Your Token¶
Environment Variable (Recommended)¶
Set the GITHUB_TOKEN environment variable:
Then use normally:
CLI Flag¶
Pass the token directly:
Security Warning
Don't commit tokens to version control or share them in logs.
Programmatic¶
import { generateFromGithub } from '@nirholas/github-to-mcp';
const result = await generateFromGithub(
'https://github.com/your-org/private-repo',
{ token: process.env.GITHUB_TOKEN }
);
Web UI Authentication¶
On the web interface:
- Click the "Settings" gear icon
- Enter your GitHub token in the "GitHub Token" field
- The token is stored locally in your browser (not sent to servers)
- Generate private repos normally
GitHub Enterprise¶
For GitHub Enterprise Server, use the full URL:
Rate Limits¶
| Authentication | Rate Limit |
|---|---|
| Anonymous | 60 requests/hour |
| With Token | 5,000 requests/hour |
| GitHub Enterprise | Varies by instance |
Always Use a Token
Even for public repos, using a token gives you 83x more API calls.
Organization Repositories¶
SSO-Enabled Organizations¶
If your organization uses SAML SSO:
- Create your token normally
- Go to Token Settings
- Find your token and click "Enable SSO"
- Authorize for your organization
Fine-Grained Tokens¶
For better security, use fine-grained tokens:
- Go to Fine-grained tokens
- Click "Generate new token"
- Select specific repositories
- Choose minimal permissions:
Contents: Read-onlyMetadata: Read-only
Security Best Practices¶
1. Rotate Tokens Regularly¶
Set a reminder to regenerate tokens every 90 days.
2. Use Minimum Scopes¶
Only request the permissions you need.
3. Never Commit Tokens¶
Add to .gitignore:
4. Use Environment Variables¶
Never hardcode tokens in your code.
5. Audit Token Usage¶
Check Security Log for token activity.
Troubleshooting¶
"Bad credentials" Error¶
- Token may be expired
- Token may lack required scopes
- SSO may not be authorized
"Not Found" Error¶
- Repo doesn't exist or is private
- Token lacks
reposcope - Token not authorized for organization
Rate Limit Exceeded¶
- Wait for the limit to reset (usually 1 hour)
- Use a token if not already
- Check for API-heavy operations
Check Token Validity¶
Should return your user information.
Example: Private Repo Workflow¶
Complete workflow for a private repository:
# 1. Set token
export GITHUB_TOKEN=ghp_xxxxxxxxxxxxx
# 2. Generate MCP server
npx @nirholas/github-to-mcp https://github.com/my-company/internal-api \
-o ~/mcp-servers/internal-api
# 3. Install dependencies
cd ~/mcp-servers/internal-api
npm install
# 4. Test the server
npm start
# 5. Add to Claude Desktop
# Edit claude_desktop_config.json
Next Steps¶
- Custom Tools - Extend your MCP server
- Claude Desktop - Connect your server
- Configuration - More options