Skip to content

Commit e1d154e

Browse files
authored
Merge pull request #5534 from modelcontextprotocol/v2/feat/5525-memory-write-caps
feat(memory): opt-in write limits on graph size
2 parents c991ded + fba4d6b commit e1d154e

6 files changed

Lines changed: 623 additions & 62 deletions

File tree

‎.changeset/memory-write-limits.md‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@modelcontextprotocol/server-memory": minor
3+
---
4+
5+
Add opt-in write limits: `MEMORY_MAX_ENTITIES`, `MEMORY_MAX_RELATIONS` and `MEMORY_MAX_FILE_BYTES` cap how large the write tools may grow the graph, and the README now documents that every write rewrites the whole memory file.

‎src/memory/README.md‎

Lines changed: 52 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,15 @@ Published on npm as [`@modelcontextprotocol/server-memory`](https://www.npmjs.co
77
## Core Concepts
88

99
### Entities
10+
1011
Entities are the primary nodes in the knowledge graph. Each entity has:
12+
1113
- A unique name (identifier)
1214
- An entity type (e.g., "person", "organization", "event")
1315
- A list of observations
1416

1517
Example:
18+
1619
```json
1720
{
1821
"name": "John_Smith",
@@ -22,17 +25,21 @@ Example:
2225
```
2326

2427
### Relations
28+
2529
Relations define directed connections between entities. They are always stored in active voice and describe how entities interact or relate to each other.
2630

2731
Example:
32+
2833
```json
2934
{
3035
"from": "John_Smith",
3136
"to": "Anthropic",
3237
"relationType": "works_at"
3338
}
3439
```
40+
3541
### Observations
42+
3643
Observations are discrete pieces of information about an entity. They are:
3744

3845
- Stored as strings
@@ -41,6 +48,7 @@ Observations are discrete pieces of information about an entity. They are:
4148
- Should be atomic (one fact per observation)
4249

4350
Example:
51+
4452
```json
4553
{
4654
"entityName": "John_Smith",
@@ -55,6 +63,7 @@ Example:
5563
## API
5664

5765
### Tools
66+
5867
- **create_entities**
5968
- Create multiple new entities in the knowledge graph
6069
- Input: `entities` (array of objects)
@@ -195,22 +204,27 @@ Add this to your claude_desktop_config.json:
195204
"mcpServers": {
196205
"memory": {
197206
"command": "docker",
198-
"args": ["run", "-i", "-v", "claude-memory:/app/dist", "--rm", "mcp/memory"]
207+
"args": [
208+
"run",
209+
"-i",
210+
"-v",
211+
"claude-memory:/app/dist",
212+
"--rm",
213+
"mcp/memory"
214+
]
199215
}
200216
}
201217
}
202218
```
203219

204220
#### NPX
221+
205222
```json
206223
{
207224
"mcpServers": {
208225
"memory": {
209226
"command": "npx",
210-
"args": [
211-
"-y",
212-
"@modelcontextprotocol/server-memory"
213-
]
227+
"args": ["-y", "@modelcontextprotocol/server-memory"]
214228
}
215229
}
216230
}
@@ -223,12 +237,7 @@ On Windows, use `cmd /c` to launch `npx`:
223237
"mcpServers": {
224238
"memory": {
225239
"command": "cmd",
226-
"args": [
227-
"/c",
228-
"npx",
229-
"-y",
230-
"@modelcontextprotocol/server-memory"
231-
]
240+
"args": ["/c", "npx", "-y", "@modelcontextprotocol/server-memory"]
232241
}
233242
}
234243
}
@@ -243,10 +252,7 @@ The server can be configured using the following environment variables:
243252
"mcpServers": {
244253
"memory": {
245254
"command": "npx",
246-
"args": [
247-
"-y",
248-
"@modelcontextprotocol/server-memory"
249-
],
255+
"args": ["-y", "@modelcontextprotocol/server-memory"],
250256
"env": {
251257
"MEMORY_FILE_PATH": "/path/to/custom/memory.jsonl"
252258
}
@@ -262,12 +268,7 @@ On Windows, use:
262268
"mcpServers": {
263269
"memory": {
264270
"command": "cmd",
265-
"args": [
266-
"/c",
267-
"npx",
268-
"-y",
269-
"@modelcontextprotocol/server-memory"
270-
],
271+
"args": ["/c", "npx", "-y", "@modelcontextprotocol/server-memory"],
271272
"env": {
272273
"MEMORY_FILE_PATH": "/path/to/custom/memory.jsonl"
273274
}
@@ -277,6 +278,11 @@ On Windows, use:
277278
```
278279

279280
- `MEMORY_FILE_PATH`: Path to the memory storage JSONL file (default: `memory.jsonl` in the server directory). A leading `~` expands to your home directory; a relative path resolves against the server directory. Set it to an absolute path you own; see [Choosing where the graph is stored](#choosing-where-the-graph-is-stored).
281+
- `MEMORY_MAX_ENTITIES`: The most entities the graph may hold. Unset by default (no limit).
282+
- `MEMORY_MAX_RELATIONS`: The most relations the graph may hold. Unset by default (no limit).
283+
- `MEMORY_MAX_FILE_BYTES`: The most bytes the memory file may hold. Unset by default (no limit).
284+
285+
The three limits are opt-in, and each is described under [Limiting how large the graph grows](#limiting-how-large-the-graph-grows). A variable that is set must hold a positive whole number; any other value, an empty one included, stops the server at startup with an error naming the variable.
280286

281287
When `MEMORY_FILE_PATH` is not set and the server directory still holds a legacy `memory.json` but no `memory.jsonl`, the server renames `memory.json` to `memory.jsonl` at startup. If that rename fails, the server exits with an error naming both paths and the cause, rather than starting on an empty graph; fix the cause (a permission problem, say) and restart, or set `MEMORY_FILE_PATH` to choose a file explicitly.
282288

@@ -299,6 +305,17 @@ Each MCP client starts its own server process, so two clients using the same `ME
299305
- A write that cannot get the lock within 60 seconds fails with a tool error rather than overwriting the file.
300306
- 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.
301307

308+
#### Limiting how large the graph grows
309+
310+
Every write tool reads the whole memory file, changes the graph and writes the whole file back, holding the lock throughout. Its cost, and how long other servers sharing the file wait for the lock, therefore grow linearly with the file, however small the change: adding one entity to a graph of 100,000 entities rewrites the whole file, about 24 MB at around 240 bytes per entity, in roughly 0.4 seconds on a laptop (#5525). The read tools load the whole file too. Size a shared file with that in mind.
311+
312+
By default nothing caps the graph. Set `MEMORY_MAX_ENTITIES`, `MEMORY_MAX_RELATIONS` or `MEMORY_MAX_FILE_BYTES` to put a ceiling on it:
313+
314+
- `create_entities`, `create_relations` and `add_observations` refuse a call that would leave the graph over a limit, with a tool error naming the variable. The whole call is refused and nothing is saved, not just the part past the limit.
315+
- A call that adds nothing new (every entity or relation already exists, say) is still answered, even when the graph is at or over a limit.
316+
- 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.
317+
- `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.
318+
302319
# VS Code Installation Instructions
303320

304321
For quick installation, use one of the one-click installation buttons below:
@@ -324,10 +341,7 @@ Alternatively, you can add the configuration to a file called `.vscode/mcp.json`
324341
"servers": {
325342
"memory": {
326343
"command": "npx",
327-
"args": [
328-
"-y",
329-
"@modelcontextprotocol/server-memory"
330-
]
344+
"args": ["-y", "@modelcontextprotocol/server-memory"]
331345
}
332346
}
333347
}
@@ -340,12 +354,7 @@ On Windows, use:
340354
"servers": {
341355
"memory": {
342356
"command": "cmd",
343-
"args": [
344-
"/c",
345-
"npx",
346-
"-y",
347-
"@modelcontextprotocol/server-memory"
348-
]
357+
"args": ["/c", "npx", "-y", "@modelcontextprotocol/server-memory"]
349358
}
350359
}
351360
}
@@ -375,7 +384,7 @@ On Windows, use:
375384

376385
The prompt for utilizing memory depends on the use case. Changing the prompt will help the model determine the frequency and types of memories created.
377386

378-
Here is an example prompt for chat personalization. You could use this prompt in the "Custom Instructions" field of a [Claude.ai Project](https://www.anthropic.com/news/projects).
387+
Here is an example prompt for chat personalization. You could use this prompt in the "Custom Instructions" field of a [Claude.ai Project](https://www.anthropic.com/news/projects).
379388

380389
```
381390
Follow these steps for each interaction:
@@ -408,7 +417,7 @@ Follow these steps for each interaction:
408417
Docker:
409418

410419
```sh
411-
docker build -t mcp/memory -f src/memory/Dockerfile .
420+
docker build -t mcp/memory -f src/memory/Dockerfile .
412421
```
413422

414423
### Upgrading a Docker install
@@ -435,7 +444,16 @@ Mounting a volume on a directory of its own and pointing `MEMORY_FILE_PATH` at i
435444
"mcpServers": {
436445
"memory": {
437446
"command": "docker",
438-
"args": ["run", "-i", "-v", "claude-memory-data:/data", "-e", "MEMORY_FILE_PATH=/data/memory.jsonl", "--rm", "mcp/memory"]
447+
"args": [
448+
"run",
449+
"-i",
450+
"-v",
451+
"claude-memory-data:/data",
452+
"-e",
453+
"MEMORY_FILE_PATH=/data/memory.jsonl",
454+
"--rm",
455+
"mcp/memory"
456+
]
439457
}
440458
}
441459
}

‎src/memory/__tests__/helpers.ts‎

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import { InMemoryTransport } from "@modelcontextprotocol/server";
1010
import type { McpServer, CallToolResult } from "@modelcontextprotocol/server";
1111
import { Client } from "@modelcontextprotocol/client";
1212
import { createServer } from "../index.js";
13+
import type { GraphLimits } from "../index.js";
1314

1415
export interface Connection {
1516
client: Client;
@@ -18,9 +19,13 @@ export interface Connection {
1819
close: () => Promise<void>;
1920
}
2021

21-
// Connect a fresh client to a fresh server over the graph file at filePath.
22-
export async function connect(filePath: string): Promise<Connection> {
23-
const server = createServer(filePath);
22+
// Connect a fresh client to a fresh server over the graph file at filePath,
23+
// with the write limits given (none by default).
24+
export async function connect(
25+
filePath: string,
26+
limits: GraphLimits = {},
27+
): Promise<Connection> {
28+
const server = createServer(filePath, "legacy", undefined, limits);
2429
const [clientTransport, serverTransport] =
2530
InMemoryTransport.createLinkedPair();
2631
const client = new Client({ name: "memory-test-client", version: "0.0.0" });

0 commit comments

Comments
 (0)