Skip to content

Commit 80226e3

Browse files
PierrunoYTclaude
andcommitted
fix(mcp): hash the normalized name so underscore-mangled calls resolve
Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
1 parent f7d59ab commit 80226e3

2 files changed

Lines changed: 22 additions & 2 deletions

File tree

‎src/core/assistant-message/__tests__/NativeToolCallParser-mcp-long-name.spec.ts‎

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { buildMcpToolName } from "../../../utils/mcp-name"
1+
import { buildMcpToolName, resolveMcpToolByName, toolNamesMatch } from "../../../utils/mcp-name"
22
import { NativeToolCallParser } from "../NativeToolCallParser"
33

44
const SERVER = "company-gateway-tools"
@@ -16,6 +16,21 @@ describe("NativeToolCallParser.parseDynamicMcpTool name for API history", () =>
1616
expect(result?.name.length).toBeLessThanOrEqual(64)
1717
})
1818

19+
it("resolves an over-long uncut name whose hyphens the model turned into underscores", () => {
20+
const mangled = `mcp--${SERVER}--${TOOL}`.replace(/-/g, "_")
21+
22+
const result = NativeToolCallParser.parseDynamicMcpTool({ id: "toolu_5", name: mangled, arguments: "{}" })
23+
24+
expect(result).not.toBeNull()
25+
expect(result!.name.length).toBeLessThanOrEqual(64)
26+
// The recorded name is the declared one, up to the hyphen/underscore spelling.
27+
expect(toolNamesMatch(result!.name, buildMcpToolName(SERVER, TOOL))).toBe(true)
28+
expect(resolveMcpToolByName([{ name: SERVER, tools: [{ name: TOOL }] }], result!.name)).toEqual({
29+
serverName: SERVER,
30+
toolName: TOOL,
31+
})
32+
})
33+
1934
it("keeps the declared name the model returned", () => {
2035
const declared = buildMcpToolName(SERVER, TOOL)
2136

‎src/utils/mcp-name.ts‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,12 @@ export function buildMcpToolName(serverName: string, toolName: string): string {
150150
return fullName
151151
}
152152

153-
const hash = createHash("sha256").update(fullName).digest("hex").slice(0, HASH_SUFFIX_LENGTH)
153+
// Models may return the name with hyphens turned into underscores; hash the normalized spelling
154+
// so such a name still rebuilds to the declared one.
155+
const hash = createHash("sha256")
156+
.update(normalizeForComparison(fullName))
157+
.digest("hex")
158+
.slice(0, HASH_SUFFIX_LENGTH)
154159
const budget = MAX_TOOL_NAME_LENGTH - HASH_SUFFIX_LENGTH - 1
155160
const prefix = `${MCP_TOOL_PREFIX}${MCP_TOOL_SEPARATOR}`
156161
// Cap the server part so the second separator and at least one tool character survive;

0 commit comments

Comments
 (0)