GitHub PR自动化工具

一个集成GitHub PR、CI/CD监控和Slack通知的MCP服务器,用于实现团队感知和工作流智能化的开发自动化系统。

  • by denven
  • Python
  • 本地部署
  • 更新于
在 GitHub 查看 denven/github-pr-mcp-server

使用说明

Claude代码的GitHub PR MCP服务器

一个MCP服务器,通过与GitHub PR、CI/CD监控和Slack通知集成,使Claude Code团队具有感知能力和工作流智能。

概述

该项目演示了如何使用MCP(模型上下文协议)构建一个全面的开发自动化系统,该系统将手动PR工作流转换为智能、自动化的流程。

模块

源代码文件位于 .\starter 文件夹现在。

模块1:智能公关管理

使用三个MCP工具建立基础:

  1. 分析file_更改 -检索git diff信息和更改的文件
  2. get_pr_templates -列出可用的PR模板
  3. 建议模板 -允许Claude建议最合适的模板

模块2:GitHub操作工作流集成

使用CI/CD监控扩展服务器:

  • Webhook服务器接收GitHub操作事件
  • 用于监控CI/CD状态的工具
  • MCP提示一致的工作流程模式

模块3:Slack通知集成

通过团队通知完成自动化:

  • 用于格式化消息的Slack webhook工具
  • 智能通知提示
  • 所有MCP原语的完全集成

设置

1.安装uv

按照官方安装说明进行操作:https://docs.astral.sh/uv/getting-started/installation/

2.安装依赖项

# Install all dependencies
uv sync

# Or install with dev dependencies for testing
uv sync --all-extras

3.开始实施!

打开 server.py 并遵循TODO注释来实现每个工具。

备注:起始代码包括返回“未实现”错误的存根实现。这使您能够:

  • 立即运行服务器
  • 与Claude一起测试您的设置
  • 用您的实际实现替换每个存根
  • 查看有关每个工具应该做什么的有用提示

设计理念

不要使用基于文件扩展名或模式的严格规则,你的工具应该为Claude提供原始的git数据,让Claude的智能决定:

  • 正在进行何种类型的更改
  • 哪个模板最合适
  • 如何为特定更改自定义模板

入门指南

模块1:基本公关工具

  1. 打开 server.py 并按照TODO注释实施这三个工具

  2. 测试: uv run pytest test_server.py -v

  3. 在Claude代码中配置:

    claude mcp add pr-agent -- uv --directory /absolute/path/to/starter run server.py
    

模块2:GitHub操作集成

  1. 设置webhook服务器(webhook_server.py)在端口8080上
  2. 配置GitHub webhook以发送操作事件
  3. 使用Cloudflare Tunnel进行本地开发
  4. 实施CI/CD监控工具

模块3:松弛通知

  1. 配置 Slack 网钩 URL
  2. 实施 send_slack_notification 工具
  3. 为成功/失败事件创建通知提示
  4. 测试端到端工作流自动化

现实世界的好处

转换手动工作流程:

  • 之前:手动创建PR,等待CI/CD,检查结果,通知团队
  • 之后:Claude智能地建议模板、监控管道并自动提醒团队

关键交互模式

🔄 刀具组合模式

Claude Code智能地组合了多个MCP工具来完成复杂的任务:

  • 分析file_更改→ PR工作流的suggest_template
  • 获取事件→ get_workflow_status→ CI监控的send_slack_notification

🎯 快速引导情报

MCP Prompts提供结构化的工作流程,指导Claude完成多步骤流程:

  • 用于一致Slack格式的模板
  • 分步故障排除指南
  • 自动化决策模式

⚡ 事件驱动架构

  • GitHub webhooks提供实时CI/CD事件数据
  • Webhook服务器在8080端口上独立运行
  • 本地存储的事件可供MCP工具快速访问

🔧 模块化设计

  • 模块1:Git分析和PR模板管理
  • 模块2:CI/CD监控和工作流程分析
  • 模块3:Slack集成和团队通知
  • 共享:用于一致性的配置和实用程序

📊 数据流摘要

  1. 输入:开发人员通过Claude Code请求
  2. 处理:MCP工具从Git、GitHub事件、模板收集数据
  3. 情报:克劳德使用提供的提示分析数据
  4. 输出:格式化响应、Slack通知、可操作的见解
  5. 反馈:团队收到通知,工作流程得到改善

该架构展示了一种复杂的集成,可以将手动开发工作流程转换为智能、自动化的流程,同时保持人性化
监督和控制。

✅ 它是如何工作的(序列图)

