Skip to content

[Bug]: Registering a tool with an existing name silently replaces the existing tool (no error, no log), including MCP tools shadowing local tools #3328

Closed

Description

@xuchaoguo1983

Describe the bug
Registering a tool whose name is already registered silently replaces the existing tool. ToolRegistry.registerTool just does tools.put(toolName, tool): no exception, no log. The toolkit ends up with only the last tool under that name, its description and schema are what the model sees, and it is what gets executed.

This matters most for MCP. MCP tools are registered after the agent's own tools, so with prefixToolNames off, a remote tool that happens to share a name with a local tool silently takes its place. Anything keyed by tool name (permission rules, tool groups, confirmation handling) then applies to the remote tool instead of the local one, and nothing tells the developer. The same happens when two local tool classes declare the same @Tool name.

To Reproduce

class LocalTools {
    @Tool(name = "dup", description = "local version")
    public String dup(@ToolParam(name = "x", description = "x") String x) { return "LOCAL " + x; }
}
class OtherTools {
    @Tool(name = "dup", description = "remote version")
    public String dup(@ToolParam(name = "y", description = "y") String y) { return "REMOTE " + y; }
}

Toolkit toolkit = new Toolkit();
toolkit.registerTool(new LocalTools());
toolkit.registerTool(new OtherTools());   // no exception, no log

toolkit.getToolNames();                   // [dup]
toolkit.getTool("dup").getDescription();  // "remote version"
toolkit.getTool("dup").getParameters();   // {properties={y=...}, required=[y]}

Verified on 2.0.3; ToolRegistry.registerTool is unchanged on main (857e3e6).

Expected behavior
Registering a duplicate name should fail fast (e.g. IllegalStateException naming both tools), or at least log a warning, with an explicit API for intentional replacement. For MCP, a name clash with an existing tool should be reported when the server's tools are registered, so the developer can enable prefixToolNames or rename.

Environment
AgentScope-Java 2.0.3 and main (857e3e6), JDK 21.

Activity

  1. amyaxy commented on Sep 28, 2026

    @amyaxy
    Contributor

    I can fix this. I'll submit a PR soon.

  2. oss-maintainer commented on Sep 28, 2026

    @oss-maintainer
    Collaborator

    Issue Evaluation

    Category: bug | Status: Confirmed (verified against main)

    The silent last-write-wins behaviour is real. Evidence from the current code:

    • ToolRegistry.registerTool (agentscope-core/src/main/java/io/agentscope/core/tool/ToolRegistry.java, the tools.put(...) / registeredTools.put(...) block) performs only a null/blank-name check — no duplicate detection, no warning, no explicit replace API.
    • Every registration path funnels through the private Toolkit.registerAgentTool (Toolkit.java), which logs INFO "Registered tool ..." identically for a first registration and a replacement. So there is a success log, but nothing indicates that an existing tool was overwritten — the "no log" part of the report is slightly imprecise, the underlying problem is not.
    • Contrast within the same subsystem: ToolGroupManager.createToolGroup does fail fast (IllegalArgumentException("Tool group '%s' already exists")). The tool layer simply does not.
    • MCP path confirmed: McpClientManager.registerMcpClient builds the tool key as toolNamePrefix + mcpTool.name(), and McpServerRegistrar.registerClient only applies the <name>__ prefix when cfg.isPrefixToolNames(). McpServerConfig.prefixToolNames defaults to false.
    • Ordering claim confirmed: HarnessAgent.Builder.build() registers all local/harness tools first and runs McpServerRegistrar.register afterwards, so by default a same-named remote MCP tool replaces the local tool.

    Impact

    • Both the tools and registeredTools entries are replaced, so RegisteredToolFunction metadata (preset parameters, extended model, mcpClientName) and group wiring silently move to the winning tool.
    • ToolExecutor resolves by name at call time, so permission rules, tool groups and confirmation/HITL handling keyed by tool name end up applying to the remote implementation. With an untrusted MCP server exposing generic names (read_file, shell, search), this is a real integrity risk, not just an ergonomic one.
    • Stale group state: the old name→group mapping stays in ToolGroupManager while the registry holds a different tool, so schema visibility can diverge from the executed implementation.

    Severity: high

    Suggested direction (important caveat)
    A plain throw on duplicate is not safe as-is, because same-name re-registration is relied on today:

    • Toolkit.copy() → copy.registerMetaTool() re-registers reset_equipped_tools after ToolRegistry.copyTo already copied it — production path, hit on every copy of a toolkit that has the meta tool (also ReActAgent.Builder.build() / toBuilder(), HarnessAgent.Builder.build()).
    • ToolkitTest.removeToolIfSame_noOpWhenReplaced asserts that the second registration wins, i.e. replacement is an explicitly contracted behaviour.
    • SkillToolFactory.registerRuntimeLoadTool and SkillBox.registerSkillLoadTool already work around the overwrite with their own existence checks.
    • McpServerRegistrar.registerClient would re-register identical MCP names if register were ever re-invoked on the same toolkit (reload/reconnect).

    A change that keeps those paths working: detect an existing different instance at registration time, logger.warn naming both tools (old class → new class, plus the MCP server for MCP registrations), and add an explicit replace API/flag that the intentional re-registration paths above opt into. Report MCP-vs-local name clashes at MCP registration time so users can enable prefixToolNames or rename.

    Maintainers: before any PR is generated, please confirm the intended contract — is silent replacement of a same-named tool a supported feature (as removeToolIfSame_noOpWhenReplaced suggests) or an accident to be closed?


    Automated evaluation by github-manager-bot

  3. added
    area/core/tool
    bug
    on Sep 28, 2026
  4. added 3 commits that reference this issue on Sep 28, 2026
    f76f79d
    8fa2e4b
    95cebc5
  5. added a commit that references this issue on Oct 11, 2026
    66a3aa7
  6. added a commit that references this issue on Oct 29, 2026
    9ddf42d
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area/core/toolbug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions