diff --git a/laya/mcp/server.py b/laya/mcp/server.py index 41a579900..d4f608d52 100644 --- a/laya/mcp/server.py +++ b/laya/mcp/server.py @@ -333,18 +333,29 @@ def laya_predict_tool( + _GUARDRAILS ), ) -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: +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". + # 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 if hooks_timeout > 0 else None, - min_confidence=min_confidence if min_confidence >= 0 else 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 a0611990e..9c675bf5e 100644 --- a/tests/test_mcp.py +++ b/tests/test_mcp.py @@ -1133,6 +1133,82 @@ 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"])) + + # 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) + 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/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()), + "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 +2841,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()