Skip to content

mcp: bound the brain_graph result with a whole-edge cut like Pi's #3677

Open

Description

@schlessera

Context

Found by the #3646 spike (the multi-root report in #3670, delivered to main through #3721; docs/decisions/multi-root-graph.md is now available on inspected main). The core MCP brain_graph returns walkLinks output whole ("brain_graph", packages/core/src/mcp-server.ts:444). On that spike's Odysseus large-neighbourhood scenario, it sent 62,597 characters of text and 132,968 bytes on the wire, because structuredResult sends the JSON twice. Pi's brainGraphTool (packages/ui-backend-pi/src/brain-tools.ts:432) already cuts by whole edges to its output budget and reports truncated: true with omitted_edges.

Scope

  • Bound the core MCP brain_graph model-visible result with a whole-edge cut that keeps every returned edge's endpoint nodes, matching Pi's semantics.
  • Report the cut additively (truncated, omitted_edges) in the text and structured result.

Out of scope

Multi-root traversal (rejected by #3646), depth-limit changes (filed separately), ranking or graph-engine changes.

Acceptance criteria

  • A graph larger than the bound returns valid JSON within the documented cap, with truncated: true and an exact omitted_edges; every returned edge has its endpoint nodes.
  • A graph under the bound is byte-identical to today's result.
  • The cap is 30,000 UTF-16 code units of the JSON text result (JSON.stringify(result).length, envelope, counts, warnings and escaping included). structuredContent carries the same cut object, and docs/integration-contract/mcp.md states that wire bytes can exceed the cap.
  • Mutating the cut makes a named MCP contract test fail before restoration.
  • Shipped as a ruled breaking default cap: breaking label, CONTRACT: commit with the authoritative MCP component and snapshots, and a changeset naming the cap.

Verification

Odysseus fixture with pinned 2026-07-12; real stdio MCP contract tests; bun run test, typecheck, bun run check:pr --base origin/main.

Contract impact

The extra truncation fields are additive, but a default cap also changes the contents of existing uncapped calls. The #383 ruling explicitly treats a new default read cap as a separate breaking change requiring a ruling; it does not approve a default graph cap. Keep contract and needs: decision until the compatibility branch, numeric bound/unit and accounting basis are selected. Update the authoritative MCP component and applicable snapshots in a CONTRACT-prefixed commit; the selected default-narrowing branch needs a maintainer ruling and the pre-1.0 breaking/minor assessment. An opt-in additive alternative ships in a minor.

Refs #3646

Decision required before implementation (resolved below)

The maintainer must choose (A) an explicitly approved default graph cap that can truncate an old call, with its compatibility/ruling and release assessment, or (B) a bounded opt-in input while preserving old uncapped calls. The #3646 no-go report supplies measurements and proposes follow-ups; it is not approval of either contract.

Select the numeric limit, unit (UTF-8 bytes or explicitly named UTF-16 code units), and whether it bounds each JSON representation, their combined MCP serialization, or both with separate numeric bounds. Include warnings, counts, escaping and envelope overhead; keep unresolved targets as raw link text and retain nodes only for sources and resolved targets. Pi's existing 30,000-unit budget is a comparison, not an approved MCP number or byte definition.

Recommendation: use an explicit opt-in first unless the maintainer accepts the default behavior change; either branch keeps whole-edge prefix selection and truthful truncation. Completion evidence is the recorded branch/limits/accounting ruling folded into the acceptance criteria with exact under/over-budget examples. No new multi-root tool follows from this issue.

The full single-root bounded-result deliverable fits epic #3640. No dependency on finishing the entire multi-root spike or on renaming Pi constants is invented.

Triage 2026-11-08 at main 1619741f291f525d17cde4970a5ada40644117c1: Verified the uncapped MCP and bounded Pi source, the integration-branch report and #383. Made the unresolved cap compatibility/units ruling explicit. Priority p2 reflects wanted unscheduled work; assignments and milestones are preserved.

Triage 2026-11-08 at main 8752570331ffebdbb9233196412781f6cd71f3d9: Verified the moved active source anchors and refreshed navigation. Historical measurements and source-bound failure receipts keep their identities; existing scope, rulings, needs, priority, assignment and release attribution are preserved. This source review supplies no new runtime qualification or epic completion.

Maintainer decision — 2026-11-08

option A, a default cap on MCP brain_graph, matched to Pi.

  • Cut: a whole-edge prefix cut with Pi's semantics. Every returned edge keeps its endpoint nodes, unresolved targets stay as raw link text, and nodes are kept only for sources and resolved targets. The cut adds truncated: true and an exact omitted_edges. Prefer extracting Pi's existing cut (brainGraphTool's fitter in packages/ui-backend-pi/src/brain-tools.ts) into one shared core function rather than duplicating it.
  • Bound and unit: 30,000 UTF-16 code units of the JSON result text (JSON.stringify(result).length), with the truncation envelope, counts, warnings and escaping included. This is the same accounting Pi uses.
  • Structured copy: structuredContent carries the identical cut object and is not separately bounded. The contract states that bytes on the wire can therefore exceed the cap (two representations plus framing, UTF-8).
  • Under the bound: the result is byte-identical to today's.
  • Classification: a new default cap changes the contents of existing large calls, so it is breaking. Before 1.0 it ships in a minor with the breaking label, a CONTRACT: commit updating docs/integration-contract/mcp.md and the applicable snapshots in the same commit, and a changeset that names the cap. This comment is the ruling that mcp: brain_read can return one section or stop at a token limit #383 requires for a new default cap on this tool.

2026-11-08: folded the ruling into the acceptance criteria; removed needs: decision, added breaking and agent-ready.

Activity

  1. added
    type: feat
    area: core
    area: mcp
    contract
    priority: p2
    needs: decision
    on Nov 8, 2026
  2. added
    agent-ready
    breaking
    and removed
    needs: decision
    on Nov 8, 2026
  3. schlessera commented on Nov 8, 2026

    @schlessera
    OwnerAuthor

    Decision (maintainer, 2026-11-08): option A, a default cap on MCP brain_graph, matched to Pi.

    • Cut: a whole-edge prefix cut with Pi's semantics. Every returned edge keeps its endpoint nodes, unresolved targets stay as raw link text, and nodes are kept only for sources and resolved targets. The cut adds truncated: true and an exact omitted_edges. Prefer extracting Pi's existing cut (brainGraphTool's fitter in packages/ui-backend-pi/src/brain-tools.ts) into one shared core function rather than duplicating it.
    • Bound and unit: 30,000 UTF-16 code units of the JSON result text (JSON.stringify(result).length), with the truncation envelope, counts, warnings and escaping included. This is the same accounting Pi uses.
    • Structured copy: structuredContent carries the identical cut object and is not separately bounded. The contract states that bytes on the wire can therefore exceed the cap (two representations plus framing, UTF-8).
    • Under the bound: the result is byte-identical to today's.
    • Classification: a new default cap changes the contents of existing large calls, so it is breaking. Before 1.0 it ships in a minor with the breaking label, a CONTRACT: commit updating docs/integration-contract/mcp.md and the applicable snapshots in the same commit, and a changeset that names the cap. This comment is the ruling that mcp: brain_read can return one section or stop at a token limit #383 requires for a new default cap on this tool.
  4. schlessera commented on Nov 9, 2026

    @schlessera
    OwnerAuthor

    2026-11-09: detached from epic #3640 by maintainer decision (ruling). This issue is now standalone. Its scope, labels and readiness are unchanged.

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

    agent-readyarea: corearea: mcpbreakingcontractpriority: p2type: feat

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions