{"record":{"id":"bc04f984ec36274b","repo":"modelcontextprotocol/servers","slug":"entity-with-name-r-to-not-found","errorCode":null,"errorMessage":"Entity with name ${r.to} not found","messagePattern":"Entity with name (.+?) not found","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src/memory/index.ts","lineNumber":226,"sourceCode":"        !entities.slice(0, index).some(earlier => earlier.name === e.name)\n      );\n      graph.entities.push(...newEntities);\n      await this.saveGraph(graph);\n      return newEntities;\n    });\n  }\n\n  async createRelations(relations: Relation[]): Promise<Relation[]> {\n    return this.withLock(async () => {\n      const graph = await this.loadGraph();\n      const entityNames = new Set(graph.entities.map(e => e.name));\n\n      relations.forEach(r => {\n        if (!entityNames.has(r.from)) {\n          throw new Error(`Entity with name ${r.from} not found`);\n        }\n        if (!entityNames.has(r.to)) {\n          throw new Error(`Entity with name ${r.to} not found`);\n        }\n      });\n\n      const isSameRelation = (a: Relation, b: Relation) =>\n        a.from === b.from &&\n        a.to === b.to &&\n        a.relationType === b.relationType;\n      const newRelations = relations.filter((r, index) =>\n        !graph.relations.some(existingRelation => isSameRelation(existingRelation, r)) &&\n        // Also skip duplicates appearing earlier in this same batch\n        !relations.slice(0, index).some(earlier => isSameRelation(earlier, r))\n      );\n      graph.relations.push(...newRelations);\n      await this.saveGraph(graph);\n      return newRelations;\n    });\n  }\n","sourceCodeStart":208,"sourceCodeEnd":244,"githubUrl":"https://github.com/modelcontextprotocol/servers/blob/d73f99efbfd40c3aa1b61e88728b3d49fb52608f/src/memory/index.ts#L208-L244","documentation":"The same validation in createRelations(), but for the 'to' endpoint of a relation. Before appending relations to the graph, each relation's target entity name is checked against the existing entity set; if the destination node does not exist the whole batch is rejected with this error. This guarantees referential integrity of edges in the knowledge graph.","triggerScenarios":"createRelations([ { from: 'Alice', to: 'Bob', relationType: 'reports_to' } ]) when 'Bob' was never created (or was renamed/deleted) in the graph; also triggered for any later relation in the batch even if earlier ones are valid, since validation runs over all relations before writing.","commonSituations":"Linking an entity to one that lives in a different graph file; stale client caches referencing deleted entities; case/whitespace mismatches on the 'to' name; constructing relations programmatically from LLM output containing hallucinated entity names.","solutions":["Create the missing 'to' entity via createEntities before calling createRelations.","Pre-validate every relation endpoint against graph.entities names in your client code.","Verify the target entity was not deleted by a previous deleteEntities call; re-add it if needed.","Normalize names (trim, consistent casing) when generating relations so they match stored entity names exactly."],"exampleFix":"// before\nawait memory.createRelations([{ from: \"Bob\", to: \"Acme Corp\", relationType: \"works_at\" }]);\n// after\nawait memory.createEntities([{ name: \"Acme Corp\", entityType: \"organization\", observations: [] }]);\nawait memory.createRelations([{ from: \"Bob\", to: \"Acme Corp\", relationType: \"works_at\" }]);","handlingStrategy":"validation","validationCode":"const graph = await memory.readGraph();\nconst names = new Set(graph.entities.map(e => e.name));\nconst bad = relations.filter(r => !names.has(r.to));\nif (bad.length) throw new Error(`Unknown relation targets: ${bad.map(r => r.to).join(\", \")}`);\nawait memory.createRelations(relations);","typeGuard":"function targetsExist(relations: Relation[], names: Set<string>): boolean {\n  return relations.every(r => typeof r.to === \"string\" && names.has(r.to));\n}","tryCatchPattern":"try {\n  await memory.createRelations(relations);\n} catch (e) {\n  if (e instanceof Error && /Entity with name (.+) not found/.test(e.message)) {\n    console.error(\"Relation endpoint missing; create it first:\", e.message);\n  } else throw e;\n}","preventionTips":["Derive relation endpoints only from entities you know exist (e.g. from a prior readGraph call).","Reject LLM-generated relations whose targets are not in the known entity list before submitting.","Re-create deleted entities before relinking; deletes remove nodes but old relations may reference them."],"tags":["knowledge-graph","missing-entity","validation","typescript"],"backgroundTag":"entity-not-found","analyzedSha":"d73f99efbfd40c3aa1b61e88728b3d49fb52608f","analyzedAt":"2026-09-07T14:54:21.545Z","contentChangedAt":"2026-09-07T14:54:21.545Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}