@chinjinyu/mcp-knowledge-graph-plus 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +700 -0
- package/dist/application/services/FactService.d.ts +16 -0
- package/dist/application/services/FactService.d.ts.map +1 -0
- package/dist/application/services/FactService.js +50 -0
- package/dist/application/services/FactService.js.map +1 -0
- package/dist/application/services/KnowledgeGraphService.d.ts +33 -0
- package/dist/application/services/KnowledgeGraphService.d.ts.map +1 -0
- package/dist/application/services/KnowledgeGraphService.js +271 -0
- package/dist/application/services/KnowledgeGraphService.js.map +1 -0
- package/dist/application/services/LinkService.d.ts +17 -0
- package/dist/application/services/LinkService.d.ts.map +1 -0
- package/dist/application/services/LinkService.js +53 -0
- package/dist/application/services/LinkService.js.map +1 -0
- package/dist/application/services/NodeService.d.ts +31 -0
- package/dist/application/services/NodeService.d.ts.map +1 -0
- package/dist/application/services/NodeService.js +69 -0
- package/dist/application/services/NodeService.js.map +1 -0
- package/dist/core/config/index.d.ts +34 -0
- package/dist/core/config/index.d.ts.map +1 -0
- package/dist/core/config/index.js +60 -0
- package/dist/core/config/index.js.map +1 -0
- package/dist/core/errors/index.d.ts +70 -0
- package/dist/core/errors/index.d.ts.map +1 -0
- package/dist/core/errors/index.js +120 -0
- package/dist/core/errors/index.js.map +1 -0
- package/dist/core/types/index.d.ts +119 -0
- package/dist/core/types/index.d.ts.map +1 -0
- package/dist/core/types/index.js +5 -0
- package/dist/core/types/index.js.map +1 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +117 -0
- package/dist/index.js.map +1 -0
- package/dist/infrastructure/database/DatabaseManager.d.ts +13 -0
- package/dist/infrastructure/database/DatabaseManager.d.ts.map +1 -0
- package/dist/infrastructure/database/DatabaseManager.js +101 -0
- package/dist/infrastructure/database/DatabaseManager.js.map +1 -0
- package/dist/infrastructure/di/Container.d.ts +21 -0
- package/dist/infrastructure/di/Container.d.ts.map +1 -0
- package/dist/infrastructure/di/Container.js +45 -0
- package/dist/infrastructure/di/Container.js.map +1 -0
- package/dist/infrastructure/repositories/BaseRepository.d.ts +23 -0
- package/dist/infrastructure/repositories/BaseRepository.d.ts.map +1 -0
- package/dist/infrastructure/repositories/BaseRepository.js +84 -0
- package/dist/infrastructure/repositories/BaseRepository.js.map +1 -0
- package/dist/infrastructure/repositories/FactRepository.d.ts +28 -0
- package/dist/infrastructure/repositories/FactRepository.d.ts.map +1 -0
- package/dist/infrastructure/repositories/FactRepository.js +101 -0
- package/dist/infrastructure/repositories/FactRepository.js.map +1 -0
- package/dist/infrastructure/repositories/LinkRepository.d.ts +28 -0
- package/dist/infrastructure/repositories/LinkRepository.d.ts.map +1 -0
- package/dist/infrastructure/repositories/LinkRepository.js +107 -0
- package/dist/infrastructure/repositories/LinkRepository.js.map +1 -0
- package/dist/infrastructure/repositories/NodeRepository.d.ts +28 -0
- package/dist/infrastructure/repositories/NodeRepository.d.ts.map +1 -0
- package/dist/infrastructure/repositories/NodeRepository.js +143 -0
- package/dist/infrastructure/repositories/NodeRepository.js.map +1 -0
- package/dist/interfaces/mcp/commands/BaseCommand.d.ts +55 -0
- package/dist/interfaces/mcp/commands/BaseCommand.d.ts.map +1 -0
- package/dist/interfaces/mcp/commands/BaseCommand.js +42 -0
- package/dist/interfaces/mcp/commands/BaseCommand.js.map +1 -0
- package/dist/interfaces/mcp/commands/FactAddCommand.d.ts +42 -0
- package/dist/interfaces/mcp/commands/FactAddCommand.d.ts.map +1 -0
- package/dist/interfaces/mcp/commands/FactAddCommand.js +73 -0
- package/dist/interfaces/mcp/commands/FactAddCommand.js.map +1 -0
- package/dist/interfaces/mcp/commands/FactDelCommand.d.ts +42 -0
- package/dist/interfaces/mcp/commands/FactDelCommand.d.ts.map +1 -0
- package/dist/interfaces/mcp/commands/FactDelCommand.js +68 -0
- package/dist/interfaces/mcp/commands/FactDelCommand.js.map +1 -0
- package/dist/interfaces/mcp/commands/FactEditCommand.d.ts +43 -0
- package/dist/interfaces/mcp/commands/FactEditCommand.d.ts.map +1 -0
- package/dist/interfaces/mcp/commands/FactEditCommand.js +68 -0
- package/dist/interfaces/mcp/commands/FactEditCommand.js.map +1 -0
- package/dist/interfaces/mcp/commands/LinkAddCommand.d.ts +43 -0
- package/dist/interfaces/mcp/commands/LinkAddCommand.d.ts.map +1 -0
- package/dist/interfaces/mcp/commands/LinkAddCommand.js +63 -0
- package/dist/interfaces/mcp/commands/LinkAddCommand.js.map +1 -0
- package/dist/interfaces/mcp/commands/LinkDelCommand.d.ts +43 -0
- package/dist/interfaces/mcp/commands/LinkDelCommand.d.ts.map +1 -0
- package/dist/interfaces/mcp/commands/LinkDelCommand.js +65 -0
- package/dist/interfaces/mcp/commands/LinkDelCommand.js.map +1 -0
- package/dist/interfaces/mcp/commands/LinkEditCommand.d.ts +47 -0
- package/dist/interfaces/mcp/commands/LinkEditCommand.d.ts.map +1 -0
- package/dist/interfaces/mcp/commands/LinkEditCommand.js +75 -0
- package/dist/interfaces/mcp/commands/LinkEditCommand.js.map +1 -0
- package/dist/interfaces/mcp/commands/ListCommand.d.ts +33 -0
- package/dist/interfaces/mcp/commands/ListCommand.d.ts.map +1 -0
- package/dist/interfaces/mcp/commands/ListCommand.js +57 -0
- package/dist/interfaces/mcp/commands/ListCommand.js.map +1 -0
- package/dist/interfaces/mcp/commands/NodeAddCommand.d.ts +46 -0
- package/dist/interfaces/mcp/commands/NodeAddCommand.d.ts.map +1 -0
- package/dist/interfaces/mcp/commands/NodeAddCommand.js +67 -0
- package/dist/interfaces/mcp/commands/NodeAddCommand.js.map +1 -0
- package/dist/interfaces/mcp/commands/NodeDelCommand.d.ts +32 -0
- package/dist/interfaces/mcp/commands/NodeDelCommand.d.ts.map +1 -0
- package/dist/interfaces/mcp/commands/NodeDelCommand.js +63 -0
- package/dist/interfaces/mcp/commands/NodeDelCommand.js.map +1 -0
- package/dist/interfaces/mcp/commands/NodeEditCommand.d.ts +43 -0
- package/dist/interfaces/mcp/commands/NodeEditCommand.d.ts.map +1 -0
- package/dist/interfaces/mcp/commands/NodeEditCommand.js +63 -0
- package/dist/interfaces/mcp/commands/NodeEditCommand.js.map +1 -0
- package/dist/interfaces/mcp/commands/NodeGetCommand.d.ts +40 -0
- package/dist/interfaces/mcp/commands/NodeGetCommand.d.ts.map +1 -0
- package/dist/interfaces/mcp/commands/NodeGetCommand.js +72 -0
- package/dist/interfaces/mcp/commands/NodeGetCommand.js.map +1 -0
- package/dist/interfaces/mcp/commands/SearchCommand.d.ts +53 -0
- package/dist/interfaces/mcp/commands/SearchCommand.d.ts.map +1 -0
- package/dist/interfaces/mcp/commands/SearchCommand.js +91 -0
- package/dist/interfaces/mcp/commands/SearchCommand.js.map +1 -0
- package/package.json +10 -0
package/README.md
ADDED
|
@@ -0,0 +1,700 @@
|
|
|
1
|
+
# MCP Knowledge Graph Plus
|
|
2
|
+
|
|
3
|
+
一个基于 **Model Context Protocol (MCP)** 的知识图谱服务器,使用 SQLite 数据库进行持久化存储。允许 AI 助手通过 MCP 协议创建、管理和查询结构化的知识数据。
|
|
4
|
+
|
|
5
|
+
## 功能特性
|
|
6
|
+
|
|
7
|
+
- 🔷 **节点管理** - 创建、编辑、删除知识节点
|
|
8
|
+
- 🔗 **关系管理** - 建立和管理节点间的关系链接
|
|
9
|
+
- 📝 **事实管理** - 为节点添加事实和备注
|
|
10
|
+
- 🔍 **搜索查询** - 支持多字段、多逻辑的关键词搜索
|
|
11
|
+
- 📊 **知识图谱** - 完整列出所有节点、关系和事实
|
|
12
|
+
- 💾 **持久化存储** - 基于 SQLite 的本地数据库
|
|
13
|
+
- 🚀 **MCP 协议** - 标准化的 AI 上下文通信协议
|
|
14
|
+
|
|
15
|
+
## 系统要求
|
|
16
|
+
|
|
17
|
+
- Node.js >= 18.0.0
|
|
18
|
+
|
|
19
|
+
## 安装
|
|
20
|
+
|
|
21
|
+
```bash
|
|
22
|
+
# 克隆或进入项目目录
|
|
23
|
+
cd mcp-knowledge-graph-plus
|
|
24
|
+
|
|
25
|
+
# 安装依赖
|
|
26
|
+
npm install
|
|
27
|
+
|
|
28
|
+
# 构建项目
|
|
29
|
+
npm run build
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
## 快速开始
|
|
33
|
+
|
|
34
|
+
### 1. 构建项目
|
|
35
|
+
|
|
36
|
+
```bash
|
|
37
|
+
npm run build
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
### 2. 配置 MCP 客户端
|
|
41
|
+
|
|
42
|
+
在你的 MCP 客户端配置中添加此服务器:
|
|
43
|
+
|
|
44
|
+
```json
|
|
45
|
+
{
|
|
46
|
+
"mcpServers": {
|
|
47
|
+
"knowledge-graph": {
|
|
48
|
+
"command": "node",
|
|
49
|
+
"args": ["/path/to/mcp-knowledge-graph-plus/dist/index.js"],
|
|
50
|
+
"env": {
|
|
51
|
+
"DB_PATH": "/path/to/your/database.db"
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
### 3. 命令行参数
|
|
59
|
+
|
|
60
|
+
支持通过命令行参数指定数据库路径:
|
|
61
|
+
|
|
62
|
+
```bash
|
|
63
|
+
# 方式 1: 使用等号
|
|
64
|
+
node dist/index.js --db-path=/path/to/db.db
|
|
65
|
+
|
|
66
|
+
# 方式 2: 使用空格
|
|
67
|
+
node dist/index.js --db-path /path/to/db.db
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
### 4. 环境变量
|
|
71
|
+
|
|
72
|
+
| 变量名 | 说明 | 默认值 |
|
|
73
|
+
|--------|------|--------|
|
|
74
|
+
| `DB_PATH` | 数据库文件路径 | `./default.db` |
|
|
75
|
+
| `NODE_ENV` | 运行环境 (development/production/test) | `production` |
|
|
76
|
+
|
|
77
|
+
## 可用工具 (Tools)
|
|
78
|
+
|
|
79
|
+
---
|
|
80
|
+
|
|
81
|
+
### 一、节点管理
|
|
82
|
+
|
|
83
|
+
#### 1. `kg_node_add` - 添加节点
|
|
84
|
+
|
|
85
|
+
添加新的知识节点及其关联的事实。
|
|
86
|
+
|
|
87
|
+
**输入参数:**
|
|
88
|
+
|
|
89
|
+
| 参数 | 类型 | 必填 | 说明 |
|
|
90
|
+
|------|------|------|------|
|
|
91
|
+
| `nodes` | `array` | ✅ | 节点数组 |
|
|
92
|
+
| `nodes[].name` | `string` | ✅ | 节点名称(唯一标识) |
|
|
93
|
+
| `nodes[].type` | `string` | ✅ | 节点类型(如:概念、人物、地点) |
|
|
94
|
+
| `nodes[].facts` | `string[]` | ❌ | 关联的事实描述数组 |
|
|
95
|
+
|
|
96
|
+
**输出参数:**
|
|
97
|
+
|
|
98
|
+
| 参数 | 类型 | 说明 |
|
|
99
|
+
|------|------|------|
|
|
100
|
+
| `added` | `number` | 成功创建的节点数量 |
|
|
101
|
+
| `nodes` | `array` | 创建的节点列表 |
|
|
102
|
+
| `nodes[].name` | `string` | 节点名称 |
|
|
103
|
+
| `nodes[].type` | `string` | 节点类型 |
|
|
104
|
+
| `nodes[].facts` | `string[]` | 创建的事实列表 |
|
|
105
|
+
|
|
106
|
+
**示例:**
|
|
107
|
+
|
|
108
|
+
```json
|
|
109
|
+
// 输入
|
|
110
|
+
{
|
|
111
|
+
"nodes": [
|
|
112
|
+
{ "name": "人工智能", "type": "概念", "facts": ["是计算机科学的一个分支", "模拟人类智能"] },
|
|
113
|
+
{ "name": "机器学习", "type": "技术", "facts": ["是 AI 的子领域"] }
|
|
114
|
+
]
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
// 输出
|
|
118
|
+
{
|
|
119
|
+
"added": 2,
|
|
120
|
+
"nodes": [
|
|
121
|
+
{ "name": "人工智能", "type": "概念", "facts": ["是计算机科学的一个分支", "模拟人类智能"] },
|
|
122
|
+
{ "name": "机器学习", "type": "技术", "facts": ["是 AI 的子领域"] }
|
|
123
|
+
]
|
|
124
|
+
}
|
|
125
|
+
```
|
|
126
|
+
|
|
127
|
+
---
|
|
128
|
+
|
|
129
|
+
#### 2. `kg_node_edit` - 编辑节点
|
|
130
|
+
|
|
131
|
+
修改现有节点的名称和/或类型。
|
|
132
|
+
|
|
133
|
+
**输入参数:**
|
|
134
|
+
|
|
135
|
+
| 参数 | 类型 | 必填 | 说明 |
|
|
136
|
+
|------|------|------|------|
|
|
137
|
+
| `nodes` | `array` | ✅ | 节点编辑数组 |
|
|
138
|
+
| `nodes[].name` | `string` | ✅ | 要编辑的节点当前名称 |
|
|
139
|
+
| `nodes[].newName` | `string` | ❌ | 新名称(不填则不变) |
|
|
140
|
+
| `nodes[].newType` | `string` | ❌ | 新类型(不填则不变) |
|
|
141
|
+
|
|
142
|
+
**输出参数:**
|
|
143
|
+
|
|
144
|
+
| 参数 | 类型 | 说明 |
|
|
145
|
+
|------|------|------|
|
|
146
|
+
| `updated` | `number` | 更新的节点数量 |
|
|
147
|
+
| `nodes` | `array` | 更新后的节点列表 |
|
|
148
|
+
|
|
149
|
+
**示例:**
|
|
150
|
+
|
|
151
|
+
```json
|
|
152
|
+
// 输入
|
|
153
|
+
{
|
|
154
|
+
"nodes": [
|
|
155
|
+
{ "name": "AI", "newName": "人工智能", "newType": "学科" },
|
|
156
|
+
{ "name": "深度学习", "newType": "算法" }
|
|
157
|
+
]
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
// 输出
|
|
161
|
+
{
|
|
162
|
+
"updated": 2,
|
|
163
|
+
"nodes": [
|
|
164
|
+
{ "name": "人工智能", "type": "学科" },
|
|
165
|
+
{ "name": "深度学习", "type": "算法" }
|
|
166
|
+
]
|
|
167
|
+
}
|
|
168
|
+
```
|
|
169
|
+
|
|
170
|
+
---
|
|
171
|
+
|
|
172
|
+
#### 3. `kg_node_del` - 删除节点
|
|
173
|
+
|
|
174
|
+
删除指定节点(级联删除相关事实和关系)。
|
|
175
|
+
|
|
176
|
+
**输入参数:**
|
|
177
|
+
|
|
178
|
+
| 参数 | 类型 | 必填 | 说明 |
|
|
179
|
+
|------|------|------|------|
|
|
180
|
+
| `names` | `string[]` | ✅ | 要删除的节点名称数组 |
|
|
181
|
+
|
|
182
|
+
**输出参数:**
|
|
183
|
+
|
|
184
|
+
| 参数 | 类型 | 说明 |
|
|
185
|
+
|------|------|------|
|
|
186
|
+
| `deleted` | `number` | 删除的节点数量 |
|
|
187
|
+
| `nodes` | `array` | 被删除节点的详细信息 |
|
|
188
|
+
|
|
189
|
+
**示例:**
|
|
190
|
+
|
|
191
|
+
```json
|
|
192
|
+
// 输入
|
|
193
|
+
{ "names": ["过时的概念", "临时节点"] }
|
|
194
|
+
|
|
195
|
+
// 输出
|
|
196
|
+
{
|
|
197
|
+
"deleted": 2,
|
|
198
|
+
"nodes": [
|
|
199
|
+
{ "name": "过时的概念", "type": "概念", "factsCount": 3, "linksCount": 5 },
|
|
200
|
+
{ "name": "临时节点", "type": "测试", "factsCount": 0, "linksCount": 2 }
|
|
201
|
+
]
|
|
202
|
+
}
|
|
203
|
+
```
|
|
204
|
+
|
|
205
|
+
---
|
|
206
|
+
|
|
207
|
+
#### 4. `kg_node_get` - 获取节点
|
|
208
|
+
|
|
209
|
+
获取指定节点的详细信息(包含事实和关系)。返回所有涉及请求节点的链接,链接中会补全另一端的节点名称。
|
|
210
|
+
|
|
211
|
+
**输入参数:**
|
|
212
|
+
|
|
213
|
+
| 参数 | 类型 | 必填 | 说明 |
|
|
214
|
+
|------|------|------|------|
|
|
215
|
+
| `names` | `string[]` | ✅ | 要查询的节点名称数组 |
|
|
216
|
+
| `page` | `boolean` | ✅ | 是否启用分页 |
|
|
217
|
+
| `pageNum` | `number` | ❌ | 页码(默认 1) |
|
|
218
|
+
| `pageSize` | `number` | ❌ | 每页数量(默认 10) |
|
|
219
|
+
|
|
220
|
+
**输出参数:**
|
|
221
|
+
|
|
222
|
+
| 参数 | 类型 | 说明 |
|
|
223
|
+
|------|------|------|
|
|
224
|
+
| `nodes` | `array` | 节点列表 |
|
|
225
|
+
| `links` | `array` | 涉及请求节点的链接列表 |
|
|
226
|
+
| `pagination` | `object` | 分页信息(仅当启用分页时) |
|
|
227
|
+
|
|
228
|
+
**示例:**
|
|
229
|
+
|
|
230
|
+
```json
|
|
231
|
+
// 输入
|
|
232
|
+
{ "names": ["人工智能", "机器学习"], "page": false }
|
|
233
|
+
|
|
234
|
+
// 输出
|
|
235
|
+
{
|
|
236
|
+
"nodes": [
|
|
237
|
+
{ "name": "人工智能", "type": "概念", "facts": ["是计算机科学的一个分支"] },
|
|
238
|
+
{ "name": "机器学习", "type": "技术", "facts": ["是 AI 的子领域"] }
|
|
239
|
+
],
|
|
240
|
+
"links": [
|
|
241
|
+
{ "from": "人工智能", "to": "机器学习", "type": "包含" }
|
|
242
|
+
]
|
|
243
|
+
}
|
|
244
|
+
```
|
|
245
|
+
|
|
246
|
+
---
|
|
247
|
+
|
|
248
|
+
### 二、链接管理
|
|
249
|
+
|
|
250
|
+
#### 5. `kg_link_add` - 添加链接
|
|
251
|
+
|
|
252
|
+
在两个节点之间创建有向关系链接。
|
|
253
|
+
|
|
254
|
+
**输入参数:**
|
|
255
|
+
|
|
256
|
+
| 参数 | 类型 | 必填 | 说明 |
|
|
257
|
+
|------|------|------|------|
|
|
258
|
+
| `links` | `array` | ✅ | 链接数组 |
|
|
259
|
+
| `links[].from` | `string` | ✅ | 起始节点名称 |
|
|
260
|
+
| `links[].to` | `string` | ✅ | 目标节点名称 |
|
|
261
|
+
| `links[].type` | `string` | ✅ | 关系类型 |
|
|
262
|
+
|
|
263
|
+
**输出参数:**
|
|
264
|
+
|
|
265
|
+
| 参数 | 类型 | 说明 |
|
|
266
|
+
|------|------|------|
|
|
267
|
+
| `added` | `number` | 创建的链接数量 |
|
|
268
|
+
| `links` | `array` | 创建的链接列表 |
|
|
269
|
+
|
|
270
|
+
**示例:**
|
|
271
|
+
|
|
272
|
+
```json
|
|
273
|
+
// 输入
|
|
274
|
+
{
|
|
275
|
+
"links": [
|
|
276
|
+
{ "from": "人工智能", "to": "机器学习", "type": "包含" },
|
|
277
|
+
{ "from": "机器学习", "to": "深度学习", "type": "包含" }
|
|
278
|
+
]
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
// 输出
|
|
282
|
+
{
|
|
283
|
+
"added": 2,
|
|
284
|
+
"links": [
|
|
285
|
+
{ "from": "人工智能", "to": "机器学习", "type": "包含" },
|
|
286
|
+
{ "from": "机器学习", "to": "深度学习", "type": "包含" }
|
|
287
|
+
]
|
|
288
|
+
}
|
|
289
|
+
```
|
|
290
|
+
|
|
291
|
+
---
|
|
292
|
+
|
|
293
|
+
#### 6. `kg_link_edit` - 编辑链接
|
|
294
|
+
|
|
295
|
+
修改现有链接的类型。
|
|
296
|
+
|
|
297
|
+
**输入参数:**
|
|
298
|
+
|
|
299
|
+
| 参数 | 类型 | 必填 | 说明 |
|
|
300
|
+
|------|------|------|------|
|
|
301
|
+
| `links` | `array` | ✅ | 链接编辑数组 |
|
|
302
|
+
| `links[].from` | `string` | ✅ | 起始节点名称 |
|
|
303
|
+
| `links[].to` | `string` | ✅ | 目标节点名称 |
|
|
304
|
+
| `links[].old` | `string` | ✅ | 原关系类型 |
|
|
305
|
+
| `links[].new` | `string` | ✅ | 新关系类型 |
|
|
306
|
+
|
|
307
|
+
**输出参数:**
|
|
308
|
+
|
|
309
|
+
| 参数 | 类型 | 说明 |
|
|
310
|
+
|------|------|------|
|
|
311
|
+
| `updated` | `number` | 更新的链接数量 |
|
|
312
|
+
| `links` | `array` | 更新后的链接列表 |
|
|
313
|
+
|
|
314
|
+
**示例:**
|
|
315
|
+
|
|
316
|
+
```json
|
|
317
|
+
// 输入
|
|
318
|
+
{
|
|
319
|
+
"links": [
|
|
320
|
+
{ "from": "人工智能", "to": "自然语言处理", "old": "相关", "new": "包含" }
|
|
321
|
+
]
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
// 输出
|
|
325
|
+
{
|
|
326
|
+
"updated": 1,
|
|
327
|
+
"links": [
|
|
328
|
+
{ "from": "人工智能", "to": "自然语言处理", "type": "包含" }
|
|
329
|
+
]
|
|
330
|
+
}
|
|
331
|
+
```
|
|
332
|
+
|
|
333
|
+
---
|
|
334
|
+
|
|
335
|
+
#### 7. `kg_link_del` - 删除链接
|
|
336
|
+
|
|
337
|
+
删除指定的链接。
|
|
338
|
+
|
|
339
|
+
**输入参数:**
|
|
340
|
+
|
|
341
|
+
| 参数 | 类型 | 必填 | 说明 |
|
|
342
|
+
|------|------|------|------|
|
|
343
|
+
| `links` | `array` | ✅ | 要删除的链接数组 |
|
|
344
|
+
| `links[].from` | `string` | ✅ | 起始节点名称 |
|
|
345
|
+
| `links[].to` | `string` | ✅ | 目标节点名称 |
|
|
346
|
+
| `links[].type` | `string` | ✅ | 关系类型 |
|
|
347
|
+
|
|
348
|
+
**输出参数:**
|
|
349
|
+
|
|
350
|
+
| 参数 | 类型 | 说明 |
|
|
351
|
+
|------|------|------|
|
|
352
|
+
| `deleted` | `number` | 删除的链接数量 |
|
|
353
|
+
| `links` | `array` | 被删除的链接列表 |
|
|
354
|
+
|
|
355
|
+
**示例:**
|
|
356
|
+
|
|
357
|
+
```json
|
|
358
|
+
// 输入
|
|
359
|
+
{
|
|
360
|
+
"links": [
|
|
361
|
+
{ "from": "人工智能", "to": "机器学习", "type": "包含" }
|
|
362
|
+
]
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
// 输出
|
|
366
|
+
{
|
|
367
|
+
"deleted": 1,
|
|
368
|
+
"links": [
|
|
369
|
+
{ "from": "人工智能", "to": "机器学习", "type": "包含" }
|
|
370
|
+
]
|
|
371
|
+
}
|
|
372
|
+
```
|
|
373
|
+
|
|
374
|
+
---
|
|
375
|
+
|
|
376
|
+
### 三、事实管理
|
|
377
|
+
|
|
378
|
+
#### 8. `kg_fact_add` - 添加事实
|
|
379
|
+
|
|
380
|
+
为现有节点添加事实。
|
|
381
|
+
|
|
382
|
+
**输入参数:**
|
|
383
|
+
|
|
384
|
+
| 参数 | 类型 | 必填 | 说明 |
|
|
385
|
+
|------|------|------|------|
|
|
386
|
+
| `facts` | `array` | ✅ | 事实数组 |
|
|
387
|
+
| `facts[].node` | `string` | ✅ | 节点名称 |
|
|
388
|
+
| `facts[].contents` | `string[]` | ✅ | 事实内容数组 |
|
|
389
|
+
|
|
390
|
+
**输出参数:**
|
|
391
|
+
|
|
392
|
+
| 参数 | 类型 | 说明 |
|
|
393
|
+
|------|------|------|
|
|
394
|
+
| `added` | `number` | 创建的事实数量 |
|
|
395
|
+
| `facts` | `array` | 创建的事实列表(按节点分组) |
|
|
396
|
+
|
|
397
|
+
**示例:**
|
|
398
|
+
|
|
399
|
+
```json
|
|
400
|
+
// 输入
|
|
401
|
+
{
|
|
402
|
+
"facts": [
|
|
403
|
+
{ "node": "人工智能", "contents": ["由图灵测试提出", "始于 1950 年代"] },
|
|
404
|
+
{ "node": "机器学习", "contents": ["使用统计学习方法"] }
|
|
405
|
+
]
|
|
406
|
+
}
|
|
407
|
+
|
|
408
|
+
// 输出
|
|
409
|
+
{
|
|
410
|
+
"added": 3,
|
|
411
|
+
"facts": [
|
|
412
|
+
{ "node": "人工智能", "contents": ["由图灵测试提出", "始于 1950 年代"] },
|
|
413
|
+
{ "node": "机器学习", "contents": ["使用统计学习方法"] }
|
|
414
|
+
]
|
|
415
|
+
}
|
|
416
|
+
```
|
|
417
|
+
|
|
418
|
+
---
|
|
419
|
+
|
|
420
|
+
#### 9. `kg_fact_edit` - 编辑事实
|
|
421
|
+
|
|
422
|
+
修改现有事实的内容。
|
|
423
|
+
|
|
424
|
+
**输入参数:**
|
|
425
|
+
|
|
426
|
+
| 参数 | 类型 | 必填 | 说明 |
|
|
427
|
+
|------|------|------|------|
|
|
428
|
+
| `facts` | `array` | ✅ | 事实编辑数组 |
|
|
429
|
+
| `facts[].node` | `string` | ✅ | 节点名称 |
|
|
430
|
+
| `facts[].old` | `string` | ✅ | 原事实内容(必须精确匹配) |
|
|
431
|
+
| `facts[].new` | `string` | ✅ | 新事实内容 |
|
|
432
|
+
|
|
433
|
+
**输出参数:**
|
|
434
|
+
|
|
435
|
+
| 参数 | 类型 | 说明 |
|
|
436
|
+
|------|------|------|
|
|
437
|
+
| `updated` | `number` | 更新的事实数量 |
|
|
438
|
+
| `facts` | `array` | 更新后的事实列表 |
|
|
439
|
+
|
|
440
|
+
**示例:**
|
|
441
|
+
|
|
442
|
+
```json
|
|
443
|
+
// 输入
|
|
444
|
+
{
|
|
445
|
+
"facts": [
|
|
446
|
+
{ "node": "人工智能", "old": "始于 1950 年代", "new": "正式诞生于 1956 年达特茅斯会议" }
|
|
447
|
+
]
|
|
448
|
+
}
|
|
449
|
+
|
|
450
|
+
// 输出
|
|
451
|
+
{
|
|
452
|
+
"updated": 1,
|
|
453
|
+
"facts": [
|
|
454
|
+
{ "node": "人工智能", "content": "正式诞生于 1956 年达特茅斯会议" }
|
|
455
|
+
]
|
|
456
|
+
}
|
|
457
|
+
```
|
|
458
|
+
|
|
459
|
+
---
|
|
460
|
+
|
|
461
|
+
#### 10. `kg_fact_del` - 删除事实
|
|
462
|
+
|
|
463
|
+
删除节点的事实记录。
|
|
464
|
+
|
|
465
|
+
**输入参数:**
|
|
466
|
+
|
|
467
|
+
| 参数 | 类型 | 必填 | 说明 |
|
|
468
|
+
|------|------|------|------|
|
|
469
|
+
| `facts` | `array` | ✅ | 要删除的事实数组 |
|
|
470
|
+
| `facts[].node` | `string` | ✅ | 节点名称 |
|
|
471
|
+
| `facts[].contents` | `string[]` | ✅ | 要删除的事实内容数组 |
|
|
472
|
+
|
|
473
|
+
**输出参数:**
|
|
474
|
+
|
|
475
|
+
| 参数 | 类型 | 说明 |
|
|
476
|
+
|------|------|------|
|
|
477
|
+
| `deleted` | `number` | 删除的事实数量 |
|
|
478
|
+
| `facts` | `array` | 被删除的事实列表 |
|
|
479
|
+
|
|
480
|
+
**示例:**
|
|
481
|
+
|
|
482
|
+
```json
|
|
483
|
+
// 输入
|
|
484
|
+
{
|
|
485
|
+
"facts": [
|
|
486
|
+
{ "node": "人工智能", "contents": ["由图灵测试提出"] }
|
|
487
|
+
]
|
|
488
|
+
}
|
|
489
|
+
|
|
490
|
+
// 输出
|
|
491
|
+
{
|
|
492
|
+
"deleted": 1,
|
|
493
|
+
"facts": [
|
|
494
|
+
{ "node": "人工智能", "content": "由图灵测试提出" }
|
|
495
|
+
]
|
|
496
|
+
}
|
|
497
|
+
```
|
|
498
|
+
|
|
499
|
+
---
|
|
500
|
+
|
|
501
|
+
### 四、查询工具
|
|
502
|
+
|
|
503
|
+
#### 11. `kg_list` - 列出所有数据
|
|
504
|
+
|
|
505
|
+
列出知识图谱中的所有节点和链接。支持分页,链接会补全另一端的节点名称。
|
|
506
|
+
|
|
507
|
+
**输入参数:**
|
|
508
|
+
|
|
509
|
+
| 参数 | 类型 | 必填 | 说明 |
|
|
510
|
+
|------|------|------|------|
|
|
511
|
+
| `page` | `boolean` | ✅ | 是否启用分页 |
|
|
512
|
+
| `pageNum` | `number` | ❌ | 页码(默认 1) |
|
|
513
|
+
| `pageSize` | `number` | ❌ | 每页数量(默认 10) |
|
|
514
|
+
|
|
515
|
+
**输出参数:**
|
|
516
|
+
|
|
517
|
+
| 参数 | 类型 | 说明 |
|
|
518
|
+
|------|------|------|
|
|
519
|
+
| `nodes` | `array` | 节点列表 |
|
|
520
|
+
| `links` | `array` | 链接列表 |
|
|
521
|
+
| `pagination` | `object` | 分页信息(仅当启用分页时) |
|
|
522
|
+
|
|
523
|
+
**示例:**
|
|
524
|
+
|
|
525
|
+
```json
|
|
526
|
+
// 输入 - 不分页
|
|
527
|
+
{ "page": false }
|
|
528
|
+
|
|
529
|
+
// 输出
|
|
530
|
+
{
|
|
531
|
+
"nodes": [
|
|
532
|
+
{ "name": "人工智能", "type": "概念", "facts": ["是计算机科学的一个分支"] },
|
|
533
|
+
{ "name": "机器学习", "type": "技术", "facts": ["是 AI 的子领域"] }
|
|
534
|
+
],
|
|
535
|
+
"links": [
|
|
536
|
+
{ "from": "人工智能", "to": "机器学习", "type": "包含" }
|
|
537
|
+
]
|
|
538
|
+
}
|
|
539
|
+
|
|
540
|
+
// 输入 - 分页
|
|
541
|
+
{ "page": true, "pageNum": 1, "pageSize": 10 }
|
|
542
|
+
|
|
543
|
+
// 输出
|
|
544
|
+
{
|
|
545
|
+
"nodes": [...],
|
|
546
|
+
"links": [...],
|
|
547
|
+
"pagination": {
|
|
548
|
+
"page": 1,
|
|
549
|
+
"pageSize": 10,
|
|
550
|
+
"total": 25,
|
|
551
|
+
"pages": 3,
|
|
552
|
+
"hasNext": true,
|
|
553
|
+
"hasPrev": false
|
|
554
|
+
}
|
|
555
|
+
}
|
|
556
|
+
```
|
|
557
|
+
|
|
558
|
+
---
|
|
559
|
+
|
|
560
|
+
#### 12. `kg_search` - 搜索节点
|
|
561
|
+
|
|
562
|
+
按关键词搜索节点。默认搜索节点名称、类型和事实内容。
|
|
563
|
+
|
|
564
|
+
**输入参数:**
|
|
565
|
+
|
|
566
|
+
| 参数 | 类型 | 必填 | 说明 |
|
|
567
|
+
|------|------|------|------|
|
|
568
|
+
| `query` | `string[]` | ✅ | 搜索关键词数组 |
|
|
569
|
+
| `page` | `boolean` | ✅ | 是否启用分页 |
|
|
570
|
+
| `pageNum` | `number` | ❌ | 页码(默认 1) |
|
|
571
|
+
| `pageSize` | `number` | ❌ | 每页数量(默认 10) |
|
|
572
|
+
| `logic` | `string` | ❌ | 搜索逻辑:`or`(或)/ `and`(与),默认 `or` |
|
|
573
|
+
| `fields` | `string[]` | ❌ | 搜索字段范围,默认 `["name", "type", "fact"]` |
|
|
574
|
+
|
|
575
|
+
**搜索字段说明:**
|
|
576
|
+
|
|
577
|
+
| 值 | 说明 |
|
|
578
|
+
|---|------|
|
|
579
|
+
| `name` | 在节点名称中搜索 |
|
|
580
|
+
| `type` | 在节点类型中搜索 |
|
|
581
|
+
| `fact` | 在事实内容中搜索 |
|
|
582
|
+
| `link` | 在链接类型中搜索(返回有该类型链接的节点) |
|
|
583
|
+
|
|
584
|
+
**输出参数:**
|
|
585
|
+
|
|
586
|
+
| 参数 | 类型 | 说明 |
|
|
587
|
+
|------|------|------|
|
|
588
|
+
| `nodes` | `array` | 匹配的节点列表 |
|
|
589
|
+
| `links` | `array` | 匹配节点相关的链接列表 |
|
|
590
|
+
| `pagination` | `object` | 分页信息(仅当启用分页时) |
|
|
591
|
+
|
|
592
|
+
**示例:**
|
|
593
|
+
|
|
594
|
+
```json
|
|
595
|
+
// 示例 1: OR 逻辑搜索(默认)
|
|
596
|
+
// 输入
|
|
597
|
+
{
|
|
598
|
+
"query": ["人工", "学习"],
|
|
599
|
+
"page": false
|
|
600
|
+
}
|
|
601
|
+
// 返回:节点名称、类型或事实中包含"人工"或"学习"的节点
|
|
602
|
+
|
|
603
|
+
// 示例 2: AND 逻辑搜索
|
|
604
|
+
// 输入
|
|
605
|
+
{
|
|
606
|
+
"query": ["工程师", "Python"],
|
|
607
|
+
"logic": "and",
|
|
608
|
+
"page": false
|
|
609
|
+
}
|
|
610
|
+
// 返回:同时包含"工程师"和"Python"的节点
|
|
611
|
+
|
|
612
|
+
// 示例 3: 指定搜索字段
|
|
613
|
+
// 输入
|
|
614
|
+
{
|
|
615
|
+
"query": ["works_at"],
|
|
616
|
+
"fields": ["link"],
|
|
617
|
+
"page": false
|
|
618
|
+
}
|
|
619
|
+
// 返回:有 works_at 链接关系的节点
|
|
620
|
+
|
|
621
|
+
// 示例 4: 分页搜索
|
|
622
|
+
// 输入
|
|
623
|
+
{
|
|
624
|
+
"query": ["工程师"],
|
|
625
|
+
"fields": ["fact"],
|
|
626
|
+
"page": true,
|
|
627
|
+
"pageNum": 1,
|
|
628
|
+
"pageSize": 10
|
|
629
|
+
}
|
|
630
|
+
// 输出
|
|
631
|
+
{
|
|
632
|
+
"nodes": [...],
|
|
633
|
+
"links": [...],
|
|
634
|
+
"pagination": {
|
|
635
|
+
"page": 1,
|
|
636
|
+
"pageSize": 10,
|
|
637
|
+
"total": 25,
|
|
638
|
+
"pages": 3,
|
|
639
|
+
"hasNext": true,
|
|
640
|
+
"hasPrev": false
|
|
641
|
+
}
|
|
642
|
+
}
|
|
643
|
+
```
|
|
644
|
+
|
|
645
|
+
---
|
|
646
|
+
|
|
647
|
+
## 项目结构
|
|
648
|
+
|
|
649
|
+
```
|
|
650
|
+
mcp-knowledge-graph-plus/
|
|
651
|
+
├── src/
|
|
652
|
+
│ ├── index.ts # 入口文件
|
|
653
|
+
│ ├── application/ # 应用层(业务逻辑)
|
|
654
|
+
│ │ └── services/ # 领域服务
|
|
655
|
+
│ ├── core/ # 核心层(配置、类型、错误)
|
|
656
|
+
│ │ ├── config/ # 配置管理
|
|
657
|
+
│ │ ├── errors/ # 错误定义
|
|
658
|
+
│ │ └── types/ # 类型定义
|
|
659
|
+
│ ├── infrastructure/ # 基础设施层
|
|
660
|
+
│ │ ├── database/ # 数据库管理
|
|
661
|
+
│ │ ├── di/ # 依赖注入容器
|
|
662
|
+
│ │ └── repositories/ # 数据仓库
|
|
663
|
+
│ └── interfaces/ # 接口层
|
|
664
|
+
│ └── mcp/ # MCP 接口实现
|
|
665
|
+
│ └── commands/ # 命令实现
|
|
666
|
+
├── tests/ # 测试文件
|
|
667
|
+
├── dist/ # 编译输出
|
|
668
|
+
├── package.json
|
|
669
|
+
├── tsconfig.json
|
|
670
|
+
└── README.md
|
|
671
|
+
```
|
|
672
|
+
|
|
673
|
+
## 开发
|
|
674
|
+
|
|
675
|
+
```bash
|
|
676
|
+
# 构建
|
|
677
|
+
npm run build
|
|
678
|
+
|
|
679
|
+
# 运行测试
|
|
680
|
+
npm test
|
|
681
|
+
```
|
|
682
|
+
|
|
683
|
+
## 架构设计
|
|
684
|
+
|
|
685
|
+
本项目采用 **整洁架构 (Clean Architecture)** 设计:
|
|
686
|
+
|
|
687
|
+
- **接口层 (Interfaces)** - MCP 协议适配器和命令处理
|
|
688
|
+
- **应用层 (Application)** - 领域服务和业务逻辑
|
|
689
|
+
- **核心层 (Core)** - 类型定义、配置和错误处理
|
|
690
|
+
- **基础设施层 (Infrastructure)** - 数据库、依赖注入和数据持久化
|
|
691
|
+
|
|
692
|
+
### 数据模型
|
|
693
|
+
|
|
694
|
+
- **实体 (Entities)** - 知识节点,包含名称和类型
|
|
695
|
+
- **关系 (Relations)** - 节点间的有向链接,包含关系类型
|
|
696
|
+
- **事实 (Facts)** - 节点的附加事实和描述
|
|
697
|
+
|
|
698
|
+
## 许可证
|
|
699
|
+
|
|
700
|
+
MIT License
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Fact Service
|
|
3
|
+
*/
|
|
4
|
+
import type { Fact, CreateFactInput, UpdateFactInput } from '../../core/types/index.js';
|
|
5
|
+
import type { FactRepository } from '../../infrastructure/repositories/FactRepository.js';
|
|
6
|
+
import type { NodeRepository } from '../../infrastructure/repositories/NodeRepository.js';
|
|
7
|
+
export declare class FactService {
|
|
8
|
+
private readonly factRepo;
|
|
9
|
+
private readonly nodeRepo;
|
|
10
|
+
constructor(factRepo: FactRepository, nodeRepo: NodeRepository);
|
|
11
|
+
createFacts(inputs: readonly CreateFactInput[]): Promise<readonly Fact[]>;
|
|
12
|
+
deleteFactsByContent(nodeName: string, contents: readonly string[]): Promise<number>;
|
|
13
|
+
updateFactsByContent(updates: readonly UpdateFactInput[]): Promise<readonly Fact[]>;
|
|
14
|
+
getNodeNameMap(entityIds: readonly number[]): Promise<Map<number, string>>;
|
|
15
|
+
}
|
|
16
|
+
//# sourceMappingURL=FactService.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"FactService.d.ts","sourceRoot":"","sources":["../../../src/application/services/FactService.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,OAAO,KAAK,EAAE,IAAI,EAAE,eAAe,EAAE,eAAe,EAAE,MAAM,2BAA2B,CAAC;AACxF,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,qDAAqD,CAAC;AAC1F,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,qDAAqD,CAAC;AAE1F,qBAAa,WAAW;IAEpB,OAAO,CAAC,QAAQ,CAAC,QAAQ;IACzB,OAAO,CAAC,QAAQ,CAAC,QAAQ;gBADR,QAAQ,EAAE,cAAc,EACxB,QAAQ,EAAE,cAAc;IAGrC,WAAW,CAAC,MAAM,EAAE,SAAS,eAAe,EAAE,GAAG,OAAO,CAAC,SAAS,IAAI,EAAE,CAAC;IAazE,oBAAoB,CAAC,QAAQ,EAAE,MAAM,EAAE,QAAQ,EAAE,SAAS,MAAM,EAAE,GAAG,OAAO,CAAC,MAAM,CAAC;IAMpF,oBAAoB,CAAC,OAAO,EAAE,SAAS,eAAe,EAAE,GAAG,OAAO,CAAC,SAAS,IAAI,EAAE,CAAC;IAgBnF,cAAc,CAAC,SAAS,EAAE,SAAS,MAAM,EAAE,GAAG,OAAO,CAAC,GAAG,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;CAIjF"}
|