Skip to content

memory: replace O(N*M + M^2) linear scans in KnowledgeGraphManager write tools with Set/Map lookups (~79x faster at 10k relations) #5693

Done

Description

@EyobelG

Which server?

memory (@modelcontextprotocol/server-memory)

Protocol era

Both, or not era-specific

MCP client

n/a (server-side graph mutation path in @modelcontextprotocol/server-memory@2.2.2)

The problem

All six write methods on KnowledgeGraphManager in src/memory/index.ts currently perform quadratic $O(N \cdot M + M^2)$ linear scans (and repeated .slice(0, index) allocations) instead of Set/Map lookups when deduplicating or matching against the loaded graph:

  • createEntities (src/memory/index.ts:1193-1200): calls graph.entities.some(...) and entities.slice(0, index).some(...) for every incoming entity.
  • createRelations (src/memory/index.ts:1229-1238): calls graph.relations.some(...) and relations.slice(0, index).some(...) for every incoming relation.
  • addObservations (src/memory/index.ts:1255-1267): calls graph.entities.find(...) per entry and entity.observations.includes(...) per observation.
  • deleteEntities (src/memory/index.ts:1280-1288): builds present = new Set(...) on line 1280, then still scans entityNames.includes(...) on every entity and relation.
  • deleteObservations (src/memory/index.ts:1301-1312): calls graph.entities.find(...) and d.observations.includes(...) inside filter.
  • deleteRelations (src/memory/index.ts:1324-1332): calls relations.some(...) for every relation in graph.relations.

On a 10,000-relation graph with a 2,000-relation batch, a Node.js benchmark of the current createRelations deduplication loop vs. a Set-indexed lookup measured 329.71 ms vs. 4.17 ms (~79.1x speedup)—and the gap widens further as graph/batch sizes grow. Because every write runs inside withLock(), slower write calls also hold the file lock longer under concurrent clients.

Solution you have in mind (optional)

Index existing and incoming items with Set / Map in $O(N + M)$:

  1. createEntities: Seed seen = new Set(graph.entities.map((e) => e.name)), then iterate entities once, adding unseen names to seen and newEntities.
  2. createRelations / deleteRelations: Index relations with a collision-free key (e.g., nested Map<string, Map<string, Set<string>>> or JSON.stringify([r.from, r.to, r.relationType])—avoiding separator collisions since entity names are arbitrary byte strings) so deduplication and deletion run in $O(N + M)$.
  3. addObservations / deleteObservations: Build entityByName = new Map(graph.entities.map((e) => [e.name, e])) once per call, and use a Set<string> for entity.observations / d.observations.
  4. deleteEntities: Use const toDelete = new Set(entityNames) for the graph.entities and graph.relations filters instead of entityNames.includes(...).

Alternatives or workarounds you have tried

Keeping batches small in the client reduces the $M^2$ term, but the $O(N \cdot M)$ scan against the stored graph remains until KnowledgeGraphManager indexes by Set/Map.

Before you submit

  • I searched existing issues and this is not a duplicate.
  • This is a request for this repository or one of its servers, not a new server, the MCP specification or an SDK.

Activity

  1. added
    enhancement
    server-memory
    on Nov 30, 2026
  2. cliffhall commented on Nov 30, 2026

    @cliffhall
    Member

    Triage: Priority Low (total 4)

    • Severity 2: the write tools scan the graph linearly, which only matters for very large graphs and doesn't break anything
    • Urgency 2: wanted eventually, nothing blocked on it
    • Bonuses: none

    Board: Servers V2 (#43), Status Incoming, awaiting maintainer review (no milestone yet).

  3. added this to the v2.7.0 milestone on Nov 30, 2026
  4. self-assigned this
    on Nov 30, 2026
  5. added 2 commits that reference this issue on Nov 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

enhancementserver-memoryv2

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions