MCP Git Ingest
一个模型上下文协议(MCP)服务器,用于帮助读取GitHub仓库结构和重要文件。
灵感来源于 gitingest。
配置
{
"mcpServers": {
"mcp-git-ingest": {
"command": "uvx",
"args": ["--from", "git+https://github.com/adhikasp/mcp-git-ingest", "mcp-git-ingest"],
}
}
}
使用
使用 mcp-client-cli:
$ llm read https://github.com/adhikasp/mcp-git-ingest and determine how the code technically works
我会帮助你理解 mcp-git-ingest 仓库的技术细节。我会先获取目录结构,然后读取重要文件来分析代码是如何工作的。
Tool Calls:
github_directory_structure
Args:
repo_url: https://github.com/adhikasp/mcp-git-ingest
现在,我会读取重要文件来理解技术实现:
Tool Calls:
github_read_important_files
Args:
repo_url: https://github.com/adhikasp/mcp-git-ingest
file_paths: ['README.md', 'src/mcp_git_ingest/main.py', 'pyproject.toml']
让我来分解一下这段代码是如何技术性工作的:
1. 目的:
`mcp-git-ingest` 是一个模型上下文协议(MCP)服务器,设计用于帮助读取GitHub仓库结构和重要文件。它提供两个主要工具:
- `github_directory_structure`:返回仓库目录结构的树状表示
- `github_read_important_files`:读取并返回仓库中指定文件的内容
2. 技术实现:
a. 依赖项:
- 使用 `fastmcp` 创建 MCP 服务器
- 使用 `gitpython` 进行 Git 仓库操作
- 需要 Python 3.8+
b. 主要函数:
`clone_repo(repo_url: str) -> str`:
- 基于仓库 URL 的哈希创建确定性临时目录
- 检查仓库是否已克隆
- 如果未克隆,则克隆仓库
- 处理错误情况和清理
- 返回克隆仓库的路径
`get_directory_tree(path: str, prefix: str = "") -> str`:
- 递归生成树状目录结构
- 跳过 `.git` 目录
- 使用 Unicode 箭头字符创建可视树表示
- 处理条目排序
`github_directory_structure(repo_url: str) -> str`:
- 克隆仓库
- 生成目录树
- 处理后清理临时仓库
- 返回树结构或错误消息
`github_read_important_files(repo_url: str, file_paths: List[str]) -> dict[str, str]`:
- 克隆仓库
- 读取指定文件
- 返回文件路径与其内容的映射字典
- 处理文件读取错误
- 清理临时仓库
3. 错误处理:
- 使用 try-except 块处理仓库克隆、文件读取错误
- 使用 finally 块确保始终清理临时目录
- 返回描述性错误消息
4. 性能优化:
- 使用基于哈希的临时目录命名以可能重用克隆的仓库
- 在克隆前检查现有仓库
- 实现清理以防止临时文件积累
5. 独特功能:
- 确定性临时目录创建
- Unicode 树表示
- 带错误处理的灵活文件读取
6. 执行:
- 可作为 CLI 工具通过 `mcp-git-ingest` 运行
- 通过 `pyproject.toml` 配置
- 依赖 `fastmcp` 实现 MCP 服务器功能
代码是一个用于编程方式探索和读取 GitHub 仓库的强大、灵活工具,重点关注错误处理和干净实现。
你想让我详细说明实现的任何特定方面吗?
