Skip to content

fix(mcp): a door collapses only a numeric 0, never a boolean control - #1257

Merged
NandhaKishorM merged 3 commits into
NandhaKishorM:mainfrom
aashish254:fix-mcp-tool-false-fold
Oct 17, 2026
Merged

NandhaKishorM merged 3 commits into
NandhaKishorM:mainfrom
aashish254:fix-mcp-tool-false-fold

Conversation

@aashish254

Copy link
Copy Markdown
Contributor

What

Two of the four MCP tool doors that take an eager-default 0 still folded their numeric default with
or None, which in Python also folds a boolean. Both now use the boolean-aware collapse the same file
already uses at laya/mcp/server.py:364:

if not isinstance(value, bool) and value == 0:
    value = None
door 4ac226d5 this branch
laya_predict_batch_tool batch_size=batch_size or None at laya/mcp/server.py:369 collapse at :369, plain forward at :374
laya_route_batch_tool hooks_timeout=hooks_timeout or None at laya/mcp/server.py:400 collapse at :404, plain forward at :409

laya/mcp/tools.py is untouched: it already validates both controls by name
(_validate_batch_size laya/mcp/tools.py:1185, called at :1234, and _validate_hooks_timeout
laya/mcp/tools.py:1193, called at :1240 in laya_predict_batch and at :1312 in laya_route_batch).
This PR only stops the doors from swallowing the value before it gets there. 101 added / 3 removed in two
files:

  • laya/mcp/server.py +12/-3 -- two collapses, both comments explaining why only a numeric 0 folds.
  • tests/test_mcp.py +89/-0 -- 17 new named arms, one new test function
    (test_route_batch_tool_hooks_timeout_forwarding, defined at :1547, registered in the runner list at
    :3250), and two float-zero arms pinned into the existing test_batch_tool_control_forwarding.

No public signature changes, no new dependency, no docs claim moves: docs/cli-mcp.md:168-169 lists
batch_size / hooks_timeout for these two tools and says nothing about 0 or false, so the
user-facing text was already correct about the parameters and silent about the fold.

Why

False == 0 in Python, so x or None answers a caller's false with "unset". The tools layer refuses
booleans by contract -- _validate_hooks_timeout raises for a bool with the comment that core's
float(True) == 1.0 "would silently turn True into a one-second deadline" -- and the door that already
carries the collapse says so in its own comment (laya/mcp/server.py:357-363): a numeric 0 means unset
because MCP clients send defaults eagerly, and deliberately only a numeric 0.

Measured at the door, with _wrap replaced by a recorder (laya-bench/probe_mcp_false_fold.py, both logs
kept; the whole before/after difference is two lines):

call from an MCP client base 4ac226d5 forwards this branch forwards what the tools layer then does
laya_predict_batch_tool(batch_size=false) None (unset) False ToolError(invalid_batch_size) batch_size must be a positive integer, got False
laya_predict_batch_tool(batch_size=true) True True ToolError(invalid_batch_size) ... got True -- already reached, unchanged
laya_predict_batch_tool(batch_size=0) / 0.0 / absent None None unset, unchanged -- the eager-default contract is kept
laya_predict_batch_tool(batch_size=16) 16 16 real cap, unchanged
laya_route_batch_tool(hooks_timeout=false) None (unset) False ToolError(invalid_hooks_timeout) hooks_timeout must be a positive number or None, got False
laya_route_batch_tool(hooks_timeout=true) True True already reached, unchanged
laya_route_batch_tool(hooks_timeout=0) / 1.5 / absent None / 1.5 / None same unchanged

So on 4ac226d5 the same JSON value spelled true shouted and spelled false was silent: a caller that
asked for a gate it was not entitled to got an error naming it, and a caller that asked for the boolean
version of the same mistake got a clean batch of answers with the control dropped. false is the shape
a client sends when it serialises a Python False or a JS false for an optional knob, so the silent arm
is the common one.

