Skip to content

feat(mcp): bound inline response bytes and harden input parsing - #364

Merged
samzong merged 1 commit into
mainfrom
feat/mcp-inline-budgets
Nov 23, 2026
Merged

samzong merged 1 commit into
mainfrom
feat/mcp-inline-budgets

Conversation

@samzong

@samzong samzong commented Nov 22, 2026

Copy link
Copy Markdown
Owner

Changes

  • Cap default MCP responses at 8,800 UTF-8 bytes across get_session, search_messages, search_sessions, list_recent_sessions, and file_history, while keeping CLI protocol_version = 2 output unchanged.
  • Bound paged get_session responses to 8,800 UTF-8 bytes with exact JSON-escaped character accounting, lossless first_message_byte_offset and next_cursor continuation, and bounded include_events output.
  • Default get_session event_ref max_bytes to 8,800 and fit evidence chunks directly to UTF-8 character boundaries instead of dividing remaining bytes by six.
  • Strip ANSI CSI/OSC sequences and normalize carriage-return progress frames on lossy MCP excerpts and summaries, then distribute remaining UTF-8 byte budgets with discrete water-filling.
  • Trim default target_project file_history pages to fit 8,800 UTF-8 bytes and advance next_cursor from the last retained event.
  • Accept dual string, array, and expanded-object forms across MCP tool inputs while preserving JSON Schemas, and return explicit stop-retry errors when nested call_mcp_tool serialization corrupts arguments.
  • Update url from 2.5.8 to 2.5.9 in Cargo.lock for RUSTSEC-2026-0592.

Why

  • MCP hosts spill large tool responses out of inline context, and CJK transcripts or multi-hit excerpts exceeded inline limits under character-only caps.
  • Dividing event_ref chunk budgets by six wasted most of each response page for normal UTF-8 JSON payloads.
  • Nested call_mcp_tool callers sometimes stringify numbers and booleans, expand opaque JSON cursors into objects, or collapse arrays into invalid values that previously triggered repeated identical retries.

Test evidence

  • Tool and OS: recall 0.7.0 (./target/debug/recall) on macOS 27.2 (Build 26B123).
  • Repository gate: make check passed (cargo audit, cargo fmt --all -- --check, workspace Clippy with --all-targets --features bench -- -D warnings, and all workspace unit, integration, and doc tests).
  • Fault injection: injected targeted faults into src/mcp/read_session.rs, src/event_evidence.rs, src/mcp/budget.rs, src/mcp/file_history.rs, src/mcp/input.rs, and src/db/message_store.rs. Observed all six new unit tests fail on the broken code (page 0 exceeded inline budget: 19613 bytes, first page wasted inline budget: 4156 bytes, unstripped ANSI/\r, target file_history page 0 exceeded budget: 28828 bytes, and accepted empty source: []), then restored the implementation and observed all tests pass.
  • Real stdio JSON-RPC run: imported ten CJK sessions (including a 6,000-character Chinese transcript with ANSI and \r bytes plus 18 structured file_write events and a 15 KB attrs_json payload) into an isolated temporary SQLite database and queried ./target/debug/recall mcp --db <temp.db> over stdio JSON-RPC 2.0:
    • list_recent_sessions: 8,799 UTF-8 bytes (<= 8,800), ANSI and overwritten \r frames stripped.
    • search_sessions (with array source: ["codex", "claude-code"]): 8,515 UTF-8 bytes (<= 8,800), 10 hits returned.
    • search_messages: 5,401 UTF-8 bytes (<= 8,800), 10 hits returned, short hit preserved in full, ANSI/OSC/\r stripped.
    • file_history (target_project, default limit omitted, resumed with expanded JSON object cursor): 4 pages of 8,606, 8,574, 8,798, and 7,417 UTF-8 bytes (<= 8,800), reconstructing all 18 events in exact order 17..0 without drops or duplicates.
    • get_session paged mode (6,000-character Chinese message with string integer selectors and expanded JSON object cursor): 3 pages of 8,652, 8,657, and 4,046 UTF-8 bytes (<= 8,800), reassembling all 6,000 characters byte-for-byte including raw ANSI and \r.
    • get_session event_ref mode (default max_bytes omitted, expanded JSON object event_ref and cursor): 2 pages of 8,800 and 8,770 UTF-8 bytes (<= 8,800), matching the SHA-256 content_digest.
    • Corrupted inputs (source: [], cursor: true, around_seq: -1): returned isError: true with call_mcp_tool corruption details and Do NOT retry the same payload unchanged.
  • Unverified: live third-party host UI rendering in Claude Desktop, Cursor, and Antigravity desktop clients was not exercised directly beyond stdio JSON-RPC tools/call transport and recall mcp capabilities --format json schema comparison.

Keep default MCP responses within an 8,800-byte UTF-8 inline budget so hosts do not spill results to disk.

Add optional rendered JSON byte caps to paged session message reads while preserving lossless UTF-8 byte offsets and cursors. Fit event evidence chunks directly to UTF-8 code point boundaries with an 8,800-byte default instead of dividing by six.

Strip ANSI control sequences and terminal carriage-return overwrites from lossy summaries, and allocate excerpt budgets with discrete water-filling. Trim default structured file-history pages to fit 8,800 bytes and emit continuation cursors.

Accept dual string, array, and object forms across MCP parameters and return explicit stop-retry guidance when nested tool serialization corrupts inputs. Update url to 2.5.9 in Cargo.lock for RUSTSEC-2026-0592.

Signed-off-by: samzong <samzong.lu@gmail.com>
@samzong
samzong marked this pull request as ready for review November 23, 2026 00:55
@samzong
samzong merged commit af30aeb into main Nov 23, 2026
3 checks passed
@samzong
samzong deleted the feat/mcp-inline-budgets branch November 23, 2026 00:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant