From dacdf62c10005cdcfd5d25997644207698d2f127 Mon Sep 17 00:00:00 2001 From: majiayu000 <1835304752@qq.com> Date: Sun, 28 Dec 2025 22:25:59 +0800 Subject: [PATCH] fix(memory): allow additional properties in entity/relation schemas Added .passthrough() to EntitySchema and RelationSchema to allow entities with extra properties to be read without schema validation errors. This fixes the issue where read_graph fails when the stored memory.jsonl contains entities with non-standard properties. Fixes #3144 Signed-off-by: majiayu000 <1835304752@qq.com> --- src/memory/__tests__/knowledge-graph.test.ts | 20 ++++++++++++++++++++ src/memory/index.ts | 6 ++++-- 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/src/memory/__tests__/knowledge-graph.test.ts b/src/memory/__tests__/knowledge-graph.test.ts index a65d527b64..4b98c96afb 100644 --- a/src/memory/__tests__/knowledge-graph.test.ts +++ b/src/memory/__tests__/knowledge-graph.test.ts @@ -390,5 +390,25 @@ describe('KnowledgeGraphManager', () => { expect(JSON.parse(lines[0])).toHaveProperty('type', 'entity'); expect(JSON.parse(lines[1])).toHaveProperty('type', 'relation'); }); + + it('should handle entities with additional properties in storage (issue #3144)', async () => { + // Manually write an entity with extra properties to the file + const entityWithExtra = JSON.stringify({ + type: 'entity', + name: 'Test_Entity', + entityType: 'test', + observations: ['Has an extra field'], + custom_id: 'xyz-123', // Extra property not in schema + metadata: { source: 'external' } // Another extra property + }); + await fs.writeFile(testFilePath, entityWithExtra); + + // Reading the graph should not throw an error + const graph = await manager.readGraph(); + expect(graph.entities).toHaveLength(1); + expect(graph.entities[0].name).toBe('Test_Entity'); + expect(graph.entities[0].entityType).toBe('test'); + expect(graph.entities[0].observations).toContain('Has an extra field'); + }); }); }); diff --git a/src/memory/index.ts b/src/memory/index.ts index c7d781d2c4..f8203c7072 100644 --- a/src/memory/index.ts +++ b/src/memory/index.ts @@ -224,17 +224,19 @@ export class KnowledgeGraphManager { let knowledgeGraphManager: KnowledgeGraphManager; // Zod schemas for entities and relations +// Using passthrough() to allow additional properties that may exist in stored data +// This prevents schema validation errors when reading entities with extra fields 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") -}); +}).passthrough(); 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") -}); +}).passthrough(); // The server instance and tools exposed to Claude const server = new McpServer({