google-gemini / google-gemini/gemini-cli

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

Open
#27,981 1 comment 0 reactions 0 assignees View on GitHub
area/agent status/need-triage
Dominant language
TypeScript
Stars
107k
Forks
14.6k
Avg merge
2d 3h
Merged PRs (30d)
45

Description

## What happened?

The `parseMcpToolName` function in `packages/core/src/tools/mcp-tool.ts` uses a regex that cannot handle MCP server names containing underscores. This causes incorrect tool name resolution for any server with an underscore in its name.

**Affected code** in `packages/core/src/tools/mcp-tool.ts` (line ~47):

```typescript
export function parseMcpToolName(name: string): {
serverName?: string;
toolName?: string;
} {
if (!isMcpToolName(name)) {
return {};
}
const withoutPrefix = name.slice(MCP_TOOL_PREFIX.length);
// BUG: regex requires server name to have NO underscores
const match = withoutPrefix.match(/^([^_]+)_(.+)$/);
if (match) {
return {
serverName: match[1],
toolName: match[2],
};
}
return {};
}
```

The regex `/^([^_]+)_(.+)$/ ` captures `[^_]+` as the server name, which means it stops at the **first** underscore. Any server name containing underscores will be incorrectly split.

**Example**:
- Qualified name: `mcp_my_server_my_tool`
- Expected: `{ serverName: 'my_server', toolName: 'my_tool' }`
- Actual: `{ serverName: 'my', toolName: 'server_my_tool' }`

## What did you expect to happen?

The function should correctly parse tool names even when server names contain underscores. Since the naming convention is `mcp_{serverName}_{toolName}` and there is no restriction on underscores in server names, the parser needs a different strategy.

Possible solutions:
1. **Use a different separator** (e.g., double underscore `__`) between server and tool names to avoid ambiguity
2. **Store a mapping** of registered server names and use longest-prefix matching
3. **Use the last underscore** as the separator: `/^(.+)_([^_]+)$/ ` (tool names are less likely to contain underscores)

## Steps to reproduce

1. Configure an MCP server with an underscore in its name (e.g., `my_server`)
2. The server registers a tool (e.g., `my_tool`)
3. The qualified tool name becomes `mcp_my_server_my_tool`
4. When the tool is called, `parseMcpToolName` incorrectly extracts `my` as the server name
5. Tool routing fails or routes to the wrong server

## Client information

Client Information

Running on Windows 11, gemini-cli latest stable.

```console
> /about
```

## Additional context

This affects any MCP server with underscores in its name. Since underscores are common in server naming conventions (e.g., `github_copilot`, `google_workspace`, `my_custom_server`), this bug likely impacts a significant number of users. The issue also affects policy matching since the `formatMcpToolName` function generates names with underscores that the parser then misinterprets.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.