CodeGraphMCP服务器
无需配置的轻量级高性能源代码分析MCP服务器
     
概述
CodeGraphMCPServer是一个了解代码库结构并提供GraphRAG(图形检索增强生成)功能的MCP服务器。凭借不需要外部数据库的自包含架构,它能够从MCP兼容的AI工具(GitHub Copilot、Claude Desktop、Cursor等)中实现结构理解和高效的代码完成。
🧠 GraphRAG功能
- 社区发现:使用Louvain算法自动对代码模块进行聚类
- LLM集成:支持OpenAI/Anthropic/LLocal LLM的多提供商设计
- 全局搜索:使用社区摘要进行代码库范围内的理解
- 本地搜索:从实体邻域检索上下文
✨ 特性
| 特性 | 描述 |
|---|---|
| 🚀 零配置 | 不需要外部DB, pip install && serve 立即开始 |
| 🌳 AST分析 | 使用Tree sitter进行快速准确的代码分析 |
| 🔗 图形构造 | 构建代码实体之间的关系图 |
| 🔍 14 MCP工具 | 依赖性分析、调用跟踪、代码搜索 |
| 📚 4 MCP资源 | 实体、文件、社区、统计数据 |
| 💬 6个MCP提示 | 代码审查、功能实现、调试协助 |
| ⚡ 快速索引 | 30秒内完成10万行,2秒内完成增量更新 |
| 🌐 多语言支持 | Python、TypeScript、JavaScript、Rust、Go、Java、PHP、C#、C、C++、HCL、Ruby、Kotlin、Swift、Scala、Lua(16种语言) |
需求
- Python 3.11+
- MCP兼容客户端(GitHub Copilot、Claude Desktop、Cursor、Windsurf)
安装
使用pip安装
pip install codegraph-mcp-server从源代码安装(用于开发)
git clone https://github.com/nahisaho/CodeGraphMCPServer.git
cd CodeGraphMCPServer
python -m venv .venv
source .venv/bin/activate # Linux/macOS
pip install -e ".[dev]"快速开始
1.为存储库建立索引
# Full index
codegraph-mcp index /path/to/repository --full
# Incremental index (default)
codegraph-mcp index /path/to/repository
# Auto re-index with file watching (v0.7.0 NEW)
codegraph-mcp watch /path/to/repository
codegraph-mcp watch /path/to/repository --debounce 2.0 # 2 second debounce
codegraph-mcp watch /path/to/repository --community # Community detection after re-index输出示例:
Indexed 16 entities, 37 relations in 0.81s2.检查统计数据
codegraph-mcp stats /path/to/repository输出示例:
Repository Statistics
=====================
Repository: /path/to/repository
Entities: 16
Relations: 37
Communities: 0
Files: 1
Entities by type:
- class: 2
- function: 2
- method: 11
- module: 13.搜索代码
codegraph-mcp query "Calculator" --repo /path/to/repository4.作为MCP服务器启动
# stdio transport (default)
codegraph-mcp serve --repo /path/to/repository
# SSE transport
codegraph-mcp start --repo /path/to/repository --port 8080MCP客户端配置
克劳德桌面版
~/.config/claude/claude_desktop_config.json:
{
"mcpServers": {
"codegraph": {
"command": "codegraph-mcp",
"args": ["serve", "--repo", "/path/to/your/project"]
}
}
}克劳德代码
# stdio transport
claude mcp add codegraph -- codegraph-mcp serve --repo /path/to/project
# HTTP transport (SSE server)
codegraph-mcp start --port 8080 # In another terminal
claude mcp add --transport http codegraph http://0.0.0.0:8080VS代码(GitHub副本)
.vscode/settings.json:
{
"mcp.servers": {
"codegraph": {
"command": "codegraph-mcp",
"args": ["serve", "--repo", "${workspaceFolder}"]
}
}
}光标
~/.cursor/mcp.json:
{
"mcpServers": {
"codegraph": {
"command": "codegraph-mcp",
"args": ["serve", "--repo", "/path/to/your/project"]
}
}
}🛠 MCP工具(14)
图形查询工具
| 工具 | 描述 | 主要参数 |
|---|---|---|
query_codebase | 用自然语言搜索代码图 | query, max_results |
find_dependencies | 查找实体依赖关系 | entity_id, depth |
find_callers | 查找函数/方法的调用者 | entity_id |
find_callees | 查找函数/方法的被调用者 | entity_id |
find_implementations | 查找接口实现 | entity_id |
analyze_module_structure | 分析模块结构 | file_path |
代码检索工具
| 工具 | 描述 | 主要参数 |
|---|---|---|
get_code_snippet | 获取实体源代码 | entity_id, include_context |
read_file_content | 获取文件内容 | file_path, start_line, end_line |
get_file_structure | 获取文件结构概述 | file_path |
GraphRAG工具
| 工具 | 描述 | 主要参数 |
|---|---|---|
global_search | 跨社区全球搜索 | query |
local_search | 实体邻域中的本地搜索 | query, entity_id |
管理工具
| 工具 | 描述 | 主要参数 |
|---|---|---|
suggest_refactoring | 建议重构 | entity_id, type |
reindex_repository | 重新索引存储库 | incremental |
execute_shell_command | 执行shell命令 | command, timeout |
📚 MCP资源(4)
| URI模式 | 描述 |
|---|---|
codegraph://entities/{id} | 实体详细信息 |
codegraph://files/{path} | 文件中的实体 |
codegraph://communities/{id} | 社区信息 |
codegraph://stats | 图形统计 |
💬 MCP提示(6)
| 提示 | 描述 | 参数 |
|---|---|---|
code_review | 执行代码审查 | entity_id, focus_areas |
explain_codebase | 解释代码库 | scope, detail_level |
implement_feature | 功能实现指南 | feature_description, constraints |
debug_issue | 调试协助 | issue_description, context |
refactor_guidance | 重构指南 | entity_id, goal |
test_generation | 测试生成 | entity_id, test_type |
使用示例
与AI助手对话
You: What are the dependencies of the UserService class?
AI: [Using find_dependencies tool]
UserService depends on:
- DatabaseConnection (database.py)
- Logger (utils/logging.py)
- UserRepository (repositories/user.py)You: What would be affected if I modify the authenticate method?
AI: [Using find_callers tool]
Callers of authenticate:
- LoginController.login() (controllers/auth.py:45)
- APIMiddleware.verify_token() (middleware/api.py:23)
- TestUserService.test_auth() (tests/test_user.py:78)You: Explain the main components of this project
AI: [Using global_search tool]
[Using explain_codebase prompt]
This project uses a 3-tier architecture:
1. Controllers layer: HTTP request handling
2. Services layer: Business logic
3. Repositories layer: Data access发展
运行测试
# Run all tests
pytest
# With coverage
pytest --cov=src/codegraph_mcp --cov-report=html
# Specific tests
pytest tests/unit/test_parser.py -v棉绒和格式
# Lint with Ruff
ruff check src tests
# Format with Ruff
ruff format src tests
# Type check with MyPy
mypy src建筑
src/codegraph_mcp/
├── __init__.py # Package initialization
├── __main__.py # CLI entry point
├── server.py # MCP server
├── config.py # Configuration management
├── core/ # Core logic
│ ├── parser.py # Tree-sitter AST parser
│ ├── graph.py # NetworkX graph engine
│ ├── indexer.py # Repository indexer
│ ├── community.py # Community detection (Louvain)
│ ├── semantic.py # Semantic analysis
│ ├── llm.py # LLM integration (OpenAI/Anthropic/Local)
│ └── graphrag.py # GraphRAG search engine
├── storage/ # Storage layer
│ ├── sqlite.py # SQLite persistence
│ ├── cache.py # File cache
│ └── vectors.py # Vector store
├── mcp/ # MCP interface
│ ├── tools.py # 14 MCP Tools
│ ├── resources.py # 4 MCP Resources
│ └── prompts.py # 6 MCP Prompts
└── languages/ # Language support (12 languages)
├── python.py # Python extractor
├── typescript.py # TypeScript extractor
├── javascript.py # JavaScript extractor
├── rust.py # Rust extractor
├── go.py # Go extractor
├── java.py # Java extractor
├── php.py # PHP extractor
├── csharp.py # C# extractor
├── c.py # C extractor
├── cpp.py # C++ extractor
├── hcl.py # HCL (Terraform) extractor
└── ruby.py # Ruby extractor演出
测量值(v0.3.0)
| 度量 | 测量 | 注释 |
|---|---|---|
| 索引速度 | 32个实体/秒 | 67个文件,941个实体 |
| 文件处理速度 | 0.44秒/文件 | Python/TS/Rust混合 |
| 增量指数 | \<2秒 | 仅更改文件 |
| 查询响应 | \<2ms | 图形搜索 |
目标值
| 指标 | 目标 |
|---|---|
| 初始索引(100K行) | \<30秒 |
| 增量索引 | \<2秒 |
| 查询响应 | \<500ms |
| 启动时间 | \<2秒 |
| 内存使用量 | \<500MB |
许可证
MIT许可证-请参阅 许可证
致谢
- 模型上下文协议 -MCP规范
- 树保姆 -AST分析
- 网络X -图形算法
- 微软GraphRAG -GraphRAG概念