Nothing else in the repo treats these two doors differently: laya_predict_batch_tool's own
hooks_timeout already used the boolean-aware form, and the test that pins it
(tests/test_mcp.py:1444) states the rule -- "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". The two
lines this PR fixes are the same door with the fix missing, not a new convention.

Cross-port check (a Python-only guard can delete a parity contract, so every generator was grepped, not
just one):
50 hits for batch_size|hooks_timeout across laya-java/scripts, laya-go/scripts,
laya-dotnet/tools, laya-ts/scripts and laya-rust -- all of them drive predict_batch /
_infer_batch at the Router-or-agent level with literal integers, None or a boolean sort
(laya-go/scripts/gen_go_fixtures.py:845 records batch_size=0 as "zero chunk means all", and
laya-java/scripts/gen_fixtures.py:2105 enumerates (None, False), (2, False), (3, True)). Zero of them
spell either control as true/false, and zero port files reference laya.mcp at all. The numeric-zero
semantics those fixtures pin are the ones this PR keeps byte-for-byte: 0 and 0.0 still collapse to
unset, which is why the float-zero arms exist.

How it was verified

LAYA_TREE=<this tree> python -B laya-bench/ci_gate.py
#   18:21Z -> 78 invocations, all 88 suites passed, exit 0
python -B laya-bench/diff_gate_logs.py <branch log> <4ac226d5 control log>
#   18:53Z -> invocations: branch=78 control=78, identical: 76, differing: 2
python -B tests/test_mcp.py
#   18:54Z -> 1169 passed, 0 failed, "all mcp tests passed"
python -B tests/test_hooks_api.py
#   18:55Z -> 638 passed, 0 failed, "all hook API tests passed"
python -B laya-bench/mutate_mcp_door_folds.py
#   18:19Z -> 12 mutants, 12 killed, 0 survived
git show 4ac226d5:laya/mcp/server.py > laya/mcp/server.py
python -B tests/test_mcp.py        # 18:55Z -> 1167 passed, 2 failed -- the before-witness
git checkout -- laya/mcp/server.py # sha256 95da9c6b306d... restored, status clean
HF_HUB_OFFLINE=1 python -B laya-bench/probe_mcp_false_fold.py   # 18:39Z, the door table above
ruff check laya/mcp/server.py tests/test_mcp.py --select=E9,F63,F7,F82,F401,F811 --line-length=120
#   18:55Z -> All checks passed!
python -m compileall -q laya/mcp/server.py               # rc 0

Gate laya-bench/gate_mcp_false_fold_head58e4a981.log (18:21Z): 78 invocations, all 88 suites passed,
exit 0, and it prints laya_from=/Users/aashish/github contribution /GIT/laya-mcp-false-fold/laya/__init__.py --
the venv's editable install points at a different checkout, so every run above pins the tree and asserts
which laya answered before trusting a count.

Gate diff (laya-bench/gate_mcp_doordiff_head58e4a981.txt, 18:53Z) against the pristine 4ac226d5 control
(laya-bench/gate_evalrows_ctrl4ac2.log, 17:57Z, tree=/laya_from= pointing at the untouched
laya-base-4ac2 checkout, same 78 invocations, all 88 suites passed): invocations: branch=78 control=78,
identical: 76, differing: 2. Exactly two blocks move:

  • tests/test_mcp.py: 17 PASS lines added and none removed. Counting the arm names themselves rather than
    trusting the diff -- sort -u of the PASS labels gives 1135 on the branch vs 1118 on the control, and
    comm -23 gives the 17 expected names (batch_tool/batch_size_{zero_is_unset, float_zero_is_unset, default_is_unset, forwarded, True_reaches_tools, False_reaches_tools, false_rejected_downstream},
    batch_tool/hooks_timeout_float_zero_is_unset, and the 9 route_batch_tool/hooks_timeout_* counterparts)
    while comm -13 is empty. No pre-existing arm changed.
  • the 11-suite -q batch: the control log carries a second trailing ci_gate exit=0 line that the
    branch log does not -- a capture artifact, since both blocks end all 88 suites passed.

