fix(mcp): truncate tool keys longer than 64 characters#25288
fix(mcp): truncate tool keys longer than 64 characters#25288saisharan0103 wants to merge 1 commit into
Conversation
Anthropic and OpenAI tool calling APIs reject tool names exceeding 64 characters. Long MCP server names combined with tool names can produce keys past that limit, causing requests to fail validation and the session to die silently. Build the registered tool key through a helper that truncates oversized keys deterministically and appends a short SHA-1 suffix to keep them unique across collisions, and emit a warn log when truncation happens.
|
This PR doesn't fully meet our contributing guidelines and PR template. What needs to be fixed:
Please edit this PR description to address the above within 2 hours, or it will be automatically closed. If you believe this was flagged incorrectly, please let a maintainer know. |
|
The following comment was made by an LLM, it may be inaccurate: Found a related PR: PR #15595: fix: truncate MCP tool names exceeding 64 characters This appears to be addressing the same issue - truncating MCP tool names that exceed 64 characters to comply with tool calling API limits. This could be a duplicate or related effort that may have already been attempted. |
|
denounce spamming ai prs |
|
This pull request has been automatically closed because it was not updated to meet our contributing guidelines within the 2-hour window. Feel free to open a new pull request that follows our guidelines. |
Issue for this PR
Closes #3523
Type of change
What does this PR do?
Anthropic and OpenAI tool calling APIs reject tool names longer than 64 characters. When an MCP server is configured with a long name, the registered tool key
<sanitized_server>_<sanitized_tool>produced inpackages/opencode/src/mcp/index.tscould exceed that limit, causing the request to fail validation and the session to die silently with no further prompts processed.The fix adds a
buildToolKey(clientName, toolName)helper that returns<sanitized_client>_<sanitized_tool>when the natural form fits within 64 characters, and otherwise truncates the key and appends an 8-character SHA-1 hash of the original key (<truncated>_<hash>) so collisions remain unique. Thetools()registration loop now goes through the helper and emits a warn log when truncation happens so users can rename if they want a stable readable key.How did you verify your code works?
bun run typecheck(tsgo) — clean.oxlint packages/opencode/src/mcp/index.ts packages/opencode/test/mcp/lifecycle.test.ts— same 11 pre-existing warnings before and after, 0 errors.packages/opencode/test/mcp/lifecycle.test.tsusing a 62-character server name that asserts all registered keys are sanitized, ≤ 64 characters, and unique across tools from the same server.bun test packages/opencode/test/mcp/lifecycle.test.ts— 20/20 pass.Screenshots / recordings
N/A
Checklist