Skip to content

Add get_node_relations function for efficient graph traversal #2310

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/memory/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,16 @@ Example:
- Relations between requested entities
- Silently skips non-existent nodes

- **get_node_relations**
- Get all relations for a specific node, including incoming and outgoing connections
- Input: `nodeName` (string)
- Returns:
- `outgoing` (array): Relations where this node is the source
- `incoming` (array): Relations where this node is the target
- `connected_entities` (string[]): Names of all connected entities
- Enables efficient graph traversal without loading entire graph
- Returns empty arrays if node has no relations

# Usage with Claude Desktop

### Setup
Expand Down
40 changes: 40 additions & 0 deletions src/memory/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,30 @@ class KnowledgeGraphManager {

return filteredGraph;
}

async getNodeRelations(nodeName: string): Promise<{
outgoing: Relation[],
incoming: Relation[],
connected_entities: string[]
}> {
const graph = await this.loadGraph();

// Find all outgoing relations (where this node is the source)
const outgoing = graph.relations.filter(r => r.from === nodeName);

// Find all incoming relations (where this node is the target)
const incoming = graph.relations.filter(r => r.to === nodeName);

// Get all connected entity names (removing duplicates)
const connected_entities = [
...new Set([
...outgoing.map(r => r.to),
...incoming.map(r => r.from)
])
];

return { outgoing, incoming, connected_entities };
}
}

const knowledgeGraphManager = new KnowledgeGraphManager();
Expand Down Expand Up @@ -369,6 +393,20 @@ server.setRequestHandler(ListToolsRequestSchema, async () => {
required: ["names"],
},
},
{
name: "get_node_relations",
description: "Get all relations for a specific node, including incoming and outgoing connections",
inputSchema: {
type: "object",
properties: {
nodeName: {
type: "string",
description: "The name of the entity to get relations for",
},
},
required: ["nodeName"],
},
},
],
};
});
Expand Down Expand Up @@ -402,6 +440,8 @@ server.setRequestHandler(CallToolRequestSchema, async (request) => {
return { content: [{ type: "text", text: JSON.stringify(await knowledgeGraphManager.searchNodes(args.query as string), null, 2) }] };
case "open_nodes":
return { content: [{ type: "text", text: JSON.stringify(await knowledgeGraphManager.openNodes(args.names as string[]), null, 2) }] };
case "get_node_relations":
return { content: [{ type: "text", text: JSON.stringify(await knowledgeGraphManager.getNodeRelations(args.nodeName as string), null, 2) }] };
default:
throw new Error(`Unknown tool: ${name}`);
}
Expand Down