Skip to content

MCP upsertNodes takes 60–140 s on a large DB graph: the batch EDN import validates the whole graph on every call #1265

Closed
logseq/logseq
#13388

Description

@arqueon

Search first

I searched open and closed issues for upsertNodes, MCP slow, validate-import and found nothing related.

What Happened?

Every upsertNodes call through the built-in MCP server (src/electron/electron/mcp_server.cljs) takes 60–140 s on a ~400 MB DB graph (~180k entities), independently of how many operations or how long the block titles are. A single-block write and a dry-run: true call differ by three orders of magnitude (36 ms vs 66–92 s). While the call runs, the db worker is busy at 100 % CPU and does not answer anything else (every other MCP call, logseq-cli sync status, etc. wait or time out).

The same write done with logseq-cli upsert block against the same graph takes 0.4–0.6 s.

Root cause (verified with a CPU profile of db-worker-node)

upsertNodes is implemented as a batch EDN import, and the import path validates the whole graph on every call:

  1. logseq.api.db-based.cli/upsert-nodes → outliner-op/batch-import-edn! (src/main/logseq/api/db_based/cli.cljs, upsert-nodes).
  2. Worker side, :thread-api/import-edn (src/main/frontend/worker/handler/export.cljs) calls sqlite-export/validate-import-txs.
  3. validate-import-tx-data (deps/db/src/logseq/db/sqlite/export.cljs) does (d/with db tx-data) and then (db-validate/validate-local-db! db-after) — i.e. it validates every entity of the resulting DB, not only the entities touched by the import — and may loop.

CPU profile of the worker during one 71 s upsertNodes (Node inspector, 36 619 samples, 76 s): 92 % of the time is inside a single remoteInvoke; ~67 % is the validator (reading all entities + malli checks), ~30 % Datascript index operations, 6 % idle.

This makes sense for a one-off import of a file, but upsertNodes is the only write tool the MCP server exposes, so agents pay a full-graph validation for every single block they add. The cost grows with graph size, not with the size of the write.

Measurements (Logseq DB 2.0.1, Electron desktop build, Linux x64, headless under Xvfb; graph ~400 MB)

Call Time
MCP upsertNodes, 1 block, title 30 chars 86 s
MCP upsertNodes, 1 block, title 300 / 1500 / 4000 chars 68 s / 67 s / 82 s
MCP upsertNodes, dry-run: true 36 ms
MCP getPage while a write is running 30–70 s (queued behind the worker)
logseq-cli upsert block, title 30 / 1500 chars 406 ms / 458 ms
19 real agent writes over 4 days (server log responseTime) all between 60 s and 140 s

Reproduce the Bug

  1. Open a large DB graph (hundreds of MB) in the desktop app with the API/MCP server enabled.
  2. POST /mcp → initialize, then tools/call upsertNodes with one add block operation whose page-id is a valid page uuid.
  3. Time the response; in parallel run logseq-cli sync status --graph <graph> and observe it times out until the write returns.
  4. Compare with logseq-cli upsert block --target-page … --content … (sub-second).

Expected Behavior

A single-block write through MCP should cost about the same as the equivalent logseq-cli upsert block. Suggestions:

  • Validate only the entities present in tx-data (plus their direct refs) in validate-import-tx-data, instead of validate-local-db! on the whole db-after; or
  • give upsert-nodes its own path that transacts the built EDN with per-entity validation and reserves the full-graph validation for file imports (import-edn); or
  • at minimum expose an option to skip the global validation for API/MCP writes.

Desktop or Mobile Platform Information

  • Logseq DB 2.0.1 (desktop build from master, commit 582080b; the code path above is unchanged on current master 93136e6).
  • Linux x64 (container, Xvfb), db worker as Node process (db-worker-node.js).
  • Graph: ~400 MB SQLite, ~180k entities, self-hosted sync (local-token mode).

Additional Context

Related observation (may deserve its own issue): If page-id is a page name instead of a uuid, upsertNodes returns the success text "Added: {:block 1}." after the same 60–90 s, but nothing is written to the graph. A validation error (or resolving the name) would be safer for agents.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions