ShadowGit MCP Server 是一个模型上下文协议(MCP)服务器,它为 AI 助手提供对 ShadowGit 仓库的安全 Git 访问权限,支持通过会话 API 创建有条理的提交。借助该服务器,AI 可以访问项目的 Git 历史记录,从而实现强大的调试、代码分析和清晰的提交管理。
ShadowGit MCP Server 为 AI 助手提供安全的 Git 访问,帮助实现有效的代码管理和调试。以下是使用该服务器的基本步骤:
npm install -g shadowgit-mcp-server
# 添加到 Claude Code
claude mcp add shadowgit -- shadowgit-mcp-server
# 重启 Claude Code 以加载服务器
将以下配置添加到 Claude Desktop 的 MCP 配置文件中:
~/.config/Claude/claude_desktop_config.json%APPDATA%\\Claude\\claude_desktop_config.json{
"mcpServers": {
"shadowgit": {
"command": "shadowgit-mcp-server"
}
}
}
await shadowgit.list_repos()
// 查看最近的提交
await shadowgit.git_command({
repo: "my-project",
command: "log --oneline -10"
})
const result = await shadowgit.start_session({
repo: "my-app",
description: "Fixing authentication bug"
})
// 返回: 会话 ID (例如, "mcp-client-1234567890")
// 修复 bug 后
const result = await shadowgit.checkpoint({
repo: "my-app",
title: "Fix null pointer exception in auth",
message: "Added null check before accessing user object",
author: "Claude"
})
// 返回格式化的提交详情,包括提交哈希
await shadowgit.end_session({
sessionId: "mcp-client-1234567890",
commitHash: "abc1234" // 可选: 来自检查点结果
})
// 1. 首先,检查可用的仓库
const repos = await shadowgit.list_repos()
// 2. 在进行更改之前开始会话
const sessionId = await shadowgit.start_session({
repo: "my-app",
description: "Refactoring authentication module"
})
// 3. 检查最近的历史记录
await shadowgit.git_command({
repo: "my-app",
command: "log --oneline -5"
})
// 4. 对代码进行更改...
// ... (编辑文件、修复 bug 等) ...
// 5. 重要: 完成任务后创建检查点
const commitHash = await shadowgit.checkpoint({
repo: "my-app",
title: "Refactor authentication module",
message: "Simplified login flow and added better error handling",
author: "Claude"
})
// 6. 完成后结束会话
await shadowgit.end_session({
sessionId: sessionId,
commitHash: commitHash // 可选但推荐
})
MCP 服务器是无状态的,使用标准输入输出(stdio)进行通信:
可以使用以下可选环境变量来配置服务器的行为:
SHADOWGIT_TIMEOUT - 命令执行超时时间(毫秒),默认值为 10000。SHADOWGIT_SESSION_API - 会话 API 的 URL,默认值为 http://localhost:45289/api。SHADOWGIT_LOG_LEVEL - 日志级别:debug、info、warn、error,默认值为 info。SHADOWGIT_HINTS - 设置为 0 可禁用 Git 命令输出中的工作流提示,默认启用。示例:
export SHADOWGIT_TIMEOUT=30000 # 30 秒超时
export SHADOWGIT_LOG_LEVEL=debug # 启用调试日志
export SHADOWGIT_HINTS=0 # 禁用工作流提示以获得更简洁的输出
会话 API(需要 ShadowGit >= 0.3.0)允许 AI 助手暂时暂停 ShadowGit 的自动提交功能,并创建清晰、有条理的提交。AI 助手在进行更改时必须遵循以下四步工作流:
start_session({repo, description}) - 在进行更改之前开始工作会话(暂停自动提交)。checkpoint({repo, title, message?, author?}) - 完成工作后创建检查点提交。end_session({sessionId, commitHash?}) - 完成后结束会话(恢复自动提交)。list_repos():列出所有 ShadowGit 跟踪的仓库。git_command({repo, command}):在特定仓库上执行只读 Git 命令。commit、push、merge 等写操作命令。branch、tag、reflog 等可能导致删除的命令。execFileSync 并传递数组参数进行安全执行。--git-dir、--work-tree、--exec、-c、--config、-C 等危险标志。使用 ShadowGit MCP Server 时,AI 助手应遵循以下最佳实践:
start_session() → 进行更改 → checkpoint() → end_session() 的顺序操作。checkpoint()。author 参数标识创建检查点的 AI。message 参数解释更改的内容和原因。end_session() 以恢复自动提交。// 查找最近一小时内出现问题的提交
await shadowgit.git_command({
repo: "my-app",
command: "log --since='1 hour ago' --oneline"
})
// 查看函数的演变过程
await shadowgit.git_command({
repo: "my-app",
command: "log -L :functionName:src/file.ts"
})
// 比较不同项目的活动情况
const repos = await shadowgit.list_repos()
for (const repo of repos) {
await shadowgit.git_command({
repo: repo.name,
command: "log --since='1 day ago' --oneline"
})
}
~/.shadowgit/repos.json 文件是否存在。list_repos() 查看确切的仓库名称。.shadowgit.git 目录。git --version。list_repos() 中的绝对路径或仓库名称。SHADOWGIT_HINTS=0 环境变量以禁用工作流提示。如果看到 "Session API is offline. Proceeding without session tracking" 提示:
对于想要修改或扩展 MCP 服务器的贡献者:
# 克隆仓库(私有 GitHub 仓库)
git clone https://github.com/shadowgit/shadowgit-mcp-server.git
cd shadowgit-mcp-server
npm install
# 构建
npm run build
# 测试
npm test
# 本地运行开发版本
npm run dev
# 本地测试构建版本
node dist/shadowgit-mcp-server.js
# 更新版本
npm version patch # 或 minor/major
# 构建并测试
npm run build
npm test
# 发布到 npm(公共注册表)
npm publish
本项目采用 MIT 许可证,详情请参阅 LICENSE 文件。
将您的开发历史转变为强大的 AI 调试助手!🚀