Skip to content

Commit 062ec90

Browse files
committed
fix: include outgoing relations in open_nodes
When opening nodes in the knowledge graph, return outgoing relations (where the entity is the 'from' endpoint) in addition to relations between opened nodes. This allows traversing the graph from specific nodes without needing to read the entire graph. Fixes modelcontextprotocol#3137
1 parent 70c549b commit 062ec90

2 files changed

Lines changed: 15 additions & 9 deletions

File tree

‎src/memory/__tests__/knowledge-graph.test.ts‎

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -336,16 +336,19 @@ describe('KnowledgeGraphManager', () => {
336336
expect(result.entities.map(e => e.name)).toContain('Bob');
337337
});
338338

339-
it('should include relations between opened nodes', async () => {
339+
it('should include relations between opened nodes and outgoing relations', async () => {
340340
const result = await manager.openNodes(['Alice', 'Bob']);
341-
expect(result.relations).toHaveLength(1);
342-
expect(result.relations[0].from).toBe('Alice');
343-
expect(result.relations[0].to).toBe('Bob');
341+
// Should include: Alice->Bob (between opened nodes) and Bob->Charlie (outgoing from opened node)
342+
expect(result.relations).toHaveLength(2);
343+
expect(result.relations.map(r => r.from).sort()).toEqual(['Alice', 'Bob']);
344344
});
345345

346-
it('should exclude relations to unopened nodes', async () => {
346+
it('should return outgoing relations from opened nodes', async () => {
347347
const result = await manager.openNodes(['Bob']);
348-
expect(result.relations).toHaveLength(0);
348+
// Opening 'Bob' should return its outgoing relation to 'Charlie'
349+
expect(result.relations).toHaveLength(1);
350+
expect(result.relations[0].from).toBe('Bob');
351+
expect(result.relations[0].to).toBe('Charlie');
349352
});
350353

351354
it('should handle opening non-existent nodes', async () => {

‎src/memory/index.ts‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -219,9 +219,12 @@ export class KnowledgeGraphManager {
219219
// Create a Set of filtered entity names for quick lookup
220220
const filteredEntityNames = new Set(filteredEntities.map(e => e.name));
221221

222-
// Filter relations to only include those between filtered entities
222+
// Filter relations to include:
223+
// 1. Relations between filtered entities (both endpoints in the list)
224+
// 2. Outgoing relations from filtered entities (from is in the list)
223225
const filteredRelations = graph.relations.filter(r =>
224-
filteredEntityNames.has(r.from) && filteredEntityNames.has(r.to)
226+
(filteredEntityNames.has(r.from) && filteredEntityNames.has(r.to)) ||
227+
filteredEntityNames.has(r.from)
225228
);
226229

227230
const filteredGraph: KnowledgeGraph = {
@@ -447,7 +450,7 @@ server.registerTool(
447450
"open_nodes",
448451
{
449452
title: "Open Nodes",
450-
description: "Open specific nodes in the knowledge graph by their names",
453+
description: "Open specific nodes in the knowledge graph by their names. Returns entities and their outgoing relations (relations where the entity is the 'from' endpoint).",
451454
inputSchema: {
452455
names: z.array(z.string()).describe("An array of entity names to retrieve")
453456
},

0 commit comments

Comments
 (0)