内容
# Git + Azure 知识图谱
一个索引 Git 仓库的知识图谱系统,用于映射代码结构和服务架构。它使用 Roslyn 解析 C# 源代码,发现 REST 端点、HTTP 调用、消息队列和数据库访问,然后通过 REST API、Angular 仪表盘和 Cursor MCP 集成公开图谱。

## 文档
| 指南 | 描述 |
|-------|-------------|
| **[用户指南](docs/USER_GUIDE.md)** | 完整设置、UI 遍历、查询、MCP、故障排除 |
| **[架构](docs/ARCHITECTURE.md)** | 系统设计、数据模型、索引流程 |
| **[文档索引](docs/README.md)** | 所有文档和 UI 图片 |
## 功能
- **代码级图谱** — 类、方法、属性、继承和调用关系(Roslyn)
- **服务级图谱** — REST API、HTTP 客户端、RabbitMQ/Kafka/Azure Service Bus、EF Core/Dapper
- **自然语言查询** — 基于关键字的图谱查询(“查找所有调用 X 的方法”)
- **增量刷新** — 基于 git-diff 的仅更改文件重新索引
- **双重访问** — Web API + MCP 服务器共享相同的 LiteDB 存储
- **交互式 UI** — 具有 D3 力导向图可视化的 Angular 仪表盘
## 架构

```
┌─────────────┐ ┌─────────────┐
│ Angular UI │ │ Cursor MCP │
│ (端口 4200)│ │ (stdio) │
└──────┬──────┘ └──────┬──────┘
│ │
▼ ▼
┌──────────────────────────────────┐
│ Web API (5196) │
│ GraphController │
│ RepositoryController │
└──────────────┬───────────────────┘
│
▼
┌──────────────────────────────────┐
│ 核心库 │
│ GraphStore (LiteDB + 缓存) │
│ GraphEngine (BFS/DFS/路径) │
│ GraphIndexer (实体 → 图谱) │
│ RepositoryIndexingService │
│ QueryProcessor (自然语言查询) │
│ ServiceGraphBuilder │
│ IncrementalRefresher │
└──────────────┬───────────────────┘
│
▼
┌──────────────────────────────────┐
│ LiteDB (%LocalAppData%\ │
│ GitKnowledgeGraph\graph.db) │
└──────────────────────────────────┘
```
参见 [docs/ARCHITECTURE.md](docs/ARCHITECTURE.md) 获取详细设计文档。
历史阶段报告存档在 [docs/archive/](docs/archive/) 中。
## 快速入门
### 前提条件
- [.NET 9 SDK](https://dotnet.microsoft.com/download)
- [Node.js 18+](https://nodejs.org/) (用于 Angular 前端)
- Git
### 1. 构建后端
```bash
dotnet build GitAzureKnowledgeGraph.sln
```
### 2. 启动 Web API
```bash
cd src/WebApi/WebApi
dotnet run
```
Swagger UI: http://localhost:5196/swagger
### 3. 启动 Angular 前端
```bash
cd frontend
npm install
npm start
```
仪表盘:http://localhost:4200
### 4. 索引仓库
**通过 UI:** 进入 **仓库** → **+ 索引仓库** → 输入 Git URL → **开始索引**
**通过 API:**
```bash
curl -X POST http://localhost:5196/api/repository/index \
-H "Content-Type: application/json" \
-d '{"gitUrl": "https://github.com/your-org/your-repo.git"}'
```
**通过 MCP(Cursor):** 参见 [用户指南 — MCP 设置](docs/USER_GUIDE.md#使用-Cursor-MCP)
### 5. 查询图谱
**通过 UI:** 进入 **图谱** → 输入查询 → **运行查询**
```bash
curl -X POST http://localhost:5196/api/graph/query \
-H "Content-Type: application/json" \
-d '{"query": "查找所有类", "scope": "all"}'
```
## Web UI
| 页面 | 路由 | 截图 |
|------|-------|------------|
| 仪表盘 | `/dashboard` | [ui-dashboard.png](docs/images/ui-dashboard.png) |
| 图谱浏览器 | `/graph` | [ui-graph-view.png](docs/images/ui-graph-view.png) |
| 仓库 | `/repositories` | [ui-repositories.png](docs/images/ui-repositories.png) |
参见 [用户指南](docs/USER_GUIDE.md) 获取每个页面的完整遍历。
## 项目结构
```
MemoryBankProject/
├── src/
│ ├── Core/Core/ 共享库(模型、图谱、Git、服务发现)
│ ├── WebApi/WebApi/ ASP.NET Core REST API
│ └── McpServer/McpServer/ Cursor MCP stdio 服务器
├── frontend/ Angular 17 SPA
├── docs/
│ ├── USER_GUIDE.md 如何安装和使用系统
│ ├── ARCHITECTURE.md 设计和数据模型
│ ├── images/ UI 截图和图表
│ └── archive/ 历史阶段报告
└── GitAzureKnowledgeGraph.sln
```
## API 端点
| 方法 | 路由 | 描述 |
|--------|-------|-------------|
| GET | `/api/graph/stats` | 图谱统计 |
| POST | `/api/graph/query` | 自然语言查询 |
| GET | `/api/graph/nodes/{id}` | 按 ID 获取节点 |
| GET | `/api/graph/nodes?type=&limit=` | 按类型列出节点 |
| GET | `/api/graph/nodes/{id}/dependencies` | 输出依赖树 |
| GET | `/api/graph/nodes/{id}/dependents` | 输入依赖树 |
| GET | `/api/graph/path?from=&to=` | 节点之间的最短路径 |
| GET | `/api/graph/cycles` | 检测循环依赖 |
| GET | `/api/repository` | 列出索引仓库 |
| POST | `/api/repository/index` | 索引新仓库 |
| POST | `/api/repository/{id}/refresh` | 增量刷新 |
## MCP 工具
| 工具 | 描述 |
|------|-------------|
| `index_repository` | 克隆和索引 Git 仓库 |
| `query_knowledge_graph` | 自然语言图谱查询 |
| `get_entity_details` | 获取特定实体的详细信息 |
| `refresh_repository` | 增量刷新索引仓库 |
## 节点类型

所有节点类型都存储为小写 kebab-case:
| 类型 | 来源 |
|------|--------|
| `class`, `method`, `property`, `interface`, ... | Roslyn 代码解析 |
| `service` | 服务发现 |
| `api-endpoint` | REST 端点提取 |
| `http-call` | 输出 HTTP 客户端调用 |
| `message-queue` | MQ 发布/订阅 |
| `database-access` | EF Core / Dapper / SQL |
## 配置
| 设置 | 默认值 | 位置 |
|---------|---------|----------|
| 数据库路径 | `%LocalAppData%\GitKnowledgeGraph\graph.db` | `Program.cs` (WebApi + MCP) |
| API 端口 | 5196 | `launchSettings.json` |
| 前端端口 | 4200 | `angular.json` |
| 缓存大小 | 10,000 个节点/边 | `GraphStore` 构造函数 |
## 许可证
内部项目 — 请联系仓库所有者获取许可条款。