@levalicious/server-memory 0.0.4 → 0.0.6
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/dist/index.js +5 -782
- package/dist/server.js +827 -0
- package/dist/tests/memory-server.test.js +438 -0
- package/dist/tests/test-utils.js +37 -0
- package/package.json +7 -3
package/dist/index.js
CHANGED
|
@@ -1,786 +1,9 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
import { Server } from "@modelcontextprotocol/sdk/server/index.js";
|
|
3
2
|
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
|
|
4
|
-
import {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
const defaultMemoryPath = path.join(path.dirname(fileURLToPath(import.meta.url)), 'memory.json');
|
|
10
|
-
// If MEMORY_FILE_PATH is just a filename, put it in the same directory as the script
|
|
11
|
-
const MEMORY_FILE_PATH = process.env.MEMORY_FILE_PATH
|
|
12
|
-
? path.isAbsolute(process.env.MEMORY_FILE_PATH)
|
|
13
|
-
? process.env.MEMORY_FILE_PATH
|
|
14
|
-
: path.join(path.dirname(fileURLToPath(import.meta.url)), process.env.MEMORY_FILE_PATH)
|
|
15
|
-
: defaultMemoryPath;
|
|
16
|
-
// The KnowledgeGraphManager class contains all operations to interact with the knowledge graph
|
|
17
|
-
class KnowledgeGraphManager {
|
|
18
|
-
bclCtr = 0;
|
|
19
|
-
bclTerm = "";
|
|
20
|
-
async loadGraph() {
|
|
21
|
-
try {
|
|
22
|
-
const data = await fs.readFile(MEMORY_FILE_PATH, "utf-8");
|
|
23
|
-
const lines = data.split("\n").filter(line => line.trim() !== "");
|
|
24
|
-
return lines.reduce((graph, line) => {
|
|
25
|
-
const item = JSON.parse(line);
|
|
26
|
-
if (item.type === "entity")
|
|
27
|
-
graph.entities.push(item);
|
|
28
|
-
if (item.type === "relation")
|
|
29
|
-
graph.relations.push(item);
|
|
30
|
-
return graph;
|
|
31
|
-
}, { entities: [], relations: [] });
|
|
32
|
-
}
|
|
33
|
-
catch (error) {
|
|
34
|
-
if (error instanceof Error && 'code' in error && error.code === "ENOENT") {
|
|
35
|
-
return { entities: [], relations: [] };
|
|
36
|
-
}
|
|
37
|
-
throw error;
|
|
38
|
-
}
|
|
39
|
-
}
|
|
40
|
-
async saveGraph(graph) {
|
|
41
|
-
const lines = [
|
|
42
|
-
...graph.entities.map(e => JSON.stringify({ type: "entity", ...e })),
|
|
43
|
-
...graph.relations.map(r => JSON.stringify({ type: "relation", ...r })),
|
|
44
|
-
];
|
|
45
|
-
await fs.writeFile(MEMORY_FILE_PATH, lines.join("\n"));
|
|
46
|
-
}
|
|
47
|
-
async createEntities(entities) {
|
|
48
|
-
const graph = await this.loadGraph();
|
|
49
|
-
const newEntities = entities.filter(e => !graph.entities.some(existingEntity => existingEntity.name === e.name));
|
|
50
|
-
graph.entities.push(...newEntities);
|
|
51
|
-
await this.saveGraph(graph);
|
|
52
|
-
return newEntities;
|
|
53
|
-
}
|
|
54
|
-
async createRelations(relations) {
|
|
55
|
-
const graph = await this.loadGraph();
|
|
56
|
-
const newRelations = relations.filter(r => !graph.relations.some(existingRelation => existingRelation.from === r.from &&
|
|
57
|
-
existingRelation.to === r.to &&
|
|
58
|
-
existingRelation.relationType === r.relationType));
|
|
59
|
-
graph.relations.push(...newRelations);
|
|
60
|
-
await this.saveGraph(graph);
|
|
61
|
-
return newRelations;
|
|
62
|
-
}
|
|
63
|
-
async addObservations(observations) {
|
|
64
|
-
const graph = await this.loadGraph();
|
|
65
|
-
const results = observations.map(o => {
|
|
66
|
-
const entity = graph.entities.find(e => e.name === o.entityName);
|
|
67
|
-
if (!entity) {
|
|
68
|
-
throw new Error(`Entity with name ${o.entityName} not found`);
|
|
69
|
-
}
|
|
70
|
-
const newObservations = o.contents.filter(content => !entity.observations.includes(content));
|
|
71
|
-
entity.observations.push(...newObservations);
|
|
72
|
-
return { entityName: o.entityName, addedObservations: newObservations };
|
|
73
|
-
});
|
|
74
|
-
await this.saveGraph(graph);
|
|
75
|
-
return results;
|
|
76
|
-
}
|
|
77
|
-
async deleteEntities(entityNames) {
|
|
78
|
-
const graph = await this.loadGraph();
|
|
79
|
-
graph.entities = graph.entities.filter(e => !entityNames.includes(e.name));
|
|
80
|
-
graph.relations = graph.relations.filter(r => !entityNames.includes(r.from) && !entityNames.includes(r.to));
|
|
81
|
-
await this.saveGraph(graph);
|
|
82
|
-
}
|
|
83
|
-
async deleteObservations(deletions) {
|
|
84
|
-
const graph = await this.loadGraph();
|
|
85
|
-
deletions.forEach(d => {
|
|
86
|
-
const entity = graph.entities.find(e => e.name === d.entityName);
|
|
87
|
-
if (entity) {
|
|
88
|
-
entity.observations = entity.observations.filter(o => !d.observations.includes(o));
|
|
89
|
-
}
|
|
90
|
-
});
|
|
91
|
-
await this.saveGraph(graph);
|
|
92
|
-
}
|
|
93
|
-
async deleteRelations(relations) {
|
|
94
|
-
const graph = await this.loadGraph();
|
|
95
|
-
graph.relations = graph.relations.filter(r => !relations.some(delRelation => r.from === delRelation.from &&
|
|
96
|
-
r.to === delRelation.to &&
|
|
97
|
-
r.relationType === delRelation.relationType));
|
|
98
|
-
await this.saveGraph(graph);
|
|
99
|
-
}
|
|
100
|
-
async readGraph() {
|
|
101
|
-
return this.loadGraph();
|
|
102
|
-
}
|
|
103
|
-
// Very basic search function
|
|
104
|
-
async searchNodes(query) {
|
|
105
|
-
const graph = await this.loadGraph();
|
|
106
|
-
// Filter entities
|
|
107
|
-
const filteredEntities = graph.entities.filter(e => e.name.toLowerCase().includes(query.toLowerCase()) ||
|
|
108
|
-
e.entityType.toLowerCase().includes(query.toLowerCase()) ||
|
|
109
|
-
e.observations.some(o => o.toLowerCase().includes(query.toLowerCase())));
|
|
110
|
-
// Create a Set of filtered entity names for quick lookup
|
|
111
|
-
const filteredEntityNames = new Set(filteredEntities.map(e => e.name));
|
|
112
|
-
// Filter relations to only include those between filtered entities
|
|
113
|
-
const filteredRelations = graph.relations.filter(r => filteredEntityNames.has(r.from) && filteredEntityNames.has(r.to));
|
|
114
|
-
const filteredGraph = {
|
|
115
|
-
entities: filteredEntities,
|
|
116
|
-
relations: filteredRelations,
|
|
117
|
-
};
|
|
118
|
-
return filteredGraph;
|
|
119
|
-
}
|
|
120
|
-
async openNodesFiltered(names) {
|
|
121
|
-
const graph = await this.loadGraph();
|
|
122
|
-
// Filter entities
|
|
123
|
-
const filteredEntities = graph.entities.filter(e => names.includes(e.name));
|
|
124
|
-
// Create a Set of filtered entity names for quick lookup
|
|
125
|
-
const filteredEntityNames = new Set(filteredEntities.map(e => e.name));
|
|
126
|
-
// Filter relations to only include those between filtered entities
|
|
127
|
-
const filteredRelations = graph.relations.filter(r => filteredEntityNames.has(r.from) && filteredEntityNames.has(r.to));
|
|
128
|
-
const filteredGraph = {
|
|
129
|
-
entities: filteredEntities,
|
|
130
|
-
relations: filteredRelations,
|
|
131
|
-
};
|
|
132
|
-
return filteredGraph;
|
|
133
|
-
}
|
|
134
|
-
async openNodes(names) {
|
|
135
|
-
const graph = await this.loadGraph();
|
|
136
|
-
// Filter entities
|
|
137
|
-
const filteredEntities = graph.entities.filter(e => names.includes(e.name));
|
|
138
|
-
// Create a Set of filtered entity names for quick lookup
|
|
139
|
-
const filteredEntityNames = new Set(filteredEntities.map(e => e.name));
|
|
140
|
-
// Filter relations to only include those between filtered entities
|
|
141
|
-
const filteredRelations = graph.relations.filter(r => filteredEntityNames.has(r.from));
|
|
142
|
-
const filteredGraph = {
|
|
143
|
-
entities: filteredEntities,
|
|
144
|
-
relations: filteredRelations,
|
|
145
|
-
};
|
|
146
|
-
return filteredGraph;
|
|
147
|
-
}
|
|
148
|
-
async getNeighbors(entityName, depth = 1) {
|
|
149
|
-
const graph = await this.loadGraph();
|
|
150
|
-
const visited = new Set();
|
|
151
|
-
const resultEntities = new Map();
|
|
152
|
-
const resultRelations = [];
|
|
153
|
-
const traverse = (currentName, currentDepth) => {
|
|
154
|
-
if (currentDepth > depth || visited.has(currentName))
|
|
155
|
-
return;
|
|
156
|
-
visited.add(currentName);
|
|
157
|
-
const entity = graph.entities.find(e => e.name === currentName);
|
|
158
|
-
if (entity) {
|
|
159
|
-
resultEntities.set(currentName, entity);
|
|
160
|
-
}
|
|
161
|
-
// Find all relations involving this entity
|
|
162
|
-
const connectedRelations = graph.relations.filter(r => r.from === currentName || r.to === currentName);
|
|
163
|
-
resultRelations.push(...connectedRelations);
|
|
164
|
-
if (currentDepth < depth) {
|
|
165
|
-
// Traverse to connected entities
|
|
166
|
-
connectedRelations.forEach(r => {
|
|
167
|
-
const nextEntity = r.from === currentName ? r.to : r.from;
|
|
168
|
-
traverse(nextEntity, currentDepth + 1);
|
|
169
|
-
});
|
|
170
|
-
}
|
|
171
|
-
};
|
|
172
|
-
traverse(entityName, 0);
|
|
173
|
-
return {
|
|
174
|
-
entities: Array.from(resultEntities.values()),
|
|
175
|
-
relations: resultRelations
|
|
176
|
-
};
|
|
177
|
-
}
|
|
178
|
-
async findPath(fromEntity, toEntity, maxDepth = 5) {
|
|
179
|
-
const graph = await this.loadGraph();
|
|
180
|
-
const visited = new Set();
|
|
181
|
-
const dfs = (current, target, path, depth) => {
|
|
182
|
-
if (depth > maxDepth || visited.has(current))
|
|
183
|
-
return null;
|
|
184
|
-
if (current === target)
|
|
185
|
-
return path;
|
|
186
|
-
visited.add(current);
|
|
187
|
-
const outgoingRelations = graph.relations.filter(r => r.from === current);
|
|
188
|
-
for (const relation of outgoingRelations) {
|
|
189
|
-
const result = dfs(relation.to, target, [...path, relation], depth + 1);
|
|
190
|
-
if (result)
|
|
191
|
-
return result;
|
|
192
|
-
}
|
|
193
|
-
visited.delete(current);
|
|
194
|
-
return null;
|
|
195
|
-
};
|
|
196
|
-
return dfs(fromEntity, toEntity, [], 0) || [];
|
|
197
|
-
}
|
|
198
|
-
async getEntitiesByType(entityType) {
|
|
199
|
-
const graph = await this.loadGraph();
|
|
200
|
-
return graph.entities.filter(e => e.entityType === entityType);
|
|
201
|
-
}
|
|
202
|
-
async getEntityTypes() {
|
|
203
|
-
const graph = await this.loadGraph();
|
|
204
|
-
const types = new Set(graph.entities.map(e => e.entityType));
|
|
205
|
-
return Array.from(types).sort();
|
|
206
|
-
}
|
|
207
|
-
async getRelationTypes() {
|
|
208
|
-
const graph = await this.loadGraph();
|
|
209
|
-
const types = new Set(graph.relations.map(r => r.relationType));
|
|
210
|
-
return Array.from(types).sort();
|
|
211
|
-
}
|
|
212
|
-
async getStats() {
|
|
213
|
-
const graph = await this.loadGraph();
|
|
214
|
-
const entityTypes = new Set(graph.entities.map(e => e.entityType));
|
|
215
|
-
const relationTypes = new Set(graph.relations.map(r => r.relationType));
|
|
216
|
-
return {
|
|
217
|
-
entityCount: graph.entities.length,
|
|
218
|
-
relationCount: graph.relations.length,
|
|
219
|
-
entityTypes: entityTypes.size,
|
|
220
|
-
relationTypes: relationTypes.size
|
|
221
|
-
};
|
|
222
|
-
}
|
|
223
|
-
async getOrphanedEntities() {
|
|
224
|
-
const graph = await this.loadGraph();
|
|
225
|
-
const connectedEntityNames = new Set();
|
|
226
|
-
graph.relations.forEach(r => {
|
|
227
|
-
connectedEntityNames.add(r.from);
|
|
228
|
-
connectedEntityNames.add(r.to);
|
|
229
|
-
});
|
|
230
|
-
return graph.entities.filter(e => !connectedEntityNames.has(e.name));
|
|
231
|
-
}
|
|
232
|
-
async validateGraph() {
|
|
233
|
-
const graph = await this.loadGraph();
|
|
234
|
-
const entityNames = new Set(graph.entities.map(e => e.name));
|
|
235
|
-
const missingEntities = new Set();
|
|
236
|
-
graph.relations.forEach(r => {
|
|
237
|
-
if (!entityNames.has(r.from)) {
|
|
238
|
-
missingEntities.add(r.from);
|
|
239
|
-
}
|
|
240
|
-
if (!entityNames.has(r.to)) {
|
|
241
|
-
missingEntities.add(r.to);
|
|
242
|
-
}
|
|
243
|
-
});
|
|
244
|
-
return Array.from(missingEntities);
|
|
245
|
-
}
|
|
246
|
-
// BCL (Binary Combinatory Logic) evaluator
|
|
247
|
-
async evaluateBCL(program, maxSteps) {
|
|
248
|
-
let stepCount = 0;
|
|
249
|
-
let max_size = program.length;
|
|
250
|
-
let mode = 0;
|
|
251
|
-
let ctr = 1;
|
|
252
|
-
let t0 = program;
|
|
253
|
-
let t1 = '';
|
|
254
|
-
let t2 = '';
|
|
255
|
-
let t3 = '';
|
|
256
|
-
let t4 = '';
|
|
257
|
-
while (stepCount < maxSteps) {
|
|
258
|
-
if (t0.length == 0)
|
|
259
|
-
break;
|
|
260
|
-
let b = t0[0];
|
|
261
|
-
t0 = t0.slice(1);
|
|
262
|
-
if (mode === 0) {
|
|
263
|
-
t1 += b;
|
|
264
|
-
let size = t1.length + t0.length;
|
|
265
|
-
if (size > max_size)
|
|
266
|
-
max_size = size;
|
|
267
|
-
if (t1.slice(-4) === '1100') {
|
|
268
|
-
mode = 1;
|
|
269
|
-
t1 = t1.slice(0, -4);
|
|
270
|
-
}
|
|
271
|
-
else if (t1.slice(-5) === '11101') {
|
|
272
|
-
mode = 3;
|
|
273
|
-
t1 = t1.slice(0, -5);
|
|
274
|
-
}
|
|
275
|
-
}
|
|
276
|
-
else if (mode === 1) {
|
|
277
|
-
t2 += b;
|
|
278
|
-
if (b == '1') {
|
|
279
|
-
ctr += 1;
|
|
280
|
-
}
|
|
281
|
-
else if (b == '0') {
|
|
282
|
-
ctr -= 1;
|
|
283
|
-
t2 += t0[0];
|
|
284
|
-
t0 = t0.slice(1);
|
|
285
|
-
}
|
|
286
|
-
if (ctr === 0) {
|
|
287
|
-
mode = 2;
|
|
288
|
-
ctr = 1;
|
|
289
|
-
}
|
|
290
|
-
}
|
|
291
|
-
else if (mode === 2) {
|
|
292
|
-
if (b == '1') {
|
|
293
|
-
ctr += 1;
|
|
294
|
-
}
|
|
295
|
-
else if (b == '0') {
|
|
296
|
-
ctr -= 1;
|
|
297
|
-
t0 = t0.slice(1);
|
|
298
|
-
}
|
|
299
|
-
if (ctr === 0) {
|
|
300
|
-
t0 = t2 + t0;
|
|
301
|
-
t2 = '';
|
|
302
|
-
mode = 0;
|
|
303
|
-
ctr = 1;
|
|
304
|
-
stepCount += 1;
|
|
305
|
-
}
|
|
306
|
-
}
|
|
307
|
-
else if (mode === 3) {
|
|
308
|
-
t2 += b;
|
|
309
|
-
if (b == '1') {
|
|
310
|
-
ctr += 1;
|
|
311
|
-
}
|
|
312
|
-
else if (b == '0') {
|
|
313
|
-
ctr -= 1;
|
|
314
|
-
t2 += t0[0];
|
|
315
|
-
t0 = t0.slice(1);
|
|
316
|
-
}
|
|
317
|
-
if (ctr === 0) {
|
|
318
|
-
mode = 4;
|
|
319
|
-
ctr = 1;
|
|
320
|
-
}
|
|
321
|
-
}
|
|
322
|
-
else if (mode === 4) {
|
|
323
|
-
t3 += b;
|
|
324
|
-
if (b == '1') {
|
|
325
|
-
ctr += 1;
|
|
326
|
-
}
|
|
327
|
-
else if (b == '0') {
|
|
328
|
-
ctr -= 1;
|
|
329
|
-
t3 += t0[0];
|
|
330
|
-
t0 = t0.slice(1);
|
|
331
|
-
}
|
|
332
|
-
if (ctr === 0) {
|
|
333
|
-
mode = 5;
|
|
334
|
-
ctr = 1;
|
|
335
|
-
}
|
|
336
|
-
}
|
|
337
|
-
else if (mode === 5) {
|
|
338
|
-
t4 += b;
|
|
339
|
-
if (b == '1') {
|
|
340
|
-
ctr += 1;
|
|
341
|
-
}
|
|
342
|
-
else if (b == '0') {
|
|
343
|
-
ctr -= 1;
|
|
344
|
-
t4 += t0[0];
|
|
345
|
-
t0 = t0.slice(1);
|
|
346
|
-
}
|
|
347
|
-
if (ctr === 0) {
|
|
348
|
-
t0 = '11' + t2 + t4 + '1' + t3 + t4 + t0;
|
|
349
|
-
t2 = '';
|
|
350
|
-
t3 = '';
|
|
351
|
-
t4 = '';
|
|
352
|
-
mode = 0;
|
|
353
|
-
ctr = 1;
|
|
354
|
-
stepCount += 1;
|
|
355
|
-
}
|
|
356
|
-
}
|
|
357
|
-
}
|
|
358
|
-
const halted = stepCount < maxSteps;
|
|
359
|
-
return {
|
|
360
|
-
result: t1,
|
|
361
|
-
info: `${stepCount} steps, max size ${max_size}`,
|
|
362
|
-
halted,
|
|
363
|
-
errored: halted && mode != 0,
|
|
364
|
-
};
|
|
365
|
-
}
|
|
366
|
-
async addBCLTerm(term) {
|
|
367
|
-
const termset = ["1", "00", "01"];
|
|
368
|
-
if (!term || term.trim() === "") {
|
|
369
|
-
throw new Error("BCL term cannot be empty");
|
|
370
|
-
}
|
|
371
|
-
// Term can be 1, 00, 01, or K, S, App (application)
|
|
372
|
-
const validTerms = ["1", "App", "00", "K", "01", "S"];
|
|
373
|
-
if (!validTerms.includes(term)) {
|
|
374
|
-
throw new Error(`Invalid BCL term: ${term}\nExpected one of: ${validTerms.join(", ")}`);
|
|
375
|
-
}
|
|
376
|
-
let processedTerm = 0;
|
|
377
|
-
if (term === "00" || term === "K")
|
|
378
|
-
processedTerm = 1;
|
|
379
|
-
else if (term === "01" || term === "S")
|
|
380
|
-
processedTerm = 2;
|
|
381
|
-
this.bclTerm += termset[processedTerm];
|
|
382
|
-
if (processedTerm === 0) {
|
|
383
|
-
if (this.bclCtr === 0)
|
|
384
|
-
this.bclCtr += 1;
|
|
385
|
-
this.bclCtr += 1;
|
|
386
|
-
}
|
|
387
|
-
else {
|
|
388
|
-
this.bclCtr -= 1;
|
|
389
|
-
}
|
|
390
|
-
if (this.bclCtr <= 0) {
|
|
391
|
-
const constructedProgram = this.bclTerm;
|
|
392
|
-
this.bclCtr = 0;
|
|
393
|
-
this.bclTerm = "";
|
|
394
|
-
return `Constructed Program: ${constructedProgram}`;
|
|
395
|
-
}
|
|
396
|
-
else {
|
|
397
|
-
return `Need ${this.bclCtr} more term(s) to complete the program.`;
|
|
398
|
-
}
|
|
399
|
-
}
|
|
400
|
-
async clearBCLTerm() {
|
|
401
|
-
this.bclCtr = 0;
|
|
402
|
-
this.bclTerm = "";
|
|
403
|
-
}
|
|
404
|
-
}
|
|
405
|
-
const knowledgeGraphManager = new KnowledgeGraphManager();
|
|
406
|
-
// The server instance and tools exposed to Claude
|
|
407
|
-
const server = new Server({
|
|
408
|
-
name: "memory-server",
|
|
409
|
-
icons: [
|
|
410
|
-
{ src: "data:image/svg+xml;base64,PHN2ZyBmaWxsPSJjdXJyZW50Q29sb3IiIGZpbGwtcnVsZT0iZXZlbm9kZCIgaGVpZ2h0PSIxZW0iIHN0eWxlPSJmbGV4Om5vbmU7bGluZS1oZWlnaHQ6MSIgdmlld0JveD0iMCAwIDI0IDI0IiB3aWR0aD0iMWVtIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjx0aXRsZT5Nb2RlbENvbnRleHRQcm90b2NvbDwvdGl0bGU+PHBhdGggZD0iTTIwLjEgNS41MlYxLjVoLS4xOGMtMy4zNi4xNS02LjE1IDIuMzEtNy44MyA0LjAybC0uMDkuMDktLjA5LS4wOUMxMC4yIDMuODEgNy40NCAxLjY1IDQuMDggMS41SDMuOXY0LjAySDB2Ni45M2MwIDEuNjguMDYgMy4zNi4xOCA0Ljc0YTUuNTcgNS41NyAwIDAgMCA1LjE5IDUuMWMyLjEzLjEyIDQuMzguMjEgNi42My4yMXM0LjUtLjA5IDYuNjMtLjI0YTUuNTcgNS41NyAwIDAgMCA1LjE5LTUuMWMuMTItMS4zOC4xOC0zLjA2LjE4LTQuNzR2LTYuOXptMCA2LjkzYzAgMS41OS0uMDYgMy4xNS0uMTggNC40MS0uMDkuODEtLjc1IDEuNDctMS41NiAxLjVhOTAgOTAgMCAwIDEtMTIuNzIgMGMtLjgxLS4wMy0xLjUtLjY5LTEuNTYtMS41LS4xMi0xLjI2LS4xOC0yLjg1LS4xOC00LjQxVjUuNTJjMi44Mi4xMiA1LjY0IDMuMTUgNi40OCA0LjMyTDEyIDEyLjA5bDEuNjItMi4yNWMuODQtMS4yIDMuNjYtNC4yIDYuNDgtNC4zMnoiLz48L3N2Zz4=",
|
|
411
|
-
mimeType: "image/svg+xml",
|
|
412
|
-
sizes: ["any"]
|
|
413
|
-
}
|
|
414
|
-
],
|
|
415
|
-
version: "0.0.4",
|
|
416
|
-
}, {
|
|
417
|
-
capabilities: {
|
|
418
|
-
tools: {},
|
|
419
|
-
},
|
|
420
|
-
});
|
|
421
|
-
server.setRequestHandler(ListToolsRequestSchema, async () => {
|
|
422
|
-
return {
|
|
423
|
-
tools: [
|
|
424
|
-
{
|
|
425
|
-
name: "create_entities",
|
|
426
|
-
description: "Create multiple new entities in the knowledge graph",
|
|
427
|
-
inputSchema: {
|
|
428
|
-
type: "object",
|
|
429
|
-
properties: {
|
|
430
|
-
entities: {
|
|
431
|
-
type: "array",
|
|
432
|
-
items: {
|
|
433
|
-
type: "object",
|
|
434
|
-
properties: {
|
|
435
|
-
name: { type: "string", description: "The name of the entity" },
|
|
436
|
-
entityType: { type: "string", description: "The type of the entity" },
|
|
437
|
-
observations: {
|
|
438
|
-
type: "array",
|
|
439
|
-
items: { type: "string" },
|
|
440
|
-
description: "An array of observation contents associated with the entity"
|
|
441
|
-
},
|
|
442
|
-
},
|
|
443
|
-
required: ["name", "entityType", "observations"],
|
|
444
|
-
},
|
|
445
|
-
},
|
|
446
|
-
},
|
|
447
|
-
required: ["entities"],
|
|
448
|
-
},
|
|
449
|
-
},
|
|
450
|
-
{
|
|
451
|
-
name: "create_relations",
|
|
452
|
-
description: "Create multiple new relations between entities in the knowledge graph. Relations should be in active voice",
|
|
453
|
-
inputSchema: {
|
|
454
|
-
type: "object",
|
|
455
|
-
properties: {
|
|
456
|
-
relations: {
|
|
457
|
-
type: "array",
|
|
458
|
-
items: {
|
|
459
|
-
type: "object",
|
|
460
|
-
properties: {
|
|
461
|
-
from: { type: "string", description: "The name of the entity where the relation starts" },
|
|
462
|
-
to: { type: "string", description: "The name of the entity where the relation ends" },
|
|
463
|
-
relationType: { type: "string", description: "The type of the relation" },
|
|
464
|
-
},
|
|
465
|
-
required: ["from", "to", "relationType"],
|
|
466
|
-
},
|
|
467
|
-
},
|
|
468
|
-
},
|
|
469
|
-
required: ["relations"],
|
|
470
|
-
},
|
|
471
|
-
},
|
|
472
|
-
{
|
|
473
|
-
name: "add_observations",
|
|
474
|
-
description: "Add new observations to existing entities in the knowledge graph",
|
|
475
|
-
inputSchema: {
|
|
476
|
-
type: "object",
|
|
477
|
-
properties: {
|
|
478
|
-
observations: {
|
|
479
|
-
type: "array",
|
|
480
|
-
items: {
|
|
481
|
-
type: "object",
|
|
482
|
-
properties: {
|
|
483
|
-
entityName: { type: "string", description: "The name of the entity to add the observations to" },
|
|
484
|
-
contents: {
|
|
485
|
-
type: "array",
|
|
486
|
-
items: { type: "string" },
|
|
487
|
-
description: "An array of observation contents to add"
|
|
488
|
-
},
|
|
489
|
-
},
|
|
490
|
-
required: ["entityName", "contents"],
|
|
491
|
-
},
|
|
492
|
-
},
|
|
493
|
-
},
|
|
494
|
-
required: ["observations"],
|
|
495
|
-
},
|
|
496
|
-
},
|
|
497
|
-
{
|
|
498
|
-
name: "delete_entities",
|
|
499
|
-
description: "Delete multiple entities and their associated relations from the knowledge graph",
|
|
500
|
-
inputSchema: {
|
|
501
|
-
type: "object",
|
|
502
|
-
properties: {
|
|
503
|
-
entityNames: {
|
|
504
|
-
type: "array",
|
|
505
|
-
items: { type: "string" },
|
|
506
|
-
description: "An array of entity names to delete"
|
|
507
|
-
},
|
|
508
|
-
},
|
|
509
|
-
required: ["entityNames"],
|
|
510
|
-
},
|
|
511
|
-
},
|
|
512
|
-
{
|
|
513
|
-
name: "delete_observations",
|
|
514
|
-
description: "Delete specific observations from entities in the knowledge graph",
|
|
515
|
-
inputSchema: {
|
|
516
|
-
type: "object",
|
|
517
|
-
properties: {
|
|
518
|
-
deletions: {
|
|
519
|
-
type: "array",
|
|
520
|
-
items: {
|
|
521
|
-
type: "object",
|
|
522
|
-
properties: {
|
|
523
|
-
entityName: { type: "string", description: "The name of the entity containing the observations" },
|
|
524
|
-
observations: {
|
|
525
|
-
type: "array",
|
|
526
|
-
items: { type: "string" },
|
|
527
|
-
description: "An array of observations to delete"
|
|
528
|
-
},
|
|
529
|
-
},
|
|
530
|
-
required: ["entityName", "observations"],
|
|
531
|
-
},
|
|
532
|
-
},
|
|
533
|
-
},
|
|
534
|
-
required: ["deletions"],
|
|
535
|
-
},
|
|
536
|
-
},
|
|
537
|
-
{
|
|
538
|
-
name: "delete_relations",
|
|
539
|
-
description: "Delete multiple relations from the knowledge graph",
|
|
540
|
-
inputSchema: {
|
|
541
|
-
type: "object",
|
|
542
|
-
properties: {
|
|
543
|
-
relations: {
|
|
544
|
-
type: "array",
|
|
545
|
-
items: {
|
|
546
|
-
type: "object",
|
|
547
|
-
properties: {
|
|
548
|
-
from: { type: "string", description: "The name of the entity where the relation starts" },
|
|
549
|
-
to: { type: "string", description: "The name of the entity where the relation ends" },
|
|
550
|
-
relationType: { type: "string", description: "The type of the relation" },
|
|
551
|
-
},
|
|
552
|
-
required: ["from", "to", "relationType"],
|
|
553
|
-
},
|
|
554
|
-
description: "An array of relations to delete"
|
|
555
|
-
},
|
|
556
|
-
},
|
|
557
|
-
required: ["relations"],
|
|
558
|
-
},
|
|
559
|
-
},
|
|
560
|
-
{
|
|
561
|
-
name: "read_graph",
|
|
562
|
-
description: "Read the entire knowledge graph",
|
|
563
|
-
inputSchema: {
|
|
564
|
-
type: "object",
|
|
565
|
-
properties: {},
|
|
566
|
-
},
|
|
567
|
-
},
|
|
568
|
-
{
|
|
569
|
-
name: "search_nodes",
|
|
570
|
-
description: "Search for nodes in the knowledge graph based on a query",
|
|
571
|
-
inputSchema: {
|
|
572
|
-
type: "object",
|
|
573
|
-
properties: {
|
|
574
|
-
query: { type: "string", description: "The search query to match against entity names, types, and observation content" },
|
|
575
|
-
},
|
|
576
|
-
required: ["query"],
|
|
577
|
-
},
|
|
578
|
-
},
|
|
579
|
-
{
|
|
580
|
-
name: "open_nodes_filtered",
|
|
581
|
-
description: "Open specific nodes in the knowledge graph by their names, filtering relations to only those between the opened nodes",
|
|
582
|
-
inputSchema: {
|
|
583
|
-
type: "object",
|
|
584
|
-
properties: {
|
|
585
|
-
names: {
|
|
586
|
-
type: "array",
|
|
587
|
-
items: { type: "string" },
|
|
588
|
-
description: "An array of entity names to retrieve",
|
|
589
|
-
},
|
|
590
|
-
},
|
|
591
|
-
required: ["names"],
|
|
592
|
-
},
|
|
593
|
-
},
|
|
594
|
-
{
|
|
595
|
-
name: "open_nodes",
|
|
596
|
-
description: "Open specific nodes in the knowledge graph by their names",
|
|
597
|
-
inputSchema: {
|
|
598
|
-
type: "object",
|
|
599
|
-
properties: {
|
|
600
|
-
names: {
|
|
601
|
-
type: "array",
|
|
602
|
-
items: { type: "string" },
|
|
603
|
-
description: "An array of entity names to retrieve",
|
|
604
|
-
},
|
|
605
|
-
},
|
|
606
|
-
required: ["names"],
|
|
607
|
-
},
|
|
608
|
-
},
|
|
609
|
-
{
|
|
610
|
-
name: "get_neighbors",
|
|
611
|
-
description: "Get neighboring entities connected to a specific entity within a given depth",
|
|
612
|
-
inputSchema: {
|
|
613
|
-
type: "object",
|
|
614
|
-
properties: {
|
|
615
|
-
entityName: { type: "string", description: "The name of the entity to find neighbors for" },
|
|
616
|
-
depth: { type: "number", description: "Maximum depth to traverse (default: 1)", default: 1 },
|
|
617
|
-
},
|
|
618
|
-
required: ["entityName"],
|
|
619
|
-
},
|
|
620
|
-
},
|
|
621
|
-
{
|
|
622
|
-
name: "find_path",
|
|
623
|
-
description: "Find a path between two entities in the knowledge graph",
|
|
624
|
-
inputSchema: {
|
|
625
|
-
type: "object",
|
|
626
|
-
properties: {
|
|
627
|
-
fromEntity: { type: "string", description: "The name of the starting entity" },
|
|
628
|
-
toEntity: { type: "string", description: "The name of the target entity" },
|
|
629
|
-
maxDepth: { type: "number", description: "Maximum depth to search (default: 5)", default: 5 },
|
|
630
|
-
},
|
|
631
|
-
required: ["fromEntity", "toEntity"],
|
|
632
|
-
},
|
|
633
|
-
},
|
|
634
|
-
{
|
|
635
|
-
name: "get_entities_by_type",
|
|
636
|
-
description: "Get all entities of a specific type",
|
|
637
|
-
inputSchema: {
|
|
638
|
-
type: "object",
|
|
639
|
-
properties: {
|
|
640
|
-
entityType: { type: "string", description: "The type of entities to retrieve" },
|
|
641
|
-
},
|
|
642
|
-
required: ["entityType"],
|
|
643
|
-
},
|
|
644
|
-
},
|
|
645
|
-
{
|
|
646
|
-
name: "get_entity_types",
|
|
647
|
-
description: "Get all unique entity types in the knowledge graph",
|
|
648
|
-
inputSchema: {
|
|
649
|
-
type: "object",
|
|
650
|
-
properties: {},
|
|
651
|
-
},
|
|
652
|
-
},
|
|
653
|
-
{
|
|
654
|
-
name: "get_relation_types",
|
|
655
|
-
description: "Get all unique relation types in the knowledge graph",
|
|
656
|
-
inputSchema: {
|
|
657
|
-
type: "object",
|
|
658
|
-
properties: {},
|
|
659
|
-
},
|
|
660
|
-
},
|
|
661
|
-
{
|
|
662
|
-
name: "get_stats",
|
|
663
|
-
description: "Get statistics about the knowledge graph",
|
|
664
|
-
inputSchema: {
|
|
665
|
-
type: "object",
|
|
666
|
-
properties: {},
|
|
667
|
-
},
|
|
668
|
-
},
|
|
669
|
-
{
|
|
670
|
-
name: "get_orphaned_entities",
|
|
671
|
-
description: "Get entities that have no relations (orphaned entities)",
|
|
672
|
-
inputSchema: {
|
|
673
|
-
type: "object",
|
|
674
|
-
properties: {},
|
|
675
|
-
},
|
|
676
|
-
},
|
|
677
|
-
{
|
|
678
|
-
name: "validate_graph",
|
|
679
|
-
description: "Validate the knowledge graph and return a list of missing entities referenced in relations",
|
|
680
|
-
inputSchema: {
|
|
681
|
-
type: "object",
|
|
682
|
-
properties: {},
|
|
683
|
-
},
|
|
684
|
-
},
|
|
685
|
-
{
|
|
686
|
-
name: "evaluate_bcl",
|
|
687
|
-
description: "Evaluate a Binary Combinatory Logic (BCL) program",
|
|
688
|
-
inputSchema: {
|
|
689
|
-
type: "object",
|
|
690
|
-
properties: {
|
|
691
|
-
program: { type: "string", description: "The BCL program as a binary string (syntax: T:=00|01|1TT) 00=K, 01=S, 1=application." },
|
|
692
|
-
maxSteps: { type: "number", description: "Maximum number of reduction steps to perform (default: 1000000)", default: 1000000 },
|
|
693
|
-
},
|
|
694
|
-
required: ["program"],
|
|
695
|
-
},
|
|
696
|
-
},
|
|
697
|
-
{
|
|
698
|
-
name: "add_bcl_term",
|
|
699
|
-
description: "Add a BCL term to the constructor, maintaining valid syntax. Returns completion status.",
|
|
700
|
-
inputSchema: {
|
|
701
|
-
type: "object",
|
|
702
|
-
properties: {
|
|
703
|
-
term: {
|
|
704
|
-
type: "string",
|
|
705
|
-
description: "BCL term to add. Valid values: '1' or 'App' (application), '00' or 'K' (K combinator), '01' or 'S' (S combinator)"
|
|
706
|
-
},
|
|
707
|
-
},
|
|
708
|
-
required: ["term"],
|
|
709
|
-
},
|
|
710
|
-
},
|
|
711
|
-
{
|
|
712
|
-
name: "clear_bcl_term",
|
|
713
|
-
description: "Clear the current BCL term being constructed and reset the constructor state",
|
|
714
|
-
inputSchema: {
|
|
715
|
-
type: "object",
|
|
716
|
-
properties: {},
|
|
717
|
-
},
|
|
718
|
-
},
|
|
719
|
-
],
|
|
720
|
-
};
|
|
721
|
-
});
|
|
722
|
-
server.setRequestHandler(CallToolRequestSchema, async (request) => {
|
|
723
|
-
const { name, arguments: args } = request.params;
|
|
724
|
-
if (!args) {
|
|
725
|
-
throw new Error(`No arguments provided for tool: ${name}`);
|
|
726
|
-
}
|
|
727
|
-
switch (name) {
|
|
728
|
-
case "create_entities":
|
|
729
|
-
return { content: [{ type: "text", text: JSON.stringify(await knowledgeGraphManager.createEntities(args.entities), null, 2) }] };
|
|
730
|
-
case "create_relations":
|
|
731
|
-
return { content: [{ type: "text", text: JSON.stringify(await knowledgeGraphManager.createRelations(args.relations), null, 2) }] };
|
|
732
|
-
case "add_observations":
|
|
733
|
-
return { content: [{ type: "text", text: JSON.stringify(await knowledgeGraphManager.addObservations(args.observations), null, 2) }] };
|
|
734
|
-
case "delete_entities":
|
|
735
|
-
await knowledgeGraphManager.deleteEntities(args.entityNames);
|
|
736
|
-
return { content: [{ type: "text", text: "Entities deleted successfully" }] };
|
|
737
|
-
case "delete_observations":
|
|
738
|
-
await knowledgeGraphManager.deleteObservations(args.deletions);
|
|
739
|
-
return { content: [{ type: "text", text: "Observations deleted successfully" }] };
|
|
740
|
-
case "delete_relations":
|
|
741
|
-
await knowledgeGraphManager.deleteRelations(args.relations);
|
|
742
|
-
return { content: [{ type: "text", text: "Relations deleted successfully" }] };
|
|
743
|
-
case "read_graph":
|
|
744
|
-
return { content: [{ type: "text", text: JSON.stringify(await knowledgeGraphManager.readGraph(), null, 2) }] };
|
|
745
|
-
case "search_nodes":
|
|
746
|
-
return { content: [{ type: "text", text: JSON.stringify(await knowledgeGraphManager.searchNodes(args.query), null, 2) }] };
|
|
747
|
-
case "open_nodes_filtered":
|
|
748
|
-
return { content: [{ type: "text", text: JSON.stringify(await knowledgeGraphManager.openNodesFiltered(args.names), null, 2) }] };
|
|
749
|
-
case "open_nodes":
|
|
750
|
-
return { content: [{ type: "text", text: JSON.stringify(await knowledgeGraphManager.openNodes(args.names), null, 2) }] };
|
|
751
|
-
case "get_neighbors":
|
|
752
|
-
return { content: [{ type: "text", text: JSON.stringify(await knowledgeGraphManager.getNeighbors(args.entityName, args.depth), null, 2) }] };
|
|
753
|
-
case "find_path":
|
|
754
|
-
return { content: [{ type: "text", text: JSON.stringify(await knowledgeGraphManager.findPath(args.fromEntity, args.toEntity, args.maxDepth), null, 2) }] };
|
|
755
|
-
case "get_entities_by_type":
|
|
756
|
-
return { content: [{ type: "text", text: JSON.stringify(await knowledgeGraphManager.getEntitiesByType(args.entityType), null, 2) }] };
|
|
757
|
-
case "get_entity_types":
|
|
758
|
-
return { content: [{ type: "text", text: JSON.stringify(await knowledgeGraphManager.getEntityTypes(), null, 2) }] };
|
|
759
|
-
case "get_relation_types":
|
|
760
|
-
return { content: [{ type: "text", text: JSON.stringify(await knowledgeGraphManager.getRelationTypes(), null, 2) }] };
|
|
761
|
-
case "get_stats":
|
|
762
|
-
return { content: [{ type: "text", text: JSON.stringify(await knowledgeGraphManager.getStats(), null, 2) }] };
|
|
763
|
-
case "get_orphaned_entities":
|
|
764
|
-
return { content: [{ type: "text", text: JSON.stringify(await knowledgeGraphManager.getOrphanedEntities(), null, 2) }] };
|
|
765
|
-
case "validate_graph":
|
|
766
|
-
return { content: [{ type: "text", text: JSON.stringify(await knowledgeGraphManager.validateGraph(), null, 2) }] };
|
|
767
|
-
case "evaluate_bcl":
|
|
768
|
-
return { content: [{ type: "text", text: JSON.stringify(await knowledgeGraphManager.evaluateBCL(args.program, args.maxSteps), null, 2) }] };
|
|
769
|
-
case "add_bcl_term":
|
|
770
|
-
return { content: [{ type: "text", text: await knowledgeGraphManager.addBCLTerm(args.term) }] };
|
|
771
|
-
case "clear_bcl_term":
|
|
772
|
-
await knowledgeGraphManager.clearBCLTerm();
|
|
773
|
-
return { content: [{ type: "text", text: "BCL term constructor cleared successfully" }] };
|
|
774
|
-
default:
|
|
775
|
-
throw new Error(`Unknown tool: ${name}`);
|
|
776
|
-
}
|
|
777
|
-
});
|
|
778
|
-
async function main() {
|
|
779
|
-
const transport = new StdioServerTransport();
|
|
780
|
-
await server.connect(transport);
|
|
781
|
-
console.error("Knowledge Graph MCP Server running on stdio");
|
|
782
|
-
}
|
|
783
|
-
main().catch((error) => {
|
|
784
|
-
console.error("Fatal error in main():", error);
|
|
3
|
+
import { createServer } from "./server.js";
|
|
4
|
+
const server = createServer();
|
|
5
|
+
const transport = new StdioServerTransport();
|
|
6
|
+
server.connect(transport).catch((error) => {
|
|
7
|
+
console.error("Fatal error:", error);
|
|
785
8
|
process.exit(1);
|
|
786
9
|
});
|