Skip to content

fix(mcp-bridge): encode stderr notifications as JSON#13549

Open
shreemaan-abhishek wants to merge 5 commits into
apache:masterfrom
shreemaan-abhishek:fix/mcp-bridge-stderr-json-encode
Open

fix(mcp-bridge): encode stderr notifications as JSON#13549
shreemaan-abhishek wants to merge 5 commits into
apache:masterfrom
shreemaan-abhishek:fix/mcp-bridge-stderr-json-encode

Conversation

@shreemaan-abhishek

Copy link
Copy Markdown
Contributor

Description

The mcp-bridge plugin forwards a spawned MCP subprocess's stderr to the
client as a notifications/stderr JSON-RPC message. The message was built
by concatenating the raw stderr line into a JSON string literal, so any
stderr output containing JSON special characters (double quotes,
backslashes, etc.) produced a malformed JSON-RPC notification and broke the
framing of the message sent to the client.

This change builds the notification with core.json.encode instead, which
properly escapes the content. A regression test in t/plugin/mcp-bridge.t
feeds a stderr line containing quotes, a backslash and a }} sequence and
asserts the resulting notification is well-formed JSON that round-trips back
to the original content.

Which issue(s) this PR fixes:

Fixes #

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)

Build the notifications/stderr message with core.json.encode instead of
string concatenation, so subprocess stderr output containing double
quotes, backslashes or other JSON special characters no longer breaks
the framing of the JSON-RPC notification sent to the client.
@dosubot dosubot Bot added size:M This PR changes 30-99 lines, ignoring generated files. bug Something isn't working labels Jun 12, 2026

Copilot AI 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.

Pull request overview

This PR fixes malformed notifications/stderr JSON-RPC notifications emitted by the mcp-bridge plugin when the subprocess writes stderr lines containing JSON-special characters, by switching from manual string concatenation to core.json.encode.

Changes:

  • Build notifications/stderr messages via core.json.encode to ensure correct JSON escaping.
  • Add a regression test that feeds stderr content containing quotes, backslashes, and }}, and asserts the notification JSON round-trips correctly.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
apisix/plugins/mcp-bridge.lua Switch stderr notification construction to JSON encoding instead of string concatenation.
t/plugin/mcp-bridge.t Add a regression test ensuring stderr content with JSON-special characters yields valid JSON.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread apisix/plugins/mcp-bridge.lua Outdated
Comment thread t/plugin/mcp-bridge.t Outdated
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>

Copilot AI 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.

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.

Comment thread apisix/plugins/mcp-bridge.lua Outdated
Comment thread t/plugin/mcp-bridge.t Outdated
Comment thread t/plugin/mcp-bridge.t Outdated
Extract the notifications/stderr message construction into a helper that
is also exercised directly by the regression test, log the encode error
on the fallback path, and reap the spawned subprocess in the test.
Document the mcp-bridge plugin in both languages, add it to the AI
plugin category in the sidebars, and note that the plugin is currently
experimental.
@dosubot dosubot Bot added size:L This PR changes 100-499 lines, ignoring generated files. and removed size:M This PR changes 30-99 lines, ignoring generated files. labels Jun 15, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working size:L This PR changes 100-499 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants