Skip to content

Commit 81973b1

Browse files
committed
fix(mcp): trim trailing underscores at the server cut so mangled names resolve
An uncut call whose hyphens the model turned into underscores was rebuilt with a trailing underscore before the separator when the server cut fell after a hyphen, so the recorded name differed from the declared one and resolveMcpToolByName could not match it.
1 parent 83ab8a7 commit 81973b1

3 files changed

Lines changed: 36 additions & 2 deletions

File tree

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,23 @@ describe("NativeToolCallParser.parseDynamicMcpTool name for API history", () =>
3535
})
3636
})
3737

38+
it("resolves an underscore-mangled uncut name whose server is cut next to a hyphen", () => {
39+
// The server cut lands right after the hyphen of "-backend"; in the model's underscore
40+
// spelling that hyphen is an underscore, which must be trimmed the same way.
41+
const server = `${"a".repeat(46)}-backend`
42+
const mangled = `mcp--${server}--${TOOL}`.replace(/-/g, "_")
43+
44+
const result = NativeToolCallParser.parseDynamicMcpTool({ id: "toolu_6", name: mangled, arguments: "{}" })
45+
46+
expect(result).not.toBeNull()
47+
expect(result!.name.length).toBeLessThanOrEqual(64)
48+
expect(toolNamesMatch(result!.name, buildMcpToolName(server, TOOL))).toBe(true)
49+
expect(resolveMcpToolByName([{ name: server, tools: [{ name: TOOL }] }], result!.name)).toEqual({
50+
serverName: server,
51+
toolName: TOOL,
52+
})
53+
})
54+
3855
it("keeps the declared name the model returned", () => {
3956
const declared = buildMcpToolName(SERVER, TOOL)
4057

‎src/utils/__tests__/mcp-name-long.spec.ts‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,19 @@ describe("long MCP tool names", () => {
6262
expect(name).not.toContain("---")
6363
expect(parseMcpToolName(name)?.serverName).toBe("a".repeat(46))
6464
})
65+
66+
it("cuts the hyphen and underscore spellings of a server name to the same length", () => {
67+
const server = `${"a".repeat(46)}-backend`
68+
69+
expect(buildMcpToolName(server.replace(/-/g, "_"), TOOL)).toBe(buildMcpToolName(server, TOOL))
70+
})
71+
72+
it("keeps one character of a server name made only of underscores", () => {
73+
const name = buildMcpToolName("_".repeat(60), TOOL)
74+
75+
expect(name.length).toBeLessThanOrEqual(64)
76+
expect(parseMcpToolName(name)?.serverName).toBe("_")
77+
})
6578
})
6679

6780
it("is deterministic and keeps tools that share a 55-character prefix distinct", () => {

‎src/utils/mcp-name.ts‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -160,9 +160,13 @@ export function buildMcpToolName(serverName: string, toolName: string): string {
160160
const prefix = `${MCP_TOOL_PREFIX}${MCP_TOOL_SEPARATOR}`
161161
// Cap the server part so the second separator and at least one tool character survive;
162162
// otherwise the shortened name no longer parses as mcp--server--tool and the call is dropped.
163-
// A trailing hyphen is removed so it cannot merge with the separator.
163+
// Trailing hyphens and underscores are removed (keeping at least one character) so the cut
164+
// cannot merge with the separator, and the hyphen and underscore spellings of a name are cut
165+
// to the same length.
164166
const maxServerLength = budget - prefix.length - MCP_TOOL_SEPARATOR.length - 1
165-
const server = sanitizeMcpName(serverName).slice(0, maxServerLength).replace(/-+$/, "")
167+
const server = sanitizeMcpName(serverName)
168+
.slice(0, maxServerLength)
169+
.replace(/(?<=.)[-_]+$/, "")
166170
const head = `${prefix}${server}${MCP_TOOL_SEPARATOR}`
167171
const tool = sanitizeMcpName(toolName).slice(0, budget - head.length)
168172

0 commit comments

Comments
 (0)