Skip to content

fix(transport): reply to oversized stdio requests - #1471

Open
jonxsong wants to merge 4 commits into
modelcontextprotocol:mainfrom
jonxsong:fix/1395-oversized-stdio-request-response
Open

jonxsong wants to merge 4 commits into
modelcontextprotocol:mainfrom
jonxsong:fix/1395-oversized-stdio-request-response

Conversation

@jonxsong

@jonxsong jonxsong commented Nov 18, 2026 •

Copy link
Copy Markdown

Summary

Oversized JSON-RPC requests on stdio were silently discarded, so the caller waited until its own timeout even when the request ID appeared near the start of the line. This change returns a correlated -32600 Invalid Request after the oversized line is drained.

Fixes #1395.

Root cause

AsyncRwTransport::read_line cleared line_buf as soon as the configured limit was crossed. receive then saw only LineRead::Oversized, logged the drop, and continued. The request ID was already gone, and the existing cancellation-safe protocol-error queue could only build an uncorrelated response.

Approach

Retain a bounded prefix while draining an oversized line. The retained bytes are capped at min(max_line_length, 8 KiB), live on the transport so cancellation cannot lose them, and are cleared after the terminating newline.

The transport logs each oversized line when it crosses the limit, even if the peer closes without sending a newline.

A top-level scanner delegates complete JSON tokens to Serde and recovers only an integer/string id paired with a string method. The recovered ID is passed through the existing queue/feed/flush state from #1370. Incomplete or ambiguous prefixes fail closed.

Scope

This is limited to AsyncRwTransport and its line-limit regression suite. Public APIs, dependencies, the configured/default size limits, normal-size messages, and unbounded mode are unchanged.

Oversized notifications, responses, non-JSON input, and requests whose ID is outside the retained prefix remain unanswered, preserving the error-storm protection from #938.

Regression tests

Before the production change, the expanded focused suite reported 10 passed / 2 failed: both a numeric ID and an escaped string ID produced no response.

All 16 line-length tests pass with the follow-up. On 6ccb893, the tests expecting a log before the newline and on an unterminated close both failed.

Coverage includes

  • early numeric and escaped string request IDs;
  • cancellation while an oversized line is still being drained;
  • one error log per oversized line, including before the newline and on an unterminated close;
  • exactly one correlated -32600 response and delivery of the following request;
  • silence for late IDs, notifications, responses, and non-JSON input;
  • the existing boundary, flood, consecutive-oversize, recovery, and unbounded controls.

Verification

Local platform: Windows, Rust 1.96.1 unless noted. Linux/macOS were not run locally.

  • cargo test -p rmcp --test test_async_rw_max_line_length --all-features — 16 passed.
  • cargo test -p rmcp --test test_async_rw_max_line_length --no-default-features --features server — 16 passed.
  • cargo test -p rmcp --lib --no-default-features — 206 passed.
  • cargo test -q --all-features -- --skip default_http_client_preserves_connection_failure_cause --skip rmcp_crate_ships_license --skip rmcp_macros_crate_ships_license — 1,219 passed, 32 ignored, and the three named Windows tests excluded.
  • Unfiltered all-feature library run — 878 passed; one pre-existing Windows connection-refused wording assertion failed, tracked by Two rmcp tests fail on Windows: connection-refused text check and npm spawn in test_with_js #1364.
  • The two filtered license tests are Windows checkout baselines: crate LICENSE symlinks are materialized as ../../LICENSE text.
  • cargo clippy --package rmcp --all-targets --all-features -- -D warnings — passed.
  • Non-local clippy across all 29 features — passed.
  • cargo check --workspace --all-targets — passed.
  • cargo +1.88.0 check --all-targets --all-features — passed at the declared MSRV.
  • cargo +nightly fmt --all -- --check and git diff --check — passed.

Compatibility

No API or dependency changes. The wire behavior changes only for an oversized, request-shaped line with a safely recovered ID. For oversized requests, stdio uses JSON-RPC -32600 Invalid Request with the recovered ID, and Streamable HTTP uses HTTP 413 when the request body exceeds its limit. Memory remains bounded by the existing line cap plus at most 8 KiB, and cancellation can interrupt/resume draining without losing reply state.

Related work

AI assistance

This PR was prepared primarily by OpenAI Codex, including issue analysis, implementation, regression tests, local verification, and the PR description.

Retain a bounded request prefix while draining an oversized line so a complete top-level request id can receive Invalid Request. Keep notifications, responses, malformed input, and late ids silent, with cancellation and recovery coverage.
@jonxsong
jonxsong requested a review from a team as a code owner November 18, 2026 00:28
@github-actions github-actions Bot added T-test Testing related changes T-core Core library changes T-transport Transport layer changes labels Nov 18, 2026
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 18, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-18T20:11:04.000484Z ab9e1d5 New commits
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@github-actions

Copy link
Copy Markdown
Contributor

@michaelneale is on triage rotation this week and is the reviewer for this one.

Labels come from the PR labeler. Please review the change.

Automated triage. Labels are a first pass and the assigned maintainer has the final say.

@1AbdullahButt 1AbdullahButt left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The fix works. I have one behaviour change to point out and one optional test.

read_line now keeps up to min(max_line_length, 8 KiB) of an oversized line in oversized_line_prefix. When the line ends, request_id_from_oversized_prefix tries to read an id and a string method from that prefix. Any id it finds is passed on through PendingErrorResponse::Queue(Some(id)). The scanner fails closed in the case I was most worried about. If the prefix ends in the middle of a number ("id":12 when the real id is 1234), serde still parses 12. But the scanner then needs a , or } after the value, and finds the end of the prefix instead, so it returns None and sends nothing.

I ran cargo test -p rmcp --test test_async_rw_max_line_length --all-features in a rust:1 container: 13 passed. With async_rw.rs reverted to the base commit, 3 fail: the early numeric id, escaped string id, and cancellation tests.

Behaviour change: on the base commit, Step::OversizedNeedsDiscard returned LineRead::Oversized right away, so receive logged "exceeded the maximum line length" as soon as the limit was crossed. Now read_line keeps draining and returns only at the newline. A peer that streams an unterminated line therefore produces no log until the newline. If it hits EOF first, read_line returns Eof and the error is never logged. If that log matters for diagnosing floods, consider emitting it when the limit is first crossed and sending the reply later.

Optional: add a test where the prefix ends inside the id number, to pin the truncated-number behaviour described above.

Reviewed with help from Claude Code.

Exercise the numeric ID boundary in a duplex transport. Verify silence when the retained prefix ends inside the number and a correlated reply once it includes the delimiter.
@jonxsong

Copy link
Copy Markdown
Author

Thanks for testing both versions, @1AbdullahButt. I added the numeric ID boundary regression in 5588847. It checks that a prefix ending midway through the ID gets no reply and that retaining the full number and its following comma produces one -32600 reply with the correct ID. All 14 line-length tests passed on Windows with both --all-features and --no-default-features --features server.

Thanks also for flagging the change in when the oversized-line error is logged.

Drafted with OpenAI Codex.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 55888475f1

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment thread crates/rmcp/src/transport/async_rw.rs Outdated
Comment on lines +290 to +291
if has_method && id.is_some() {
return id;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Reject malformed retained bytes before replying

When an oversized line begins with complete id and method fields but is already syntactically invalid later in the retained prefix (for example {"jsonrpc":"2.0","id":1,"method":"ping",! followed by padding), this early return accepts the ID without examining the invalid byte. receive then emits a correlated error instead of following the existing syntax/EOF policy that deliberately keeps unparsable input silent, so request-shaped garbage can again provoke the response/error loops that policy avoids; continue validating the retained prefix and reject definite syntax errors before queuing the reply.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 6ccb893. The scanner now checks the entire retained prefix before replying and distinguishes malformed JSON from a valid prefix cut short by the size limit. Four regression cases failed before the fix and now pass. The line-length suite also passed on Windows with both all features and the minimal server feature.

Drafted with OpenAI Codex.

Keep scanning retained JSON after the request ID and method. Distinguish definite errors from a cutoff at the prefix boundary and preserve only delimited request IDs.

@jokemanfire jokemanfire left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verified at 6ccb893 from the code and a local run, not the description.

What I ran (Linux, stable 1.96; Windows not run here)

  • cargo test -p rmcp --test test_async_rw_max_line_length --all-features — 14 passed, 0 failed.
  • A scratch probe (not committed) that installs a capturing tracing subscriber around receive, to check when the oversized-line error is emitted. Output in the finding below.

Code read

  • read_line (async_rw.rs:144): the copy into line_buf / oversized_line_prefix and the matching consume happen in one synchronous block with no await between them, so the cancellation claim holds — a receive dropped mid-line can neither lose bytes nor leave discarding half-set.
  • request_id_from_oversized_prefix (async_rw.rs:229): fields are recorded only after a ,/} delimiter, so "id":12 cut at the prefix boundary produces no reply; result/error keys, duplicate id/method, and non-string method all fail closed; an id nested inside params is invisible because each value is consumed whole. @1AbdullahButt was right that this is the risky part, and it is closed.
  • The reply is queued in pending_error_response before the next read_line, so it is flushed even while the peer keeps writing (async_rw.rs:412-423).

Finding: the oversized-line error log no longer fires for the shape this PR is about

The only tracing::error! on this path is in receive, when read_line returns LineRead::Oversized (async_rw.rs:414-417). Since this PR, that result is produced only when the terminating newline arrives. A peer that never sends one — the report in #1395, and the unterminated_flood_is_discarded_and_stream_recovers test — stays in Step::OversizedNeedsDiscard (async_rw.rs:210); if it closes instead of finishing the line, read_line returns Eof and receive returns None (async_rw.rs:426) without logging anything.

Measured with the capturing subscriber (4096-byte limit, 8 KiB of A):

  • line ends with \n: ERROR rmcp::transport::async_rw: Incoming message exceeded the maximum line length, discarding it max_line_length=4096 — exactly once.
  • peer writes 8 KiB then closes, no newline: no event at all, receive() returns None.

Before this change the log was emitted the moment the cap was crossed (165f8fd:async_rw.rs:189 returned LineRead::Oversized as soon as discard started), so an unterminated flood used to be visible in logs and now is silent. The with_max_line_length doc still promises that over-long lines are "dropped and logged" (async_rw.rs:124). @1AbdullahButt raised the timing change and your reply acknowledges it, but the head commit does not move the log.

Suggestion: emit the error once when entering discarding (the Step::OversizedNeedsDiscard arm), and keep the current one for the newline case — one line per oversized line, never per chunk. That keeps the diagnostic without reintroducing log spam.

Question, non-blocking: the error code

A line that is valid JSON and a well-formed tools/call request, just too large, is answered with -32600 Invalid Request. The HTTP transport answers the same situation with 413 Payload Too Large (streamable_http_server/tower.rs:296-301). -32600 tells a client its message was malformed when it was not; if that code stays, a data field such as {"reason":"line_too_long"} would let callers tell the two apart. I do not have a strong opinion on which is right, but the divergence is worth one sentence in the PR body.

The scanner itself I am happy with: bounded, fail-closed, and I could not construct an input that answers an id belonging to a different message. The log point is the one I would fix before merge, because it hides the failure mode the PR exists for.

@jonxsong

Copy link
Copy Markdown
Author

Thanks for reproducing this and catching the logging regression, @jokemanfire. Review

Fixed in ab9e1d5. Stdio now logs once as soon as a line exceeds the limit, even if the peer closes without sending a newline. The regression tests also verify that resuming a cancelled read and draining the rest of the line produce no duplicate log.

I updated the PR description to clarify that stdio uses the correlated JSON-RPC -32600 response for this size rejection and Streamable HTTP uses HTTP 413 at its body limit.

@jordiboehme jordiboehme left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for picking this up. I tested the design against the case from the issue: a client that sends a large base64 file in a tools/call.

The prefix scan covers clients that write id early (rmcp, the Python SDK). The TypeScript SDK does not: it builds each request as { ...request, jsonrpc: '2.0', id: messageId } (src/shared/protocol.ts in 1.32.2 and on main). JSON.stringify keeps that order, so the line is {"method":"tools/call","params":{...},"jsonrpc":"2.0","id":5}. The id is behind the large params and outside the prefix, so the server stays silent and the client still times out. Most clients that send large inline files are built on the TypeScript SDK, so this is the common case.

A small addition would cover it: keep a short tail of the line as well (some bytes), and when the prefix has a top-level method but no id, read the id from the tail. A raw " cannot appear inside a JSON string, so "id": in the raw bytes is always a key. The member right before the final }, with no other } after its value, belongs to the top-level object. Numbers and strings without escapes would be enough there. A test with the TypeScript key order would pin it.

One smaller point: the answer only says "Invalid request". If the message named the limit, for example "Request exceeds the maximum line length of 16777216 bytes", a client or agent could tell why and send a smaller file or a path instead.

@jonxsong

Copy link
Copy Markdown
Author

Thanks, @jordiboehme. I checked the TypeScript SDK's request builder and stdio serializer. A large params value can put the ID beyond the retained prefix, so the client still times out in the case you reported. Your review · TypeScript SDK request builder · Stdio serializer

The TypeScript field order deserves a regression test. For late-ID recovery, I'd want the reader to establish that the ID belongs to the top-level request and is unambiguous across the whole line before sending a correlated reply. That requires tracking JSON structure through the discarded bytes while keeping memory bounded.

I agree about the error message too. Including the configured line limit in bytes while keeping -32600 would tell callers why the request was rejected and how to size a retry.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

T-core Core library changes T-test Testing related changes T-transport Transport layer changes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

stdio transport: a request over the maximum line length gets no answer

4 participants