From 1454580991f9edaaf430af246a0f4cf3380ff757 Mon Sep 17 00:00:00 2001 From: ahmedandaloes Date: Sun, 11 Oct 2026 19:51:20 +0300 Subject: [PATCH 1/2] fix(mcp): send an out-of-range batch control to the validator instead of dropping it laya_predict_batch_tool collapsed every `hooks_timeout < 0` to None and every `min_confidence < 0` to None before calling the tools layer, so an invalid value became "unset": the client got a clean response and no way to tell the deadline or the abstention gate it asked for had been dropped. laya_predict_batch owns validation for both -- `_validate_hooks_timeout` and `validate_min_confidence` raise a ToolError naming the value -- and the above-range side already reached it (1.5 is refused), so only the below-range side was silently swallowed. hooks_timeout now uses the sibling tool's spelling, `or None`, which folds the eager-client 0 into "unset" and leaves anything else alone. min_confidence uses None as its unset marker, like every other tool in the file, and is forwarded verbatim. -1 was never a valid threshold (the range is [0, 1]) and the tools layer already refuses it, so the declaration default was the only thing reading it as "unset". Verified: tests/test_mcp.py gains test_batch_tool_control_forwarding(), which fails 4 checks without this change and passes with it (968 passed, 0 failed); tests/test_mcp_remote.py 53 passed; ruff check laya/ --select=E9,F63,F7,F82,F401,F811 --line-length=120 All checks passed. Refs #707 --- laya/mcp/server.py | 14 +++++++--- tests/test_mcp.py | 66 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 77 insertions(+), 3 deletions(-) diff --git a/laya/mcp/server.py b/laya/mcp/server.py index 41a579900..d837d4d88 100644 --- a/laya/mcp/server.py +++ b/laya/mcp/server.py @@ -334,17 +334,25 @@ def laya_predict_tool( ), ) def laya_predict_batch_tool(requests: list, batch_size: int = 0, hooks_timeout: float = 0, - min_confidence: float = -1, sort_by_length: bool = False) -> str: + min_confidence: float | None = None, + sort_by_length: bool = False) -> str: """Answer many typed-question requests in one batched call.""" router = _router_or_error() # batch_size=0 means "unset": MCP clients send defaults eagerly, and # None is what Router.predict_batch takes as "no forward-pass cap". + # hooks_timeout follows the same rule, and only the unset value is collapsed: + # `or None` folds an eager 0 into "unset" while leaving anything else alone. + # min_confidence is unset as None, like every sibling tool, so a value out of + # range reaches the tools layer, which owns validation and answers with a + # ToolError naming it. Collapsing those here instead turned an invalid deadline + # into "no deadline" and an invalid gate into "no gate": the caller got a clean + # response and no way to tell the control it asked for had been dropped. return _wrap( laya_predict_batch, requests=requests, batch_size=batch_size or None, - hooks_timeout=hooks_timeout if hooks_timeout > 0 else None, - min_confidence=min_confidence if min_confidence >= 0 else None, + hooks_timeout=hooks_timeout or None, + min_confidence=min_confidence, sort_by_length=sort_by_length, router=router, ) diff --git a/tests/test_mcp.py b/tests/test_mcp.py index a0611990e..cc1c6f11b 100644 --- a/tests/test_mcp.py +++ b/tests/test_mcp.py @@ -1133,6 +1133,71 @@ def test_batch_predict(): "internal_error") +def test_batch_tool_control_forwarding(): + """A control the tool cannot honour must reach the layer that rejects it, not vanish. + + `laya_predict_batch` owns validation and answers an out-of-range `hooks_timeout` or + `min_confidence` with a ToolError naming the value. Collapsing those to None before the call + turned an invalid deadline into "no deadline" and an invalid gate into "no gate": the client + got a clean response and no way to tell the control it asked for had been dropped (#707). + """ + import laya.mcp.server as server_mod + + seen = {} + + def fake_wrap(fn, **kwargs): + seen["kwargs"] = kwargs + return "{}" + + real_router_or_error, real_wrap = server_mod._router_or_error, server_mod._wrap + + def fake_router_or_error(): + return BatchRouter() + + server_mod._router_or_error = fake_router_or_error + server_mod._wrap = fake_wrap + try: + # Unset stays unset: 0 is what an eager MCP client sends for a default, and the + # declaration defaults are unset too. + server_mod.laya_predict_batch_tool(requests=BATCH_REQUESTS, hooks_timeout=0) + ok("batch_tool/hooks_timeout_zero_is_unset", seen["kwargs"]["hooks_timeout"] is None, + repr(seen["kwargs"]["hooks_timeout"])) + server_mod.laya_predict_batch_tool(requests=BATCH_REQUESTS) + ok("batch_tool/hooks_timeout_default_is_unset", seen["kwargs"]["hooks_timeout"] is None, + repr(seen["kwargs"]["hooks_timeout"])) + ok("batch_tool/min_confidence_default_is_unset", seen["kwargs"]["min_confidence"] is None, + repr(seen["kwargs"]["min_confidence"])) + + # A value in range is forwarded as given. + server_mod.laya_predict_batch_tool(requests=BATCH_REQUESTS, hooks_timeout=1.5, + min_confidence=0.75) + ok("batch_tool/hooks_timeout_forwarded", seen["kwargs"]["hooks_timeout"] == 1.5, + repr(seen["kwargs"]["hooks_timeout"])) + ok("batch_tool/min_confidence_forwarded", seen["kwargs"]["min_confidence"] == 0.75, + repr(seen["kwargs"]["min_confidence"])) + + # Out of range reaches the tools layer untouched instead of being folded into "unset". + for bad in (-1.0, -0.5): + server_mod.laya_predict_batch_tool(requests=BATCH_REQUESTS, hooks_timeout=bad) + ok("batch_tool/hooks_timeout_%r_reaches_tools" % (bad,), + seen["kwargs"]["hooks_timeout"] == bad, repr(seen["kwargs"]["hooks_timeout"])) + server_mod.laya_predict_batch_tool(requests=BATCH_REQUESTS, min_confidence=bad) + ok("batch_tool/min_confidence_%r_reaches_tools" % (bad,), + seen["kwargs"]["min_confidence"] == bad, repr(seen["kwargs"]["min_confidence"])) + finally: + server_mod._router_or_error, server_mod._wrap = real_router_or_error, real_wrap + + # And the tools layer does refuse them, which is what makes the forwarding observable. + expect_tool_error("batch_tool/hooks_timeout_rejected_downstream", + lambda: laya_predict_batch(BATCH_REQUESTS, hooks_timeout=-0.5, + router=BatchRouter()), + "invalid_hooks_timeout") + expect_tool_error("batch_tool/min_confidence_rejected_downstream", + lambda: laya_predict_batch(BATCH_REQUESTS, min_confidence=-0.5, + router=BatchRouter()), + "invalid_min_confidence") + + def test_batch_route(): expect_tool_error("batch/route_no_router", lambda: laya_route_batch(BATCH_REQUESTS, router=None), "models_not_ready") @@ -2765,6 +2830,7 @@ def row_cell(tool): test_shortlist() test_batch_validation() test_batch_predict() +test_batch_tool_control_forwarding() test_batch_route() test_decide() test_shortlist_lang_parity() From 32d262bd3dc4c7d01d50bc713b68447328a577c7 Mon Sep 17 00:00:00 2001 From: ahmedandaloes Date: Sun, 11 Oct 2026 20:00:13 +0300 Subject: [PATCH 2/2] test(mcp): pin the batch tool's controls in the API contract suite; keep booleans out of "unset" Review follow-up. `or None` also collapsed `False`, because `False == 0`, so a boolean still disappeared instead of reaching `_validate_hooks_timeout`, whose contract is to refuse booleans. Fold only a numeric 0 now, and cover both booleans in the forwarding and rejection checks. `hooks_timeout` is annotated `float | None` because None is the unset marker it folds to. Per AGENTS.md the public API contract suite has to move with a signature change, so tests/test_hooks_api.py pins `laya_predict_batch_tool`'s controls and their defaults (batch_size 0, hooks_timeout 0, min_confidence None, sort_by_length False). The import is guarded: the mcp extra is optional and a core install must still run this suite. Verified: tests/test_mcp.py 971 passed / 0 failed; tests/test_hooks_api.py 551 passed / 0 failed; tests/test_mcp_remote.py 53 passed; ruff and python -m compileall -q laya/ tests/ both clean. --- laya/mcp/server.py | 23 +++++++++++++---------- tests/test_hooks_api.py | 14 ++++++++++++++ tests/test_mcp.py | 11 +++++++++++ 3 files changed, 38 insertions(+), 10 deletions(-) diff --git a/laya/mcp/server.py b/laya/mcp/server.py index d837d4d88..d4f608d52 100644 --- a/laya/mcp/server.py +++ b/laya/mcp/server.py @@ -333,25 +333,28 @@ def laya_predict_tool( + _GUARDRAILS ), ) -def laya_predict_batch_tool(requests: list, batch_size: int = 0, hooks_timeout: float = 0, +def laya_predict_batch_tool(requests: list, batch_size: int = 0, hooks_timeout: float | None = 0, min_confidence: float | None = None, sort_by_length: bool = False) -> str: """Answer many typed-question requests in one batched call.""" router = _router_or_error() # batch_size=0 means "unset": MCP clients send defaults eagerly, and - # None is what Router.predict_batch takes as "no forward-pass cap". - # hooks_timeout follows the same rule, and only the unset value is collapsed: - # `or None` folds an eager 0 into "unset" while leaving anything else alone. - # min_confidence is unset as None, like every sibling tool, so a value out of - # range reaches the tools layer, which owns validation and answers with a - # ToolError naming it. Collapsing those here instead turned an invalid deadline - # into "no deadline" and an invalid gate into "no gate": the caller got a clean - # response and no way to tell the control it asked for had been dropped. + # None is what Router.predict_batch takes as "no forward-pass cap". hooks_timeout reads + # a numeric 0 the same way -- and deliberately only a numeric 0, because `False == 0` + # would otherwise fold a boolean, which `_validate_hooks_timeout` refuses by contract + # ("would silently turn True into a one-second deadline"). + # Everything else -- negatives and booleans included -- goes to the tools layer, which + # owns validation and answers with a ToolError naming the value. Collapsing those here + # instead turned an invalid deadline into "no deadline" and an invalid gate into + # "no gate": the caller got a clean response and no way to tell the control it asked + # for had been dropped. + if not isinstance(hooks_timeout, bool) and hooks_timeout == 0: + hooks_timeout = None return _wrap( laya_predict_batch, requests=requests, batch_size=batch_size or None, - hooks_timeout=hooks_timeout or None, + hooks_timeout=hooks_timeout, min_confidence=min_confidence, sort_by_length=sort_by_length, router=router, diff --git a/tests/test_hooks_api.py b/tests/test_hooks_api.py index 9b4b0c6d0..a582c2f91 100644 --- a/tests/test_hooks_api.py +++ b/tests/test_hooks_api.py @@ -91,6 +91,20 @@ def check_param(name, fn, param, default, kind=None): check_true("Router/resolve", callable(getattr(Router, "resolve", None))) check_param("Router.unregister", Router.unregister, "name", inspect.Parameter.empty) +# The MCP batch tool's controls are public too, so they are pinned here rather than only in the +# forwarding test: `min_confidence` is unset as None (not the old -1) so a value out of range +# reaches the tools layer's validator instead of being folded into "unset" (#707), and +# `hooks_timeout` keeps 0 as the unset marker an eager MCP client sends for the default. +try: + from laya.mcp.server import laya_predict_batch_tool as _mcp_batch_tool +except ImportError: # the mcp extra is optional; the core install skips this + _mcp_batch_tool = None +if _mcp_batch_tool is not None: + check_param("laya_predict_batch_tool", _mcp_batch_tool, "batch_size", 0) + check_param("laya_predict_batch_tool", _mcp_batch_tool, "hooks_timeout", 0) + check_param("laya_predict_batch_tool", _mcp_batch_tool, "min_confidence", None) + check_param("laya_predict_batch_tool", _mcp_batch_tool, "sort_by_length", False) + # What the constructor does NOT raise over is part of its contract too: a shared # `agent_kwargs["expected_sha256"]` overlapping a per-checkpoint `sha256_digests` entry on one file # name is the ordinary shape, not a contradiction -- `model.safetensors` is the one name every diff --git a/tests/test_mcp.py b/tests/test_mcp.py index cc1c6f11b..9c675bf5e 100644 --- a/tests/test_mcp.py +++ b/tests/test_mcp.py @@ -1176,6 +1176,13 @@ def fake_router_or_error(): ok("batch_tool/min_confidence_forwarded", seen["kwargs"]["min_confidence"] == 0.75, repr(seen["kwargs"]["min_confidence"])) + # A bool is never an unset marker: `False == 0` in Python, so a plain equality or + # `or None` fold would hide the tools layer's explicit refusal of booleans. + for bad in (True, False): + server_mod.laya_predict_batch_tool(requests=BATCH_REQUESTS, hooks_timeout=bad) + ok("batch_tool/hooks_timeout_%r_reaches_tools" % (bad,), + seen["kwargs"]["hooks_timeout"] is bad, repr(seen["kwargs"]["hooks_timeout"])) + # Out of range reaches the tools layer untouched instead of being folded into "unset". for bad in (-1.0, -0.5): server_mod.laya_predict_batch_tool(requests=BATCH_REQUESTS, hooks_timeout=bad) @@ -1192,6 +1199,10 @@ def fake_router_or_error(): lambda: laya_predict_batch(BATCH_REQUESTS, hooks_timeout=-0.5, router=BatchRouter()), "invalid_hooks_timeout") + expect_tool_error("batch_tool/hooks_timeout_false_rejected_downstream", + lambda: laya_predict_batch(BATCH_REQUESTS, hooks_timeout=False, + router=BatchRouter()), + "invalid_hooks_timeout") expect_tool_error("batch_tool/min_confidence_rejected_downstream", lambda: laya_predict_batch(BATCH_REQUESTS, min_confidence=-0.5, router=BatchRouter()),