Skip to content

fix(mcp): send an out-of-range batch control to the validator instead of dropping it - #1118

Merged
NandhaKishorM merged 2 commits into
NandhaKishorM:mainfrom
ahmedandaloes:fix/mcp-batch-controls-reach-validator
Oct 12, 2026
Merged

NandhaKishorM merged 2 commits into
NandhaKishorM:mainfrom
ahmedandaloes:fix/mcp-batch-controls-reach-validator

Conversation

@ahmedandaloes

@ahmedandaloes ahmedandaloes commented Oct 11, 2026 •

Copy link
Copy Markdown
Contributor

What

laya_predict_batch_tool sends an out-of-range hooks_timeout or min_confidence to the tools layer instead of collapsing it to None first.

# before: every negative became "unset"
hooks_timeout=hooks_timeout if hooks_timeout > 0 else None,
min_confidence=min_confidence if min_confidence >= 0 else None,

# after: only a numeric zero is "unset"; everything else is validated downstream
if not isinstance(hooks_timeout, bool) and hooks_timeout == 0:
    hooks_timeout = None
...
hooks_timeout=hooks_timeout,
min_confidence=min_confidence,

min_confidence also switches its declaration default from -1 to None, so -1 is no longer a second meaning for "unset".

Why

A client that sends min_confidence=-0.5 asked for a gate; it got "no gate" and an error-free response. The same for hooks_timeout=-0.5, which silently removed the hook deadline. Neither is detectable from the response.

laya_predict_batch owns validation for both and answers with a ToolError naming the value, and the above-range side already reached it — min_confidence=1.5 is refused today. Only the below-range side was swallowed, so the two halves of the same rule behaved differently.

-1 was never a valid threshold (the documented range is [0, 1]), and _validate_hooks_timeout(0) already refuses 0 — which is why 0 stays "unset" and is the one value that must still be folded. The isinstance guard is deliberate: False == 0, so an equality test alone would fold a boolean too, and that validator refuses booleans by contract.

How it was verified

tests/test_mcp.py gains test_batch_tool_control_forwarding(), which records what the server tool hands the tools layer (the router and the wrapper are stubbed, so no checkpoint is loaded):

with the fix:     971 passed, 0 failed
with it reverted: 964 passed, 4 failed
  FAIL batch_tool/hooks_timeout_-1.0_reaches_tools      None
  FAIL batch_tool/min_confidence_-1.0_reaches_tools     None
  FAIL batch_tool/hooks_timeout_-0.5_reaches_tools      None
  FAIL batch_tool/min_confidence_-0.5_reaches_tools     None

It also pins that unset stays unset (hooks_timeout=0, and both declaration defaults), that in-range values are forwarded unchanged (1.5, 0.75), that both booleans reach the validator, and that the tools layer then refuses each of them.

tests/test_hooks_api.py pins the tool's controls and their defaults, since this changes its advertised schema:

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 there is guarded, so a core install without the mcp extra still runs the suite.

ruff check laya/ --select=E9,F63,F7,F82,F401,F811 --line-length=120   # All checks passed
python -m compileall -q laya/ tests/                                  # clean
python tests/test_mcp.py                                              # 971 passed, 0 failed
python tests/test_hooks_api.py                                        # 551 passed, 0 failed
python -m pytest tests/test_mcp_remote.py -q                          # 53 passed

Docs need no change: docs/cli-mcp.md lists both controls as optional and pins no default.

Checklist

  • Focused on one change (split unrelated work into another PR)
  • Rebased on the latest main (branched from 6880462, release: 0.4.2)
  • Tests pass locally
  • Docs or examples updated when the public API changed — tests/test_hooks_api.py updated per AGENTS.md; docs pin no default, so no docs edit

Refs #707 — this is the MCP-layer item from that thread's follow-up comments, not the TypeError/ValueError contract question, which is untouched.

Known sibling with the same hole, left out to keep this focused: laya_route_batch_tool forwards hooks_timeout with the identical or None, so it also folds False. Happy to take it as a follow-up.

… 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 NandhaKishorM#707
Copilot AI balanced review requested due to automatic review settings October 11, 2026 16:55

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟡 Changes recommended

Boolean timeout handling remains silently dropped, and the required public API contract test is missing.

2 open findings
What changed in this PR

Fixes MCP batch controls so invalid negative values reach downstream validation.

Changes:

  • Uses None as the unset min_confidence default.
  • Adds forwarding and rejection regression tests.
File Description
laya/​mcp/​server.py Updates batch-control forwarding.
tests/​test_mcp.py Tests defaults, valid values, and negative values.

🧠 Review effort: Balanced


💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread laya/mcp/server.py
)
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,

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.

Comment thread laya/mcp/server.py Outdated
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,

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.

Correct, and it is the same class of bug this PR is about, so it should not ship in a fix that claims to stop swallowing invalid values. Fixed in 32d262b by folding only a numeric 0:

if not isinstance(hooks_timeout, bool) and hooks_timeout == 0:
    hooks_timeout = None

The isinstance check is the whole point: False == 0 is True, so an equality test alone folds a boolean too, and _validate_hooks_timeout refuses booleans by contract ("would silently turn True into a one-second deadline"). hooks_timeout is now annotated float | None, since None is what it folds to.

Both booleans are covered in the test, forwarding and rejection:

PASS batch_tool/hooks_timeout_True_reaches_tools  True
PASS batch_tool/hooks_timeout_False_reaches_tools  False
PASS batch_tool/hooks_timeout_false_rejected_downstream  got 'invalid_hooks_timeout' want 'invalid_hooks_timeout'

laya_route_batch_tool has the identical or None on its hooks_timeout, so it has the same hole. I left it alone to keep this PR to one change — happy to take it as a follow-up if you'd rather it land here.

…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.
@NandhaKishorM
NandhaKishorM merged commit faab2ef into NandhaKishorM:main Oct 12, 2026
@NandhaKishorM

Copy link
Copy Markdown
Owner

Merged. Measured the thing the PR is about, on the merged tree:

min_confidence=5.0   -> ToolError: min_confidence must be a float in [0.0, 1.0], got 5.0
min_confidence=-0.5  -> ToolError: min_confidence must be a float in [0.0, 1.0], got -0.5
min_confidence=1.5   -> ToolError: min_confidence must be a float in [0.0, 1.0], got 1.5
hooks_timeout=-1.0   -> ToolError: hooks_timeout must be a positive finite number or None; got -1.0
hooks_timeout=nan    -> ToolError: hooks_timeout must be a positive finite number or None; got nan

Before this, -0.5 and -1.0 were folded into None at the tool boundary, so the caller got a clean 200-shaped answer with no gate and no deadline, and no way to tell the control it asked for had been dropped. That is the worse failure of the two: an out-of-range threshold is a caller bug, but silently answering without the gate is a server one.

The isinstance(hooks_timeout, bool) guard is the detail I checked most carefully, since it looks like defensive noise. It is not: False == 0 is true in Python, so without it a client sending hooks_timeout=False would be folded to None instead of reaching _validate_hooks_timeout, which refuses a boolean by contract precisely so True does not become a one-second deadline. Keeping 0 as the unset marker for an eager MCP client while still sending False to the validator is the right split.

Pinning the two defaults in tests/test_hooks_api.py is also the right place for it. These are public tool parameters, and the previous -1 sentinel for min_confidence was only discoverable by reading the forwarding code.

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.

3 participants