🔧 解决方案:使用GitHub MCP服务器(不是GitHub Copilot)
你需要使用 官方GitHub MCP服务器 来自ModelContextProtocol,而不是GitHub Copilot API端点。
✅ 正确的设置步骤
第一步:安装GitHub MCP服务器
# Install the official GitHub MCP server
npm install -g @modelcontextprotocol/server-github
# Or using npx (no installation needed)
# You'll use npx in the config步骤2:创建GitHub个人访问令牌
- 转到GitHub:https://github.com/settings/tokens
- 点击 “生成新令牌” → “生成新令牌(经典)”
- 给它起个名字:
Claude MCP Access - 选择范围:
- ✅ repo (完全控制私有存储库) - ✅ read:org (阅读组织和团队成员) - ✅ read:user (读取用户配置文件数据) - ✅ user:email (访问用户电子邮件地址)
- 点击 “生成令牌”
- 复制令牌 (你不会再看到它了!)
步骤3:配置Claude桌面
编辑您的Claude配置文件:
# Open the config file
nano ~/.claude.json
# Or use your preferred editor
code ~/.claude.json添加此配置:
{
"mcpServers": {
"github": {
"command": "npx",
"args": [
"-y",
"@modelcontextprotocol/server-github"
],
"env": {
"GITHUB_PERSONAL_ACCESS_TOKEN": "ghp_your_actual_token_here"
}
}
}
}替换 ghp_your_actual_token_here 使用您的真实GitHub代币!
步骤4:重新启动克劳德桌面
# Completely quit Claude Desktop (not just close window)
# Then restart it
# On Linux, you can force quit:
pkill -9 claude
# Then relaunch Claude Desktop步骤5:验证连接
在Claude Desktop中,键入 /mcp 你应该看到:
Github MCP Server
Status: ● enabled______________________________________________________________________
🔐 备选方案:项目特定配置
如果你只想让GitHub MCP服务器用于你的Hackathon2项目:
cd /path/to/hackathon2-todo-app
# Create project-specific config
nano .claude.json添加相同的配置:
{
"mcpServers": {
"github": {
"command": "npx",
"args": [
"-y",
"@modelcontextprotocol/server-github"
],
"env": {
"GITHUB_PERSONAL_ACCESS_TOKEN": "ghp_your_token_here"
}
}
}
}这使得GitHub访问令牌仅限于此项目。
______________________________________________________________________
🛠️ 故障排除
问题:“找不到命令:npx”
# Install Node.js and npm first
# On Ubuntu/Debian:
sudo apt update
sudo apt install nodejs npm
# Verify installation
node --version
npm --version
npx --version问题:“权限被拒绝”
# Fix npm permissions
mkdir ~/.npm-global
npm config set prefix '~/.npm-global'
echo 'export PATH=~/.npm-global/bin:$PATH' >> ~/.bashrc
source ~/.bashrc问题:令牌不起作用
确保您的令牌具有正确的作用域:
- ✅
repo(最重要) - ✅
read:org - ✅
read:user - ✅
user:email
问题:仍然显示“禁用”
- 检查你的
.claude.json语法(有效的JSON) - 完全退出并重新启动Claude Desktop
- 检查克劳德日志:
# Linux logs location
~/.config/Claude/logs/
# Check recent errors
tail -f ~/.config/Claude/logs/main.log______________________________________________________________________
📋 使用GitHub MCP服务器可以获得什么
启用后,您可以要求Claude:
- 搜索存储库:“查找我仓库中的所有Python文件”
- 读取文件:“显示main.py的内容”
- 创建问题:“为这个bug创建一个问题”
- 列出分支:“此回购中存在哪些分支?”
- 获取提交历史记录:“显示最近的提交”
- 搜索代码:“查找我们在哪里使用FastAPI”
______________________________________________________________________
🎯 为您的Hackathon2项目
设置GitHub MCP后,您可以使用它来:
# Example prompts for Claude:
"List all files in my hackathon2-todo-app repository"
"Show me the current directory structure of my project"
"Create a new branch called 'feature/basic-crud'"
"Commit the constitution file with message 'feat: add project constitution'"
"Create an issue titled 'Implement recurring tasks feature' with the specification from specs/recurring-tasks.md"
"Search for all TODO comments in my codebase"______________________________________________________________________
⚠️ 安全说明
永不承诺 .claude.json 将您的代币发送到GitHub!
添加到 .gitignore:
echo ".claude.json" >> .gitignore
git add .gitignore
git commit -m "chore: ignore Claude config with sensitive tokens"______________________________________________________________________
✅ 快速摘要
走错了路 (你尝试了什么):
{
"url": "https://api.githubcopilot.com/mcp/" ❌
}正确的方式:
{
"mcpServers": {
"github": {
"command": "npx",
"args": ["-y", "@modelcontextprotocol/server-github"],
"env": {
"GITHUB_PERSONAL_ACCESS_TOKEN": "ghp_your_token"
}
}
}
}遵循以下步骤,您的GitHub MCP服务器将完美运行! 🚀
