feat: a tool that takes its search term as q also takes it as query
CI & Build / TypeScript typecheck (push) Successful in 14s
CI & Build / Python lint (push) Successful in 2s
CI & Build / Plugin hooks (push) Successful in 20s
CI & Build / Python tests (push) Successful in 1m58s
CI & Build / integration (push) Successful in 2m45s
CI & Build / Build & push image (push) Successful in 44s
CI & Build / TypeScript typecheck (push) Successful in 14s
CI & Build / Python lint (push) Successful in 2s
CI & Build / Plugin hooks (push) Successful in 20s
CI & Build / Python tests (push) Successful in 1m58s
CI & Build / integration (push) Successful in 2m45s
CI & Build / Build & push image (push) Successful in 44s
query was the most common misnamed argument in the live logs, and on a tool that declares q it cannot mean anything else. Passing both is still refused by the unknown-argument check. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -295,6 +295,23 @@ def _body_calls_write_tool(body: bytes) -> bool:
|
|||||||
return False
|
return False
|
||||||
|
|
||||||
|
|
||||||
|
# Spellings callers reach for that mean exactly one declared argument. A tool
|
||||||
|
# that declares the canonical name and not the alias takes the alias in its
|
||||||
|
# place: `query` is the most common miss for the search tools' `q`, and it
|
||||||
|
# cannot mean anything else. Passing both is left to the unknown-argument check.
|
||||||
|
_ALIASES = {"query": "q"}
|
||||||
|
|
||||||
|
|
||||||
|
def _with_aliases(arguments: dict | None, declared: set[str]) -> dict | None:
|
||||||
|
if not arguments:
|
||||||
|
return arguments
|
||||||
|
out = dict(arguments)
|
||||||
|
for alias, name in _ALIASES.items():
|
||||||
|
if alias in out and alias not in declared and name in declared and name not in out:
|
||||||
|
out[name] = out.pop(alias)
|
||||||
|
return out
|
||||||
|
|
||||||
|
|
||||||
class StrictArgsMCPServer(MCPServer):
|
class StrictArgsMCPServer(MCPServer):
|
||||||
"""An MCPServer that REJECTS tool calls carrying undeclared arguments.
|
"""An MCPServer that REJECTS tool calls carrying undeclared arguments.
|
||||||
|
|
||||||
@@ -323,6 +340,7 @@ class StrictArgsMCPServer(MCPServer):
|
|||||||
tool = None # unknown tool → let upstream produce its own error
|
tool = None # unknown tool → let upstream produce its own error
|
||||||
if tool is not None:
|
if tool is not None:
|
||||||
declared = set((tool.parameters or {}).get("properties", {}))
|
declared = set((tool.parameters or {}).get("properties", {}))
|
||||||
|
arguments = _with_aliases(arguments, declared)
|
||||||
unknown = sorted(set(arguments or {}) - declared)
|
unknown = sorted(set(arguments or {}) - declared)
|
||||||
if unknown:
|
if unknown:
|
||||||
hints = []
|
hints = []
|
||||||
|
|||||||
@@ -44,6 +44,44 @@ async def test_unknown_argument_is_an_error_not_a_silent_drop():
|
|||||||
assert "Nothing was created or changed" in msg
|
assert "Nothing was created or changed" in msg
|
||||||
|
|
||||||
|
|
||||||
|
def _query_server() -> StrictArgsMCPServer:
|
||||||
|
mcp = StrictArgsMCPServer("alias-test")
|
||||||
|
|
||||||
|
@mcp.tool()
|
||||||
|
def find(q: str = "") -> str:
|
||||||
|
return f"found:{q}"
|
||||||
|
|
||||||
|
@mcp.tool()
|
||||||
|
def own(query: str = "") -> str:
|
||||||
|
return f"own:{query}"
|
||||||
|
|
||||||
|
return mcp
|
||||||
|
|
||||||
|
|
||||||
|
async def test_query_is_taken_as_q_by_a_tool_that_declares_q():
|
||||||
|
mcp = _query_server()
|
||||||
|
assert "found:backups" in str(await mcp.call_tool("find", {"query": "backups"}))
|
||||||
|
# A tool that declares `query` itself keeps it.
|
||||||
|
assert "own:backups" in str(await mcp.call_tool("own", {"query": "backups"}))
|
||||||
|
|
||||||
|
|
||||||
|
async def test_query_beside_q_is_still_refused():
|
||||||
|
mcp = _query_server()
|
||||||
|
with pytest.raises(ToolError) as exc:
|
||||||
|
await mcp.call_tool("find", {"q": "a", "query": "b"})
|
||||||
|
assert "'query'" in str(exc.value)
|
||||||
|
|
||||||
|
|
||||||
|
async def test_the_real_search_tool_takes_query():
|
||||||
|
"""Against the REAL server: search declares `q`, so `query` is renamed
|
||||||
|
before the unknown-argument check rather than refused by it."""
|
||||||
|
from grimoire.mcp.server import _with_aliases, build_mcp_server
|
||||||
|
|
||||||
|
tool = build_mcp_server()._tool_manager.get_tool("search")
|
||||||
|
declared = set(tool.parameters["properties"])
|
||||||
|
assert _with_aliases({"query": "x"}, declared) == {"q": "x"}
|
||||||
|
|
||||||
|
|
||||||
async def test_empty_arguments_pass():
|
async def test_empty_arguments_pass():
|
||||||
mcp = _echo_server()
|
mcp = _echo_server()
|
||||||
assert await mcp.call_tool("echo", {}) is not None
|
assert await mcp.call_tool("echo", {}) is not None
|
||||||
|
|||||||
Reference in New Issue
Block a user