fix(mcp): resolve non-UUID agent IDs

main
“ZhuangShiHao1207” 2026-07-30 16:25:27 +08:00 committed by lyingbug
parent 2611babfef
commit b67ed27e1d
2 changed files with 43 additions and 4 deletions

View File

@ -0,0 +1,34 @@
from weknora_mcp_server import WeKnoraClient
def test_resolve_agent_id_accepts_non_uuid_agent_id(monkeypatch):
client = WeKnoraClient("http://example.test/api/v1", "")
agents = {
"data": [
{
"id": "builtin-wiki-researcher",
"name": "维基问答",
}
]
}
monkeypatch.setattr(client, "_request", lambda *args, **kwargs: agents)
assert (
client.resolve_agent_id("builtin-wiki-researcher")
== "builtin-wiki-researcher"
)
def test_resolve_agent_id_accepts_agent_name_case_insensitively(monkeypatch):
client = WeKnoraClient("http://example.test/api/v1", "")
agents = {
"data": [
{
"id": "builtin-wiki-researcher",
"name": "Wiki Questioner",
}
]
}
monkeypatch.setattr(client, "_request", lambda *args, **kwargs: agents)
assert client.resolve_agent_id("wiki questioner") == "builtin-wiki-researcher"

View File

@ -213,11 +213,12 @@ class WeKnoraClient:
)
def resolve_agent_id(self, agent_id_or_name: str) -> str:
"""Resolve an agent name to its UUID if needed.
"""Resolve an agent ID or name to its canonical ID.
If *agent_id_or_name* is already a UUID it is returned unchanged.
Otherwise all agents are listed and the first one whose
``name`` matches (case-insensitive) is returned.
Otherwise all agents are listed and the first one whose ``id``
matches exactly or whose ``name`` matches case-insensitively is
returned.
Raises ValueError when no match is found.
"""
if self._UUID_RE.match(agent_id_or_name):
@ -228,7 +229,11 @@ class WeKnoraClient:
agents = agents.get("list", agents.get("items", []))
needle = agent_id_or_name.lower()
for agent in (agents or []):
if isinstance(agent, dict) and agent.get("name", "").lower() == needle:
if not isinstance(agent, dict):
continue
if agent.get("id") == agent_id_or_name:
return agent["id"]
if agent.get("name", "").lower() == needle:
return agent["id"]
raise ValueError(
f"Agent {agent_id_or_name!r} not found. "