Skip to content

fix(cli): submit MCP prompt text without JSON encoding - #29205

Open
CoralGarden52 wants to merge 1 commit into
google-gemini:mainfrom
CoralGarden52:fix/mcp-prompt-text
Open

fix(cli): submit MCP prompt text without JSON encoding#29205
CoralGarden52 wants to merge 1 commit into
google-gemini:mainfrom
CoralGarden52:fix/mcp-prompt-text

Conversation

@CoralGarden52

Copy link
Copy Markdown

Summary

  • Submit MCP prompt response text directly instead of JSON-encoding it.
  • Preserve embedded quotes and newlines exactly as returned by the MCP server.
  • Replace the prior encoded-output expectation and add a focused regression test.

Details

McpPromptLoader wrapped every successful text response in JSON.stringify(). That added literal outer quotes and escaped characters before the value reached submit_prompt, so MCP-generated prompts were altered before entering the conversation. The loader now passes maybeContent.text through unchanged.

Related Issues

Fixes #29204

How to Validate

  1. Configure an MCP prompt that returns He said "hello". followed by a newline and Next line; invoke its slash command and verify the submitted prompt contains the exact original text without outer JSON quotes or escaped newline characters.
  2. Run npm test --workspace @google/gemini-cli -- src/services/McpPromptLoader.test.ts (32 passing).
  3. Run GEMINI_CLI_TRUST_WORKSPACE=true npm test --workspace @google/gemini-cli (464 test files, 7,023 passing, 4 skipped).
  4. Run npm run typecheck --workspace @google/gemini-cli.

Pre-Merge Checklist

  • Updated relevant documentation and README (if needed)
  • Added/updated tests (if needed)
  • Noted breaking changes (if any)
  • Validated on required platforms/methods:
    • MacOS
      • npm run
      • npx
      • Docker
      • Podman
      • Seatbelt
    • Windows
      • npm run
      • npx
      • Docker
    • Linux
      • npm run
      • npx
      • Docker

@CoralGarden52
CoralGarden52 requested a review from a team as a code owner September 4, 2026 07:19
@google-cla

google-cla Bot commented Sep 4, 2026

Copy link
Copy Markdown

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.

@gemini-code-assist

Copy link
Copy Markdown
Contributor

Summary of Changes

Hello, 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

  • MCP Prompt Handling: Removed JSON encoding from MCP prompt response text to ensure content is submitted exactly as returned by the server.
  • Data Integrity: Preserved original formatting, including embedded quotes and newlines, within prompt responses.
  • Regression Testing: Updated existing tests and added a new test case to verify that special characters and formatting are correctly handled.
Using Gemini Code Assist

The 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 /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

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 .gemini/ folder in the base of the repository. Detailed instructions can be found here.

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

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

@github-actions github-actions Bot added the size/s A small PR label Sep 4, 2026
@github-actions

github-actions Bot commented Sep 4, 2026

Copy link
Copy Markdown

📊 PR Size: size/S

  • Lines changed: 23
  • Additions: +21
  • Deletions: -2
  • Files changed: 2

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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.

@gemini-cli gemini-cli Bot added area/agent Issues related to Core Agent, Tools, Memory, Sub-Agents, Hooks, Agent Quality priority/p2 Important but can be addressed in a future release. area/core Issues related to User Interface, OS Support, Core Functionality labels Sep 4, 2026

@Hahaknight Hahaknight 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.

Verified the fix against main and traced both consumers — this is correct, and importantly nothing downstream depended on the encoded form:

What I checked

  1. Consumers don't decode, they pass through. slashCommandProcessor.ts (case 'submit_prompt') returns result.content untouched, and nonInteractiveCliCommands.ts:91 does return result.content directly. 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.
  2. Type shape is unchanged. SubmitPromptResult.content is PartListUnion, which accepts a plain string (partToString() returns strings unchanged). FileCommandLoader uses the array form ([{text}]), McpPromptLoader the string form — both valid, so no contract change for callers.
  3. 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]?.content keeps 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.

@CoralGarden52

Copy link
Copy Markdown
Author

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.

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

Labels

area/agent Issues related to Core Agent, Tools, Memory, Sub-Agents, Hooks, Agent Quality area/core Issues related to User Interface, OS Support, Core Functionality priority/p2 Important but can be addressed in a future release. size/s A small PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

MCP prompt responses are submitted as JSON-encoded text

2 participants