An MCP server that exposes a Bluesky/BITS instrument to LLM agents by talking to its bluesky-queueserver over the 0MQ control API. bait_mcp holds no ophyd devices of its own: device reads/writes run in the queueserver's live session, and plans go through the queue. The whole server is an MCP frontend + one 0MQ client — no ophyd, no EPICS, no second device session.
- Device I/O runs bait_mcp's injected
read_device/set_devicehelpers in the RE Worker (function_execute; see How stuff works). Reads run in the background (safe during a plan); writes run in the foreground, so the RunEngine serializes them — a write during a running plan is refused by the queueserver. That is the safety interlock. - Plans are listed, enqueued, and run through the queueserver.
bait_mcp works with any BITS instrument that satisfies this contract, and it
does not require editing the instrument's startup.py. bait_mcp injects its
read_device/set_device helpers into the running RE Worker namespace via
script_upload (they read/set the live oregistry). The instrument must provide:
-
Permission to call the injected functions. The queueserver enforces
allowed_functionsregardless of how a function was defined, so add the names to theallowed_functionsof the group bait_mcp connects as (qserver.user_group, defaultprimary) inqserver/user_group_permissions.yaml:primary: allowed_functions: - "read_device" - "set_device"
(Alternatively, connect as a group whose
allowed_functionsis[null]— allow-all, e.g.root— which needs no per-name entry but grants broader privilege than required.) -
An
oregistryin the worker namespace. The injected helpers resolve devices viaoregistry[name]— the standard BITS/Guarneri registry thatstartup.pybuilds. Device names passed to the tools are its names. (Readings must be JSON-serializable; numpy-valued signals may need coercion.) -
A running queueserver with the environment open. bait_mcp does not manage the queueserver lifecycle. If the environment is closed or the RE Manager is unreachable, device I/O returns
{"ok": false, "error": ...}.
bait_mcp is a thin 0MQ client of the queueserver. Two kinds of operation:
- Plans / queue — direct RE Manager calls (
list_plans,add_plan,start_queue,run_plan, …). Clean and boring. - Device I/O — the ugly part, described below.
The queueserver has no generic "read/set this device" endpoint. It runs plans
and permitted functions that already exist in the RE Worker namespace. We want
ad-hoc reads/writes against the live devices without (a) standing up a second
ophyd session inside bait_mcp, or (b) editing the instrument's startup.py.
So bait_mcp does something a bit gross: on the first device call it
script_uploads a small string of Python (qserver_client.py:_DEVICE_IO_SCRIPT)
that defines read_device/set_device into the running worker namespace,
then invokes them with function_execute. They run in the worker — where the live
oregistry is — and hand the value back via the task result.
Per device call (_call_function):
_ensure_device_functions()— if not already done this session,script_uploadthe definitions and cache a flag (one upload per environment, not per call).function_execute(read_device|set_device, …)→ wait for the task → return value.- If it fails because the function is missing (the environment was closed/reopened,
wiping the injected functions), reset the flag, re-inject once, and retry. The
check (
_is_missing_function) matches stable tokens in the queueserver error — the function name plus a "not available" phrase — rather than one exact string, so a reworded message still self-heals.
- It
execs a code string into a live process. That's a side channel, not an API — bait_mcp carries device-I/O source as a string constant. - The queueserver still gates it.
function_executeenforcesallowed_functionsno matter how the function was defined, so the names must be permitted (or you connect as an allow-all group). Injection buys "nostartup.pyedit," not "no config at all." - It assumes
oregistryexists in the worker namespace (BITS/Guarneri builds it). - A first call while a plan is running may not inject (the worker is busy). In practice the functions inject on the first idle call and persist for the environment's life, so this is a rare edge.
The obvious clean design is to define the two functions in the instrument's
startup.py. The constraint is that startup.py must not be modified — so the
injection is the price of keeping the instrument untouched.
uv sync
# Or install into the conda env that has bluesky-queueserver-api:
conda run -n bait_mcp_dev pip install -e .
cp configs/example.yaml configs/local.yaml # then edit qserver.zmq_control_addrTwo transports. streamable-http (default) serves a long-lived HTTP endpoint; stdio speaks MCP over stdin/stdout for drop-in use in Claude Code or another harness that launches the server as a subprocess.
uv run bait-mcp --config configs/local.yaml # launcher + frontend
uv run bait-mcp-server --config configs/local.yaml # frontend only (standalone)With the defaults the MCP endpoint is http://127.0.0.1:8051/mcp.
uv run bait-mcp-server --transport stdio --config configs/local.yamlUse bait-mcp-server, not bait-mcp: the launcher manages an HTTP subprocess
and a port, which is the wrong lifecycle for stdio (the harness owns the process and
the pipes). host/port/path are ignored under stdio. You can also set
mcp.transport: stdio in the config instead of passing the flag.
{
"mcpServers": {
"bait_mcp": { "url": "http://127.0.0.1:8051/mcp", "transport": "http" }
}
}This is the shape eaa_core.tool.mcp_client.MCPTool expects.
{
"mcpServers": {
"bait_mcp": {
"command": "uv",
"args": ["run", "bait-mcp-server", "--transport", "stdio",
"--config", "configs/local.yaml"],
"cwd": "/path/to/bait_mcp"
}
}
}If bait_mcp is installed into an env already on PATH (pip install -e .), drop the
uv run wrapper: "command": "bait-mcp-server" with the same args tail.
All tools return {"ok": true, ...} on success or {"ok": false, "error": ...}
on failure (e.g. queueserver unreachable, environment closed, unknown name).
| Tool | Purpose |
|---|---|
read_device(name, timeout=None) |
Read a device's value (background; works during a plan). Returns {ok, value}. |
set_device(name, value, timeout=None) |
Set a device (foreground; refused mid-plan). Actuates immediately — no HITL. |
list_devices() |
Device names exposed to this user group. |
describe_device(name) |
The queueserver's description of one device. |
list_plans() |
Plan names exposed to this user group. |
describe_plan(name) |
A plan's parameter signature. |
load_plans(path) |
Upload a Python plan file (read on the bait_mcp host) into the worker so its plans become queueable; returns the refreshed allowed-plans list. Needs env open + idle worker; the plan name must be permitted. |
queue_status() |
RE Manager status (manager_state, running item, queue size). |
add_plan(name, args=None, kwargs=None) |
Enqueue a plan (does not start it). |
start_queue() / stop_queue() |
Start / stop queue execution. |
run_plan(name, args=None, kwargs=None) |
Execute a plan immediately. Requires the env open. Actuates — no HITL. |
Preconditions: the queueserver must be running with its environment open, and
the instrument must satisfy the contract above — permit the injected functions and
expose an oregistry. When they aren't met, tools return
{"ok": false, "error": ...} rather than hanging.
Config layering: DEFAULT_CONFIG (config.py) → YAML (--config) → CLI flags,
per process.
| Key | Meaning | Default | CLI override |
|---|---|---|---|
mcp.transport |
streamable-http or stdio (stdio for Claude Code / a harness) |
streamable-http |
--transport |
mcp.host |
HTTP bind host (loopback by default; it can write/run) | 127.0.0.1 |
--mcp-host / --host |
mcp.port |
HTTP bind port | 8051 |
--mcp-port / --port |
mcp.path |
HTTP path | /mcp |
--mcp-path / --path |
qserver.zmq_control_addr |
RE Manager 0MQ control address | tcp://localhost:60615 |
— |
qserver.timeout |
Seconds to await a task/function | 600 |
— |
qserver.user |
Identity bait_mcp presents | bait_mcp |
— |
qserver.user_group |
Permission group (must allow the functions/plans) | primary |
— |
launcher.shutdown_timeout_s |
Grace period before children are killed | 5.0 |
— |
Device I/O returns {"ok": false, "error": ...} instead of raising, so problems show
up as failed tool calls, not crashes. The common ones and how to clear them quickly:
Symptom. Right after bait_mcp starts, the first read_device/set_device call
returns {"ok": false} with a "not found in the worker namespace" error — but only
when that first call happens while a plan is running.
Cause. bait_mcp injects its read_device/set_device helpers lazily, on the
first device call, via script_upload — which needs an idle worker. If the very
first call lands mid-plan, the worker is busy and injection can't happen, so the call
fails. It is not a permissions or connectivity problem.
Fix / avoid. Warm up the injection once while the queueserver is idle, before
starting a plan — e.g. call read_device on any device once after the environment
opens. The helpers then persist for the life of the environment, so later reads work
normally during plans. If you hit the error mid-plan, just retry the call once the
queue goes idle and it will self-heal. (An environment restart wipes the helpers; the
next call re-injects them automatically.)
The queueserver must be running with its environment open — bait_mcp does not
manage its lifecycle. Open the environment, and check qserver.zmq_control_addr
matches the RE Manager's qs-config.yml.
The injected functions aren't permitted for your group. Add read_device/set_device
to the group's allowed_functions (see the instrument contract)
— injection buys "no startup.py edit," not "no config." After editing any
qserver/*.yaml, reload permissions so the change takes effect (see below).
Symptom. You edited qserver/user_group_permissions.yaml (e.g. permitted
read_device/set_device, or changed which devices/plans bait_mcp's group may use)
or qserver/existing_plans_and_devices.yaml, but list_devices/list_plans — or a
device call — behave exactly as before.
Cause. Discovery and the function gate read the RE Manager's in-memory permissions, not the files on disk. Editing a YAML changes nothing until the running queueserver reloads it. (bait_mcp itself does not need a restart — see Fix.)
Fix. Tell the running queueserver to reload, then just call the tool again:
qserver permissions reload # reload user_group_permissions.yaml (permissions only)
qserver permissions reload lists # also reload existing_plans_and_devices.yaml from disk(Run qserver where it can reach the RE Manager; if the manager isn't at the
default tcp://localhost:60615, pass -a <qserver.zmq_control_addr>.)
- Plain
permissions reloadcovers the common case — editinguser_group_permissions.yaml(allowing the injected functions, or changing which existing devices/plans a group may use). No environment restart. - Use
permissions reload listswhen you also editedexisting_plans_and_devices.yaml. Note that list is generated when the environment opens from the instrument'sstartup.py, so a brand-new device/plan added in the instrument won't appear until you close and reopen the environment (or regenerate the file) — a reload alone can't surface a device the worker hasn't imported.
No bait_mcp restart is needed: permissions reload always bumps the queueserver's
plans_allowed_uid/devices_allowed_uid, and bait_mcp's discovery cache (inside
REManagerAPI) invalidates on that UID — so the next list_devices/list_plans
(within ~0.5 s, the client's status-cache TTL) refetches the new lists automatically.
The API equivalents are RM.permissions_reload() and
RM.permissions_reload(restore_plans_devices=True).
set_device and run_plan actuate immediately. There is no human-in-the-loop
approval gate here — the consumer (e.g. EAA) must gate actuation. bait_mcp only
enforces the machine-level interlock: foreground writes serialize behind the
RunEngine, so a write during a running plan is refused by the queueserver.
That interlock is scoped to the queueserver's own RunEngine, not to the
hardware. It stops bait_mcp from colliding with the queueserver's plans and
writes; it does not see motion started outside the queueserver — a manual
caput, another EPICS client, or an operator jogging a motor. bait_mcp assumes the
queueserver is the sole controller of the hardware (the standard
bluesky-queueserver model). If that does not hold at your beamline, fix it at the
access-control layer (EPICS access security, or route all writes through the
queueserver) — not with a per-device check here.
It is tempting to make the instrument's set_device refuse when device.moving is
true, to catch external motion. Don't — it does more harm than good:
- False safety (race). "Check
moving, then set" is a time-of-check/time-of-use race: the device can start moving in the gap. It looks like an interlock but does not guarantee one. - Partial, inconsistent coverage. Only devices with a busy signal
(
EpicsMotor.moving) are covered; plainSignals, detectors, and sim devices (SynAxis) have none, so the guard is a silent no-op there. Protection that exists for some devices but not others is worse than none, because it gets trusted. - Blocks legitimate retargeting. An agent intentionally changing a moving motor's target — a valid operation — would be refused.
- False confidence. It masks the real problem (two independent controllers on one PV is unsafe regardless) and discourages the actual fix: single-controller enforcement.
The safe default is the operational model above, not a .moving check.