Repository navigation
Description
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:
logseq.api.db-based.cli/upsert-nodes→outliner-op/batch-import-edn!(src/main/logseq/api/db_based/cli.cljs,upsert-nodes).- Worker side,
:thread-api/import-edn(src/main/frontend/worker/handler/export.cljs) callssqlite-export/validate-import-txs. 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
- Open a large DB graph (hundreds of MB) in the desktop app with the API/MCP server enabled.
POST /mcp→initialize, thentools/callupsertNodeswith oneaddblockoperation whosepage-idis a valid page uuid.- Time the response; in parallel run
logseq-cli sync status --graph <graph>and observe it times out until the write returns. - 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) invalidate-import-tx-data, instead ofvalidate-local-db!on the wholedb-after; or - give
upsert-nodesits 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 currentmaster93136e6). - 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.
Activity