Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
d104316
test(memory): pin what a save does when the graph file path is a symlink
cliffhall Nov 30, 2026
9ff9867
docs(memory): say what happens to lines of the memory file the server…
cliffhall Nov 30, 2026
c268819
test(memory): pin the neighbouring faces of three pinned invariants
cliffhall Nov 30, 2026
9167dc5
fix(memory): store a repeated observation once in create_entities
cliffhall Nov 30, 2026
edb349e
fix(memory): report relations create_relations skipped
cliffhall Nov 30, 2026
2b64c89
fix(memory): count a repeated delete request item once
cliffhall Nov 30, 2026
90e20f7
fix(memory): fail every tool with an actionable error when the memory…
cliffhall Nov 30, 2026
b076e50
fix(memory): clear a crash-left lock guard by rename-aside, not by path
cliffhall Nov 30, 2026
f7adcea
perf(memory): index the write methods' lookups with Set and Map
cliffhall Nov 30, 2026
2b02c2f
test(memory): drive the clock in the heartbeat freshness test instead…
cliffhall Nov 30, 2026
f0e16a0
docs(memory): separate an unreachable line from a reachable namesake,…
cliffhall Nov 30, 2026
0875cae
fix(memory): give reads the missing-directory error for ENOTDIR too
cliffhall Nov 30, 2026
d871592
test(memory): bound the heartbeat test's waits and clean up on a failure
cliffhall Nov 30, 2026
157c06c
docs(memory): name every gap the guard put-back leaves
cliffhall Nov 30, 2026
df758df
Merge #5707 (#5653) into the #5693 branch to resolve the createEntiti…
cliffhall Nov 30, 2026
0aa4a38
Merge #5711 (#5655) into the #5693 branch to resolve the delete-metho…
cliffhall Nov 30, 2026
f3f9f97
test(memory): simulate the stat failure rather than rely on a mode-00…
cliffhall Nov 30, 2026
f58b019
Merge remote-tracking branch 'origin/v2/chore/5644-memory-symlink-gra…
cliffhall Nov 30, 2026
ceba5bf
Merge remote-tracking branch 'origin/v2/chore/5645-memory-sibling-fac…
cliffhall Nov 30, 2026
9d231f1
Merge remote-tracking branch 'origin/v2/docs/5609-memory-readme-unrea…
cliffhall Nov 30, 2026
22fc093
Merge remote-tracking branch 'origin/v2/fix/5653-memory-create-entiti…
cliffhall Nov 30, 2026
0b56ead
Merge remote-tracking branch 'origin/v2/fix/5654-memory-create-relati…
cliffhall Nov 30, 2026
453fa38
Merge remote-tracking branch 'origin/v2/fix/5655-memory-delete-repeat…
cliffhall Nov 30, 2026
694b235
Merge remote-tracking branch 'origin/v2/fix/5680-memory-lock-heartbea…
cliffhall Nov 30, 2026
e86ca4f
Merge remote-tracking branch 'origin/v2/fix/5624-memory-guard-clear-r…
cliffhall Nov 30, 2026
fd58ac5
Merge remote-tracking branch 'origin/v2/feat/5693-memory-write-lookup…
cliffhall Nov 30, 2026
951374b
Merge remote-tracking branch 'origin/v2/fix/5689-memory-missing-paren…
cliffhall Nov 30, 2026
79b9f02
test(memory): assert the created relations without pinning create_rel…
cliffhall Nov 30, 2026
c93008f
Merge branch 'v2/feat/5693-memory-write-lookups' into v2/chore/5696-m…
cliffhall Nov 30, 2026
11ec86d
Merge remote-tracking branch 'origin/v2/main' into v2/chore/5696-memo…
cliffhall Nov 30, 2026
3e272dd
Merge remote-tracking branch 'origin/v2/main' into v2/chore/5696-memo…
cliffhall Nov 30, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/memory-create-entities-repeated-observations.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@modelcontextprotocol/server-memory": patch
---

`create_entities` now stores a string repeated within one entity's observations once, as `add_observations` does, so a later `delete_observations` reports the count it was asked for.
5 changes: 5 additions & 0 deletions .changeset/memory-create-relations-skipped.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@modelcontextprotocol/server-memory": patch
---

`create_relations` now reports the relations it skipped because they already exist (or repeat an earlier relation in the same call): the structured result lists them in a new optional `skipped` array, a second text item names them, and the tool description says so. Skipped relations are still not added again ([#5654](https://github.com/modelcontextprotocol/servers/issues/5654)).
5 changes: 5 additions & 0 deletions .changeset/memory-delete-repeated-items.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@modelcontextprotocol/server-memory": patch
---

`delete_relations` and `delete_observations` count a request item listed more than once as one item, so a repeated relation or observation no longer reports "Deleted 1 of 2" as if part of the deletion missed. A requested observation or relation stored more than once also counts once.
5 changes: 5 additions & 0 deletions .changeset/memory-guard-clear-race.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@modelcontextprotocol/server-memory": patch
---

Clearing a lock guard (`<memory file>.lock.break`) left by a crash no longer deletes it by path. It is renamed aside and checked to be the file judged stale, so when two servers clear the same crash-left guard at once, the second no longer removes a live guard a third server took in between (#5624).
5 changes: 5 additions & 0 deletions .changeset/memory-missing-directory-error.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@modelcontextprotocol/server-memory": patch
---

When the directory holding `MEMORY_FILE_PATH` does not exist, every tool now fails with an error naming the memory file and its directory, instead of reads returning an empty graph while writes failed with a raw `ENOENT` on the internal `.lock` file.
5 changes: 5 additions & 0 deletions .changeset/memory-write-lookups.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@modelcontextprotocol/server-memory": patch
---

The write tools (`create_entities`, `create_relations`, `add_observations`, `delete_entities`, `delete_observations`, `delete_relations`) match requested items against the graph through Set and Map lookups instead of a scan per item, so large graphs and batches write much faster and hold the file lock for less time. Results, their order and every message are unchanged.
23 changes: 18 additions & 5 deletions src/memory/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ Example:
- `name` (string): Entity identifier
- `entityType` (string): Type classification
- `observations` (string[]): Associated observations
- A string repeated within one entity's `observations` is stored once, as `add_observations` does
- Ignores entities with existing names (and repeats of a name earlier in the same call); their observations are not added
- Returns the created entities. When any were skipped, the structured result also lists their names in `skipped`, and a second text item says which were skipped (use `add_observations` to extend an existing entity)

Expand All @@ -81,7 +82,8 @@ Example:
- `from` (string): Source entity name
- `to` (string): Target entity name
- `relationType` (string): Relationship type in active voice
- Skips duplicate relations
- Skips relations that already exist (and repeats of a relation earlier in the same call)
- Returns the created relations. When any were skipped, the structured result also lists them in `skipped`, and a second text item says which were skipped
- Fails if either the source or target entity doesn't exist

- **add_observations**
Expand All @@ -106,7 +108,7 @@ Example:
- Each object contains:
- `entityName` (string): Target entity
- `observations` (string[]): Observations to remove
- No error if an observation doesn't exist; the response reports how many were deleted
- No error if an observation doesn't exist; the response reports how many of the requested observations were deleted, counting an observation listed more than once for the same entity as one

- **delete_relations**
- Remove specific relations from the graph, keeping the entities they connect (to remove an entity itself, use `delete_entities`)
Expand All @@ -116,7 +118,7 @@ Example:
- `to` (string): Target entity name
- `relationType` (string): Relationship type
- A relation is deleted only when `from`, `to` and `relationType` all match a stored one exactly; read the stored `relationType` with `open_nodes` (or `search_nodes`) first if unsure
- No error if a relation doesn't exist; the response reports how many were deleted
- No error if a relation doesn't exist; the response reports how many of the requested relations were deleted, counting a relation listed more than once as one

- **read_graph**
- Read the entire knowledge graph
Expand Down Expand Up @@ -291,7 +293,7 @@ When `MEMORY_FILE_PATH` is not set and the server directory still holds a legacy
**Set `MEMORY_FILE_PATH` to an absolute path you own**, such as `~/memory.jsonl`, as in the configuration above. The default `memory.jsonl` sits in the server's own directory, next to its code. Under `npx` or an npm install, that directory is inside a `node_modules` tree, which npm manages as its own: reinstalling or updating the server, or clearing the npx cache, can delete the file and the knowledge graph in it. A relative `MEMORY_FILE_PATH` resolves against that same directory, so it has the same problem.

- When the memory file it resolves is inside a `node_modules` directory, the server prints a warning on stderr at startup naming the file; most clients show a server's stderr in its log. Move the file to the new path when you set the variable, or the server starts on an empty graph.
- The directory must already exist: the server creates the memory file, not the directories above it.
- The directory must already exist: the server creates the memory file, not the directories above it. While it is missing, every tool, read or write, and the `memory://knowledge-graph` resource fail with an error naming the memory file and its directory, rather than reads reporting an empty graph that no write could save to. Create the directory (no restart is needed) or point `MEMORY_FILE_PATH` at one that exists. A missing memory file in an existing directory is an empty graph, and the first write creates it.
- The default stays where it is so that existing graphs keep loading. The [Docker](#docker) setup above keeps it in a named volume mounted over `/app/dist`, so the graph there survives restarts and draws no warning.

Saves are atomic. Every write tool writes the whole graph to a temporary file in the same directory and renames it over the memory file, so a server stopped or killed mid-save leaves the previous graph intact rather than a half-written file. The file's permission bits are kept across the rename. The temporary file is always newly created by the save. Writes from several processes are serialized by a lock, described next.
Expand All @@ -302,7 +304,7 @@ Each MCP client starts its own server process, so two clients using the same `ME

- A server stopped mid-write by `SIGINT` or `SIGTERM` (Ctrl-C, `docker stop`, a client shutting it down), or exiting normally, removes the lock it holds before it exits, so the next writer does not wait on it.
- A lock left behind by a server that crashed mid-write is removed automatically: at once if its process is known to have exited on the same machine, otherwise once it has gone 30 seconds without being refreshed (a server holding the lock refreshes it every 15 seconds while it works). Only one waiting server breaks a stale lock at a time, guarded by a momentary `<memory file>.lock.break` file. It never deletes the lock where it stands: it renames it aside to a momentary `<memory file>.lock.<random>.stale` file, checks that what it took is the very file it judged stale, and only then deletes it. A live lock that replaced the stale one in between is put straight back, untouched, and the breaker goes on waiting.
- The same momentary `<memory file>.lock.break` file orders every hand-off of the lock, not only the breaking of a stale one. A server takes it to release its lock, and a server that has just created the lock waits until no `.lock.break` file is there, then checks the lock is still its own, before it writes. So a lock cannot be broken and taken over halfway through its holder's release, and a writer cannot start in the moment a live lock spends moved aside. A `.lock.break` file left by a crash is cleared once it is 30 seconds old, so it delays the next write by at most that long. This holds as long as no server stalls for 30 seconds in the middle of a hand-off.
- The same momentary `<memory file>.lock.break` file orders every hand-off of the lock, not only the breaking of a stale one. A server takes it to release its lock, and a server that has just created the lock waits until no `.lock.break` file is there, then checks the lock is still its own, before it writes. So a lock cannot be broken and taken over halfway through its holder's release, and a writer cannot start in the moment a live lock spends moved aside. A `.lock.break` file left by a crash is cleared once it is 30 seconds old, so it delays the next write by at most that long. It is cleared the way a stale lock is broken: renamed aside to a momentary `<memory file>.lock.break.<random>.stale` file and checked to be the very file judged stale, so a live `.lock.break` file another server took in its place is put back rather than deleted. This holds as long as no server stalls for 30 seconds in the middle of a hand-off.
- Servers of different versions can share a memory file: the lock file, what it holds and the `.lock.break` file are the same in every version. A server at 2.2.1 or earlier takes the `.lock.break` file only to break a stale lock, so its own releases and acquisitions are not ordered this way, and the gaps described above stay open for that server (each needs a lock to go stale first). Upgrade every server sharing a file to close them.
- A write that cannot get the lock within 60 seconds fails with a tool error rather than overwriting the file.
- The lock relies on exclusive file creation, which local filesystems provide; a network filesystem that does not honour it cannot be shared safely this way.
Expand All @@ -318,6 +320,17 @@ By default nothing caps the graph. Set `MEMORY_MAX_ENTITIES`, `MEMORY_MAX_RELATI
- The delete tools are never refused, so a graph over a limit (one that grew before the limit was set, or lowered) can always be shrunk. While it is over, calls that would grow it further on that dimension are refused.
- `MEMORY_MAX_FILE_BYTES` counts the bytes of the file as it would be saved, unreadable lines included, so it bounds the cost of each write directly. The counts are easier to reason about but do not bound how long each entity's observations grow.

#### Lines the server cannot read

Each line of the memory file is expected to be one JSON object with `"type": "entity"` or `"type": "relation"` and the fields described under [Core Concepts](#core-concepts). The server reads the whole file on every tool call, and leaves any other non-blank line out of the graph it loads: a line that is not JSON, a JSON value that is not an object (a number, a string, `null`), a JSON array, an object whose `type` is missing or is neither `entity` nor `relation`, and an entity or relation that fails its schema (an entity with no `entityType`, an observation that is not a string, a relation with no `relationType`, say).

- **Most skipped lines are logged to stderr, not all.** A line that is not JSON logs `Skipping malformed line in memory file`, a non-object value logs `Skipping non-object line in memory file`, and an entity or relation that fails its schema logs `Skipping invalid entity in memory file:` or `Skipping invalid relation in memory file:` followed by the fields at fault. A JSON array, and an object whose `type` is missing or unknown, are skipped without a message. The messages repeat each time the file is read, so on every tool call.
- **Skipped lines are kept.** Every save writes them back unchanged, after the graph's own lines, so a write never drops data the server could not read. Blank lines are not kept: a save removes them.
- **Skipped lines count toward `MEMORY_MAX_FILE_BYTES`**, since they are part of the file each save writes.
- **No tool can reach a skipped line.** `read_graph`, `search_nodes` and `open_nodes` do not show it. While no readable entity has its name, `delete_entities` and `delete_observations` report the name as not found, and `add_observations` and `create_relations` refuse it as an entity that does not exist. `create_entities` under the same name stores a second, readable record beside the skipped line rather than replacing it; from then on every tool acts on that new record, and deleting it removes only the new record, never the skipped line. A skipped relation matches no `delete_relations` call, and stays in the file after the entities it names are deleted.

**The only fix is to edit the file by hand.** Stop every server process using the memory file and wait for each to exit, so that none can save over your edit: quitting a client stops the stdio server it started, but a [Streamable HTTP](#streamable-http) server runs on its own and must be stopped itself. Then correct or delete the line in a text editor and start the servers and clients again. A stderr line from the list above, or a `not found` for an entity you can see in the file, is the sign to look.

# VS Code Installation Instructions

For quick installation, use one of the one-click installation buttons below:
Expand Down
72 changes: 72 additions & 0 deletions src/memory/__tests__/create-repeated-observations.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
// Regression tests for #5653, driven through an SDK Client over an in-memory
// transport: create_entities stores a string repeated within one entity's
// observations once, as add_observations does since #5201. A stored repeat
// would otherwise make delete_observations report deleting more than it was
// asked to. The fix dedupes in place, so these tests also check that the
// skip report, which tells created entities apart by identity, still holds.
import { describe, it, expect, beforeEach, afterEach } from "vitest";
import { call, connect, makeTempGraph, textOf } from "./helpers.js";
import type { Connection } from "./helpers.js";

describe("create_entities with repeated observations (#5653)", () => {
let cleanup: () => Promise<void>;
let conn: Connection;

beforeEach(async () => {
const graph = await makeTempGraph();
cleanup = graph.cleanup;
conn = await connect(graph.filePath);
});

afterEach(async () => {
await conn.close();
await cleanup();
});

async function observationsOf(name: string): Promise<unknown> {
const result = await call(conn.client, "open_nodes", { names: [name] });
const { entities } = result.structuredContent as {
entities: { observations: string[] }[];
};
return entities[0].observations;
}

it("stores and returns an observation repeated in one entity once", async () => {
const result = await call(conn.client, "create_entities", {
entities: [
{ name: "A", entityType: "thing", observations: ["x", "y", "x"] },
],
});
expect(result.isError).toBeUndefined();
expect(result.structuredContent).toEqual({
entities: [{ name: "A", entityType: "thing", observations: ["x", "y"] }],
});
expect(await observationsOf("A")).toEqual(["x", "y"]);
});

it("lets delete_observations report the count it was asked for", async () => {
await call(conn.client, "create_entities", {
entities: [{ name: "A", entityType: "thing", observations: ["x", "x"] }],
});
const result = await call(conn.client, "delete_observations", {
deletions: [{ entityName: "A", observations: ["x"] }],
});
expect(result.isError).toBeUndefined();
expect(textOf(result)).toBe("Observations deleted successfully");
expect(await observationsOf("A")).toEqual([]);
});

it("still reports a skipped entity alongside a deduped one", async () => {
const result = await call(conn.client, "create_entities", {
entities: [
{ name: "A", entityType: "thing", observations: ["x", "x"] },
{ name: "A", entityType: "thing", observations: ["z"] },
],
});
expect(result.isError).toBeUndefined();
expect(result.structuredContent).toEqual({
entities: [{ name: "A", entityType: "thing", observations: ["x"] }],
skipped: ["A"],
});
});
});
Loading