fix(cli): submit MCP prompt text without JSON encoding - #29205
fix(cli): submit MCP prompt text without JSON encoding#29205CoralGarden52 wants to merge 1 commit into
Conversation
|
Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA). View this failed invocation of the CLA check for more information. For the most up to date status, view the checks section at the bottom of the pull request. |
Summary of ChangesHello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request addresses an issue where MCP prompt responses were being incorrectly wrapped in JSON stringification. By removing this unnecessary encoding, the CLI now correctly preserves the raw text content returned by MCP servers, ensuring that special characters and formatting are maintained when prompts are submitted to the conversation. Highlights
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize the Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counterproductive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here. Footnotes
|
|
📊 PR Size: size/S
|
There was a problem hiding this comment.
Code Review
This pull request updates McpPromptLoader to return the raw text content of a prompt response instead of a JSON-stringified version. It also updates the corresponding unit tests and adds a new test case to verify that quotes and newlines are correctly preserved in the prompt response text. There are no review comments, and I have no feedback to provide.
Hahaknight
left a comment
There was a problem hiding this comment.
Verified the fix against main and traced both consumers — this is correct, and importantly nothing downstream depended on the encoded form:
What I checked
- Consumers don't decode, they pass through.
slashCommandProcessor.ts(case 'submit_prompt') returnsresult.contentuntouched, andnonInteractiveCliCommands.ts:91doesreturn result.contentdirectly. Nothing on either path JSON-parses the value, so the stringify could only ever surface as literal characters in the submitted prompt — corruption, not a load-bearing escape. - Type shape is unchanged.
SubmitPromptResult.contentisPartListUnion, which accepts a plain string (partToString()returns strings unchanged).FileCommandLoaderuses the array form ([{text}]),McpPromptLoaderthe string form — both valid, so no contract change for callers. - Bug confirmed as described in #29204.
JSON.stringify('He said "hello".\nNext line')yields the outer-quotes + backslash-escape form, which then reaches the model verbatim. The new regression test pins exactly this.
Non-blocking observations (both pre-existing, out of scope — just flagging for the future)
result.messages?.[0]?.contentkeeps only the first message of a multi-message MCP prompt response; the rest are silently dropped. If that's intentional (spec ambiguity), a comment saying so would help; if not, it's a small follow-up.- A non-empty but non-text content (e.g. an image part) falls into the
'Received an empty or invalid prompt response'error — accurate-ish but slightly misleading wording.
The fix itself is minimal and lands exactly where it should.
|
Thanks for flagging this. I agree that the current message conflates an empty or invalid response with a valid response containing unsupported non-text content. Since this behavior predates this change and this PR is intentionally scoped to preserving MCP text prompt content verbatim, I'd prefer to keep it out of scope here. A small follow-up could improve the diagnostic and add coverage for unsupported content types. |
Summary
Details
McpPromptLoaderwrapped every successful text response inJSON.stringify(). That added literal outer quotes and escaped characters before the value reachedsubmit_prompt, so MCP-generated prompts were altered before entering the conversation. The loader now passesmaybeContent.textthrough unchanged.Related Issues
Fixes #29204
How to Validate
He said "hello".followed by a newline andNext line; invoke its slash command and verify the submitted prompt contains the exact original text without outer JSON quotes or escaped newline characters.npm test --workspace @google/gemini-cli -- src/services/McpPromptLoader.test.ts(32 passing).GEMINI_CLI_TRUST_WORKSPACE=true npm test --workspace @google/gemini-cli(464 test files, 7,023 passing, 4 skipped).npm run typecheck --workspace @google/gemini-cli.Pre-Merge Checklist