Skip to content

Preserve Google Agent Identity token-sharing policy when constructing MCP tools#6037

Open
White-Mouse wants to merge 2 commits into
google:mainfrom
White-Mouse:codex/adk-mcp-bound-token-policy
Open

Preserve Google Agent Identity token-sharing policy when constructing MCP tools#6037
White-Mouse wants to merge 2 commits into
google:mainfrom
White-Mouse:codex/adk-mcp-bound-token-policy

Conversation

@White-Mouse

Copy link
Copy Markdown
Contributor

Summary

  • remove constructor-side mutation of GOOGLE_API_PREVENT_AGENT_TOKEN_SHARING_FOR_GCP_SERVICES from MCP tool construction
  • keep deployment-level Google auth / Agent Identity token-sharing policy controlled by the application environment
  • add regression coverage for both McpToolset and MCPTool constructors preserving an existing environment policy value

Motivation

MCP tool construction currently mutates GOOGLE_API_PREVENT_AGENT_TOKEN_SHARING_FOR_GCP_SERVICES as a process-wide side effect.

This PR removes the constructor-side environment mutation so deployment-level Google auth / Agent Identity policy remains controlled by the application environment. The constructors do not need to rewrite the process environment to initialize MCP tools.

Testing

  • uv run --extra mcp --extra test python -m pytest tests/unittests/tools/mcp_tool/test_mcp_tool.py tests/unittests/tools/mcp_tool/test_mcp_toolset.py tests/unittests/tools/mcp_tool/test_mcp_toolset_auth.py -q
  • uvx ruff check src/google/adk/tools/mcp_tool/mcp_tool.py src/google/adk/tools/mcp_tool/mcp_toolset.py tests/unittests/tools/mcp_tool/test_mcp_tool.py tests/unittests/tools/mcp_tool/test_mcp_toolset.py
  • uv run --extra mcp --extra test python -m py_compile src/google/adk/tools/mcp_tool/mcp_tool.py src/google/adk/tools/mcp_tool/mcp_toolset.py tests/unittests/tools/mcp_tool/test_mcp_tool.py tests/unittests/tools/mcp_tool/test_mcp_toolset.py
  • git diff --check

@adk-bot adk-bot added mcp [Component] Issues about MCP support tools [Component] This issue is related to tools labels Jun 9, 2026
@adk-bot

adk-bot commented Jun 9, 2026

Copy link
Copy Markdown
Collaborator

An analysis of Pull Request #6037 has been completed.

🔍 ADK Pull Request Analysis: PR #6037

Title: Preserve Google Agent Identity token-sharing policy when constructing MCP tools
Author: @White-Mouse
Status: open
Impact: 47 additions, 23 deletions across 4 files


Executive Summary

  1. Core Objective: Removes constructor-side mutation of GOOGLE_API_PREVENT_AGENT_TOKEN_SHARING_FOR_GCP_SERVICES during MCP Tool & Toolset initialization to prevent global/process-wide side effects and allow deployment-level environment configurations to control token-sharing policies.
  2. Justification & Value: Justified Fix & High Value — Dynamic alteration of os.environ inside library class constructors is a severe anti-pattern with global side effects that silences security restrictions (such as opting-in to bound tokens) across unrelated services running in the same process.
  3. Alignment with Principles: Pass — Zero breaking changes to the public API surface area, excellent test coverage mimicking real-world bound-token configurations, and strict alignment with clean code practices.
  4. Recommendation: Approve

Detailed Findings & Analysis

1. Objectives & Impact ("What does it do?")

  • Context & Background: As part of Issue #5361, a temporary patch was applied to MCPTool and McpToolset to set GOOGLE_API_PREVENT_AGENT_TOKEN_SHARING_FOR_GCP_SERVICES = "false" internally. This was because the underlying mcp_session_manager relied on a custom implementation of asyncio which lacked mTLS bound token support, leading to 401 Unauthorized errors in Agent Engine runtimes.
  • Implementation Mechanism: The PR cleanly removes the dynamic assignment of this environment variable during constructor execution of MCPTool and McpToolset. It reverts this setting control entirely back to the application environment, preventing unintended process-wide configuration rewrites.
  • Affected Surface: No public-facing API signatures, attributes, or visibility modifiers are altered. The change is robust and entirely backward-compatible.

2. Justification & Value ("Is it a valid and useful change?")

  • Workspace Verification:
    • Investigated current workspace files: mcp_tool.py and mcp_toolset.py (using view_file and grep_search).
    • Found that: In the baseline code, both classes systematically overwrite os.environ["GOOGLE_API_PREVENT_AGENT_TOKEN_SHARING_FOR_GCP_SERVICES"] = "false" on construction.
  • Value Assessment: Exceptionally high. Environment variables are process-global. Having a library or SDK component dynamically mutate them under-the-hood represents an implicit configuration bypass. If a user intentionally locks down their agent engine using "true" to enforce bound tokens elsewhere, initializing an MCP tool would silently disable this system security policy process-wide.
  • Scope & Depth: Systematic & Architectural Fix. This addresses the underlying root-cause side effect cleanly rather than applying superficial point patches.

3. Principle & Style Alignment Checklist ("Does it follow rules?")

  • Public API & Visibility Boundaries:
    • Status: Pass
    • Analysis: No signature adjustments. No public symbols added or removed. Unused imports are cleaned up.
  • Code Quality, Typing & Conventions:
    • Status: Pass
    • Analysis: import os was safely removed from both main modules as it is no longer required. Formatting conforms perfectly to standard style requirements.
  • Robustness & Edge Cases:
    • Status: Pass
    • Analysis: Restoring explicit environment controls ensures that deployment/infrastructure administrators can tune policies through secure orchestrators (e.g., Kubernetes env / GKE Metadata Server) without the application codebase reverting state dynamically during class initialization.
  • Test Integrity & Quality:
    • Status: Pass
    • Analysis: High-quality unit tests added to both test_mcp_tool.py and test_mcp_toolset.py. It uses monkeypatch to simulate an environment-configured bound token policy, verifies constructability, and asserts that the policy is conserved post-construction.

The PR is ready for approval. Please let me know if you would like me to perform any further tasks!

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

Labels

mcp [Component] Issues about MCP support tools [Component] This issue is related to tools

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants