Server: @modelcontextprotocol/server-memory
(modelcontextprotocol/servers/src/memory)
Layer: agent ↔ tool · Finding: server accepts referentially-inconsistent state
Status: reproduced (code + live probe). Filed upstream: modelcontextprotocol/servers#4457.
The server models a knowledge graph where relations and observations reference
entities by name. The README's workflow is create entities → create relations
→ add observations, and delete_entities is documented to also delete "their
associated relations" — i.e. relations are meant to be tied to existing entities.
A well-formed graph therefore never contains a relation or observation pointing
at an entity that does not exist.
We encode that as a session type (protocol.py): any interleaving of
CreateEntities / DeleteEntities / RelOk / ObsOk / ReadOnly is legal;
a relation or observation whose target isn't a live entity (RelDangling /
ObsDangling) is a violation.
create_relations performs no entity-existence check — it accepts relations
between entities that were never created, silently persisting a dangling relation.
The very same file validates existence for add_observations, so the behaviour is
inconsistent, not a deliberate design choice.
src/memory/index.ts (fetched from main):
// createRelations — only de-dupes, then pushes. No existence check.
async createRelations(relations: Relation[]): Promise<Relation[]> {
const graph = await this.loadGraph();
const newRelations = relations.filter(r => !graph.relations.some(er =>
er.from === r.from && er.to === r.to && er.relationType === r.relationType));
graph.relations.push(...newRelations); // <-- dangling relations allowed
await this.saveGraph(graph);
return newRelations;
}
// addObservations — DOES validate the entity exists.
async addObservations(observations ...) {
...
const entity = graph.entities.find(e => e.name === o.entityName);
if (!entity) { throw new Error(`Entity with name ${o.entityName} not found`); }
...
}Live server (python3 probe.py, exits 0 when the bug reproduces):
create_relations(nonexistent) -> isError: False
returned: [ { "from": "Ghost_A", "to": "Ghost_B", "relationType": "knows" } ]
add_observations(nonexistent) -> isError: True
returned: Entity with name Ghost_C not found
read_graph -> entities: 0 | relations: 1
BUG REPRODUCED: relations accepted=True, dangling in graph=True, observations correctly rejected=True
The monitor flags the same sequences offline (python3 -m pytest tests/): a
create_relations before its entities projects to RelDangling, which the
protocol rejects.
read_graph now returns a graph whose relations reference entities absent from
entities. Downstream consumers that assume referential integrity (graph
traversal, visualization, an agent reasoning over the graph) get corrupt state,
and the corruption is silent — no error, and it persists to
MEMORY_FILE_PATH. A stricter contrast: add_observations fails loudly on the
same missing entity.
Mirror addObservations: in createRelations, verify every from/to names a
live entity and throw (or skip + report) otherwise.
The faithful invariant — "a relation endpoint must be a live entity" — is a per-resource (per-entity-name) correlation, which a plain DFA over opaque labels can't hold internally. Today the projection tracks the live-entity set and the monitor enforces the ordering; a future llmcontract with named registers would carry the entity set inside the contract. This case is therefore both a Wave-0 finding and a motivating example for the register upgrade.