Skip to content

The agent that grows with you

fix(mcp): prefer server-native tool over generated utility on name collision (#87112)

PRATHAMESH75d6f18cd7db26
diff --git a/tests/tools/test_mcp_tool.py b/tests/tools/test_mcp_tool.py
index 831154bdd75..1f92de0a1b5 100644
--- a/tests/tools/test_mcp_tool.py
+++ b/tests/tools/test_mcp_tool.py
@@ -768,6 +768,58 @@ class TestDiscoverAndRegister:
for record in caplog.records
)
+ def test_native_tool_wins_over_generated_utility_on_collision(self, caplog):
+ """A server-native tool named `read_resource` must survive its collision
+ with the generated `read_resource` utility (#87112).
+
+ Before the fix the collision handler treated the pair as ambiguous and
+ skipped BOTH, so the server's own tool vanished on every boot. The
+ generated utility is only sugar for servers that lack such a tool, so
+ the native tool wins and the utility is dropped.
+ """
+ from tools.mcp_tool import _register_server_tools
+ from tools.registry import ToolRegistry
+
+ registry = ToolRegistry()
+ server = _make_mock_server(
+ "srv",
+ session=MagicMock(),
+ tools=[
+ _make_mcp_tool("read_resource", "Native read-resource tool"),
+ _make_mcp_tool("safe_tool"),
+ ],
+ )
+ # Resources enabled (default) so the read_resource utility is generated
+ # and collides; prompts disabled to keep the candidate set focused.
+ config = {"tools": {"prompts": False}}
+
+ with patch("tools.registry.registry", registry), \
+ patch("tools.mcp_tool._track_mcp_tool_server"), \
+ caplog.at_level(logging.INFO, logger="tools.mcp_tool"):
+ registered = _register_server_tools("srv", server, config)
+
+ # The native tool is registered (before the fix it was dropped) and it
+ # is the server's tool, not the utility stub.
+ assert "mcp__srv__read_resource" in registered
+ entry = registry.get_entry("mcp__srv__read_resource")
+ assert entry is not None
+ assert entry.description == "Native read-resource tool"
+ assert "mcp__srv__safe_tool" in registered
+
+ # The collision was resolved in favour of the native tool, not skipped
+ # as ambiguous.
+ assert not any(
+ "name normalization collision" in record.message
+ and "mcp__srv__read_resource" in record.message
+ for record in caplog.records
+ )
+ assert any(
+ record.levelno == logging.INFO
+ and "keeping the native tool and dropping the utility" in record.message
+ and "read_resource" in record.message
+ for record in caplog.records
+ )
+
# ---------------------------------------------------------------------------
# MCPServerTask (run / start / shutdown)
# ---------------------------------------------------------------------------
diff --git a/tools/mcp_tool.py b/tools/mcp_tool.py
index f380b8cea4f..49fbf6f7eea 100644
--- a/tools/mcp_tool.py
+++ b/tools/mcp_tool.py
@@ -6420,11 +6420,41 @@ def _register_server_tools(name: str, server: MCPServerTask, config: dict) -> Li
candidate["origin"]
)
- ambiguous_names = {
- registry_name: sorted(origins)
- for registry_name, origins in origins_by_name.items()
- if len(origins) > 1
- }
+ # A generated resource/prompt utility that normalizes onto a server-native
+ # tool's name must not knock that native tool out of the registry: the
+ # native tool is the capability the user connected the server for, while the
+ # generated utility (read_resource/list_resources/list_prompts/get_prompt)
+ # is optional sugar that only matters when the server exposes no such tool
+ # of its own (#87112). Resolve that specific collision in favour of the
+ # native tool — keep it, drop the shadowed utility — and fall back to the
+ # conservative skip-everything only for genuinely ambiguous collisions (two
+ # or more native tools normalizing to one name, which we cannot
+ # disambiguate). The four utility keys are distinct, so a colliding set
+ # holds at most one utility origin.
+ ambiguous_names: Dict[str, List[str]] = {}
+ shadowed_utilities: set[tuple[str, str]] = set()
+ for registry_name, origins in origins_by_name.items():
+ if len(origins) <= 1:
+ continue
+ utility_origins = sorted(
+ o for o in origins if o.startswith("generated utility ")
+ )
+ native_origins = sorted(origins - set(utility_origins))
+ if len(native_origins) == 1 and utility_origins:
+ for util_origin in utility_origins:
+ shadowed_utilities.add((registry_name, util_origin))
+ logger.info(
+ "MCP server '%s': generated utility %s normalizes onto "
+ "server-native %s — keeping the native tool and dropping the "
+ "utility (the utility only applies when the server has no such "
+ "tool of its own)",
+ name,
+ ", ".join(utility_origins),
+ native_origins[0],
+ )
+ continue
+ ambiguous_names[registry_name] = sorted(origins)
+
for registry_name, origins in sorted(ambiguous_names.items()):
logger.error(
"MCP server '%s': name normalization collision for '%s' from %s; "
@@ -6439,6 +6469,8 @@ def _register_server_tools(name: str, server: MCPServerTask, config: dict) -> Li
registry_name = candidate["registry_name"]
if registry_name in ambiguous_names:
continue
+ if (registry_name, candidate["origin"]) in shadowed_utilities:
+ continue
existing_toolset = registry.get_toolset_for_tool(registry_name)
if existing_toolset and existing_toolset != toolset_name: