Skip to content
This repository was archived by the owner on May 15, 2026. It is now read-only.

Commit fbbe3c1

Browse files
committed
feat: replace hyphen encoding with fuzzy matching for MCP tool names
Replace triple-underscore encoding (___) with fuzzy matching that treats hyphens and underscores as equivalent. This keeps MCP tool names short and readable while still handling model mangling of hyphens. - Remove HYPHEN_ENCODING and decodeMcpName() - Add normalizeForComparison() and toolNamesMatch() for fuzzy matching - Update McpHub.findServerNameBySanitizedName() with fuzzy match fallback - Update tests to reflect new behavior Related to: #10766
1 parent 8b9f02a commit fbbe3c1

4 files changed

Lines changed: 291 additions & 152 deletions

File tree

‎src/core/prompts/tools/native-tools/__tests__/mcp_server.spec.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ describe("getMcpServerTools", () => {
8989

9090
// Should only have one tool (from project server)
9191
expect(result).toHaveLength(1)
92-
expect(getFunction(result[0]).name).toBe("mcp--context7--resolve___library___id")
92+
expect(getFunction(result[0]).name).toBe("mcp--context7--resolve-library-id")
9393
// Project server takes priority
9494
expect(getFunction(result[0]).description).toBe("Project description")
9595
})

‎src/services/mcp/McpHub.ts‎

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ import { fileExistsAtPath } from "../../utils/fs"
3838
import { arePathsEqual, getWorkspacePath } from "../../utils/path"
3939
import { injectVariables } from "../../utils/config"
4040
import { safeWriteJson } from "../../utils/safeWriteJson"
41-
import { sanitizeMcpName } from "../../utils/mcp-name"
41+
import { sanitizeMcpName, toolNamesMatch } from "../../utils/mcp-name"
4242

4343
// Discriminated union for connection states
4444
export type ConnectedMcpConnection = {
@@ -940,16 +940,30 @@ export class McpHub {
940940
* Find a connection by sanitized server name.
941941
* This is used when parsing MCP tool responses where the server name has been
942942
* sanitized (e.g., hyphens replaced with underscores) for API compliance.
943+
* Uses fuzzy matching to handle cases where models convert hyphens to underscores.
943944
* @param sanitizedServerName The sanitized server name from the API tool call
944945
* @returns The original server name if found, or null if no match
945946
*/
946947
public findServerNameBySanitizedName(sanitizedServerName: string): string | null {
948+
// First, check for an exact match
947949
const exactMatch = this.connections.find((conn) => conn.server.name === sanitizedServerName)
948950
if (exactMatch) {
949951
return exactMatch.server.name
950952
}
951953

952-
return this.sanitizedNameRegistry.get(sanitizedServerName) ?? null
954+
// Check the registry for sanitized name mapping
955+
const registryMatch = this.sanitizedNameRegistry.get(sanitizedServerName)
956+
if (registryMatch) {
957+
return registryMatch
958+
}
959+
960+
// Use fuzzy matching: treat hyphens and underscores as equivalent
961+
const fuzzyMatch = this.connections.find((conn) => toolNamesMatch(conn.server.name, sanitizedServerName))
962+
if (fuzzyMatch) {
963+
return fuzzyMatch.server.name
964+
}
965+
966+
return null
953967
}
954968

955969
private async fetchToolsList(serverName: string, source?: "global" | "project"): Promise<McpTool[]> {

0 commit comments

Comments
 (0)