read_graph
Access and retrieve the entire knowledge graph stored in the Knowledge Graph Memory Server to maintain persistent user information across interactions.
Instructions
Read the entire knowledge graph
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
No arguments | |||
Implementation Reference
- src/memory/index.ts:390-408 (registration)Registration of the 'read_graph' MCP tool, including title, description, input/output schemas, and the handler function.server.registerTool( "read_graph", { title: "Read Graph", description: "Read the entire knowledge graph", inputSchema: {}, outputSchema: { entities: z.array(EntitySchema), relations: z.array(RelationSchema) } }, async () => { const graph = await knowledgeGraphManager.readGraph(); return { content: [{ type: "text" as const, text: JSON.stringify(graph, null, 2) }], structuredContent: { ...graph } }; } );
- src/memory/index.ts:170-172 (handler)Core handler method in KnowledgeGraphManager that loads and returns the entire KnowledgeGraph by delegating to private loadGraph().async readGraph(): Promise<KnowledgeGraph> { return this.loadGraph(); }
- src/memory/index.ts:71-86 (helper)Private helper method that reads the memory file (JSONL format), parses each line into entities or relations, and constructs the KnowledgeGraph. Handles missing file gracefully.private async loadGraph(): Promise<KnowledgeGraph> { try { const data = await fs.readFile(this.memoryFilePath, "utf-8"); const lines = data.split("\n").filter(line => line.trim() !== ""); return lines.reduce((graph: KnowledgeGraph, line) => { const item = JSON.parse(line); if (item.type === "entity") graph.entities.push(item as Entity); if (item.type === "relation") graph.relations.push(item as Relation); return graph; }, { entities: [], relations: [] }); } catch (error) { if (error instanceof Error && 'code' in error && (error as any).code === "ENOENT") { return { entities: [], relations: [] }; } throw error; }
- src/memory/index.ts:62-65 (schema)TypeScript interface defining the structure of the knowledge graph returned by read_graph tool.export interface KnowledgeGraph { entities: Entity[]; relations: Relation[]; }
- src/memory/index.ts:227-237 (schema)Zod schemas for Entity and Relation used in the output schema of read_graph and other tools.const EntitySchema = z.object({ name: z.string().describe("The name of the entity"), entityType: z.string().describe("The type of the entity"), observations: z.array(z.string()).describe("An array of observation contents associated with the entity") }); const RelationSchema = z.object({ from: z.string().describe("The name of the entity where the relation starts"), to: z.string().describe("The name of the entity where the relation ends"), relationType: z.string().describe("The type of the relation") });