obsidian_vault_graph
Retrieve a vault's link graph as structured data (nodes, edges, adjacency lists) for analyzing note relationships.
Instructions
Export the vault link graph as nodes, edges, and adjacency lists.
Tool call template
This tool declares no required parameters. Add optional parameters from the input schema as needed.
{
"id": 1,
"jsonrpc": "2.0",
"method": "tools/call",
"params": {
"arguments": {},
"name": "obsidian_vault_graph"
}
}Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| vault | No | Optional configured vault name. Defaults to the server default vault. | |
| includeUnresolved | No | ||
| limit | No |
Implementation Reference
- src/tools.ts:832-855 (registration)Registration and handler for the 'obsidian_vault_graph' tool. It exports the vault link graph as nodes, edges, and adjacency lists, with optional unresolved link filtering and a limit.
tool( "obsidian_vault_graph", "Export the vault link graph as nodes, edges, and adjacency lists.", { vault: vaultArg, includeUnresolved: z.boolean().optional().default(true), limit: z.number().int().min(1).max(10000).optional().default(5000), }, async (args) => { const graph = buildGraph(await loadNotes(vaults, args.vault)); const edges = graph.edges.filter((edge) => args.includeUnresolved || !edge.unresolved).slice(0, args.limit); const adjacency = new Map<string, string[]>(); for (const edge of edges) { if (edge.unresolved) continue; adjacency.set(edge.source, [...(adjacency.get(edge.source) ?? []), edge.target]); } return { nodes: graph.nodes.slice(0, args.limit), edges, adjacency: Object.fromEntries([...adjacency.entries()].map(([key, value]) => [key, [...new Set(value)].sort()])), }; }, { readOnlyHint: true }, ); - src/graph.ts:22-26 (schema)VaultGraph type definition with nodes, edges, and byPath map.
export type VaultGraph = { nodes: GraphNode[]; edges: GraphEdge[]; byPath: Map<string, GraphNode>; }; - src/graph.ts:5-11 (schema)GraphNode type definition: path, title, tags, outDegree, inDegree.
export type GraphNode = { path: string; title: string; tags: string[]; outDegree: number; inDegree: number; }; - src/graph.ts:13-20 (schema)GraphEdge type definition: source, target, rawTarget, kind, unresolved, line.
export type GraphEdge = { source: string; target: string; rawTarget: string; kind: "wiki" | "markdown"; unresolved: boolean; line: number; }; - src/graph.ts:28-65 (helper)buildGraph function - constructs a VaultGraph from NoteRecord[] by extracting wiki and markdown links, resolving targets, and computing in/out degrees.
export function buildGraph(notes: NoteRecord[]): VaultGraph { const resolver = createResolver(notes); const edges: GraphEdge[] = []; for (const note of notes) { for (const link of extractWikiLinks(note.content)) { const target = resolver(link.target); edges.push({ source: note.path, target: target ?? link.target, rawTarget: link.target, kind: "wiki", unresolved: !target, line: link.line, }); } for (const link of extractMarkdownLinks(note.content)) { const target = resolver(link.target); edges.push({ source: note.path, target: target ?? link.target, rawTarget: link.target, kind: "markdown", unresolved: !target, line: link.line, }); } } const outCounts = countBy(edges.filter((e) => !e.unresolved), (edge) => edge.source); const inCounts = countBy(edges.filter((e) => !e.unresolved), (edge) => edge.target); const nodes = notes.map((note) => ({ path: note.path, title: note.title, tags: note.tags, outDegree: outCounts.get(note.path) ?? 0, inDegree: inCounts.get(note.path) ?? 0, })); return { nodes, edges, byPath: new Map(nodes.map((node) => [node.path, node])) }; }