read_graph
Retrieve the complete knowledge graph to access stored user information and interaction history for persistent memory in chat applications.
Instructions
Read the entire knowledge graph
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
No arguments | |||
Implementation Reference
- index.ts:213-215 (handler)The core handler function for the 'read_graph' tool, which loads the knowledge graph from the memory file and returns it.async readGraph(): Promise<KnowledgeGraph> { return this.loadGraph(); }
- index.ts:500-508 (registration)Registration of the 'read_graph' tool in the ListToolsRequestSchema response, including name, description, and empty input schema.{ name: "read_graph", description: "Read the entire knowledge graph", inputSchema: { type: "object", properties: {}, }, }, {
- index.ts:568-569 (handler)Dispatch case in the CallToolRequestSchema handler that executes the read_graph tool by calling knowledgeGraphManager.readGraph() and formatting the response.case "read_graph": return createResponse(JSON.stringify(await knowledgeGraphManager.readGraph(), null, 2));
- index.ts:50-53 (schema)Type definition for the KnowledgeGraph returned by the read_graph tool.interface KnowledgeGraph { entities: Entity[]; relations: Relation[]; }
- index.ts:63-114 (helper)Helper method called by readGraph to parse the JSON lines from the memory file into entities and relations.private async loadGraph(): Promise<KnowledgeGraph> { try { // Check if file exists first try { await fs.access(this.memoryFilePath); } catch (error) { console.error(`[Debug] File does not exist, creating empty file`); await fs.writeFile(this.memoryFilePath, '', 'utf-8'); return { entities: [], relations: [] }; } const data = await fs.readFile(this.memoryFilePath, "utf-8"); console.error(`[Debug] Loading graph from: ${this.memoryFilePath}`); console.error(`[Debug] File contents: ${data}`); const lines = data.split("\n").filter(line => line.trim() !== ""); console.error(`[Debug] Found ${lines.length} lines in the file`); const graph: KnowledgeGraph = { entities: [], relations: [] }; for (const line of lines) { try { console.error(`[Debug] Processing line: ${line}`); const item = JSON.parse(line); if (item.type === "entity") { const entity: Entity = { name: item.name, entityType: item.entityType, observations: item.observations, subdomain: item.subdomain }; console.error(`[Debug] Adding entity: ${JSON.stringify(entity, null, 2)}`); graph.entities.push(entity); } else if (item.type === "relation") { const { type, ...relation } = item; graph.relations.push(relation as Relation); } } catch (parseError) { console.error(`[Debug] Error parsing line: ${parseError}`); continue; } } console.error(`[Debug] Loaded ${graph.entities.length} entities and ${graph.relations.length} relations`); return graph; } catch (error) { console.error(`[Debug] Error loading graph:`, error); throw error; } }