Skip to content

fix(mcp): keep the model's thoughts out of to_mcp_server results - #7835

Closed
adityasingh2400 wants to merge 1 commit into
google:mainfrom
adityasingh2400:fix/agent-to-mcp-skip-thoughts
Closed

adityasingh2400 wants to merge 1 commit into
google:mainfrom
adityasingh2400:fix/agent-to-mcp-skip-thoughts

Conversation

@adityasingh2400

Copy link
Copy Markdown
Contributor

Please ensure you have read the contribution guide before creating a pull request.

Link to Issue or Description of Change

1. Link to an existing issue (if applicable):

Problem:
_part_to_content turned every part with text into a TextContent block, thought=True parts included, and the progress messages joined all part text. An agent exposed with to_mcp_server therefore sent the model's hidden reasoning to MCP clients, both in the tool result and in report_progress messages.

Solution:

  • _part_to_content returns None for thought parts, like other parts with nothing to render (e.g. function calls).
  • The progress text skips thought parts.

AgentTool already does the same when it builds its result. Non-thought text, images, audio and blobs are mapped exactly as before.

Testing Plan

Unit Tests:

  • I have added or updated unit tests for my change.
  • All unit tests pass locally.

Added to tests/unittests/tools/mcp_tool/test_agent_to_mcp.py:

  • test_run_agent_leaves_thoughts_out_of_the_result: a final event with a thought part and an answer returns only the answer.
  • test_run_agent_leaves_thoughts_out_of_progress: a thought-only partial event reports nothing, and a mixed one reports only its visible text.
# On main (before the fix)
AssertionError: assert ['text', 'text'] == ['text']
AssertionError: assert ['planning', ...oningworking'] == ['working']

# With the fix
$ pytest tests/unittests/tools/mcp_tool
455 passed

pre-commit run passes on the changed files, and mypy reports no issues in _agent_to_mcp.py.

Manual End-to-End (E2E) Tests:

I exposed an agent that answers like a thinking model, with a thought part and then the answer, via to_mcp_server. I called it from an MCP ClientSession connected over in-memory streams (the connected_client_session helper from this test package):

=== BEFORE (main) ===
MCP client received: 'The user is on the free tier; do not mention the internal escalation path.'
MCP client received: 'Your contract renews on Nov 30.'
=== AFTER (this PR) ===
MCP client received: 'Your contract renews on Nov 30.'

Checklist

  • I have read the CONTRIBUTING.md document.
  • I have performed a self-review of my own code.
  • I have commented my code, particularly in hard-to-understand areas.
  • I have added tests that prove my fix is effective or that my feature works.
  • New and existing unit tests pass locally with my changes.
  • I have manually tested my changes end-to-end.
  • Any dependent changes have been merged and published in downstream modules.

Additional context

None.

_part_to_content turned every part with text into a TextContent block,
thought parts included, and progress messages joined all part text. An
agent exposed with to_mcp_server therefore sent its hidden reasoning to
MCP clients. Skip thought parts in both places, as AgentTool already does.

Fixes google#7834
copybara-service Bot pushed a commit that referenced this pull request Oct 30, 2026
@adk-bot

adk-bot commented Oct 30, 2026

Copy link
Copy Markdown
Collaborator

Thank you @adityasingh2400 for your contribution! 🎉

Your changes have been successfully imported and merged via Copybara in commit 9688feb.

Closing this PR as the changes are now in the main branch.

@adk-bot adk-bot added the merged [Status] This PR is merged label Oct 30, 2026
@adk-bot adk-bot closed this Oct 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

merged [Status] This PR is merged

Projects

None yet

Development

Successfully merging this pull request may close these issues.

to_mcp_server returns the model's thought parts to MCP clients

3 participants