Skip to content

feat(mcp): propagate grounding metadata from MCP _meta - #7047

Open
claxman wants to merge 1 commit into
google:mainfrom
claxman:fix/6081-mcp-grounding-metadata
Open

feat(mcp): propagate grounding metadata from MCP _meta#7047
claxman wants to merge 1 commit into
google:mainfrom
claxman:fix/6081-mcp-grounding-metadata

Conversation

@claxman

@claxman claxman commented Sep 7, 2026

Copy link
Copy Markdown

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:
_maybe_add_grounding_metadata returns early unless a tool is named google_search_agent. I ran _handle_after_model_callback with a dummy tool and temp:_adk_grounding_metadata={'foo': 'bar'} set: the callback returned None and LlmResponse.grounding_metadata stayed unset. git log -S propagate_grounding_metadata -- src/google/adk/flows/llm_flows/base_llm_flow.py shows d689a04f replaced the name check with getattr(tool, 'propagate_grounding_metadata', False), and b2daf83d put the name check back. McpTool._run_async_impl dumps CallToolResult and never writes the temp key.

Solution:
Restore the d689a04f gate: attach when some canonical tool has propagate_grounding_metadata and the temp key is set. Keep the canonical_tools_cache read. Add propagate_grounding_metadata: bool = False on McpTool and McpToolset. When True, validate adk_grounding_metadata from dumped meta / _meta as GroundingMetadata and write the temp key. The tool return dict is unchanged. A ValidationError is logged and skipped.

Testing Plan

Unit Tests:

  • I have added or updated unit tests for my change.
  • All unit tests pass locally.
PYTHONPATH=src .venv/bin/python -m pytest -q tests/unittests/flows/llm_flows/test_base_llm_flow.py::test_handle_after_model_callback_grounding_with_no_callbacks tests/unittests/flows/llm_flows/test_base_llm_flow.py::test_handle_after_model_callback_grounding_with_callback_override tests/unittests/flows/llm_flows/test_base_llm_flow.py::test_handle_after_model_callback_grounding_with_plugin_override tests/unittests/flows/llm_flows/test_base_llm_flow.py::test_handle_after_model_callback_caches_canonical_tools tests/unittests/tools/mcp_tool/test_mcp_tool.py::TestMCPTool::test_run_async_impl_propagates_grounding_metadata_from_meta tests/unittests/tools/mcp_tool/test_mcp_tool.py::TestMCPTool::test_run_async_impl_skips_grounding_metadata_when_flag_off

15 passed.

The cache test uses a tool named research_agent with propagate_grounding_metadata=True. With the three src files checked out from upstream/main, that test fails with AttributeError: 'NoneType' object has no attribute 'grounding_metadata', and the MCP flag-on test raises TypeError: McpTool.__init__() got an unexpected keyword argument 'propagate_grounding_metadata'.

Manual End-to-End (E2E) Tests:

Not run. No live MCP server or Gemini Enterprise UI on this machine.

Additional context

Claim: #6081 (comment)

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.

_maybe_add_grounding_metadata returned early unless a tool was named
google_search_agent. git log -S shows d689a04 (Xuan Yang) replaced that
with getattr(tool, 'propagate_grounding_metadata', False), and b2daf83
(Haiyuan Cao) put the name check back. I ran _handle_after_model_callback
with dummy_tool and temp:_adk_grounding_metadata={'foo': 'bar'}: result
was None.

Restore the d689a04 gate and the canonical_tools_cache read. Add
propagate_grounding_metadata on McpTool and McpToolset so
_meta.adk_grounding_metadata can populate the same temp key.

Verification: 15 passed. upstream/main src: cache test AttributeError on
None grounding_metadata; MCP flag-on test TypeError on the new kwarg.

Fixes google#6081
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.

Support Grounding Metadata / Citation Propagation in in mcp_tool class

1 participant