def create_mcp_server(target: ClabeTarget):
"""Builds the MCP server acting on ``target``."""
from mcp.server.mcpserver import Context, MCPServer
server = MCPServer(
name="clabe",
instructions="Follow and drive clabe experiment runs. Read the run with get_run; when it has an open prompt, "
"answer it with answer_prompt (a value valid for its answer_schema) or ask_the_user. On a hub, pass `rig` "
"(see list_rigs). Prefer asking the user over guessing answers about animals or hardware.",
)
@server.tool()
def list_rigs() -> list[dict[str, Any]]:
"""The rigs this server can act on, with their status and whether they wait for an answer (hub only;
otherwise the single run or daemon)."""
return target.rigs()
@server.tool()
def get_run(rig: str | None = None) -> dict[str, Any]:
"""The run's state: workflow, status, steps, current step, and the open prompt (with its JSON answer schema)."""
return _state(target.run(rig))
@server.tool()
def answer_prompt(prompt_id: str, value: Any, rig: str | None = None) -> dict[str, Any]:
"""Answers the open prompt. The value must be valid for the prompt's answer_schema; a rejected answer
returns the reason and the prompt stays open."""
return target.run(rig).answer(prompt_id, value).model_dump()
@server.tool()
async def ask_the_user(ctx: Context, rig: str | None = None) -> dict[str, Any]:
"""Asks the MCP client's user to answer the run's open prompt (simple prompts only), then submits it."""
return await _ask_user(ctx, target.run(rig))
@server.tool()
async def wait_for_prompt(timeout_seconds: float = 60.0, rig: str | None = None) -> dict[str, Any]:
"""Waits until the run opens a prompt or ends (or the timeout passes), then returns its state."""
return await _wait_for_prompt(target.run(rig), timeout_seconds)
@server.tool()
def go_back(step: str, rig: str | None = None) -> dict[str, Any]:
"""Goes back to a step: it and the steps depending on it run again (refused for locked steps)."""
return target.run(rig).back(step).model_dump()
@server.tool()
def retry_step(step: str, rig: str | None = None) -> dict[str, Any]:
"""Retries a step that failed."""
return target.run(rig).retry(step).model_dump()
@server.tool()
def skip_step(step: str, rig: str | None = None) -> dict[str, Any]:
"""Skips an optional step that asks whether to run."""
return target.run(rig).skip(step).model_dump()
@server.tool()
def abort_run(reason: str, rig: str | None = None) -> dict[str, Any]:
"""Aborts the run. Only when the user asked for it."""
return target.run(rig).abort(reason).model_dump()
return server