read_graph
Access and retrieve the complete knowledge graph stored in the Knowledge Graph Memory Server to enable persistent memory integration for chat applications.
Instructions
Read the entire knowledge graph
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
No arguments | |||
Input Schema (JSON Schema)
{
"properties": {},
"type": "object"
}
Implementation Reference
- index.ts:213-215 (handler)The handler function implementing the core logic of the 'read_graph' tool. It loads the knowledge graph from the memory file using the private loadGraph method.async readGraph(): Promise<KnowledgeGraph> { return this.loadGraph(); }
- index.ts:500-507 (registration)Registration of the 'read_graph' tool in the ListToolsRequestSchema handler, defining its name, description, and input schema (which requires no arguments).{ name: "read_graph", description: "Read the entire knowledge graph", inputSchema: { type: "object", properties: {}, }, },
- index.ts:568-569 (handler)The execution handler in the CallToolRequestSchema switch statement that invokes the readGraph method on the KnowledgeGraphManager instance and returns the JSON-formatted graph.case "read_graph": return createResponse(JSON.stringify(await knowledgeGraphManager.readGraph(), null, 2));
- index.ts:503-506 (schema)The input schema for the 'read_graph' tool, which is an empty object indicating no input parameters are required.inputSchema: { type: "object", properties: {}, },
- index.ts:63-114 (helper)Private helper method that performs the actual file reading and parsing of the knowledge graph from the JSON lines file, handling creation of empty file if missing and parsing entities/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; } }