ChatGPT+GitHub DevOps代理
一个结合了以下功能的Node.js应用程序 ChatGPT的智能规划 随着 真实的GitHub API操作 创建一个自主的DevOps助手。此脚本理解自然语言请求,并执行GitHub操作,如列出存储库、创建分支和管理问题。
🌟 特性
- 🧠 智能规划:使用ChatGPT了解和规划DevOps工作流程
- 🔧 真实的GitHub操作:用于存储库管理的直接GitHub API集成
- 📊 智能分页:自动获取您的所有存储库(而不仅仅是前10个)
- 🎯 自然语言接口:“显示我的存储库”→ 自动化执行
- 📝 智能摘要:ChatGPT提供人性化的结果摘要
- 🔄 双模式:交互式聊天模式或单命令执行
- ✅ Windows兼容的:绕过Windows上的MCP stdio问题
- 🐳 Docker MCP集成:可选的高级MCP服务器设置可增强功能
📋 先决条件
在运行此脚本之前,请确保您已经:
- Node.js (版本18或更高)- 点击此处下载
- npm (附带Node.js)
- OpenAI API密钥 - 在这里买你的
- GitHub个人访问令牌 - 在此处创建一个
- 码头工人 (可选,用于MCP服务器设置)- 点击此处下载
🚀 安装
第一步:克隆或下载
# Create project directory
mkdir chatgpt-github-agent
cd chatgpt-github-agent步骤2:初始化项目
# Initialize npm project
npm init -y
# Install dependencies
npm install axios readline步骤3:配置ES模块
编辑您的 package.json 添加ES模块支持:
{
"name": "chatgpt-github-agent",
"version": "1.0.0",
"type": "module",
"description": "ChatGPT + GitHub DevOps Agent",
"main": "simple-chatgpt-github.js",
"scripts": {
"start": "node simple-chatgpt-github.js --interactive",
"test": "node simple-chatgpt-github.js \"show me my repositories\"",
"mcp-test": "docker run -i --rm -e GITHUB_PERSONAL_ACCESS_TOKEN=%GITHUB_TOKEN% ghcr.io/github/github-mcp-server"
},
"dependencies": {
"axios": "^1.0.0",
"readline": "^1.0.0"
}
}步骤4:创建脚本
创建 simple-chatgpt-github.js 并粘贴我们对话中提供的完整脚本代码。
🔑 API密钥设置
OpenAI API密钥
- 访问 OpenAI平台
- 登录并创建新的API密钥
- 复制密钥(以开头
sk-)
GitHub个人访问令牌
- 首选
- 点击 “生成新令牌(经典)”
- 设置过期时间并选择范围:
- ✅ repo (完全控制私有存储库) - ✅ read:user (读取用户配置文件数据) - ✅ workflow (更新GitHub Action工作流) - ✅ read:packages (从GitHub软件包注册表下载软件包)
- 生成并复制令牌(以开头
ghp_)
环境变量(推荐)
Windows(PowerShell):
# Set for current session
$env:OPENAI_API_KEY="sk-your-openai-key-here"
$env:GITHUB_TOKEN="ghp_your-github-token-here"
# Set permanently (optional)
[Environment]::SetEnvironmentVariable("OPENAI_API_KEY", "sk-your-key", "User")
[Environment]::SetEnvironmentVariable("GITHUB_TOKEN", "ghp-your-token", "User")macOS/Linux(Bash):
# Set for current session
export OPENAI_API_KEY="sk-your-openai-key-here"
export GITHUB_TOKEN="ghp_your-github-token-here"
# Set permanently (add to ~/.bashrc or ~/.zshrc)
echo 'export OPENAI_API_KEY="sk-your-key"' >> ~/.bashrc
echo 'export GITHUB_TOKEN="ghp_your-token"' >> ~/.bashrc🐳 Docker+GitHub MCP服务器设置(高级)
为了增强功能和与MCP协议的兼容性,您可以通过Docker设置官方GitHub MCP服务器。
什么是GitHub MCP服务器?
GitHub MCP(模型上下文协议)服务器是GitHub的官方实现,它提供:
- AI GitHub集成的标准化协议
- 增强的安全性和身份验证
- 更广泛的工具支持(60多个GitHub操作)
- 与多个AI客户端(Claude、VS Code等)的兼容性
- 实时访问GitHub API
Docker安装
第一步:安装Docker
- Windows/Mac:
- Linux:
验证安装:
docker --version
docker ps步骤2:拉取GitHub MCP服务器映像
# Pull the official GitHub MCP server
docker pull ghcr.io/github/github-mcp-server
# Verify the image
docker images | grep github-mcp-server步骤3:测试GitHub MCP服务器
# Test with your GitHub token
docker run -i --rm \
-e GITHUB_PERSONAL_ACCESS_TOKEN=ghp_your-token-here \
ghcr.io/github/github-mcp-server预期产量:
GitHub MCP Server running on stdio🎮 用法
方法1:简单模式(Direct GitHub API-推荐)
此方法绕过Docker,直接连接到GitHub的API。它更简单,适用于所有平台。
交互模式
# Set your API keys
export OPENAI_API_KEY="sk-your-openai-key-here" # macOS/Linux
export GITHUB_TOKEN="ghp_your-github-token-here"
# Windows PowerShell:
$env:OPENAI_API_KEY="sk-your-openai-key-here"
$env:GITHUB_TOKEN="ghp_your-github-token-here"
# Run interactive mode
node simple-chatgpt-github.js --interactive单命令模式
# List repositories
node simple-chatgpt-github.js "show me my repositories"
# Create a branch
node simple-chatgpt-github.js "create a branch called feature/ai-integration in my main project"
# List issues
node simple-chatgpt-github.js "show me open issues in my repositories"方法2:高级模式(Docker MCP服务器)
此方法使用在Docker中运行的官方GitHub MCP服务器来增强功能。
第一步:启动Docker MCP服务器(终端1)
打开第一个终端/命令提示符并运行:
Windows(PowerShell):
# Make sure Docker Desktop is running first
docker --version
# Start the GitHub MCP Server
docker run -i --rm `
-e GITHUB_PERSONAL_ACCESS_TOKEN=ghp_your-github-token-here `
ghcr.io/github/github-mcp-servermacOS/Linux(Bash):
# Make sure Docker is running first
docker --version
# Start the GitHub MCP Server
docker run -i --rm \
-e GITHUB_PERSONAL_ACCESS_TOKEN=ghp_your-github-token-here \
ghcr.io/github/github-mcp-server1号航站楼的预期输出:
Unable to find image 'ghcr.io/github/github-mcp-server:latest' locally
latest: Pulling from github/github-mcp-server
[...download progress...]
GitHub MCP Server running on stdio重要:让这个终端继续运行!MCP服务器需要保持活动状态,以便您的Node.js脚本能够连接到它。
步骤2:运行Node.js脚本(终端2)
打开第二个终端/命令提示符,导航到您的项目目录:
Windows(PowerShell):
# Navigate to your project
cd C:\path\to\your\chatgpt-github-agent
# Set your API keys
$env:OPENAI_API_KEY="sk-your-openai-key-here"
$env:GITHUB_TOKEN="ghp_your-github-token-here"
# Run the script
node simple-chatgpt-github.js --interactivemacOS/Linux(Bash):
# Navigate to your project
cd /path/to/your/chatgpt-github-agent
# Set your API keys
export OPENAI_API_KEY="sk-your-openai-key-here"
export GITHUB_TOKEN="ghp_your-github-token-here"
# Run the script
node simple-chatgpt-github.js --interactive步骤3:测试集成
在2号航站楼,您应该看到:
🚀 Script starting...
Node.js version: v22.18.0
Arguments: [...]
✅ All imports successful
📍 Entering main() function
🧠 ChatGPT Planning Phase
...步骤4:使用MCP检查器(可选测试)
对于高级测试和调试,您可以使用MCP检查器:
3号航站楼(可选):
# Install MCP Inspector globally
npm install -g @modelcontextprotocol/inspector
# Start MCP Inspector
npx @modelcontextprotocol/inspector然后打开 http://localhost:6274 并配置:
- 传输类型:
stdio - 命令:
docker - 参数:
run -i --rm -e GITHUB_PERSONAL_ACCESS_TOKEN=ghp_your-token ghcr.io/github/github-mcp-server
示例工作流(Docker模式)
以下是您的完整工作流程:
# Terminal 1: Start MCP Server
$ docker run -i --rm -e GITHUB_PERSONAL_ACCESS_TOKEN=ghp_xyz123 ghcr.io/github/github-mcp-server
GitHub MCP Server running on stdio
# Keep this running...
# Terminal 2: Run your script
$ cd /your/project/directory
$ export OPENAI_API_KEY="sk-your-key"
$ export GITHUB_TOKEN="ghp_xyz123"
$ node simple-chatgpt-github.js --interactive
🎯 Enter your DevOps request: show me my repositories
🚀 ChatGPT + GitHub Integration
==================================================
🧠 ChatGPT Planning Phase
Request: "show me my repositories"
✅ ChatGPT Plan:
Reasoning: User wants to see their GitHub repositories
Tasks: 1
⏳ Executing tasks...
🔧 Executing GitHub Tool: list_repositories
🔍 Fetching ALL repositories with pagination...
📄 Fetching page 1...
✅ Retrieved 100 repositories from page 1
📄 Fetching page 2...
✅ Retrieved 23 repositories from page 2
🎯 Total repositories fetched: 123
✅ GitHub API Success
✨ SUMMARY:
==================================================
Successfully retrieved all 123 of your GitHub repositories!
Your most active languages are JavaScript (45 repos),
TypeScript (32 repos), and Python (28 repos).
Your latest repository "ai-experiments" was updated 3 days ago.
==================================================
🎯 Enter your DevOps request: quit可用的自然语言命令
| 命令 | 它的作用 |
|---|---|
"show me my repositories" | 列出所有GitHub存储库及其详细信息 |
"list my recent repos" | 显示最近更新的存储库 |
"create a branch called feature/new-ui in my-repo" | 创建新分支(指定仓库名称) |
"show open issues in my project" | 列出未解决的问题(需要repo规范) |
"list issues in [repo-name]" | 显示特定存储库的问题 |
"create an issue in [repo-name] about [topic]" | 创建新问题(使用MCP服务器) |
"show my pull requests" | 列出您的拉取请求(使用MCP服务器) |
"quit" 或 "exit" | 退出交互模式 |
GitHub MCP服务器配置选项
基本配置
docker run -i --rm \
-e GITHUB_PERSONAL_ACCESS_TOKEN=ghp_your-token \
ghcr.io/github/github-mcp-server使用工具集进行高级配置
# Enable specific toolsets only
docker run -i --rm \
-e GITHUB_PERSONAL_ACCESS_TOKEN=ghp_your-token \
-e GITHUB_TOOLSETS="repos,issues,pull_requests,code_security" \
ghcr.io/github/github-mcp-server
# Enable all toolsets
docker run -i --rm \
-e GITHUB_PERSONAL_ACCESS_TOKEN=ghp_your-token \
-e GITHUB_TOOLSETS="all" \
ghcr.io/github/github-mcp-serverGitHub企业服务器设置
docker run -i --rm \
-e GITHUB_PERSONAL_ACCESS_TOKEN=ghp_your-token \
-e GITHUB_HOST="https://github.your-company.com" \
ghcr.io/github/github-mcp-server可用工具集
| 工具集 | 描述 | 示例工具 |
|---|---|---|
repos | 存储库操作 | list_files, create_branch, get_commits |
issues | 问题管理 | create_issue, list_issues, add_comment |
users | 用户信息 | get_user, list_followers |
pull_requests | 公关运营 | create_pull_request, merge_pr, review_pr |
code_security | 安全功能 | list_security_alerts, get_vulnerabilities |
experiments | Beta功能 | 前沿GitHub集成 |
环境变量引用
| 变量 | 描述 | 示例 |
|---|---|---|
GITHUB_PERSONAL_ACCESS_TOKEN | 必需 GitHub PAT | ghp_1234567890abcdef |
GITHUB_HOST | GitHub服务器URL | https://github.com |
GITHUB_TOOLSETS | 已启用的工具集 | repos,issues,pull_requests |
OPENAI_API_KEY | 必需 OpenAI API密钥 | sk-1234567890abcdef |
🛠 支持的GitHub操作
直接API模式(内置)
- ✅ 列出存储库 -使用分页功能获取所有存储库
- ✅ 创建分支 -从现有分支创建新分支
- ✅ 列出问题 -查看存储库中的打开/关闭问题
- 🔄 易于扩展 -根据需要添加更多的GitHub API端点
MCP服务器模式(Docker)
- ✅ 库管理 -仓库上的完整CRUD操作
- ✅ 问题跟踪 -创建、更新、评论问题
- ✅ 拉取请求工作流 -创建、审核、合并PR
- ✅ 转移操作 -创建、删除、合并分支
- ✅ 文件操作 -读取、创建、更新存储库文件
- ✅ 安全功能 -访问安全警报和漏洞
- ✅ 用户管理 -访问用户和组织数据
- ✅ 高级搜索 -复杂的GitHub搜索和过滤
🔧 故障排除
常见问题
错误:“找不到模块”
# Make sure dependencies are installed
npm install axios readline错误:“未配置API密钥”
# Check your environment variables
echo $OPENAI_API_KEY # macOS/Linux
echo $env:OPENAI_API_KEY # Windows PowerShell错误:“GitHub API速率限制”
- 等待一个小时重置速率限制,或
- 使用GitHub个人访问令牌(更高的速率限制)
错误:“找不到存储库”
- 确保您的GitHub令牌可以访问存储库
- 检查存储库名称拼写
- 验证存储库可见性(公共与私有)
Docker特定问题
找不到Docker:
# Check Docker installation
docker --version
# If not installed, download Docker DesktopGitHub MCP服务器连接失败:
# Test Docker access
docker run hello-world
# Test GitHub MCP server manually
docker run -i --rm \
-e GITHUB_PERSONAL_ACCESS_TOKEN=ghp_your-token \
ghcr.io/github/github-mcp-server权限被拒绝(Windows):
# Run PowerShell as Administrator
# Ensure Docker Desktop is running图像拉取失败:
# Login to GitHub Container Registry (if needed)
echo $GITHUB_TOKEN | docker login ghcr.io -u YOUR_USERNAME --password-stdin
# Pull image manually
docker pull ghcr.io/github/github-mcp-server集装箱立即退出:
# Check if your GitHub token is valid
curl -H "Authorization: token ghp_your-token" https://api.github.com/user
# Verify token has required scopes (repo, read:packages)“GitHub MCP服务器在stdio上运行”然后什么都没有:
- 这很正常!服务器正在等待连接
- 保持终端1运行,并将终端2用于Node.js脚本
- 服务器仅在脚本连接到它时才响应
Windows特定问题
PowerShell执行策略错误:
Set-ExecutionPolicy RemoteSigned -Scope CurrentUserNode.js ES模块问题:
- 确保
package.json有"type": "module" - 使用
.js脚本文件的扩展名 - 使用
import声明而不是require()
MCP服务器stdio问题:
- 使用Docker模式而不是直接stdio连接
- 在启动容器之前,确保Docker Desktop正在运行
📊 样本输出
简单模式输出
🚀 ChatGPT + GitHub Integration
==================================================
🧠 ChatGPT Planning Phase
Request: "show me my repositories"
✅ ChatGPT Plan:
Reasoning: To list all the user's repositories using the GitHub API
Tasks: 1
⏳ Executing tasks...
🔧 Executing GitHub Tool: list_repositories
Arguments: {}
🔍 Fetching ALL repositories with pagination...
📄 Fetching page 1...
✅ Retrieved 100 repositories from page 1
📄 Fetching page 2...
✅ Retrieved 23 repositories from page 2
🎯 Total repositories fetched: 123
✅ GitHub API Success
📊 Generating Summary...
✨ SUMMARY:
==================================================
Successfully retrieved all 123 of your GitHub repositories!
Here's what I found:
• **Most Active Languages**: JavaScript (45 repos), Python (32 repos),
TypeScript (28 repos), HTML (18 repos)
• **Recent Activity**: Your most recently updated repository is
"ai-experiments" modified 3 days ago
• **Repository Types**: Mix of public and private repositories
• **Total Stars**: Your repositories have received 342 stars combined
Your development activity shows strong focus on web technologies
and AI/ML projects. Keep up the great work!
==================================================MCP模式输出
🐳 GitHub MCP Server running on stdio
Available Tools: 63 tools discovered
✅ Repository Tools: list_files, create_branch, get_commits
✅ Issue Tools: create_issue, list_issues, add_comment
✅ PR Tools: create_pull_request, merge_pr, review_pr
✅ Security Tools: list_security_alerts, get_vulnerabilities
🎯 Ready for advanced GitHub operations via MCP protocol!🚀 快速启动命令
初学者(简单模式)
# 1. Set up project
mkdir chatgpt-github-agent && cd chatgpt-github-agent
npm init -y
npm install axios readline
# 2. Set API keys
export OPENAI_API_KEY="sk-your-key"
export GITHUB_TOKEN="ghp_your-token"
# 3. Create and run script
# (Paste the script content into simple-chatgpt-github.js)
node simple-chatgpt-github.js "show me my repositories"高级用户(Docker模式)
# Terminal 1: Start MCP Server
docker run -i --rm -e GITHUB_PERSONAL_ACCESS_TOKEN=ghp_your-token ghcr.io/github/github-mcp-server
# Terminal 2: Run Node.js script
export OPENAI_API_KEY="sk-your-key" && export GITHUB_TOKEN="ghp_your-token"
node simple-chatgpt-github.js --interactive🔗 相关项目
💡 未来的增强功能
近期
- 🔄 添加更多GitHub操作(PR、部署、Webhook)
- 🌐 支持其他平台(GitLab、Bitbucket)
- 📊 增强的分析和报告功能
- 🔒 改进了安全性和令牌管理
长期
- 🤖 通过迭代循环转换为完全自主的代理
- 🧠 多步骤工作流自动化
- 📈 高级GitHub分析和见解
- 🔌 用于自定义集成的插件系统
- 🌍 多组织支持
- 📱 Web界面和移动支持
______________________________________________________________________
由以下材料制成❤️ 由ChatGPT+GitHub API+Docker MCP提供支持
