Skip to content

fix(mcp): truncate tool keys longer than 64 characters#25288

Closed
saisharan0103 wants to merge 1 commit into
anomalyco:devfrom
saisharan0103:fix/issue-3523-mcp-tool-name-length
Closed

fix(mcp): truncate tool keys longer than 64 characters#25288
saisharan0103 wants to merge 1 commit into
anomalyco:devfrom
saisharan0103:fix/issue-3523-mcp-tool-name-length

Conversation

@saisharan0103
Copy link
Copy Markdown

@saisharan0103 saisharan0103 commented May 1, 2026

Issue for this PR

Closes #3523

Type of change

  • Bug fix
  • New feature
  • Refactor / code improvement
  • Documentation

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 in packages/opencode/src/mcp/index.ts could 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. The tools() 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.
  • Added a regression test in packages/opencode/test/mcp/lifecycle.test.ts using 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

  • I have tested my changes locally
  • I have not included unrelated changes in this PR

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.
@github-actions github-actions Bot added the needs:compliance This means the issue will auto-close after 2 hours. label May 1, 2026
@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented May 1, 2026

This PR doesn't fully meet our contributing guidelines and PR template.

What needs to be fixed:

  • PR description is missing required template sections. Please use the PR template.

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.

@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented May 1, 2026

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
#15595

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.

@rekram1-node
Copy link
Copy Markdown
Collaborator

denounce spamming ai prs

opencode-agent Bot pushed a commit that referenced this pull request May 1, 2026
@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented May 1, 2026

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.

@github-actions github-actions Bot removed the needs:compliance This means the issue will auto-close after 2 hours. label May 1, 2026
@github-actions github-actions Bot closed this May 1, 2026
oleksii-honchar pushed a commit to oleksii-honchar/better-opencode that referenced this pull request May 6, 2026
arthurmiao54-afk pushed a commit to lsHaoo/cimicode that referenced this pull request May 7, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] tools[21].function.name': string too long. Expected a string with maximum length 64, but got a string with length 65 instead.

3 participants