Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
test(mcp): pin the batch tool's controls in the API contract suite; k…
…eep 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.
  • Loading branch information
ahmedandaloes committed Oct 11, 2026
commit 32d262bd3dc4c7d01d50bc713b68447328a577c7
23 changes: 13 additions & 10 deletions laya/mcp/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right — AGENTS.md requires tests/test_hooks_api.py to move with a public API change, and I changed the advertised schema without touching it. Fixed in 32d262b.

tests/test_hooks_api.py now pins the tool's controls and their defaults:

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)

The import is guarded (except ImportError: _mcp_batch_tool = None) so a core install without the mcp extra still runs the suite, matching how tests/test_mcp.py skips.

I also ran the second gate named in AGENTS.md, which I had skipped: python -m compileall -q laya/ tests/ is clean, and tests/test_hooks_api.py reports 551 passed, 0 failed.

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,
Expand Down
14 changes: 14 additions & 0 deletions tests/test_hooks_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 11 additions & 0 deletions tests/test_mcp.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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()),
Expand Down