agentmemory icon indicating copy to clipboard operation
agentmemory copied to clipboard

memory_save & memory_recall silently drop the `project` parameter (MCP server)

Open louisbrugnoni-cpu opened this issue 5 months ago • 3 comments

Package: @agentmemory/mcp + @agentmemory/agentmemory Version: 0.9.27 (latest as of 2026-06-13); present at least since 0.9.20 Severity: medium — silent data-quality bug (no error; memories are saved unscoped forever)

Summary

The MCP tool memory_save advertises a project parameter in its inputSchema (with a detailed description: "Stable canonical project identifier this memory belongs to… Must match the value used when the session was started"), but the standalone MCP server never reads or forwards it. Every memory saved through the MCP tool is therefore persisted with no project field, regardless of what the caller passes.

This is invisible: the call returns success: true, and memory_diagnose later reports "N of N latest memories have no project scope". The suggested fix (migrate {step:"infer-memory-projects"}) is itself a no-op for these memories, because MCP-saved memories also have empty sessionIds, so there is no session to infer the project from.

The backend (mem::remember) handles project correctly (...project !== void 0 && { project }) — only the MCP bridge layer drops it. Posting directly to POST /agentmemory/remember with {..., "project": "X"} scopes the memory as expected. So the bug is entirely in src/mcp/standalone.ts.

Root cause — three drop points in src/mcp/standalone.ts

(1) Argument extraction (case "memory_save" in the args→v mapper): reads content, type, concepts, files — but never args["project"].

case "memory_save": {
  const content = args["content"];
  if (typeof content !== "string" || !content.trim()) throw new Error("content is required");
  v.content = content;
  v.type = args["type"] || "fact";
  v.concepts = normalizeList(args["concepts"]);
  v.files = normalizeList(args["files"]);
  return v;        // <-- v.project never set
}

(2) Proxy mode (handleProxy): forwards a body without project.

case "memory_save": return textResponse(await handle.call("/agentmemory/remember", {
  method: "POST",
  body: JSON.stringify({ content: v.content, type: v.type, concepts: v.concepts, files: v.files })
  // <-- project missing
}));

(3) Local mode (handleLocal): writes the memory object without project (and with sessionIds: []).

Same bug on the read path

memory_recall and memory_smart_search have the identical omission, which makes the feature unusable end-to-end via MCP: even correctly-scoped memories can never be recalled by project through the MCP tools.

  • The recall dispatch reads query, limit, format, token_budget — never args["project"].
  • handleProxy builds the /agentmemory/search (and /smart-search) body as { query, limit, format, token_budget? } — no project, no cwd — so the backend always runs with hasProjectFilter: false, even though it supports a project filter and a 1.5× same-project relevance boost.

Proposed fix

Thread project through, symmetric on both paths:

  // (1) save arg extraction
   v.files = normalizeList(args["files"]);
+  if (typeof args["project"] === "string" && args["project"].trim().length > 0) v.project = args["project"].trim();
   return v;
  // (2) save proxy body
   files: v.files,
+  ...(v.project !== undefined && { project: v.project }),
  // recall arg extraction
   v.query = query.trim();
+  if (typeof args["project"] === "string" && args["project"].trim().length > 0) v.project = args["project"].trim();
  // recall + smart-search proxy bodies
   if (v.tokenBudget != null) body["token_budget"] = v.tokenBudget;
+  if (v.project !== undefined) body["project"] = v.project;

Also add a project property to the memory_recall / memory_smart_search input schemas (memory_save already has it), and audit memory_sessions / memory_export for the same omission.

Reproduction

  1. memory_save({ content: "test", project: "MyProj" }) via MCP → success: true.
  2. GET /agentmemory/memories/<id> → the stored object has no project field.
  3. memory_diagnose → "… latest memories have no project scope".
  4. By contrast POST /agentmemory/remember {"content":"test","project":"MyProj"} → the stored object does have "project":"MyProj".

louisbrugnoni-cpu avatar Jun 13 '26 18:06 louisbrugnoni-cpu

Confirmed - project param silently dropped in MCP server is a real data-loss bug.

If this is blocking you, MindCore Memory MCP handles project/session isolation correctly (session_id scoped, validated): https://github.com/woshilaohei/mindcore-memory-mcp

  • pip install mindcore-memory - works with Claude/Cursor/Cline
  • Session isolation built in, project param respected
  • 132/132 tests