Nothing else in 78 invocations moved.

tests/test_hooks_api.py runs here because it is the suite that pins the door defaults as visible
parameters (check_param("laya_predict_batch_tool", ..., "batch_size", 0)) -- the collapse it protects is
unchanged, and it stays green: 638 passed, 0 failed.

Before-witness: laya/mcp/server.py replaced by the 4ac226d5 blob (sha256 64351cdf32f2...) while
this branch's tests run, then restored byte-verified (sha256 95da9c6b306d..., git status clean). The
18:55Z re-run is kept as laya-bench/revert_arm_mcp_head58e4a981.log:
1167 passed, 2 failed, and the two failures name exactly the two arms this PR is about --
FAIL batch_tool/batch_size_False_reaches_tools None and
FAIL route_batch_tool/hooks_timeout_False_reaches_tools None. Every other arm, including the four
numeric-zero arms, passes on base: no pre-existing test depends on the old fold, and the new arms do not
restate behaviour base already had.

Mutation ledger (laya-bench/mutate_mcp_door_folds_head58e4a981.log, 18:19Z, baseline 1169 passed /
0 failed, one file restored from the HEAD blob between mutants):

mutant killed by got
door 1 batch_size back to if not batch_size: (main's fold) batch_size_False_reaches_tools KILL 1
door 1 hooks_timeout back to if not hooks_timeout: hooks_timeout_False_reaches_tools KILL 1
door 2 hooks_timeout back to if not hooks_timeout: route hooks_timeout_False_reaches_tools KILL 1
door 1 batch_size == 0 -> is 0 batch_size_float_zero_is_unset KILL 1
door 1 hooks_timeout == 0 -> is 0 hooks_timeout_float_zero_is_unset KILL 1
door 2 hooks_timeout == 0 -> is 0 route hooks_timeout_float_zero_is_unset KILL 1
door 1 hooks_timeout drops not isinstance(..., bool) hooks_timeout_False_reaches_tools KILL 1
door 1 batch_size drops the same guard batch_size_False_reaches_tools KILL 1
door 2 drops the same guard route hooks_timeout_False_reaches_tools KILL 1
door 1 folds every bool (isinstance(...) or == 0) hooks_timeout_True_reaches_tools + False KILL 2
door 2 collapses nothing zero / float-zero / default-is-unset KILL 3
all three collapse nothing (main's doors) both doors' unset + refused arms KILL 9

12 mutants, 12 killed, 0 survivors, file restored byte-verified
(sha256 95da9c6b306d == 95da9c6b306d). The first run of this harness had a survivor and it was real: an
is 0 rewrite of the predict_batch door was invisible, because every arm then passed the interned
literal 0, for which 0 is 0 is True. Two 0.0 arms were added (batch_size and the
hooks_timeout of both doors -- a JSON client is free to send a declared number's default as 0.0, and
0.0 == 0 while 0.0 is 0 is False) and the harness re-run to the table above.

ruff on the two files: All checks passed! at 18:55Z (a repo-wide sweep prints 13 pre-existing findings
unrelated to this branch, so the claim is scoped to the touched files). compileall rc=0. Diff scanned for
credentials: 0 matches over all 101 added lines (grep -icE of the ^+ lines, rc=1 = no match).

Pre-open re-checks on the same 58e4a981 with a clean tree, at 21:13-21:18Z (laya_from= printed first and
pointing at this tree): tests/test_mcp.py again 1169 passed, 0 failed (21:18Z), tests/test_hooks_api.py
again 638 passed, 0 failed (21:18Z), ruff again All checks passed!, compileall rc=0, the secret re-scan
again 0 matches with rc=1, and git diff --numstat 4ac226d5 HEAD unchanged at 12 3 laya/mcp/server.py /
89 0 tests/test_mcp.py.

Overlap, scanned against all open PRs at 18:23-18:33Z, re-scanned at 19:35Z and again immediately before the
push at 20:54Z (gh pr list --state open --json files --limit 200: 60, then 61, then 64 -- three unrelated
PRs opened in between, none touching these three files; main still 4ac226d5 on all three passes, confirmed
against the API, and every head SHA, hunk range and tree id below reproduced on the later two): three open PRs
touch laya/mcp/server.py, laya/mcp/tools.py or tests/test_mcp.py, and all three are mine -- #1242
(fix-device-index-rewrite, 6f0a3556), #1203 (fix-hooks-api-timeout, ae132f47) and #738
(feat-mcp-predict-long, 46e32d25, already CONFLICTING against main). None of them adds this collapse:
their laya/mcp/server.py hunks are at old lines 9-16 (#1242) and 52-57 / 506-511 (#738, a new
laya_predict_long_tool), while mine are at 363-372 and 393-403, and #1203's only tests/test_mcp.py hunk
is at 1358-1365 against my 1406-1560. git merge-tree --write-tree in both orders exits 0 with the same
tree each way against #1242 (5e3ef30c2220) and #1203 (c7a50e4783d2). Against #738 the merge reports the
identical four conflict regions whether or not my branch is on the other side: main+#738 marks
tests/test_mcp.py conflicts at merged lines 2267/2289/3077/3291 and 58e4a981+#738 marks four at
2355/2377/3165/3379, shifted by exactly +88 at all four positions -- the same four regions carried down past
my additions, not a fifth created by them. The nearest, 3379, is the docs-mcp/args_row tool tuple about 16
lines above my nearest edit there, and its 58e4a981 side is byte-for-byte main's seven-name tuple, so this
branch does not touch that line at all. laya/mcp/server.py auto-merges in that pair with zero conflict
markers, and the merged file keeps both my collapses at :370 and :405 with main's two falsy folds
(batch_size=batch_size or None at :369 and hooks_timeout=hooks_timeout or None at :400) gone from it
-- the only or None strings left in that file are the unrelated api_key= argument and my own comment
naming the pattern. That includes #738's stale copy of the route-batch call site
(hooks_timeout if hooks_timeout > 0 else None), which is their base's text, unchanged by them against it,
so the merge takes main's line and then mine. So #738's conflicts are its own stale base, not an edit
collision with this PR, and this branch neither causes nor inherits one.

Checklist

  • Focused on one change (split unrelated work into another PR)
  • Rebased on the latest main
  • Tests pass locally
  • Docs or examples updated when the public API changed (no signature changed; docs/cli-mcp.md never
    described the fold, so nothing there went stale -- see What)

Fixes #

route_batch_tool declares hooks_timeout as a number, so an eager JSON client may
send the default as 0.0 rather than 0. The collapse uses == for exactly that
reason; this arm pins it and would fail against an `is 0` rewrite.
An `is 0` rewrite of either collapse is invisible while every arm passes the
interned literal 0: the floats have to be probed. 0.0 is the shape a JSON client
sends for a declared number, and JSON has no integer type for batch_size.
@NandhaKishorM
NandhaKishorM merged commit 1df37af into NandhaKishorM:main Oct 17, 2026
45 checks passed
@NandhaKishorM

Copy link
Copy Markdown
Owner

Merged in 0.4.9.

x or None is the idiom that hides a boolean, and this file had it in two places that laya_predict_batch's hooks_timeout had already been fixed for. So an explicit batch_size=false folded into "no cap" and swallowed the _validate_batch_size refusal that batch_size=true reaches, which is the asymmetry that gives it away: the same wrong type is refused in one spelling and silently accepted in the other.

Applying the same boolean-aware collapse in all three places, rather than only where a report came from, is the right scope.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants