MONKEYTESTERDOCS
Docs / Use

Device plugins

A device plugin is a separate HTTP service that owns one kind of hardware — boards, displays, anything else. MonkeyTester never touches the hardware itself, and a plugin that implements the contract works from any test with no change to MonkeyTester.

The contract (contract_version 1)

Full text: PLUGIN_API.md in the MonkeyTester repo. Everything a test asks of a plugin is an action run as a job:

  1. POST /jobs {device, action, params, timeout_ms} — the plugin answers at once with a job id.
  2. GET /jobs/<id>?wait=<seconds> blocks until the job finishes or wait runs out.
  3. The job ends in exactly one terminal state; the step passes only on passed. A job that fails is still HTTP 200 with state: "failed".
EndpointPurpose
GET /healthOpen; must answer with the plugin contract (monkeytester-plugin, version 1) — this gates registration.
GET /devicesOwned hardware with state, capabilities and details.
GET /actionsEvery action with a parameter schema — how the runner types key=value strings (int, float, bool, json) and validates calls.
POST /jobs, GET /jobs/<id>, POST /jobs/<id>/cancelStart, wait for, cancel a job.
GET /events?device=…&since=…Device event log (for long-polling state changes).
POST /uploadsUpload files for actions when the path can't live on the plugin's host; the action then gets upload:<id>.

Conventions: JSON/UTF-8, milliseconds unless named otherwise; a plugin started with a token (PLUGIN_TOKEN) demands X-Plugin-Token on every request except GET /health. After the security update the token is mandatory on any non-loopback bind: a plugin told to listen beyond 127.0.0.1/::1/localhost without one refuses to start; on loopback it is optional; HTTP errors carry {"error", "code"} with codes bad_request unauthorized not_found unknown_action unknown_device device_busy internal. A plugin must run on Linux and macOS; hardware paths are paths on the plugin's host.

Registering plugins in the viewer

  • Settings → Plugins → Manage device plugins: add name + address. The address is saved only when its GET /health answers with the plugin contract. Saved as the plugins map in Run/config.json.
  • The environment variable MT_PLUGINS=name=url[,name=url] is imported into that map once at viewer start and ignored after that — don't keep editing it.
  • AI is the exception: its address is set only in that row's Configure panel (saved as visual_plugin_url), never in the manage box.
  • The page lists active plugins with their live state, capabilities and connected devices; add/edit/remove from there.
  • Tokens: each plugin's token is entered in the viewer's Settings and sent as X-Plugin-Token. The field is write-only — the viewer checks the token against the plugin before saving, then shows only whether it is configured, its last four characters and when it was saved (Replace / Remove to change it). The old variables PLUGIN_TOKEN and MT_PLUGIN_TOKEN_<NAME> are imported into Settings once, when Settings has no token, and ignored after that. A missing or wrong token shows 401.
  • Plugins on the viewer's own host (the lab's ESP32 plugins) listen on 127.0.0.1 by default and need no token; one on another machine must have a token.

Using a plugin from a test

Example with a fictional plugin fisher that owns an aquarium controller:

PLUGIN: fisher factory-reset
PLUGIN: fisher set-temp celsius=24
PLUGIN: fisher expect
  GET /status -> 200 {"heater":"off","ok":true}
END
VERIFY: The dashboard shows the heater off
PLUGIN: fisher clear
  • The runner checks GET /health on a plugin's first use in a run and caches GET /actions; an unknown plugin or action fails the step with the list of known ones.
  • Actions run one job at a time per device; jobs wait for the action's own default timeout unless timeout= overrides it.
  • Failures are machine-readable — codes like unknown_device (HTTP 404 on POST /jobs), bad_request, device_busy or plugin-specific ones; see each plugin's ACTIONS.md for its codes.

Writing your own plugin

A plugin is just an HTTP server implementing the contract; the shipped plugin repos keep the generic server (jobs, long-poll waits, events, uploads) in one file and add discovery and actions in another. The contract is the whole coupling: implement it and your hardware works from any test.

  1. Answer GET /health with the contract, GET /devices with your hardware, GET /actions with actions and parameter schemas.
  2. Implement POST /jobs + GET /jobs/<id>; end every job in exactly one terminal state and pass only on real success.
  3. Follow the family's security rules: default to a loopback bind, refuse to start on a non-loopback bind without a token, compare tokens in constant time, and keep GET /health free of configuration and secrets (see Security).
  4. Ship a systemd user unit (Linux) and a launchd plist (macOS) in packaging/, and document your actions in an ACTIONS.md.
  5. Register it in the viewer and drive it from a test with PLUGIN: steps.