使用说明
Git MCP服务器
一个模型上下文协议(MCP)服务器,提供Git操作作为AI助手的工具。该服务器能够将Git功能无缝集成到AI工作流中。
为什么选择Git MCP服务器?
- AI原生Git集成 -使AI助手能够使用自然语言管理Git存储库
- 完整Git工具包 -状态、提交、分支、差异等——所有这些都可以由AI访问
- 零配置 -适用于任何Git存储库
- 通用兼容性 -与Claude Desktop、OpenAI、Anthropic API和任何兼容MCP的客户端集成
- 异步性能 -基于Python的asyncio构建,实现最佳性能
- 战斗测试 -93%的代码覆盖率和全面的测试套件
特性
使用以下工具将您的AI助手转变为Git高级用户:
| 工具 | 描述 | 用例 |
|---|---|---|
| git_status | 获取存储库状态 | “我更改了哪些文件?” |
| git_log | 查看提交历史记录 | “显示最近5次提交” |
| 使用 git_diff | 显示更改 | “我在auth.py中修改了什么?” |
| git_提交 | 创建提交 | “用描述性消息提交我的bug修复” |
| git_branch | 列出分支 | “存在哪些分支?” |
| git_checkout | 切换分支 | “切换到功能分支” |
快速开始
安装
# Clone the repository
git clone https://github.com/asklokesh/git-mcp-server.git
cd git-mcp-server
# Install dependencies
pip install -r requirements.txt
# Or install as a package
pip install -e .
在行动中看到它
# Ask your AI assistant:
"What's the status of my project?"
"Show me what changed in the authentication module"
"Create a commit for the bug fix I just made"
"Switch to the development branch"
用法
选项1:使用克劳德桌面(推荐)
-
编辑Claude桌面配置:
# macOS nano ~/Library/Application\ Support/Claude/claude_desktop_config.json # Windows notepad %APPDATA%\Claude\claude_desktop_config.json -
添加配置:
{ "mcpServers": { "git-mcp": { "command": "python", "args": [ "/path/to/git-mcp-server/git_mcp.py" ] } } } -
重新启动克劳德桌面
选项2:作为独立的MCP服务器
python git_mcp.py
选项3:使用GenAI API(OpenAI、Anthropic等)
请参阅 示例目录 对于集成示例:
- example_client.py -直接使用MCP客户端
- api_集成.py -REST API包装
- genai_integration示例.py -OpenAI/人工智能集成
快速示例:
from examples.genai_integration_example import GitMCPToolkit
toolkit = GitMCPToolkit()
result = toolkit.execute_tool("git_status", {"path": "/path/to/repo"})
print(result)
真实世界的例子
示例1:AI驱动的代码审查
# Ask your AI assistant:
"Check the git status and tell me what files need to be committed"
"Show me the diff for the changes in the API module"
"Create a descriptive commit message based on the changes"
示例2:分行管理
# Ask your AI assistant:
"List all branches and tell me which one is active"
"Create a new feature branch for the authentication update"
"Switch to the main branch and show me the latest commits"
示例3:项目健康检查
# Ask your AI assistant:
"Analyze the last 10 commits and summarize the recent changes"
"Check if there are any uncommitted changes"
"Show me who contributed to this project recently"
工具参考
git_status
获取git存储库的状态。
参数:
path(string,必填):git存储库的路径
例子:
{
"tool": "git_status",
"arguments": {
"path": "/Users/john/my-project"
}
}
git_log
查看存储库的提交历史记录。
参数:
path(string,必填):git存储库的路径limit(整数,可选):要显示的提交数(默认值:10)
例子:
{
"tool": "git_log",
"arguments": {
"path": "/Users/john/my-project",
"limit": 5
}
}
使用 git_diff
显示存储库中的更改。
参数:
path(string,必填):git存储库的路径staged(布尔值,可选):显示阶段性更改(默认值:false)
例子:
{
"tool": "git_diff",
"arguments": {
"path": "/Users/john/my-project",
"staged": true
}
}
git_提交
创建一个git commit。
参数:
path(string,必填):git存储库的路径message(字符串,必填):提交消息files(array[string],可选):要提交的特定文件
例子:
{
"tool": "git_commit",
"arguments": {
"path": "/Users/john/my-project",
"message": "Fix bug in authentication",
"files": ["auth.py", "tests/test_auth.py"]
}
}
git_branch
列出存储库中的分支。
参数:
path(string,必填):git存储库的路径
例子:
{
"tool": "git_branch",
"arguments": {
"path": "/Users/john/my-project"
}
}
git_checkout
切换到其他分支。
参数:
path(string,必填):git存储库的路径branch(字符串,必填):要结账的分行名称create(boolean,可选):创建新分支(默认值:false)
例子:
{
"tool": "git_checkout",
"arguments": {
"path": "/Users/john/my-project",
"branch": "feature/new-feature",
"create": true
}
}
发展
运行测试
# Install test dependencies
pip install pytest pytest-asyncio pytest-cov
# Run tests
python -m pytest tests/ -v
# Run with coverage
python -m pytest tests/ -v --cov=git_mcp --cov-report=html
项目结构
git-mcp-server/
├── git_mcp.py # Main MCP server
├── requirements.txt # Python dependencies
├── pyproject.toml # Package configuration
├── tests/ # Unit tests
│ ├── __init__.py
│ └── test_git_mcp.py
├── examples/ # Integration examples
│ ├── example_client.py
│ ├── api_integration.py
│ ├── genai_integration_example.py
│ └── ...
└── .github/
└── workflows/
└── test.yml # CI/CD pipeline
贡献
我们热爱贡献!以下是您可以提供帮助的方式:
- 分叉存储库
- 创建要素分支(
git checkout -b feature/amazing-feature) - 进行更改
- 编写或更新测试
- 提交您的更改(
git commit -m 'Add amazing feature') - 推到分支(
git push origin feature/amazing-feature) - 打开拉取请求
贡献方式
- 报告错误 -帮助我们识别问题
- 建议功能 -分享你的想法
- 改进文档 -帮助他人理解项目
- 添加测试 -提高代码覆盖率
- 翻译 -使项目在全球范围内可访问
路线图
- []添加对更多Git操作(合并、重基、隐藏)的支持
- []实现Git LFS支持
- []为私有存储库添加身份验证
- []创建基于web的仪表板
- []支持Git钩子集成
- []多存储库管理
- []Git工作流自动化模板
许可证
MIT许可证-请参阅 许可证 详细信息文件
支持与社区
- 问题: GitHub问题跟踪器
- 讨论: GitHub讨论
- 标记此回购 以示支持!
Made with care by developers, for developers
Star us on GitHub
