Skip to content

Commit b70860f

Browse files
Your Nameclaude
andcommitted
Fix modelcontextprotocol#3137: Include incoming and outgoing relations in openNodes results
The openNodes method was only returning relations where both endpoints were in the requested entity set (using &&). This prevented graph traversal from specific nodes without fetching the entire graph. Changed the filter logic to use || instead of &&, so openNodes now returns: - Outgoing relations (where the entity is the source) - Incoming relations (where the entity is the destination) This matches standard graph database behavior and enables efficient graph traversal by returning all edges connected to queried nodes. Updated tests to reflect the new expected behavior where openNodes returns all connected relations for the requested entities. Co-Authored-By: Claude Haiku 4.5 <[email protected]>
1 parent a83b145 commit b70860f

2 files changed

Lines changed: 21 additions & 14 deletions

File tree

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

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -336,16 +336,22 @@ 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 all relations connected to opened nodes', 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+
expect(result.relations).toHaveLength(2);
342+
// Alice -> Bob (Alice and Bob both opened)
343+
expect(result.relations).toContainEqual({ from: 'Alice', to: 'Bob', relationType: 'knows' });
344+
// Bob -> Charlie (Bob is opened, so its outgoing relations are included)
345+
expect(result.relations).toContainEqual({ from: 'Bob', to: 'Charlie', relationType: 'knows' });
344346
});
345347

346-
it('should exclude relations to unopened nodes', async () => {
348+
it('should include both incoming and outgoing relations', async () => {
347349
const result = await manager.openNodes(['Bob']);
348-
expect(result.relations).toHaveLength(0);
350+
expect(result.relations).toHaveLength(2);
351+
// Alice -> Bob (incoming)
352+
expect(result.relations).toContainEqual({ from: 'Alice', to: 'Bob', relationType: 'knows' });
353+
// Bob -> Charlie (outgoing)
354+
expect(result.relations).toContainEqual({ from: 'Bob', to: 'Charlie', relationType: 'knows' });
349355
});
350356

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

‎src/memory/index.ts‎

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -212,23 +212,24 @@ export class KnowledgeGraphManager {
212212

213213
async openNodes(names: string[]): Promise<KnowledgeGraph> {
214214
const graph = await this.loadGraph();
215-
215+
216216
// Filter entities
217217
const filteredEntities = graph.entities.filter(e => names.includes(e.name));
218-
218+
219219
// Create a Set of filtered entity names for quick lookup
220220
const filteredEntityNames = new Set(filteredEntities.map(e => e.name));
221-
222-
// Filter relations to only include those between filtered entities
223-
const filteredRelations = graph.relations.filter(r =>
224-
filteredEntityNames.has(r.from) && filteredEntityNames.has(r.to)
221+
222+
// Filter relations to include those connected to any of the filtered entities
223+
// This includes both outgoing relations (where from matches) and incoming relations (where to matches)
224+
const filteredRelations = graph.relations.filter(r =>
225+
filteredEntityNames.has(r.from) || filteredEntityNames.has(r.to)
225226
);
226-
227+
227228
const filteredGraph: KnowledgeGraph = {
228229
entities: filteredEntities,
229230
relations: filteredRelations,
230231
};
231-
232+
232233
return filteredGraph;
233234
}
234235
}

0 commit comments

Comments
 (0)