Repository navigation
[Fix] Over-long MCP tool names reach API history and break the task #2118
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
PierrunoYT
wants to merge
3
commits into
Zoo-Code-Org:main
Choose a base branch
from
PierrunoYT:fix/mcp-tool-name-history
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
src/core/assistant-message/__tests__/NativeToolCallParser-mcp-long-name.spec.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| import { buildMcpToolName, resolveMcpToolByName, toolNamesMatch } from "../../../utils/mcp-name" | ||
| import { NativeToolCallParser } from "../NativeToolCallParser" | ||
|
|
||
| const SERVER = "company-gateway-tools" | ||
| const TOOL = "confluence-confluence_get_space_page_tree" | ||
|
|
||
| describe("NativeToolCallParser.parseDynamicMcpTool name for API history", () => { | ||
| it("records an over-long uncut name under the declared name", () => { | ||
| const result = NativeToolCallParser.parseDynamicMcpTool({ | ||
| id: "toolu_1", | ||
| name: `mcp--${SERVER}--${TOOL}`, | ||
| arguments: "{}", | ||
| }) | ||
|
|
||
| expect(result?.name).toBe(buildMcpToolName(SERVER, TOOL)) | ||
| expect(result?.name.length).toBeLessThanOrEqual(64) | ||
| }) | ||
|
|
||
| it("resolves an over-long uncut name whose hyphens the model turned into underscores", () => { | ||
| const mangled = `mcp--${SERVER}--${TOOL}`.replace(/-/g, "_") | ||
|
|
||
| const result = NativeToolCallParser.parseDynamicMcpTool({ id: "toolu_5", name: mangled, arguments: "{}" }) | ||
|
|
||
| expect(result).not.toBeNull() | ||
| expect(result!.name.length).toBeLessThanOrEqual(64) | ||
| // The recorded name is the declared one, up to the hyphen/underscore spelling. | ||
| expect(toolNamesMatch(result!.name, buildMcpToolName(SERVER, TOOL))).toBe(true) | ||
| expect(resolveMcpToolByName([{ name: SERVER, tools: [{ name: TOOL }] }], result!.name)).toEqual({ | ||
| serverName: SERVER, | ||
| toolName: TOOL, | ||
| }) | ||
| }) | ||
|
|
||
| it("keeps the declared name the model returned", () => { | ||
| const declared = buildMcpToolName(SERVER, TOOL) | ||
|
|
||
| const result = NativeToolCallParser.parseDynamicMcpTool({ id: "toolu_2", name: declared, arguments: "{}" }) | ||
|
|
||
| expect(result?.name).toBe(declared) | ||
| }) | ||
|
|
||
| it("parses the declared name of a tool on a server with a very long name", () => { | ||
| const declared = buildMcpToolName("s".repeat(60), TOOL) | ||
|
|
||
| const result = NativeToolCallParser.parseDynamicMcpTool({ id: "toolu_4", name: declared, arguments: "{}" }) | ||
|
|
||
| expect(result).toMatchObject({ type: "mcp_tool_use", name: declared }) | ||
| }) | ||
|
|
||
| it("keeps a short name verbatim", () => { | ||
| const result = NativeToolCallParser.parseDynamicMcpTool({ | ||
| id: "toolu_3", | ||
| name: "mcp--weather--get_forecast", | ||
| arguments: "{}", | ||
| }) | ||
|
|
||
| expect(result).toMatchObject({ name: "mcp--weather--get_forecast", serverName: "weather" }) | ||
| }) | ||
| }) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| import type { Anthropic } from "@anthropic-ai/sdk" | ||
|
|
||
| import type { ApiMessage } from "../../task-persistence" | ||
| import { buildMcpToolName } from "../../../utils/mcp-name" | ||
| import { capMcpToolUseNames } from "../capMcpToolUseNames" | ||
|
|
||
| type Block = Anthropic.Messages.ContentBlockParam | ||
|
|
||
| const SERVER = "company-gateway-tools" | ||
| const TOOL = "confluence-confluence_get_space_page_tree" | ||
| const UNCUT = `mcp--${SERVER}--${TOOL}` | ||
|
|
||
| const use = (name: string): Block => ({ type: "tool_use", id: "toolu_1", name, input: {} }) | ||
| const assistant = (...content: Block[]): ApiMessage => ({ role: "assistant", content }) | ||
|
|
||
| describe("capMcpToolUseNames", () => { | ||
| it("renames an over-long MCP tool_use to the declared name", () => { | ||
| const [message] = capMcpToolUseNames([assistant({ type: "text", text: "calling" }, use(UNCUT))]) | ||
|
|
||
| expect(message.content).toEqual([{ type: "text", text: "calling" }, use(buildMcpToolName(SERVER, TOOL))]) | ||
| }) | ||
|
|
||
| it("leaves short names, non-MCP names and other roles untouched", () => { | ||
| const messages: ApiMessage[] = [ | ||
| { role: "user", content: "hi" }, | ||
| assistant(use("read_file"), use("mcp--weather--get_forecast")), | ||
| ] | ||
|
|
||
| expect(capMcpToolUseNames(messages)).toEqual(messages) | ||
| }) | ||
|
|
||
| it("leaves an over-long name it cannot parse as an MCP tool", () => { | ||
| const name = "x".repeat(70) | ||
|
|
||
| expect(capMcpToolUseNames([assistant(use(name))])[0].content).toEqual([use(name)]) | ||
| }) | ||
|
|
||
| it("does not mutate its input", () => { | ||
| const input = [assistant(use(UNCUT))] | ||
| const snapshot = structuredClone(input) | ||
|
|
||
| capMcpToolUseNames(input) | ||
|
|
||
| expect(input).toEqual(snapshot) | ||
| }) | ||
| }) |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Assert the exact recorded spelling in the mangled-name test.
toolNamesMatchtreats-and_as equal, so line 27 also passes when the parser records the underscore spelling. For this input the parser recordsmcp--company_gateway_tools--confluence_confluence_get_s_<hash>. The parser has no registry to restore the original hyphens. Assert the spelling the parser produces, so a change to that spelling fails the test.🧪 Suggested fix
Committable suggestion