Skip to content

armgit / laya

Public Imported from GitHub
0

Non-autoregressive System 1 decision engine. Typed choice, score and yes/no decisions over any text in a single forward pass, in 100+ languages, with a router that picks the right checkpoint per request.

fix(mcp): validate model against core's registry instead of a copy

Aashish956e53f368ba
diff --git a/laya/mcp/server.py b/laya/mcp/server.py
index 0618af61..1d98bc77 100644
--- a/laya/mcp/server.py
+++ b/laya/mcp/server.py
@@ -73,6 +73,16 @@ _GUARDRAILS = (
"questions without shortlisting (use the laya_shortlist tool)."
)
+# The `model` argument's contract, spelled out for clients because the tools take a free string.
+# The accepted names are deliberately not listed: that registry belongs to laya.router, it grows
+# with the checkpoints, and a copy here would be a second thing to keep current. An invalid name
+# comes back as an error that lists every accepted one.
+_MODEL_DOC = (
+ " model: 'auto' (default) lets the router pick the checkpoint; anything else names one, and "
+ "core's aliases and casing are honoured ('en', 'ML', 'laya-multilingual'). The answer's "
+ "routing.model always reports the canonical name."
+)
+
def _models_from_env() -> list[str]:
"""Preload list from LAYA_MODELS (laya.serve contract: comma list of names).
@@ -185,6 +195,7 @@ def laya_route_tool(state: dict, questions: dict) -> str:
"Returns answers with confidence, routing metadata and, when it can be read, the real "
"device of the checkpoint that answered. "
+ _GUARDRAILS
+ + _MODEL_DOC
),
)
def laya_predict_tool(state: dict, questions: dict, model: str = "auto") -> str:
@@ -209,6 +220,7 @@ def laya_predict_tool(state: dict, questions: dict, model: str = "auto") -> str:
"Returns the answers plus per-question shortlist metadata (kept labels, cosine "
"scores, k, option count). "
+ _GUARDRAILS
+ + _MODEL_DOC
),
)
def laya_shortlist_tool(state: dict, questions: dict, model: str = "auto", k: int = 20) -> str:
diff --git a/laya/mcp/tools.py b/laya/mcp/tools.py
index 810f5edb..a540afef 100644
--- a/laya/mcp/tools.py
+++ b/laya/mcp/tools.py
@@ -28,7 +28,12 @@ PRESETS: dict[str, str] = {
}
VALID_TYPES = {"choice", "score", "noul"}
-VALID_MODELS = {"auto", "english", "multilingual", "typed-decisions"}
+# `auto` is this layer's own sentinel -- "route it, do not pin a checkpoint". Every other value is a
+# checkpoint name, and the registry of those (names, aliases, casing) is core's: `laya.router` runs
+# every model argument through `normalise_name` before it loads anything, which is the same call the
+# tools below end up making through `router.predict(model=...)`. A second list here could only ever
+# be narrower than that one, so it is not repeated.
+AUTO = "auto"
def validate_questions(questions: Any) -> dict:
@@ -101,14 +106,25 @@ def validate_preset(preset: Any) -> str:
def validate_model(model: Any) -> str:
+ """Canonical checkpoint name for a tool argument, or ``"auto"``.
+
+ Core's ``normalise_name`` is what decides whether something names a checkpoint: it trims,
+ lowercases and resolves ``laya.router._ALIASES``. Running the argument through it here means
+ this layer cannot reject a name that ``router.predict(model=...)`` would have accepted a few
+ lines later, and an alias comes back canonical so the ``routing.model`` a caller reads does not
+ depend on how the checkpoint was spelled. Deferred import: nothing else in this module pulls
+ torch in, and importing this file is how an MCP client starts the server.
+ """
if model is None:
- return "auto"
- if model not in VALID_MODELS:
- raise ToolError(
- "invalid_model",
- f"model must be one of {sorted(VALID_MODELS)}, got {model!r}",
- )
- return model
+ return AUTO
+ if isinstance(model, str) and model.strip().lower() == AUTO:
+ return AUTO
+ from laya.router import normalise_name
+
+ try:
+ return normalise_name(model)
+ except ValueError as error:
+ raise ToolError("invalid_model", "%s, or %r" % (error, AUTO)) from None
def _normalize_answers(raw: Any) -> dict:
diff --git a/tests/test_mcp.py b/tests/test_mcp.py
index 4d3afab5..e1f5eca1 100644
--- a/tests/test_mcp.py
+++ b/tests/test_mcp.py
@@ -147,6 +147,67 @@ def test_schema():
expect_tool_error("schema/model_bad", lambda: validate_model("gpt4"), "invalid_model")
+def test_model_names():
+ """`model` is core's registry, not a second list kept here.
+
+ The names and aliases live in `laya.router`, and `router.predict(model=...)` runs whatever
+ arrives through `normalise_name` a few lines after `validate_model` sees it. So any name core
+ resolves has to survive this layer, and it has to come back canonical: the `routing.model` a
+ caller reads should not depend on how the checkpoint was spelled in the request.
+ """
+ from laya.router import DEFAULT_MODELS, _ALIASES, normalise_name
+
+ for name in sorted(DEFAULT_MODELS):
+ ok("model/canonical_%s" % name, validate_model(name) == name)
+ for alias, canonical in sorted(_ALIASES.items()):
+ got = validate_model(alias)
+ ok("model/alias_%s" % alias, got == canonical, "got %r want %r" % (got, canonical))
+ for spelling in ("EN", " Multilingual ", "Typed-Decisions", "AUTO"):
+ want = "auto" if spelling.strip().lower() == "auto" else normalise_name(spelling)
+ ok("model/casing_and_spacing_%r" % spelling, validate_model(spelling) == want)
+ for auto in (None, "auto", "Auto", " auto "):
+ ok("model/auto_%r" % (auto,), validate_model(auto) == "auto")
+ for bad in ("gpt4", "", " ", "english-ish", "laya-typed", 5, [], {}):
+ expect_tool_error("model/rejected_%r" % (bad,), lambda b=bad: validate_model(b),
+ "invalid_model")
+
+ # The list a client learns from is now core's words, so it must still name every option:
+ # the checkpoints, the aliases, and this layer's own `auto` sentinel.
+ try:
+ validate_model("gpt4")
+ message = ""
+ except ToolError as exc:
+ message = exc.message
+ ok("model/error_lists_checkpoints", all(n in message for n in sorted(DEFAULT_MODELS)), repr(message))
+ ok("model/error_lists_aliases", "alias" in message and "'en'" in message, repr(message))
+ ok("model/error_keeps_auto", "'auto'" in message, repr(message))
+
+
+def test_model_forwarding():
+ """An alias reaches core canonical, and changes nothing else about the answer."""
+
+ class EchoRouter:
+ _agents = {"english": FakeAgent()}
+
+ def __init__(self):
+ self.calls = []
+
+ def predict(self, state, questions, **kwargs):
+ # No `routing` key on purpose: what the tool then reports is the name it settled on,
+ # which is the piece an alias could have leaked through.
+ self.calls.append(kwargs)
+ return {"answers": {"department": {"choice": "billing", "confidence": 0.94}}}
+
+ router = EchoRouter()
+ by_alias = laya_predict(STATE, QUESTIONS, model="laya", router=router)
+ by_name = laya_predict(STATE, QUESTIONS, model="english", router=router)
+ ok("forward/model_seen_by_core", [c.get("model") for c in router.calls] == ["english", "english"],
+ repr(router.calls))
+ ok("forward/reports_canonical", by_alias["routing"]["model"] == "english", repr(by_alias["routing"]))
+ ok("forward/answers_identical", by_alias["answers"] == by_name["answers"])
+
+
+
# --- shape (mocked router, no weights) ---------------------------------------
class FakeAgent:
@@ -560,6 +621,8 @@ test_device()
test_real_device()
test_private_contract()
test_schema()
+test_model_names()
+test_model_forwarding()
test_shape()
test_question_forwarding()
test_shortlist()