Linora

MCP adapter (apps/mcp)

Owner: WP-F (now M1). ADRs 0012, 0022, 0601, 0602, 0603, 0604. Contracts: MCP_TOOLS and output types in shared/src/mcp.ts; API routes in shared/src/api.ts; realtime connection from @linorahq/sync-client/connection (WP-D).

Current (wave 2)

Shape

apps/mcp/src/
  main.ts       entry: stdio (LINORA_URL + LINORA_TOKEN) or Streamable HTTP (LINORA_MCP_TRANSPORT=http)
  http.ts       Streamable HTTP host on /mcp (limits: see "Hosted limits"):
                  PAT shape checked first, then GET /v1/me on initialize (cached 30 s per token);
                  each session is bound to its token's SHA-256, checked before the body is read;
                  bodies capped while streaming; sessions capped per token/user/host;
                  tool calls gated per session and per user; idle sessions expire after 30 min;
                  X-Forwarded-For to the API; DNS-rebinding guard when bound to loopback
  limits.ts     Gate (bounded queue), ByteBudget, KeyedBuckets (per-IP), TtlCache
  ids.ts        the one id-list parser: ids, refs and runs within the 10,000-id budget per call
  server.ts     SDK low-level Server:
                  tools/list = MCP_TOOLS (+ readOnlyHint);
                  tools/call -> validate -> tool;
                  INSTRUCTIONS (shared conventions, sent once per session)
  session.ts    AgentSession / OpenPage:
                  API client, short handles;
                  the live connection (presence kind 'agent'; realtime origin override and
                  bounded join, ADR 0604);
                  undo/redo, refs, others' change log, top-key cache;
                  read helpers (paging, ancestor spaces)
  api.ts        typed /v1 client from the shared route table:
                  errors -> ToolErrors with fix hints;
                  503/429 retried per Retry-After within 6 s
  validate.ts   JSON Schema subset validator; messages written for agents (path, fix, "did you mean")
  convert.ts    MCP form <-> JSON-op form, local kind/range checks, text measuring (@linorahq/doc/text), creation defaults
  geometry.ts   affine math identical to packages/doc (rotation about the box center, children in parent space)
  format.ts     compact renderings (tables, id runs), conservative token estimator
  handles.ts    stable short ids for projects/files/pages (sorted; 20,000 per kind)
  tools/        navigation.ts (navigation + files/pages), read.ts, edit.ts, common.ts

Tool -> API mapping

| Tool | Calls | |—|—| | list_projects / list_files / list_pages | listProjects, listFiles, getFile | | open_page | getFile -> realtime token (origin replaced by LINORA_MCP_REALTIME_URL when set) + openRoom(sinceSeq = page head) (presence), both within LINORA_MCP_JOIN_TIMEOUT_MS (15 s) -> summary (topLevelLimit 25) | | get_outline | outline (default depth 2; maxNodes sized to the token budget, see below) | | find_nodes / get_nodes | searchNodes / getNodes (field selection; ["all"]) | | get_changes | the live log of others’ broadcasts after a head check (changes?since=&limit=1); falls back to changes for older seqs | | get_peers | presence of the open room (cursor, viewport, selection, tool) | | render | render (PNG as an MCP image, SVG as text) | | create_nodes | the top-key cache (one getNodes(parent, childDepth 1, [key]) the first time a parent is used) -> CREATE_BULK blocks with ids from the connection’s clientId -> OPS -> ACK | | update_nodes | getNodes(ids, touched fields) (old values; current text/line geometry) -> SET -> ACK | | delete_nodes | getNodes(ids, all, childDepth 64, paged) -> DELETE; undo = RESTORE blocks (<= 50,000 nodes per call) | | transform_nodes | getNodes(geometry) + ancestor spaces (one getNodes per tree level) -> SET x/y/w/h/rotation (groups scale their content) | | arrange_nodes | sibling keys (getNodes(parent, childDepth 1)) -> SET PARENT_INDEX (reparent re-expresses x/y/rotation in the new parent) | | duplicate_nodes | getNodes(subtrees, all) -> CREATE_BULK with fresh ids, each copy right above its original (or on top of parentId) | | set_selection | getNodes(bounds) -> presence (cursor at the center, selection = ids) | | undo / redo | the agent’s own stack; property steps re-read current values first (ADR 0602) | | create_file / create_page / update_page / delete_page | createFile, createPage, updatePage, deletePage |

