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): laya_preset answered about a field the state did not have

Aashishbbbefe096282
diff --git a/laya/mcp/server.py b/laya/mcp/server.py
index 1d98bc77..0a0ddade 100644
--- a/laya/mcp/server.py
+++ b/laya/mcp/server.py
@@ -39,6 +39,7 @@ from laya.serve import _apply_thread_limit, _env_bool
from .device import env_device
from .tools import (
ToolError,
+ get_available_presets,
laya_predict,
laya_preset,
laya_route,
@@ -239,16 +240,35 @@ def laya_shortlist_tool(state: dict, questions: dict, model: str = "auto", k: in
)
+_PRESET_INFO = get_available_presets()
+
+# Built from tools.get_available_presets() rather than typed out, because the names in a
+# tools/list description are what a client sends back: a hand-written list here could advertise a
+# preset the tool rejects, or miss one it accepts. The field each preset reads is the field its own
+# instructions name, so this line cannot go stale against laya/presets.py either.
+_PRESET_DOC = (
+ "Run a built-in workflow: %s. "
+ "Use when the task matches one of those presets instead of hand-writing questions. "
+ "state: each preset reads one field (%s); a state holding a single string is placed under it "
+ "for you, and a state with more keys is passed through as given, so put your text under the "
+ "field that applies. Aliases: %s. "
+ % (
+ " | ".join("'%s'" % name for name in _PRESET_INFO),
+ ", ".join("'%s' reads `%s`" % (name, info["state_field"])
+ for name, info in _PRESET_INFO.items() if info.get("state_field")),
+ ", ".join("'%s' is '%s'" % (alias, name)
+ for name, info in _PRESET_INFO.items()
+ for alias in info.get("aliases", [])),
+ )
+) + _GUARDRAILS
+
+
@server.tool(
name="laya_preset",
- description=(
- "Run a built-in workflow: 'guard' | 'moderation' | 'triage' | 'model_router'. "
- "Use when the task matches one of those presets instead of hand-writing questions. "
- + _GUARDRAILS
- ),
+ description=_PRESET_DOC,
)
def laya_preset_tool(preset: str, state: dict) -> str:
- """Run a built-in workflow: 'guard' | 'moderation' | 'triage' | 'model_router'."""
+ """Run a built-in workflow preset; the tool description carries the names and state fields."""
router = _router_or_error()
return _wrap(
laya_preset,
diff --git a/laya/mcp/tools.py b/laya/mcp/tools.py
index a540afef..e320c802 100644
--- a/laya/mcp/tools.py
+++ b/laya/mcp/tools.py
@@ -5,8 +5,10 @@ from __future__ import annotations
import time
from typing import Any, Callable, Protocol, Sequence
+from ..presets import state_field
from .device import agent_device, device_report, router_agent
+
class ToolError(Exception):
"""Raised for user-facing tool failures. Message is safe to return to the LLM."""
@@ -25,8 +27,46 @@ PRESETS: dict[str, str] = {
"moderation": "moderation_questions",
"triage": "triage_questions",
"model_router": "router_questions",
+ "email": "email_questions",
}
+# `router` is what the CLI calls this preset, and it is the word a caller reaches for first;
+# `model_router` stayed because clients already have it in their prompts. Both name one preset,
+# and the canonical key is what comes back in the result.
+PRESET_ALIASES: dict[str, str] = {"router": "model_router"}
+
+
+def get_available_presets() -> dict[str, dict[str, Any]]:
+ """The built-in presets a ``laya_preset`` call can name, with what each one reads.
+
+ The state field comes from the questions themselves rather than from a table beside them, so
+ this cannot drift from the presets: a caller that has to hand-build a state (and the MCP
+ ``tools/list`` description, which is built from here) sees the field each preset actually asks
+ about. Builders are resolved through ``laya`` at call time, the way ``laya.mcp.server`` resolves
+ them, so a preset added in a newer version shows up here as soon as it is importable.
+ """
+ import laya
+
+ aliases: dict[str, list[str]] = {}
+ for alias, target in PRESET_ALIASES.items():
+ aliases.setdefault(target, []).append(alias)
+
+ out: dict[str, dict[str, Any]] = {}
+ for name, attr in sorted(PRESETS.items()):
+ entry: dict[str, Any] = {"questions": attr}
+ if name in aliases:
+ entry["aliases"] = sorted(aliases[name])
+ builder = getattr(laya, attr, None)
+ if builder is not None:
+ questions = builder()
+ entry["n_questions"] = len(questions)
+ field = state_field(questions)
+ if field is not None:
+ entry["state_field"] = field
+ out[name] = entry
+ return out
+
+
VALID_TYPES = {"choice", "score", "noul"}
# `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
@@ -97,12 +137,21 @@ def validate_state(state: Any) -> dict:
def validate_preset(preset: Any) -> str:
- if preset not in PRESETS:
+ """Canonical :data:`PRESETS` key for a preset argument.
+
+ The one spelling a caller reaches for that is not a table key is ``router`` -- what the CLI
+ calls ``model_router``. Aliases resolve here so both names work, and the canonical key is what
+ comes back, so the preset a caller reads is not the one it happened to type.
+ """
+ name = PRESET_ALIASES.get(preset, preset) if isinstance(preset, str) else preset
+ if not isinstance(name, str) or name not in PRESETS:
+ # The arguments arrive from a model, so a JSON list or object is a real possibility and
+ # belongs in the same invalid_preset as an unknown name.
raise ToolError(
"invalid_preset",
f"preset must be one of {sorted(PRESETS)}, got {preset!r}",
)
- return preset
+ return name
def validate_model(model: Any) -> str:
@@ -338,12 +387,24 @@ def laya_preset(
agent: Any = None,
preset_builder: Callable[[str], dict] | None = None,
) -> dict:
- """Run a built-in workflow preset (guard / moderation / triage / model_router)."""
+ """Run a built-in workflow preset (guard / moderation / triage / model_router / email).
+
+ A preset's questions read one named field of the state -- ``guard`` asks about `` `prompt` ``,
+ ``triage`` about `` `message` `` -- and a caller that hands over its text under any other key is
+ asked to trust an answer about a field that is not there. So a state that is one string gets
+ placed under the field the questions actually name; anything richer than that is the caller's
+ shape and is passed through untouched.
+ """
preset_name = validate_preset(preset)
state_d = validate_state(state)
if preset_builder is None:
raise ToolError("internal_error", "preset_builder is not configured")
questions = preset_builder(PRESETS[preset_name])
+ field = state_field(questions)
+ if field is not None and field not in state_d and len(state_d) == 1:
+ (key, value), = state_d.items()
+ if isinstance(value, str):
+ state_d = {field: value}
return laya_predict(
state_d,
questions,
diff --git a/tests/test_mcp.py b/tests/test_mcp.py
index e1f5eca1..a674ed53 100644
--- a/tests/test_mcp.py
+++ b/tests/test_mcp.py
@@ -28,7 +28,10 @@ except ImportError:
from laya.mcp.device import agent_device, device_report, env_device, resolve_device, router_agent # noqa: E402
from laya.mcp.server import _models_from_env, server as mcp_server # noqa: E402
from laya.mcp.tools import ( # noqa: E402
+ PRESETS,
+ PRESET_ALIASES,
ToolError,
+ get_available_presets,
laya_predict,
laya_preset,
laya_route,
@@ -183,6 +186,95 @@ def test_model_names():
ok("model/error_keeps_auto", "'auto'" in message, repr(message))
+def test_presets():
+ """The preset list, and the state field each preset reads, come from core.
+
+ Two things used to be able to drift here: the table of names (which was missing `email`, so a
+ caller had no way to ask for the preset the CLI has), and which field of the state a preset's
+ questions read. A preset says that out loud -- "What does the customer want in `message`?" -- so
+ the field is read back out of the questions instead of kept beside them, and `laya_preset` puts a
+ caller's lone string under it. Nothing else about the state is touched: more than one key is the
+ caller's shape, and guessing there would be a worse failure than the honest one.
+ """
+ import laya
+ from laya.presets import state_field
+
+ def build(attr):
+ return getattr(laya, attr)()
+
+ class StateRouter(FakeRouter):
+ def predict(self, state, questions, **kwargs):
+ self.state = state
+ self.questions = questions
+ return super().predict(state, questions, **kwargs)
+
+ # The names are a list only in one place, and every entry has to resolve in core.
+ fields = {}
+ for name, attr in sorted(PRESETS.items()):
+ questions = build(attr)
+ field = state_field(questions)
+ fields[name] = field
+ ok("preset/questions_%s" % name, isinstance(questions, dict) and bool(questions))
+ ok("preset/names_one_field_%s" % name, isinstance(field, str), repr(field))
+ ok("preset/field_is_asked_for_%s" % name,
+ any(field in q["instructions"] for q in questions.values()), repr(field))
+
+ # `email` was the whole missing half of the table; the CLI has had it all along.
+ ok("preset/email_exposed", "email" in PRESETS, repr(sorted(PRESETS)))
+
+ for name, field in sorted(fields.items()):
+ router = StateRouter()
+ laya_preset(name, {"text": "the request"}, router=router, preset_builder=build)
+ ok("preset/lone_string_placed_%s" % name, router.state == {field: "the request"},
+ repr(router.state))
+ laya_preset(name, {field: "the request"}, router=router, preset_builder=build)
+ ok("preset/right_key_untouched_%s" % name, router.state == {field: "the request"},
+ repr(router.state))
+ laya_preset(name, {"text": "the request", "lang": "en"}, router=router, preset_builder=build)
+ ok("preset/multi_key_untouched_%s" % name,
+ router.state == {"text": "the request", "lang": "en"}, repr(router.state))
+ laya_preset(name, {"payload": {"nested": 1}}, router=router, preset_builder=build)
+ ok("preset/lone_non_string_untouched_%s" % name,
+ router.state == {"payload": {"nested": 1}}, repr(router.state))
+ # the questions that get answered are the preset's own, not a hand-copied set
+ ok("preset/questions_forwarded_%s" % name, router.questions == build(PRESETS[name]))
+
+ for name in sorted(PRESETS):
+ ok("preset/canonical_%s" % name, validate_preset(name) == name)
+ for alias, canonical in sorted(PRESET_ALIASES.items()):
+ ok("preset/alias_%s" % alias, validate_preset(alias) == canonical)
+ # an alias has to reach the same preset through the tool, not just the validator
+ router = StateRouter()
+ laya_preset(alias, {"text": "the request"}, router=router, preset_builder=build)
+ ok("preset/alias_through_tool_%s" % alias,
+ router.questions == build(PRESETS[canonical]), repr(router.questions))
+ ok("preset/aliases_are_not_names", not (set(PRESET_ALIASES) & set(PRESETS)))
+
+ for bad in ("nope", "", " ", "Email", "model router", "guard_questions", 5, [], {}, None):
+ expect_tool_error("preset/rejected_%r" % (bad,), lambda b=bad: validate_preset(b),
+ "invalid_preset")
+
+ # What a client can discover without calling: the table, and the tools/list description built
+ # from it. Both are derived, so neither can advertise a name the tool rejects.
+ info = get_available_presets()
+ ok("preset/table_covered_by_info", sorted(info) == sorted(PRESETS), repr(sorted(info)))
+ for name, entry in sorted(info.items()):
+ ok("preset/info_builder_%s" % name, entry["questions"] == PRESETS[name], repr(entry))
+ ok("preset/info_field_%s" % name, entry.get("state_field") == fields[name], repr(entry))
+ ok("preset/info_lists_aliases", info["model_router"].get("aliases") == ["router"],
+ repr(info["model_router"]))
+ ok("preset/info_alias_names_are_not_entries", "router" not in info, repr(sorted(info)))
+
+ description = next(t.description for t in asyncio.run(mcp_server.list_tools())
+ if t.name == "laya_preset")
+ for name in sorted(PRESETS):
+ ok("preset/desc_names_%s" % name, "'%s'" % name in description, description)
+ for name, field in sorted(fields.items()):
+ ok("preset/desc_field_%s" % name, "'%s' reads `%s`" % (name, field) in description,
+ description)
+ ok("preset/desc_names_the_alias", "'router' is 'model_router'" in description, description)
+
+
def test_model_forwarding():
"""An alias reaches core canonical, and changes nothing else about the answer."""
@@ -622,6 +714,7 @@ test_real_device()
test_private_contract()
test_schema()
test_model_names()
+test_presets()
test_model_forwarding()
test_shape()
test_question_forwarding()