Skip to content

fix(mcp): use longest-prefix matching in parseMcpToolName for server names with underscores#28033

Open
bisma-nawaz wants to merge 1 commit into
google-gemini:mainfrom
bisma-nawaz:fix-mcp-parsename-underscore
Open

fix(mcp): use longest-prefix matching in parseMcpToolName for server names with underscores#28033
bisma-nawaz wants to merge 1 commit into
google-gemini:mainfrom
bisma-nawaz:fix-mcp-parsename-underscore

Conversation

@bisma-nawaz

Copy link
Copy Markdown

What

Adds an optional knownServerNames parameter to parseMcpToolName that enables longest-prefix matching, fixing incorrect tool routing when MCP server names contain underscores.

Why

Fixes #27981.

The current regex withoutPrefix.match(/^([^_]+)_(.+)$/) stops at the first underscore, so a server named my_server is misidentified as my. For example:

  • Qualified name: mcp_my_server_my_tool
  • Before (broken): { serverName: 'my', toolName: 'server_my_tool' }
  • After (fixed): { serverName: 'my_server', toolName: 'my_tool' }

Underscores are common in server names (github_copilot, google_workspace, my_custom_server), making this a broad correctness issue affecting wildcard routing and policy matching.

How

parseMcpToolName now accepts an optional knownServerNames?: string[] second argument. When provided, the function sorts the known names longest-first and uses prefix matching to unambiguously identify the server component. The original first-underscore regex is used as a fallback so all existing call sites without a registry remain backward compatible.

local-executor.ts — the call site that routes mcp_<server>_* wildcards — now extracts the registered server names from parentToolRegistry and passes them to parseMcpToolName, fixing the wildcard case for underscore servers.

Testing

bun run test -- --run src/tools/mcp-tool.test.ts

8 new tests added to mcp-tool.test.ts:

  • Returns empty for non-MCP names
  • Parses simple (no-underscore) server names correctly
  • Documents the first-underscore fallback behaviour when no server list is provided
  • Correctly resolves underscore server names with knownServerNames
  • Handles mcp_my_server_* wildcards when server name has underscores
  • Picks the longest prefix when two server names share a common prefix (my vs my_server)
  • Falls back to first-underscore split when no registered server matches
  • Returns empty when the name has no tool component

All 70 tests in mcp-tool.test.ts pass.

@bisma-nawaz bisma-nawaz requested review from a team as code owners June 19, 2026 09:01
@github-actions github-actions Bot added the size/m A medium sized PR label Jun 19, 2026
@gemini-code-assist

Copy link
Copy Markdown
Contributor

Summary of Changes

Hello, 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 an issue where MCP tool names containing underscores were incorrectly parsed due to a naive regex split. By introducing an optional registry of known server names, the system can now perform longest-prefix matching to unambiguously identify the server component. This change ensures that wildcard routing and policy matching function correctly for servers with complex names while preserving existing behavior for legacy call sites.

Highlights

  • Longest-Prefix Matching: Updated parseMcpToolName to accept an optional knownServerNames parameter, enabling accurate parsing of MCP server names that contain underscores.
  • Backward Compatibility: Maintained the original regex-based splitting logic as a fallback mechanism for call sites that do not provide a list of known server names.
  • Integration: Updated local-executor.ts to dynamically retrieve and pass registered server names to parseMcpToolName, ensuring correct wildcard routing for underscore-containing server names.
  • Testing: Added 8 comprehensive test cases to mcp-tool.test.ts covering various scenarios, including common prefixes and underscore handling.
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 the 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 counterproductive. 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.

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.

@github-actions

Copy link
Copy Markdown

📊 PR Size: size/M

  • Lines changed: 108
  • Additions: +103
  • Deletions: -5
  • Files changed: 3

@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 updates parseMcpToolName to support longest-prefix matching using a list of known server names, which allows correctly parsing MCP tool names when the server name contains underscores. It also updates LocalAgentExecutor to pass the known server names from the tool registry and adds comprehensive unit tests. The reviewer identified a performance issue where parentToolRegistry.getAllTools() is called repeatedly inside a loop, creating an O(N^2) bottleneck during initialization. Caching the known server names on the registry instance is recommended to resolve this.

Comment on lines +228 to +238
const knownServerNames = Array.from(
new Set(
parentToolRegistry
.getAllTools()
.filter(
(t): t is DiscoveredMCPTool => t instanceof DiscoveredMCPTool,
)
.map((t) => t.serverName),
),
);
const parsed = parseMcpToolName(toolName, knownServerNames);

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.

high

Calling parentToolRegistry.getAllTools() is an expensive operation because it performs configuration resolution, metadata construction, alias expansion, and sorting for all registered tools. Since registerToolByName is called inside a loop over all available tools, computing knownServerNames on every iteration introduces an $O(N^2)$ performance bottleneck that can significantly slow down agent initialization.

We should cache the computed knownServerNames on the parentToolRegistry instance to ensure it is only computed once.

        const registry = parentToolRegistry as any;
        registry._knownServerNamesCache ??= Array.from(
          new Set(
            parentToolRegistry
              .getAllTools()
              .filter(
                (t): t is DiscoveredMCPTool => t instanceof DiscoveredMCPTool,
              )
              .map((t) => t.serverName),
          ),
        );
        const parsed = parseMcpToolName(toolName, registry._knownServerNamesCache);

@github-actions

Copy link
Copy Markdown

🛑 Action Required: Evaluation Approval

Steering changes have been detected in this PR. To prevent regressions, a maintainer must approve the evaluation run before this PR can be merged.

Maintainers:

  1. Go to the Workflow Run Summary.
  2. Click the yellow 'Review deployments' button.
  3. Select the 'eval-gate' environment and click 'Approve'.

Once approved, the evaluation results will be posted here automatically.

@gemini-cli gemini-cli Bot added the area/agent Issues related to Core Agent, Tools, Memory, Sub-Agents, Hooks, Agent Quality label Jun 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/agent Issues related to Core Agent, Tools, Memory, Sub-Agents, Hooks, Agent Quality size/m A medium sized PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

bug(MCP): parseMcpToolName fails when server name contains underscores

1 participant