PR分路器MCP🔀
](https://badge.fury.io/py/pr-splitter-mcp)  
一个强大的模型上下文协议(MCP)服务器,可以智能地将大型Pull Request拆分为多个较小的PR,以更好地进行代码审查和KPI跟踪。
🤝 旨在与无缝协作 编码流MCP!
📦 安装
通过PyPI(推荐)
pip install pr-splitter-mcp通过uvx(快速入门)
uvx pr-splitter-mcp源自
git clone https://github.com/Polly2014/PR-Splitter-MCP.git
cd PR-Splitter-MCP
poetry install⚙️ MCP配置
添加到您的VS代码MCP设置(settings.json 或 mcp.json):
{
"mcp": {
"servers": {
"pr-splitter": {
"command": "uvx",
"args": ["pr-splitter-mcp"]
}
}
}
}或者,如果通过pip安装:
{
"mcp": {
"servers": {
"pr-splitter": {
"command": "pr-splitter-mcp"
}
}
}
}🎯 问题陈述
许多公司将PR计数要求作为KPI,大型功能通常需要拆分为多个较小的PR,用于:
- 更好的代码审查质量
- 更容易跟踪和管理
- 达到PR计数目标
- 增量功能交付
PR分路器MCP 通过分析代码结构和智能地拆分更改来自动化此过程。
🔄 带有编码流MCP的工作流
推荐的工作流程将PR Splitter MCP与编码流相结合,以获得最佳结果:
┌─────────────────────────────────────────────────────────────────────────┐
│ PR Split Workflow │
├─────────────────────────────────────────────────────────────────────────┤
│ 1. coding-flow.get_pr_content(prId) → Get PR files & changes │
│ ↓ │
│ 2. pr-splitter.generate_split_plan_from_pr(files, count, strategy) │
│ ↓ │
│ 3. Git operations (create branches, copy files, push) │
│ ↓ │
│ 4. coding-flow.create_draft_pr() × N → Create sub-PRs │
└─────────────────────────────────────────────────────────────────────────┘对话示例
User: Split PR #6243094 into 5 smaller PRs
AI Agent:
1. Uses coding-flow.get_pr_content() to get 12 changed files
2. Uses pr-splitter.generate_split_plan_from_pr() to create split plan:
- PR 1/5: configs (4 files)
- PR 2/5: utils (3 files)
- PR 3/5: components (2 files)
- PR 4/5: models (2 files)
- PR 5/5: inference (1 file)
3. Creates branches and commits for each split
4. Uses coding-flow.create_draft_pr() to create 5 draft PRs✨ 特性
- 🔍 代码分析:分析代码结构、检测模块并了解依赖关系
- 🧠 智能拆分:基于AI的拆分策略:
- 模块边界 - 文件类型 - 逻辑组 - 依赖顺序
- 🤝 编码流集成:
generate_split_plan_from_pr直接处理PR数据 - 📁 文件夹支持:
split_folder_to_plan和split_and_push_folder本地代码 - 🌿 分行管理:自动创建要素分支和子分支
- 📤 自动推送:将所有分支推送到远程存储库
- 📝 公关创作:在Azure DevOps或GitHub上创建PR草案
- 🔐 零配置身份验证:使用系统凭据(
az login,gh auth login)
🔐 认证
零配置身份验证 -与编码流等专业工具的模式相同!
此工具使用带有系统凭据的本机SDK:
Azure DevOps
用途 ChainedTokenCredential 自动回退:
- AzureCliCredential -使用来自的令牌
az login - 交互式浏览器证书 -如有需要,返回浏览器登录
# One-time setup
az loginGitHub
令牌解析顺序:
GITHUB_PAT_TOKEN环境变量GITHUB_TOKEN环境变量gh auth token命令输出
# One-time setup
gh auth login必需的Python包
# For Azure DevOps
pip install azure-devops azure-identity msrest
# For GitHub
pip install PyGithub🚀 快速开始
安装
# Clone the repository
git clone https://github.com/Polly2014/PR-Splitter-MCP.git
cd PR-Splitter-MCP
# Install with Poetry
poetry install
# Or with pip
pip install -e .配置
这 config.env file是可选的,主要用于设置默认值:
# Git configuration
DEFAULT_REMOTE=origin
DEFAULT_BASE_BRANCH=main
# Split configuration
DEFAULT_PR_COUNT=8
SPLIT_STRATEGY=by_module # by_module, by_file, by_type, balanced使用VS代码+复制码
添加到您的VS代码 mcp.json:
{
"servers": {
"pr-splitter": {
"command": "python",
"args": ["/path/to/PR-Splitter-MCP/server.py"],
"cwd": "/path/to/PR-Splitter-MCP",
"type": "stdio"
}
}
}🛠️ MCP工具
核心工具(用于编码流集成)
generate_split_plan_from_pr ⭐ 新
直接从PR文件数据(来自编码流.get_PR_content)生成拆分计划。
这是与编码流MCP一起使用的推荐工具!
Input:
- pr_files: List of files from coding-flow.get_pr_content()
- target_pr_count: Target number of PRs (default: 5)
- strategy: Split strategy (by_module, by_file, by_type, balanced)
- base_branch: Base branch for split PRs
- branch_prefix: Prefix for branch names
- pr_title_prefix: Prefix for PR titles
Output:
- plan: { prs: [...], base_branch, branch_prefix }
- summary: { total_files, total_lines, files_per_pr, lines_per_pr }
- merge_order: Recommended merge sequence
- workflow_next_steps: Git commands to executegenerate_pr_descriptions ⭐ 新
为拆分计划生成详细的PR标题和描述。
Input:
- plan: Split plan from generate_split_plan_from_pr
- project_name: Name for PR titles
- include_dependencies: Include dependency info
Output:
- prs: Enhanced PRs with professional titles and descriptions
- ready_for_creation: Boolean indicating readiness分析工具
check_auth_status
检查PR创建的身份验证和依赖关系状态。
Output:
- azure_devops: { authenticated, method, message }
- github: { authenticated, method, message }
- dependencies: { installed, missing, install_command }
- ready: { ado: bool, github: bool }analyze_code_structure
分析目录或PR的代码结构。
Input:
- source_path: Path to source directory
- include_patterns: File patterns to include (e.g., "*.py")
Output:
- modules: List of detected modules
- files: File inventory with metadata
- dependencies: Dependency graph between filesget_split_strategies
通过描述和工作流程指导获取可用的拆分策略。
Output:
- strategies: { by_module, by_file, by_type, balanced }
- workflow: Recommended steps with coding-flow基于文件夹的拆分工具⭐ 新
split_folder_to_plan
从本地文件夹生成拆分计划(类似于Generate_split_plan_from_pr)。
Input:
- folder_path: Path to folder containing code to split
- target_pr_count: Target number of PRs (default: 5)
- strategy: Split strategy (by_module, by_file, by_type, balanced)
- base_branch: Base branch for split PRs
- branch_prefix: Prefix for branch names
- include_patterns: File patterns to include (e.g., ["*.py", "*.js"])
- exclude_patterns: File patterns to exclude (e.g., ["__pycache__/*"])
Output:
- plan: Split plan with PR definitions
- summary: { total_files, total_lines, files_per_pr, lines_per_pr }
- workflow_next_steps: Instructions for next stepssplit_and_push_folder ⭐ 端到端
集分析文件夹、创建分支、复制文件、提交和推送于一体。
Input:
- source_folder: Path to folder containing code
- target_repo_path: Path to target git repository
- target_pr_count: Target number of PRs (default: 5)
- strategy: Split strategy
- base_branch: Base branch in target repo
- branch_prefix: Prefix for branch names
- relative_path_in_repo: Where to put files in repo (e.g., "src/feature/")
- include_patterns: File patterns to include
- exclude_patterns: File patterns to exclude
- dry_run: Preview without changes (default: True)
- push: Push branches to remote (default: True)
Output:
- plan: The split plan used
- branches: Created branches with status
- next_steps: How to create the PRs计划和执行工具
generate_split_plan
为本地代码目录生成智能拆分计划。
Input:
- source_path: Path to source directory
- target_pr_count: Target number of PRs (default: 8)
- strategy: Split strategy (by_module, by_file, by_type)
- base_branch: Base branch name
Output:
- plan: List of PR definitions with files and order
- estimated_sizes: Size estimates per PR
- dependency_order: Recommended merge orderexecute_split
通过创建分支和提交来执行拆分计划。
Input:
- plan: Split plan from generate_split_plan
- source_path: Path to source files
- target_repo_path: Target repository path
- dry_run: Preview without making changes (default: True)
Output:
- branches: List of created branches with commit hashes
- summary: Success/failure statistics
- status: Execution status公关创建工具
create_ado_pr
在Azure DevOps中创建拉取请求。
Input:
- org_url: Azure DevOps org URL (e.g., https://dev.azure.com/your-org)
- project: Project name (e.g., MyProject)
- repo: Repository name (e.g., my-repo)
- source_branch: Source branch name
- target_branch: Target branch name
- title: PR title
- description: PR description (optional)
- draft: Create as draft (default: True)
- work_item_id: ADO work item to link (optional)
Output:
- pr_id: Created PR ID
- pr_url: PR URL
- status: Creation statuscreate_github_pr
在GitHub中创建Pull请求。
Input:
- repo: Repository in format "owner/repo"
- source_branch: Source branch name
- target_branch: Target branch name (default: main)
- title: PR title
- body: PR description (optional)
- draft: Create as draft (default: True)
Output:
- pr_id: Created PR ID
- pr_url: PR URL
- status: Creation statuscreate_prs_from_plan
从拆分计划批量创建PR。
Input:
- plan: Split plan from generate_split_plan
- platform: "ado" or "github"
- repo: Repository name
- org_url: ADO org URL (required for ADO)
- project: ADO project name (required for ADO)
- draft: Create as draft PRs (default: True)
Output:
- prs_created: Number of PRs created
- pr_urls: List of PR URLs
- results: Detailed results per PR📋 示例工作流
工作流程1:拆分现有PR(带编码流)⭐ 推荐
User: Split PR #6243094 into 5 PRs targeting user/feature-test
AI Agent + MCP Servers:
1. coding-flow.get_pr_content(prIdOrUrl="PR#6243094")
→ Returns 12 changed files with paths and change types
2. pr-splitter.generate_split_plan_from_pr(
pr_files=,
target_pr_count=5,
strategy="by_module",
base_branch="user/feature-test"
)
→ Returns split plan with 5 PRs
3. Git operations:
- git checkout -b user/feature-test (base branch)
- For each PR in plan:
- git checkout -b
user/feature-test
- git checkout --
- git commit && git push
4. For each PR:
coding-flow.create_draft_pr(
branchName=
,
targetBranch="user/feature-test",
title=,
description=
)工作流2:拆分本地文件夹(端到端)⭐ 新
User: Split /path/to/my-feature into 5 PRs and push to my-repo
AI Agent + MCP Server:
1. pr-splitter.split_and_push_folder(
source_folder="/path/to/my-feature",
target_repo_path="/path/to/my-repo",
target_pr_count=5,
strategy="by_module",
base_branch="user/myname/feature",
branch_prefix="user/myname/feature",
relative_path_in_repo="src/feature/",
dry_run=False,
push=True
)
→ Creates 5 branches, copies files, commits, and pushes
2. pr-splitter.create_prs_from_plan(
plan=
,
platform="ado",
org_url="https://dev.azure.com/myorg",
project="MyProject",
repo="my-repo"
)
→ Creates 5 draft PRs
Done! 5 PRs created from local folder.工作流程3:拆分本地代码(逐步)
User: Split my code into 8 PRs
Source: /path/to/my-feature
Target: user/myname/feature-x
AI + MCP Server:
1. check_auth_status → Verify SDK authentication
2. split_folder_to_plan → Generate split plan with preview
3. [User confirms plan]
4. split_and_push_folder(dry_run=False) → Create branches and push
5. create_prs_from_plan → Create all PRs at once🔄 身份验证流程
┌─────────────────────────────────────────────────────────────┐
│ PR-Splitter-MCP │
├─────────────────────────────────────────────────────────────┤
│ │
│ Azure DevOps GitHub │
│ ┌─────────────────────┐ ┌─────────────────────┐ │
│ │ ChainedTokenCred │ │ Token Resolution │ │
│ │ ├─ AzureCliCred ◄──┼── az │ ├─ GITHUB_PAT_TOKEN│ │
│ │ │ (from az login)│ login │ ├─ GITHUB_TOKEN │ │
│ │ └─ BrowserCred │ │ └─ gh auth token ◄─┼─gh │
│ │ (fallback) │ │ │auth│
│ └─────────────────────┘ └─────────────────────┘ │
│ │ │ │
│ ▼ ▼ │
│ ┌─────────────────────┐ ┌─────────────────────┐ │
│ │ azure-devops SDK │ │ PyGithub SDK │ │
│ │ (native API calls) │ │ (native API calls) │ │
│ └─────────────────────┘ └─────────────────────┘ │
│ │
└─────────────────────────────────────────────────────────────┘🏗️ 项目结构
PR-Splitter-MCP/
├── server.py # Main MCP server
├── src/
│ ├── __init__.py
│ ├── analyzer.py # Code structure analyzer
│ ├── splitter.py # Split plan generator
│ ├── git_manager.py # Git operations
│ └── pr_creator.py # PR creation (ADO/GitHub)
├── config.env # Configuration (optional)
├── pyproject.toml # Dependencies
└── README.md🔧 CLI命令参考
Azure DevOps命令行界面
# Login
az login
# Install DevOps extension
az extension add --name azure-devops
# Create PR manually
az repos pr create \
--org https://dev.azure.com/your-org \
--project MyProject \
--repository my-repo \
--source-branch user/yourname/feature \
--target-branch main \
--title "My PR" \
--draftGitHub命令行界面
# Login
gh auth login
# Create PR manually
gh pr create \
--repo owner/repo \
--head feature-branch \
--base main \
--title "My PR" \
--draft🤝 贡献
欢迎投稿!请随时提交拉取请求。
📄 许可证
MIT许可证-请参阅 许可证 了解详情。
🙏 致谢
- 受到大型团队需要更好的公关管理的启发
- 建于 FastMCP
- 感谢MCP社区
