Skip to content

feat(policy): Propagate Tool Annotations for MCP Servers - #20083

Merged
jerop merged 1 commit into
mainfrom
feat/policy-annotations-plumbing-v3
Feb 24, 2026
Merged

feat(policy): Propagate Tool Annotations for MCP Servers#20083
jerop merged 1 commit into
mainfrom
feat/policy-annotations-plumbing-v3

Conversation

@jerop

@jerop jerop commented Feb 23, 2026

Copy link
Copy Markdown
Contributor

Fixes #19654
Followup on #20029

  • Add annotations property to ToolBuilder and DeclarativeTool base classes to store semantic metadata.
  • Update DiscoveredMCPTool to preserve and propagate annotations captured during MCP discovery.
  • Propagate tool metadata to the Policy Engine during execution via checkPolicy.
  • Migrate hardcoded Plan Mode read-only logic to declarative rules in plan.toml, maintaining ask_user behavior for MCP tools.
  • Add unit and integration tests for metadata propagation and standard MCP behavioral hints.

@jerop
jerop requested review from a team as code owners February 23, 2026 21:53
@gemini-code-assist

Copy link
Copy Markdown
Contributor

Summary of Changes

Hello @jerop, 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 significantly enhances the policy engine's capabilities by introducing a mechanism to propagate semantic metadata (annotations) from tools to the policy evaluation process. This change allows for more granular and declarative policy enforcement, moving away from hardcoded logic, particularly for Plan Mode. By enabling tools to declare their characteristics and behaviors, the system can make more intelligent and customizable decisions about tool execution, improving both security and flexibility.

Highlights

  • Tool Metadata Propagation: Introduced an annotations property to ToolBuilder and DeclarativeTool base classes to store semantic metadata, enabling tools to carry behavioral hints.
  • Policy Engine Integration: Updated DiscoveredMCPTool to preserve and propagate annotations captured during MCP discovery, ensuring this metadata is passed to the Policy Engine during execution via checkPolicy.
  • Declarative Plan Mode Policies: Migrated hardcoded Plan Mode read-only logic to declarative rules within plan.toml, allowing for more flexible and customizable policy enforcement based on tool annotations, while maintaining ask_user behavior for MCP tools.
  • Documentation Updates: Added new documentation and examples for customizing Plan Mode policies using toolAnnotations, specifically demonstrating how to automatically approve read-only MCP tools.
  • Testing: Included comprehensive unit and integration tests to validate the metadata propagation pipeline and the new standard MCP behavioral hints within the policy engine.
Changelog
  • docs/cli/plan-mode.md
    • Updated documentation to reflect that Plan Mode policies are defined in plan.toml.
    • Added a new example demonstrating how to automatically approve read-only MCP tools using toolAnnotations in custom policies.
  • docs/reference/policy-engine.md
    • Documented the new toolAnnotations field for policy rules, explaining its use for semantic matching based on tool metadata.
    • Added an example illustrating how to allow all read-only MCP tools using toolAnnotations.
  • packages/cli/src/config/policy-engine.integration.test.ts
    • Added integration tests to verify that read-only MCP tools are correctly handled by the declarative plan.toml policy in Plan Mode.
    • Expanded integration tests for toolAnnotations to cover various annotation combinations and their impact on policy decisions.
  • packages/core/src/policy/policies/plan.toml
    • Added a new policy rule to plan.toml that sets the decision to ask_user for all MCP tools with readOnlyHint = true in Plan Mode.
  • packages/core/src/policy/policy-engine.ts
    • Modified debug logging to include the modes property when a policy rule matches, providing more context for debugging.
  • packages/core/src/scheduler/policy.test.ts
    • Added a test case to ensure that tool annotations are correctly passed from the checkPolicy function to the policy engine.
  • packages/core/src/scheduler/policy.ts
    • Updated the checkPolicy function to include toolCall.tool.annotations when calling the policy engine's check method.
  • packages/core/src/telemetry/loggers.test.ts
    • Adjusted a test call to logToolCall to accommodate the newly added annotations parameter.
  • packages/core/src/tools/mcp-client.test.ts
    • Removed the dynamic addition of policy rules for read-only MCP tools, as this is now handled declaratively in plan.toml.
    • Updated tests to verify that DiscoveredMCPTool correctly captures and stores all annotations during discovery.
    • Removed unused imports related to ApprovalMode and PolicyDecision.
  • packages/core/src/tools/mcp-client.ts
    • Removed the logic responsible for dynamically adding policy rules for read-only MCP tools.
    • Modified the discoverTools function to pass the full toolDef.annotations object to the DiscoveredMCPTool constructor.
    • Removed unused imports related to ApprovalMode and PolicyDecision.
  • packages/core/src/tools/mcp-tool.test.ts
    • Added new test cases to confirm that DiscoveredMCPTool correctly stores and retrieves the annotations property.
    • Updated existing DiscoveredMCPTool constructor calls to include the new annotations parameter.
  • packages/core/src/tools/mcp-tool.ts
    • Modified the DiscoveredMCPTool constructor to accept and store an optional annotations object.
    • Updated constructor calls within DiscoveredMCPTool to pass annotations to its base class.
    • Adjusted import statements for various types to use type keyword where appropriate.
  • packages/core/src/tools/tools.ts
    • Added an optional annotations property to the ToolBuilder interface and DeclarativeTool abstract class.
Activity
  • No specific activity (comments, reviews, etc.) was provided in the context.
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 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 counter productive. 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.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

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.

Base automatically changed from feat/policy-annotations-core-v2 to main February 23, 2026 21:53

@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 introduces a metadata propagation pipeline for tools by adding an annotations property. This allows for more declarative policy rules, as demonstrated by migrating the hardcoded Plan Mode logic for read-only MCP tools to a plan.toml configuration file. The changes are well-supported by updated documentation and new integration tests. I've found one high-severity issue related to the handling of empty toolAnnotations in policy rules, which could cause them to not match as expected.

@jerop
jerop force-pushed the feat/policy-annotations-plumbing-v3 branch from 4ecfe5b to 17b622b Compare February 23, 2026 22:13
@github-actions

github-actions Bot commented Feb 23, 2026

Copy link
Copy Markdown

Size Change: +2.46 kB (+0.01%)

Total Size: 25.7 MB

Filename Size Change
./bundle/gemini.js 25.3 MB +2.46 kB (+0.01%)
ℹ️ View Unchanged
Filename Size
./bundle/node_modules/@google/gemini-cli-devtools/dist/client/main.js 221 kB
./bundle/node_modules/@google/gemini-cli-devtools/dist/src/_client-assets.js 227 kB
./bundle/node_modules/@google/gemini-cli-devtools/dist/src/index.js 11.5 kB
./bundle/node_modules/@google/gemini-cli-devtools/dist/src/types.js 132 B
./bundle/sandbox-macos-permissive-open.sb 890 B
./bundle/sandbox-macos-permissive-proxied.sb 1.31 kB
./bundle/sandbox-macos-restrictive-open.sb 3.36 kB
./bundle/sandbox-macos-restrictive-proxied.sb 3.56 kB
./bundle/sandbox-macos-strict-open.sb 4.82 kB
./bundle/sandbox-macos-strict-proxied.sb 5.02 kB

compressed-size-action

@gemini-cli gemini-cli Bot added area/core Issues related to User Interface, OS Support, Core Functionality area/enterprise Issues related to Telemetry, Policy, Quota / Licensing 🔒 maintainer only ⛔ Do not contribute. Internal roadmap item. labels Feb 23, 2026
@jerop jerop changed the title feat(policy): Implement Tool Metadata Propagation Pipeline feat(policy): Implement Tool Annotation Propagation Pipeline Feb 23, 2026
@jerop jerop changed the title feat(policy): Implement Tool Annotation Propagation Pipeline feat(policy): Propagate Tool Annotations for MCP Servers Feb 23, 2026
@jerop
jerop enabled auto-merge February 23, 2026 23:30

@Abhijit-2592 Abhijit-2592 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.

LGTM! Approved!

@jerop
jerop added this pull request to the merge queue Feb 24, 2026
@jerop
jerop removed this pull request from the merge queue due to a manual request Feb 24, 2026
Wire MCP tool annotations (readOnlyHint, destructiveHint, etc.) through
both policy check paths so declarative TOML rules can match on
toolAnnotations. This removes the hardcoded per-tool rule injection in
mcp-client.ts in favor of a generic annotation-based rule in plan.toml.

Key changes:
- Store full annotations on DiscoveredMCPTool and expose via getter
- Pass toolAnnotations through scheduler, message-bus, and
  coreToolScheduler policy check calls
- Add toolAnnotations field to ToolConfirmationRequest
- Update getExcludedTools() to accept tool metadata for annotation rules
- Add annotation-based ASK_USER rule for read-only MCP tools in plan.toml
- Sync docs with toolAnnotations support

Fixes #19654
@jerop
jerop force-pushed the feat/policy-annotations-plumbing-v3 branch from 17b622b to 7c1d3d1 Compare February 24, 2026 14:06
@jerop
jerop enabled auto-merge February 24, 2026 14:08
@jerop
jerop added this pull request to the merge queue Feb 24, 2026
Merged via the queue into main with commit 15f6c8b Feb 24, 2026
27 checks passed
@jerop
jerop deleted the feat/policy-annotations-plumbing-v3 branch February 24, 2026 14:30
BryanBradfo pushed a commit to BryanBradfo/gemini-cli that referenced this pull request Mar 5, 2026
liamhelmer pushed a commit to badal-io/gemini-cli that referenced this pull request Mar 12, 2026
warrenzhu25 pushed a commit to warrenzhu25/gemini-cli that referenced this pull request Apr 9, 2026
@sripasg sripasg added the size/l A large sized PR label Jun 2, 2026
software-0ficial pushed a commit to software-0ficial/gemini-cli that referenced this pull request Jul 9, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/core Issues related to User Interface, OS Support, Core Functionality area/enterprise Issues related to Telemetry, Policy, Quota / Licensing 🔒 maintainer only ⛔ Do not contribute. Internal roadmap item. size/l A large sized PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Enable MCP Tool Annotation Matching in Policies

3 participants