Skip to content

clabe.agents

Agents and clabe runs: an MCP server (clabe mcp, needs the agents extra). The AG-UI projection of a run is served by the run itself (POST /api/agui, see clabe.server).

ClabeTarget

ClabeTarget(
    url: str,
    token: str | None = None,
    kind: Literal["hub", "daemon", "run"] | None = None,
)

Where the MCP server's tools act: one run (directly or through a daemon), or a hub's rigs.

Source code in src/clabe/agents/_mcp.py
def __init__(self, url: str, token: str | None = None, kind: Literal["hub", "daemon", "run"] | None = None) -> None:
    self.url = url.rstrip("/")
    self.token = token
    self.kind = kind or self._detect()

rigs

rigs() -> list[dict[str, Any]]

The hub's rigs (a single entry for a run or a daemon).

Source code in src/clabe/agents/_mcp.py
def rigs(self) -> list[dict[str, Any]]:
    """The hub's rigs (a single entry for a run or a daemon)."""
    if self.kind != "hub":
        return [{"id": None, "url": self.url, "kind": self.kind}]
    response = self._get("/api/hub/rigs")
    response.raise_for_status()
    return response.json()

run

run(rig: str | None = None) -> RunClient

A client of the run (of rig, through a hub).

Source code in src/clabe/agents/_mcp.py
def run(self, rig: str | None = None) -> RunClient:
    """A client of the run (of ``rig``, through a hub)."""
    if self.kind == "hub":
        if not rig:
            raise ValueError("This is a hub: pass `rig` (see list_rigs).")
        return RunClient(f"{self.url}/rigs/{rig}/run", token=self.token, name="mcp")
    base = f"{self.url}/run" if self.kind == "daemon" else self.url
    return RunClient(base, token=self.token, name="mcp")

create_mcp_server

create_mcp_server(target: ClabeTarget)

Builds the MCP server acting on target.

Source code in src/clabe/agents/_mcp.py
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