Skip to content

fix(tools): keep MCP tool names unique for non-ASCII inputs - #5360

Open
yancyyan058 wants to merge 2 commits into
HKUDS:mainfrom
yancyyan058:fix/mcp-sanitize-non-ascii
Open

fix(tools): keep MCP tool names unique for non-ASCII inputs#5360
yancyyan058 wants to merge 2 commits into
HKUDS:mainfrom
yancyyan058:fix/mcp-sanitize-non-ascii

Conversation

@yancyyan058

@yancyyan058 yancyyan058 commented Aug 12, 2026

Copy link
Copy Markdown

Problem

_sanitize_name replaces every character outside [a-zA-Z0-9_-] with _.
A fully non-ASCII name such as "获取天气" collapses to "_" after sanitization,
causing silent name collisions when multiple such tools are registered from the
same MCP server.

Changes

  • Extract _short_digest(value, length) to consolidate the repeated
    hashlib.sha1 pattern shared by _limit_tool_name and the new fallback path.
  • Add a fallback in _sanitize_mcp_tool_name: when sanitization produces an
    all-underscore result, return tool_<8-char sha1 hex> instead.
  • _sanitize_name and _limit_tool_name are unchanged.

Behavior

Input Before After
"获取天气" "_" "tool_a3f9c21b"
"日本語ツール" "_" "tool_8704490a"
"get_weather" "get_weather" "get_weather"
"My Tool" "My_Tool" "My_Tool"

Related

Touches the same area as #3468 (spaces in MCP resource/prompt names), but
addresses a distinct failure mode: fully non-ASCII names that sanitize to
a single underscore rather than an invalid character.

_sanitize_name collapsed fully non-ASCII names (e.g. Chinese) to a
single "_", causing silent registration collisions. Fall back to a
deterministic SHA-1-based name when the sanitized result is all
underscores, and cover the fallback with regression tests.

Co-Authored-By: Claude <noreply@anthropic.com>
@chengyongru chengyongru added bug Something isn't working fix priority: p2 Normal backlog: minor bug, enhancement, docs, cleanup, edge case, or unvalidated proposal. test labels Aug 12, 2026
The previous fallback only fired for fully non-ASCII names, but the real
call path always prepends ``mcp_{server}_``, so Chinese tool names still
collapsed to ``mcp_server_`` and collided. Detect non-ASCII anywhere in
the name and append a deterministic digest to the stripped core.

Co-Authored-By: Claude <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working fix priority: p2 Normal backlog: minor bug, enhancement, docs, cleanup, edge case, or unvalidated proposal. test

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants