get_graph
Retrieve graph data showing connections between pages, including links, backlinks, and tags, to visualize relationships in your Logseq knowledge graph.
Instructions
페이지 간 연결 그래프 데이터 조회. 링크/백링크/태그 관계 포함
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| center | No | 중심 페이지 이름 (선택) | |
| depth | No | 탐색 깊이 (기본값: 1) |
Implementation Reference
- src/graph.ts:333-408 (handler)Core handler implementing getGraph tool logic: builds graph nodes (pages, journals, tags) and edges (links, backlinks, tags) from page metadata, with optional center node BFS traversal limited by depth.async getGraph(options?: { center?: string; depth?: number }): Promise<Graph> { const nodes: GraphNode[] = []; const edges: GraphEdge[] = []; const nodeSet = new Set<string>(); const pages = await this.listPages(); const addNode = (id: string, name: string, type: GraphNode['type']) => { if (!nodeSet.has(id)) { nodeSet.add(id); nodes.push({ id, name, type }); } }; if (options?.center) { // Build graph around center node const visited = new Set<string>(); const queue: { name: string; depth: number }[] = [{ name: options.center, depth: 0 }]; // 보안: depth는 0 이상, MAX_DEPTH 이하로 제한 const requestedDepth = options.depth ?? 1; const maxDepth = Math.max(0, Math.min(requestedDepth, MAX_DEPTH)); while (queue.length > 0) { const { name, depth } = queue.shift()!; if (visited.has(name) || depth > maxDepth) continue; visited.add(name); const page = pages.find(p => p.name === name); if (!page) continue; const nodeType = page.isJournal ? 'journal' : 'page'; addNode(name, name, nodeType); // Add links for (const link of page.links) { addNode(link, link, 'page'); edges.push({ source: name, target: link, type: 'link' }); if (depth < maxDepth) { queue.push({ name: link, depth: depth + 1 }); } } // Add backlinks for (const backlink of page.backlinks) { addNode(backlink, backlink, 'page'); edges.push({ source: backlink, target: name, type: 'backlink' }); if (depth < maxDepth) { queue.push({ name: backlink, depth: depth + 1 }); } } // Add tags for (const tag of page.tags) { addNode(`tag:${tag}`, tag, 'tag'); edges.push({ source: name, target: `tag:${tag}`, type: 'tag' }); } } } else { // Full graph for (const page of pages) { const nodeType = page.isJournal ? 'journal' : 'page'; addNode(page.name, page.name, nodeType); for (const link of page.links) { addNode(link, link, 'page'); edges.push({ source: page.name, target: link, type: 'link' }); } for (const tag of page.tags) { addNode(`tag:${tag}`, tag, 'tag'); edges.push({ source: page.name, target: `tag:${tag}`, type: 'tag' }); } } } return { nodes, edges }; }
- src/index.ts:95-98 (schema)Input schema validation using Zod for get_graph tool parameters: center (optional page name), depth (optional integer 0-10).const GetGraphSchema = z.object({ center: z.string().max(MAX_NAME_LENGTH).optional().describe('중심 페이지 이름 (선택)'), depth: z.number().int().min(0).max(10).optional().describe('탐색 깊이 (기본값: 1, 최대: 10)'), });
- src/index.ts:205-215 (registration)MCP tool registration in server's TOOLS array defining get_graph with name, description, and inputSchema.{ name: 'get_graph', description: '페이지 간 연결 그래프 데이터 조회. 링크/백링크/태그 관계 포함', inputSchema: { type: 'object' as const, properties: { center: { type: 'string', description: '중심 페이지 이름 (선택)' }, depth: { type: 'number', description: '탐색 깊이 (기본값: 1)' }, }, }, },
- src/index.ts:314-320 (handler)MCP CallToolRequestSchema handler case for get_graph: parses input, invokes GraphService.getGraph, returns JSON stringified response.case 'get_graph': { const { center, depth } = GetGraphSchema.parse(args); const graphData = await graph.getGraph({ center, depth }); return { content: [{ type: 'text', text: JSON.stringify(graphData, null, 2) }], }; }
- src/types.ts:45-60 (schema)TypeScript interfaces defining output schema for get_graph: GraphNode, GraphEdge, and Graph structures.export interface GraphNode { id: string; name: string; type: 'page' | 'tag' | 'journal'; } export interface GraphEdge { source: string; target: string; type: 'link' | 'tag' | 'backlink'; } export interface Graph { nodes: GraphNode[]; edges: GraphEdge[]; }