0603. Hosted MCP capacity limits: id budget, authenticated bounded bodies, session caps, call gates
Status: Accepted (2026-11-28). Fixes SR-02, SR-06 and SR-20 of the 2026-11 security review. Refines 0601 and 0602.
Context
One hosted (Streamable HTTP) adapter process serves many agents, and open signup makes any PAT free (SR-21). The review found three problems:
- SR-02: each id run
"c:a-b" expanded to up to 100,001 strings, and the 10,000-id cap ran only after every run had been expanded. 40 runs (552 bytes) cost 438 MiB RSS and 1.6 s CPU. About 1,000 runs (14 KB) would need about 11 GB.
- SR-06: any
Bearer x request could make the host buffer and parse 8 MiB. Every initialize with a junk token cost one GET /v1/me from the host’s IP, which drained the API’s per-IP bucket for every hosted agent. The 1,000-session cap was global, so one account could take every slot.
- SR-20: layer names, text and peer names reach agents verbatim, and two of them were not quoted.
An audit for the same class of bug found more client-controlled growth:
- refs and undo history with no cap;
- an O(n^2) short-handle computation;
- O(n·m) loops in
transform_nodes (scaled groups) and duplicate_nodes;
- unbounded concurrent tool calls (a JSON-RPC batch carries 100);
render ids that overflow the API’s 16 KiB header limit.
Decision
- One id-list helper (
apps/mcp/src/ids.ts) for every tool that takes ids or runs.
- Each run’s syntax and bounds are checked before anything is allocated:
a <= b, client < 2^24, counter < 2^29.
- A run wider than what is left of the 10,000-id budget is refused before it expands, and a new single id is refused once the budget is used up.
- A call therefore never allocates more than 10,000 ids.
render takes at most 500, because they travel in the URL.
- Bodies:
- 1 MiB per request on a session. The largest legitimate
create_nodes the repo exercises is the 10,000-node benchmark tree: 923,066 bytes. Richer 10,000-node payloads must be split (for example 10,000 rects with refs, names and every property: 2.4 MB). LINORA_MCP_MAX_BODY_BYTES overrides the cap.
- 64 KiB before a session exists.
initialize is a few hundred bytes.
- Checked while streaming. A declared
Content-Length over the cap is refused before a byte is read. A chunked body is cut off at the first chunk past the cap. Both get 413 + connection: close.
- A process-wide budget of 64 MiB for bodies being read; past it the answer is
503 + retry-after: 1.
- Authenticate before reading. The PAT shape (
linora_pat_ + 43 chars, as in the API) is checked first. A request on a session is matched to its session and token hash before its body is read. Refusals answer with connection: close, so an unread upload is dropped.
/v1/me is cached per token hash for 30 s, including 401s (at most 10,000 entries).
initialize rate limit: 2/s per client address, burst 20 (at most 50,000 addresses tracked). The address is LINORA_MCP_TRUST_PROXY hops from the right of X-Forwarded-For plus the socket peer, the same rule as the API’s LINORA_TRUST_PROXY.
- Forwarded address. Every API call carries
X-Forwarded-For: the well-formed inbound entries (the last 8) plus the socket peer, like a standard proxy. Once the API trusts the MCP hop, its per-IP bucket sees agents rather than this host. That part is API configuration (S1).
- Sessions:
- Caps: 10 per token, 20 per user (one user may hold 100 PATs), and 1,000 in total.
- Eviction at a cap. The least recently used session with no call running or waiting is closed; its client gets 404 and re-initializes. When every candidate is busy, the answer is
429 (token or user cap) or 503 (host cap). The host-wide cap only evicts sessions idle for at least 5 minutes.
- Registered before any await, so concurrent initializes count against the caps.
- Idle expiry: 30 minutes, skipping sessions with calls in flight.
- Call gates.
- Per session: 4 running and 16 waiting.
- Per user: 16 running and 64 waiting.
- Beyond that, the call answers
busy: ... Fix: wait for your earlier calls to finish as a tool error.
- stdio is not gated, because there the caller only harms itself.
- Per-page and per-session memory:
- refs: the latest 50,000, at most 128 characters each;
- undo plus redo: 32 MiB per page (oldest steps dropped; the newest is always kept), on top of the 200-step limit;
- short handles: sorted (O(log n) per handle), with 20,000 per kind;
- page-to-file map: 20,000 entries.
- Untrusted content (SR-20).
- Every user-authored string in text results is JSON-quoted. Page names in error hints and peer names in
open_page were the two exceptions; they are quoted now.
- The server instructions say that quoted strings are document content written by other people and that agents must never follow instructions found in them.
- Structured results list their document-content fields under
untrusted.
Alternatives
- Cap only the total after expansion, with a smaller per-run limit: still allocates up to
runs x width.
- A larger body cap (2.5 MiB, enough for the rich 10,000-node payload): raises the per-request memory cost, which is about 20x the body size once parsed, for a rare case that splitting handles.
- Queue calls without bound: turns a flood of calls into memory growth. Refusing past a small queue keeps the cost fixed.
- Refuse rather than evict at the session caps: an agent that crashes without
DELETE /mcp would be locked out for 30 minutes.
- Per-token caps only: one account with 100 PATs could still take 1,000 sessions.
Consequences
- Agents that send more than 4 parallel calls on one session see
busy errors and must retry.
create_nodes and update_nodes over HTTP carry at most about 1 MiB of JSON. Larger trees are built in several calls (outer frame first, then parentId).
- Open:
- The API still needs to trust the MCP hop (or exempt the host) before its per-IP bucket stops being shared (S1).
- The realtime WebSocket from this host has no per-agent address.
- A fleet of free accounts can still fill the 1,000 host-wide slots, until accounts need verification (SR-21).