graph-path
Identify paths between nodes in a graph by specifying start and end points using IDs or labels. Supports exploration of relationships with customizable depth for efficient analysis.
Instructions
Find a path of nodes from A to B (by id or label/type).
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| from | Yes | ||
| maxDepth | No | ||
| to | Yes |
Implementation Reference
- src/mcp.ts:1279-1283 (handler)MCP tool handler for 'graph-path': parses arguments using GraphPathSchema, invokes graph.path() method, and returns JSON serialized path nodes.case 'graph-path': { const parsed = GraphPathSchema.parse(args); const nodes = graph.path(parsed.from as any, parsed.to as any, parsed.maxDepth); return { content: [{ type: 'text', text: JSON.stringify(nodes) }] }; }
- src/types.ts:70-81 (schema)Zod schema definition GraphPathSchema used for input validation of the graph-path tool, supporting node IDs or label/type objects for 'from' and 'to', with optional maxDepth.export const GraphPathSchema = z.object({ from: z.union([ z.number().int().positive(), z.object({ label: z.string(), type: z.string().optional() }), ]), to: z.union([ z.number().int().positive(), z.object({ label: z.string(), type: z.string().optional() }), ]), maxDepth: z.number().int().positive().max(10).optional().default(4), }); export type GraphPathInput = z.infer<typeof GraphPathSchema>;
- src/mcp.ts:175-178 (registration)Tool registration object added to the MCP tools array, defining name, description, and inputSchema for graph-path.name: 'graph-path', description: 'Find a path of nodes from A to B (by id or label/type).', inputSchema: { type: 'object', properties: { from: { anyOf: [{ type: 'number' }, { type: 'object', properties: { label: { type: 'string' }, type: { type: 'string' } }, required: ['label'] }] }, to: { anyOf: [{ type: 'number' }, { type: 'object', properties: { label: { type: 'string' }, type: { type: 'string' } }, required: ['label'] }] }, maxDepth: { type: 'number' } }, required: ['from', 'to'] }, },
- src/graph.ts:187-196 (helper)Core path-finding implementation in SqliteGraphStore using BFS (via bfsPath helper), resolving node IDs, querying DB for path nodes, and mapping to GraphNode objects.path(from: number | { label: string; type?: string }, to: number | { label: string; type?: string }, maxDepth = 4): GraphNode[] { const fromId = this.resolveNodeId(from); const toId = this.resolveNodeId(to); if (!fromId || !toId) return []; const ids = this.bfsPath(fromId, toId, maxDepth); if (!ids.length) return []; const rows = this.db.prepare(`SELECT id,label,type,props FROM graph_nodes WHERE id IN (${ids.map(() => '?').join(',')})`).all(...ids) as any[]; const byId = new Map(rows.map((r) => [r.id, r] as const)); return ids.map((id) => this.mapRow(byId.get(id)!)); }
- src/graph.ts:27-27 (helper)IGraphStore interface definition for the path method, which the tool handler invokes.path(from: number | { label: string; type?: string }, to: number | { label: string; type?: string }, maxDepth?: number): GraphNode[];