Skip to content

fix(mcp): reject an ambiguous agent name instead of the first match - #6253

Draft
shunxinyi-ljs wants to merge 1 commit into
Tencent:mainfrom
shunxinyi-ljs:fix/mcp-ambiguous-agent-name
Draft

shunxinyi-ljs wants to merge 1 commit into
Tencent:mainfrom
shunxinyi-ljs:fix/mcp-ambiguous-agent-name

Conversation

@shunxinyi-ljs

Copy link
Copy Markdown
Contributor

Description

resolve_agent_id fell back to the first case-insensitive name match:

if name_match is None and agent.get("name", "").lower() == needle:
    name_match = agent["id"]

Agent names are not unique, and the id it returns selects the model, knowledge bases and system prompt the chat runs against. With two agents named Helper, agent_chat silently ran on one of them, and nothing in the response said which.

Resolution now collects the name matches and refuses when there is more than one:

if len(name_matches) > 1:
    raise ValueError(
        f"Agent name {agent_id_or_name!r} matches {len(name_matches)} agents "
        f"({', '.join(name_matches)}). Use the id instead."
    )

An exact id match still wins and short-circuits, so callers that pass an id are untouched. This is the same rule the knowledge-base resolver got in #6240.

Type of Change

  • 🐛 Bug fix

Related Issue

Follow-up to #6240 (ambiguous knowledge-base name). Same failure mode in the agent resolver.

Testing

  • tests/test_agent_id_resolution.py::test_resolve_agent_id_rejects_ambiguous_name — two agents named Helper / helper resolve to a ValueError naming both ids.
  • test_resolve_agent_id_exact_id_still_wins — passing agent-2 returns agent-2 even when both agents share the name.
  • Reverse-verified by file-level rollback of weknora_mcp_server.py: the new test fails with Failed: DID NOT RAISE ValueError on the pre-patch tree and passes with the patch.
  • python -m pytest tests/test_agent_id_resolution.py — 5 passed.
  • Full suite python -m pytest test_*.py tests/ — passes; one skip (test_file_path_security.py needs symlink privileges on Windows).

Checklist

  • git diff --check origin/main...HEAD passes
  • Targeted tests pass locally
  • Diff-scoped lint (no Python linter configured for mcp-server/)
  • Self-reviewed the code
  • Added tests covering the change
  • Updated related documentation (docstring unchanged in meaning; the rule is documented in the new code path)
  • Breaking changes: an ambiguous name only — it used to succeed against an arbitrary agent, now it raises. Unique names and ids behave exactly as before.

Screenshots / Recordings

Not a UI change.

resolve_agent_id fell back to the first case-insensitive name match. Agent
names are not unique, and the resolved id selects the model, knowledge bases
and prompt a chat runs against, so a duplicate name silently ran the
conversation on an arbitrary agent.

Collect the name matches and fail when there is more than one, naming the
candidates. An exact id match still wins, as before.

This branch has not been deployed

No deployments
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.

1 participant