Skip to content

fix(mcp-bridge): retain partial stdout and stderr across read timeouts - #13988

Open
bhuvan-somisetty wants to merge 1 commit into
apache:masterfrom
bhuvan-somisetty:fix/mcp-bridge-partial-chunk-drop
Open

bhuvan-somisetty wants to merge 1 commit into
apache:masterfrom
bhuvan-somisetty:fix/mcp-bridge-partial-chunk-drop

Conversation

@bhuvan-somisetty

Copy link
Copy Markdown

Description

In apisix/plugins/mcp-bridge.lua, the event loop reads lines from the spawned process pipe via proc:stdout_read_line() and proc:stderr_read_line(). When a read timeout occurs mid-line, ngx.pipe returns nil, "timeout", partial_data.

Previously, line, _, stdout_partial = proc:stdout_read_line() assigned the 3rd return value to stdout_partial on every read. When the remaining portion of the line arrived in a subsequent iteration, proc:stdout_read_line() returned line, nil, nil, which immediately overwritten stdout_partial with nil before the concatenation could happen. As a result, the buffered partial chunk was discarded and only the trailing fragment was dispatched to the MCP client.

This change fixes partial chunk handling in mcp-bridge:

  1. Accumulates partial stdout and stderr chunks across read timeouts (stdout_partial = (stdout_partial or "") .. partial).
  2. Slices and prepends any buffered partial data to the completed line upon a successful read before sending.
  3. Clears stdout_partial / stderr_partial only after successfully constructing the complete message.

Which issue(s) this PR fixes:

Fixes #13987

Checklist

  • I have explained the need for this PR and the problem it solves
  • I have explained the changes or the new features added to this PR
  • I have added tests corresponding to this change
  • I have updated the documentation to reflect this change
  • I have verified that this change is backward compatible (If not, please discuss on the APISIX mailing list first)

When reading lines from the spawned MCP process via proc:stdout_read_line()
and proc:stderr_read_line(), a read timeout sets the 3rd return value
(partial data). On subsequent reads, the multiple assignment was
overwriting the partial buffer with nil upon reading the remaining line,
discarding the buffered data and sending truncated/corrupted payloads.

Retain and accumulate partial stdout/stderr buffers across read timeouts,
clearing them only after successfully joining with the completed line.

Fixes apache#13987

Signed-off-by: bhuvan-somisetty <somisettybhuvan5@gmail.com>
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.

bug: mcp-bridge drops partial stdout and stderr chunks on read timeouts

1 participant