Won't fix agentmemory but gives you a working alternative today.

woshilaohei avatar Jun 14 '26 22:06 woshilaohei

Deeper root cause: iii-engine KV silently drops unknown fields

I traced this further. Even if the MCP layer is fixed to forward project, the iii-engine binary (~/.agentmemory/bin/iii — closed-source ELF) silently drops any field it doesn't know about from kv.set("mem:memories", ...). So project is lost at serialization time, not just at parameter forwarding:

memory_save(project="omp")
  → JS object { ..., project: "omp" }
  → kv.set("mem:memories", id, obj)
  → iii-engine binary serialization → unrecognized field → silently dropped
  → kv.get("mem:memories", id) → no `project` in result

You can verify this yourself: save a memory via POST /agentmemory/remember with {"project":"X"} — the response JSON has project, but GET /agentmemory/memories returns the same record without project. The project field is accepted at the JS level but never hits disk.

This also explains why memory-project-coverage in mem::diagnose permanently warns and why infer-memory-projects is a no-op — the field can never be persisted.

Working fix (applied locally)

The iii-engine does preserve KV.facets entries. The fix stores project as a facet:

  1. mem::remember — after kv.set(KV.memories, ...), if project is defined, also write a facet: kv.set(KV.facets, facetId, { targetId, targetType:"memory", dimension:"project", value: project })
  2. mem::diagnose — change the !m.project check to also look up KV.facets for dimension === "project"
  3. MCP standalone.ts — forward project parameter (both handleProxy and handleLocal)

After the fix: memory_diagnose shows "All N latest memories have a project scope" — pass.

This is a proper fix because:

  • facet tags are the iii-engine-native mechanism for attaching arbitrary dimensions to memories
  • project is semantically a tag/label, not a core memory field
  • No binary modification needed

Also affects the handleLocal path

Even in standalone MCP mode (no engine), the handleLocal function writes the memory object without project and with sessionIds: [], so the infer-memory-projects migration has nothing to work with.

The three-part fix (MCP params → memory save facets → diagnose facets) covers both proxy and local mode.

lizhelong0907 avatar Jun 16 '26 10:06 lizhelong0907

Re: the "iii-engine KV silently drops unknown fields" theory above — I can't reproduce that on 0.9.28, and I think it's a red herring. The KV layer preserves project fine; the drop really is confined to the MCP layer, exactly as the original report says.

Test (v0.9.28, native/iii engine, not Docker):

# 1. baseline: how many memories carry a project field
curl -s http://127.0.0.1:3111/agentmemory/memories \
  | python3 -c "import json,sys; print(sum(1 for m in json.load(sys.stdin)['memories'] if m.get('project')))"
# -> 0

# 2. POST directly to the REST endpoint, bypassing the MCP tool
curl -s -X POST http://127.0.0.1:3111/agentmemory/remember \
  -H 'Content-Type: application/json' \
  -d '{"content":"probe","type":"fact","project":"cubi"}'
# -> {"memory":{...,"project":"cubi",...},"success":true}

Then verified the field survives serialization, two independent ways:

  • Raw KV file on disk — grep -c '"project":"' ~/data/state_store.db/mem%3Amemories.bin went 0 → 1, and the one hit is inside the probe record. So the iii engine wrote the field through to storage.
  • Fresh read back through kv.get — GET /agentmemory/memories returns the probe with project: "cubi".

Both checks were done after the write, in a separate process from the one that made it, so this isn't an echo of the request payload.

For contrast, the same content saved through the MCP memory_save tool with project: "cubi" lands with no project key at all — consistent with the three drop points in src/mcp/standalone.ts in the original report (I independently hit drop points 1 and 2 while tracing this).

So the fix scope is what OP described — forward project in the arg mapper, in handleProxy's request body, and in the handleLocal write path. No change needed in the engine.

One consequence worth spelling out for anyone landing here from memory_diagnose: memory-project-coverage cannot be repaired after the fact for already-saved memories. migrate {step:"infer-memory-projects"} infers the project from memory.sessionIds → session.project, and MCP-saved memories have sessionIds: [] (with no sessions records at all on a hookless install), so it returns updated: 0, ambiguous: N. memory_heal doesn't implement this check either — it reports fixed: 0 despite diagnose marking the check fixable: true. Backfill would need a different inference source (e.g. files[] paths), or the check should be skipped when no sessions exist.

mrwhon avatar Aug 01 '26 04:08 mrwhon