sequenceDiagram
      participant User as 👤 Developer
      participant Claude as 🤖 Claude Code
      participant MCP as 📡 MCP Server
      participant PR as 🔧 PR Tools
      participant Git as 📚 Git Repository
      participant Templates as 📄 PR Templates
      participant GHA as ⚙️ GitHub Actions
      participant Webhook as 🎣 Webhook Server
      participant GitHub as 🐙 GitHub
      participant Events as 📊 Events Storage
      participant Slack as 💬 Slack Integration
      participant SlackAPI as 📢 Slack API
      participant Team as 👥 Team Channel

      Note over User, Team: PR Agent Workflow - Complete Interaction Flow

      %% Scenario 1: PR Analysis and Template Suggestion
      rect rgb(240, 248, 255)
          Note over User, Claude: Scenario 1: PR Creation Workflow
          User->>Claude: "Analyze my changes and suggest a PR template"
          Claude->>MCP: Call analyze_file_changes tool
          MCP->>PR: analyze_file_changes(base_branch="main")
          PR->>Git: git diff --name-status main...HEAD
          Git-->>PR: Changed files list
          PR->>Git: git diff main...HEAD
          Git-->>PR: Full diff content
          PR-->>MCP: JSON response with changes
          MCP-->>Claude: File changes data

          Claude->>MCP: Call suggest_template tool
          MCP->>PR: suggest_template(changes_summary, change_type)
          PR->>Templates: Read template files
          Templates-->>PR: Template content
          PR-->>MCP: Template suggestion with content
          MCP-->>Claude: Template recommendation

          Claude-->>User: "Based on your changes, I suggest using the 'feature' template..."
      end

      %% Scenario 2: CI/CD Monitoring
      rect rgb(255, 248, 240)
          Note over GitHub, Team: Scenario 2: CI/CD Monitoring Workflow
          GitHub->>Webhook: POST /webhook/github (workflow_run event)
          Webhook->>Events: Store event in github_events.json

          User->>Claude: "Check the CI/CD status"
          Claude->>MCP: Call get_recent_actions_events tool
          MCP->>GHA: get_recent_actions_events(limit=10)
          GHA->>Events: Read github_events.json
          Events-->>GHA: Recent workflow events
          GHA-->>MCP: Events data
          MCP-->>Claude: CI/CD events

          Claude->>MCP: Call get_workflow_status tool
          MCP->>GHA: get_workflow_status()
          GHA->>Events: Filter workflow_run events
          Events-->>GHA: Workflow status data
          GHA-->>MCP: Current workflow states
          MCP-->>Claude: Workflow status summary

          Claude-->>User: "Your CI pipeline is failing on test-auth-service..."
      end

      %% Scenario 3: Automated Slack Notification
      rect rgb(248, 255, 248)
          Note over Claude, Team: Scenario 3: Slack Notification Workflow
          Claude->>MCP: Use notify_ci_status_to_slack prompt
          MCP->>GHA: Check recent events for failures
          GHA-->>MCP: Failed workflow detected

          Claude->>MCP: Use format_ci_failure_alert prompt
          MCP-->>Claude: Slack markdown template

          Claude->>MCP: Call send_slack_notification tool
          MCP->>Slack: send_slack_notification(formatted_message)
          Slack->>SlackAPI: POST to webhook URL
          SlackAPI->>Team: Deliver formatted alert

          Note over Team: ❌ CI Failed - Repository Name<br/>Workflow: test-pipeline<br/>Branch: feature-branch<br/>🔗 View Action Logs

          Slack-->>MCP: Success response
          MCP-->>Claude: Notification sent confirmation
          Claude-->>User: "Slack notification sent to team channel"
      end

      %% Scenario 4: Daily Summary Automation
      rect rgb(255, 240, 255)
          Note over User, Team: Scenario 4: Daily CI Summary
          User->>Claude: "Generate daily CI summary for the team"
          Claude->>MCP: Use daily_ci_summary_to_slack prompt
          MCP->>GHA: get_recent_actions_events(limit=50)
          GHA-->>MCP: Today's CI activity

          Claude->>MCP: Analyze patterns and create summary
          MCP->>Slack: send_slack_notification(daily_summary)
          Slack->>SlackAPI: POST formatted daily report
          SlackAPI->>Team: Deliver summary

          Note over Team: 📊 Daily CI/CD Report<br/>Total runs: 25<br/>Success rate: 80%<br/>Failed workflows: 3<br/>Action items: Fix test-auth

          Claude-->>User: "Daily summary sent to team"
      end

      %% Configuration and Error Handling
      rect rgb(248, 248, 248)
          Note over MCP, Slack: Configuration & Error Handling
          Note over PR: Uses config.py for:<br/>- Template paths<br/>- Default values<br/>- Git settings
          Note over GHA: Uses config.py for:<br/>- Events file path<br/>- Limits & timeouts
          Note over Slack: Uses config.py for:<br/>- Webhook URL<br/>- Message formatting<br/>- Timeout settings
          Note over MCP: Uses utils.py for:<br/>- JSON formatting<br/>- File operations<br/>- Input validation<br/>- Error handling
      end