Reads after edits pass minSeq = the newest seq the agent has seen (read-your-writes). Every result reports seq.

Edits (ADR 0602)

Context management (ADR 0601)

Hosted limits (ADR 0603)

Nothing a client sends grows without a bound. Defaults are in HTTP_DEFAULTS (http.ts).

What Limit When exceeded
Ids per call (runs expanded) 10,000 (render: 500) bad_args, before anything is allocated
Request body on a session 1 MiB (LINORA_MCP_MAX_BODY_BYTES). The 10,000-node benchmark tree is 923,066 bytes 413: a declared length is refused unread, a stream is cut at the cap
Body before a session (initialize) 64 KiB 413
Bodies being read, process-wide 64 MiB 503 + retry-after: 1
Refused upload the rest is discarded, never buffered, up to 8 MiB or 10 s connection cut
Bearer value must match linora_pat_ + 43 chars 401, no API call, body not read
/v1/me per token cached 30 s (401s too; 10,000 entries) -
initialize per client address 2/s, burst 20 429 + retry-after
Sessions per token / per user / per host 10 (LINORA_MCP_SESSIONS_PER_TOKEN) / 20 / 1,000 (LINORA_MCP_MAX_SESSIONS) the least recently used idle session is evicted (host-wide: only after 5 min idle); 429 / 503 if all are busy
Tool calls per session 4 running, 16 waiting busy: ... Fix: wait ... tool error
Tool calls per user 16 running, 64 waiting busy tool error
Idle session 30 min (not while a call is in flight) closed; the client gets 404 and initializes again
Refs per page the latest 50,000; 128 chars each the oldest are forgotten / bad_args
Undo + redo per page 200 steps and 32 MiB (the newest step always kept) oldest steps dropped
Short handles / page-to-file map 20,000 per kind / 20,000 oldest forgotten

Running

Setting up an agent against a running stack (token, transports, Claude Code and Claude Desktop, troubleshooting): guides/mcp.md. The notes below are for running the adapter by hand.

Acceptance (WP-F)

Measured on an M4 Pro with a local stack (realtime + API + doc worker, Docker Postgres) by npm test -w @linorahq/mcp (e2e) and npm run bench -w @linorahq/mcp. The page is WP-A’s 4,000-page site, 662,190 nodes. Tokens use estimateTokens (format.ts): letter runs /4, digit runs /3, every symbol and newline 1. That over-counts structured text (2.35 chars/token here, against about 3.6 for real tokenizers).

Criterion Result
Handshake over stdio and Streamable HTTP; tools/list returns every MCP_TOOLS entry met (e2e: SDK clients on both transports; PAT-bound HTTP sessions; 401/403 cases)
1,000-node nested tree in one call < 1 s, visible to a browser session met: median 10 ms in-process, 10 ms over stdio, 9 ms over HTTP (max 28 ms), ACK included. 10,000 nodes: 35 ms. A human peer receives all 1,000 nodes in one broadcast
Agent cursor visible met (e2e: the human peer sees kind agent, the cursor on the new frame, its selection)
Undo reverts only agent edits met (e2e: a human’s later value and a human’s node survive the agent’s undo; redo restores the same ids)
Outline of the 662K page with defaults <= 2,000 tokens met: 1,779 estimated tokens (4,179 chars, 65 lines; about 1,160 at 3.6 chars/token), 17 ms. open_page: 673 tokens
Viewer PAT gets clear read_only errors met (viewer member and files:read-only PAT: read_only: ... Fix: ask a project owner for editor access; reads still work)
No private paths met (check-deps: shared, @linorahq/doc json/keys/inverse/text, @linorahq/sync-client/connection; the e2e test records every URL: only /v1/ and /v1/realtime)

Also covered: