fix(core): sanitize and length-check MCP tool qualified names#20987
fix(core): sanitize and length-check MCP tool qualified names#20987abhipatel12 merged 2 commits intomainfrom
Conversation
Summary of ChangesHello, 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 and resolves "Invalid function name" 400 errors encountered with the Gemini API. These errors stemmed from MCP tool qualified names exceeding character limits or containing disallowed characters, a regression introduced by previous changes. The solution involves refining the name sanitization logic to ensure all generated names comply with API requirements, thereby improving the robustness of tool integration. Highlights
Changelog
Activity
Using Gemini Code AssistThe 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
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 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
|
There was a problem hiding this comment.
Code Review
This pull request effectively addresses a critical bug causing "Invalid function name" errors from the Gemini API by sanitizing and length-checking the full MCP tool qualified names. However, the implementation introduces a significant security risk: sanitized tool names used by the LLM may mismatch the unsanitized names used for policy enforcement. This discrepancy, coupled with a fail-open error handling pattern in the policy engine, could lead to the bypass of security policies and user confirmation prompts for certain MCP tools. Additionally, there is a high-severity suggestion to remove a redundant block of code to improve maintainability.
|
Size Change: +305 B (0%) Total Size: 25.8 MB ℹ️ View Unchanged
|
Ensures that MCP tool names combined with server prefixes are valid for the Gemini API by sanitizing special characters, ensuring they start with a letter/underscore, and truncating to a 63-character limit while preserving uniqueness via middle truncation. This fixes 'Invalid function name' 400 errors encountered when server names have spaces or combined lengths exceed 64 characters.
Addresses feedback regarding naming markers, security policy synchronization, and validation logic. Switches to '...' as truncation marker, ensures consistent sanitization between registry and invocation, and updates validation to allow dots/colons.
bfcfa8d to
0c278c9
Compare
Summary
Fixes "Invalid function name" 400 errors from the Gemini API caused by MCP tool names exceeding character limits or containing invalid characters.
Details
The enforcement of qualified names (Server + Tool) for subagents (introduced in b7a8f0d) exposed a limitation in the naming sanitization logic. Server names containing spaces and long tool names combined to create function names that were either illegal or exceeded the 64-character limit enforced by the Gemini API.
This PR:
generateValidNameto handle digit-prefixes, colons, and strict 63-character length enforcement.Related Issues
Fixes a regression introduced in b7a8f0d.
How to Validate
Run the new regression tests:
npm test -w @google/gemini-cli-core -- src/tools/mcp-tool.test.tsExpected result: 51 tests passed, including the new "MCP Tool Naming Regression Fixes" suite.
Pre-Merge Checklist