MDK Logo

Run your Worker with the CLI

[⏱️ ~10 min] From a tested Worker package to a running stack with a dashboard and an agent, with no app code

TL;DR

  • Declare your Worker package with mdk add worker: --path <dir> for a checkout on disk, or a package name for npm and GitHub. Either way it seeds the device options from the package's own smoke.config.js.
  • mdk run boots the Kernel, your Worker, its mock devices, every app, and the Gateway on the policy mdk onboard wrote; the first operator is created in a second terminal

Overview

This page walks from a Worker package to the operator experience:

  • The device reporting through the Kernel
  • A page in the UI shell
  • An agent answering questions about it over MCP

It serves two personas: a Worker provider bringing the package they tested from a checkout on disk, and an operator installing a Worker published to npm or GitHub.

Everything runs from one project directory that mdk onboard writes, and the only code you add is what mdk create app scaffolds. The mdk CLI hosts the Worker plugin on WorkerRuntimeV2 for you, so the host module from the testing guide is not needed here.

Prerequisites

  • The mdk CLI installed
  • A Worker package: one you built that passes mdk validate worker --path <dir> --smoke with its bundled mock, or a published one from npm or GitHub
  • Outbound UDP only for the public DHT: a project from mdk onboard runs a private DHT on loopback, and a stack whose spec.network is removed needs outbound UDP to its bootstrap nodes

Create the project

Run onboarding from an empty directory beside your Worker checkout, so a checkout on disk resolves as ../my-miner-worker. Onboarding asks a few questions with defaults, then writes mdk.yaml, package.json, .gitignore, and a project README.md:

mkdir my-site && cd my-site
mdk onboard

Answer yes to the UI dashboard prompt so the shell lands in apps/dashboard. The next step declares your Worker by hand.

Run every later command from my-site. Runtime: mdk run. Stack state goes to my-site/.mdk, with the Kernel key in .mdk/kernel.key, and a package from npm or GitHub installs into my-site (node_modules, recorded in package.json).

Declare your Worker

You have two options.

A. From a checkout on disk

Point mdk add worker at the directory. It validates the package, references it by path under spec.workers — npm is not involved and nothing is added to package.json — and seeds the device options from the package's own smoke.config.js:

mdk add worker --path ../my-miner-worker

It writes an entry like this one:

spec:
  workers:
    - name: my-miner
      package: ../my-miner-worker        # relative to the project directory
      config:
        mock: true                       # start <package>/mock/server.js per device
        devices:
          - id: my-miner-0               # <worker name>-0
            opts: { host: 127.0.0.1, port: 18080 }   # keys from the package's smoke.config.js

The rest of this section is what each part means and what to change. To write the entry by hand instead, to add devices beyond the seed one, or to place devices, add the optional keys:

spec:
  workers:
    - name: my-miner
      package: ../my-miner-worker
      config:
        mock: true
        telemetryPollIntervalMs: 60000   # only when the contract declares no interval; null turns polling off
        metadata:                        # the label fields devices may carry
          rack: { type: string }
        devices:
          - id: my-miner-0
            opts: { host: 127.0.0.1, port: 18080 }
            metadata: { rack: rack-1 }   # aggregate reads group by metadata.rack

config.devices[].opts is whatever your plugin reads per device, and it is the shape your smoke.config.js returns as config — which is exactly why mdk add worker can seed it for you, and why it is worth keeping that file honest. To place devices (a container, a PDU socket, a rack), declare the fields in config.metadata and set them on each device's metadata; the Kernel keeps them by device id for apps to query, as its device metadata section shows. With mock: true the CLI starts one simulator per device from the package's mock/server.js and the Worker talks to it instead of hardware. A package that ships its simulator under another name sets mock to that module's path inside the package, for example mock: mock/cgi-server.js. mdk run refuses a mock module that is missing, is not a file, sits outside the package, or does not export createServer.

telemetryPollIntervalMs is what turns the Worker's telemetry collector on: every device is swept on that interval and the points are stored, which is what snapshot and aggregate reads — mdkClient.pullSnapshot() and mdkClient.pullAggregate() — answer from. The interval comes from the package's mdk-contract.json when it declares one, and this key overrides it per instance; null turns polling off. If neither is set, the Worker opens no store. Those reads then fail with ERR_TELEMETRY_STORE_DISABLED, so set it here for a package whose contract is silent. aggregateCacheTtlMs, telemetryRetention and telemetryArchive tune the same collector.

B. From npm or GitHub

Install a published package, or a repository, and let the CLI write the entry:

mdk add worker @your-org/mdk-worker-my-miner
mdk add worker github:your-org/my-miner-worker#v0.1.0 --name my-miner

mdk add worker runs npm install --ignore-scripts --save-exact, so no install script of the package runs, checks the package with the same rules as mdk validate, and only then adds it under spec.workers in mdk.yaml. A GitHub source is recorded in package.json at the commit it resolved to, github:<owner>/<repo>#<sha>, rather than at #v0.1.0. The entry's name is the package name with its mdk-worker- prefix stripped, so the npm form lands as my-miner by itself and the GitHub form gets --name to match. A package without a bundled mock is added without mock: true, so fill in config.devices[].opts with the address of your hardware.

Either form seeds config.devices[].opts from the package's own smoke.config.js, so a plugin that needs more than a host and port — a password, a unit id, an API version — is declared with those keys rather than silently missing them. A package with a bundled mock gets the example's values and runs against that mock as written; one without gets the same keys with null values, since the example's point at its mock and yours points at hardware. Reading it runs that one file of the package in a separate process, so for a third-party npm or GitHub package it happens only with --seed-opts; the CLI reference has the details and the --no-seed-opts opt-out.

With either form, a package whose contract declares no telemetryPollIntervalMs is added with config.telemetryPollIntervalMs: 60000 seeded, so the Worker polls and stores a series rather than answering ERR_TELEMETRY_STORE_DISABLED; the command says so when it writes one. Tune it to the interval the device tolerates, or set it to null to turn polling off. A contract that sets its own interval is left alone — the entry carries no key and the contract's value governs.

Either way, check the declared entry resolves and passes the package rules:

mdk validate worker my-miner

Give the stack a summary over the Worker

The UI shell ships only a static Home page; data arrives through apps. The app mdk create app scaffolds already lists every Worker with its device count on a summary route, builds a starter UI with a Home panel and an Overview page, and adds itself to mdk.yaml with the Kernel access it needs granted in spec.rbac:

mdk create app site

Start the stack

mdk onboard wrote the stack's initial policy to spec.rbac, and mdk create app granted the app its access, so run everything together:

mdk run

mdk run keeps the terminal in the foreground. In a second terminal, confirm the Worker is READY with its device count. Right after boot the health can read UNKNOWN until the first report; --wait waits for it:

mdk status --wait

mdk run worker my-miner starts only the Worker and its mocks, which suits iterating on the plugin while the rest of the stack stays up in other terminals.

To keep the stack up without a terminal, run it in the background with mdk run --detach, which needs pm2 on the PATH. mdk status --wait then waits for it to come up, and mdk stop or mdk restart manages it.

Create the first operator

The dashboard signs a user in, and users live in the Kernel's identity store, so this needs the stack from the previous step running. In a second terminal, from the project directory, create the operator and bind it to admin:

mdk user add ops@example.com --admin   # prompts for a password, twice

Creating the first operator covers what --admin writes, signing in over HTTP or with mdk login, and the failures to expect. The users and service accounts commands cover everything past that first operator, such as roles bound by group.

Open the dashboard

In another terminal, start the shell's dev server. It reads spec.apps from mdk.yaml and loads each app's built UI, so the site app's panel appears on Home and its page under /app/site/ once you sign in as the operator you created:

mdk run dashboard

Edit apps/site/ui-src/ and rebuild with npm run build:ui to show the telemetry channels your contract declares.

(Optional) Hand the stack to an agent

mdk run mcp turns each app's routes into MCP tools, so the site summary becomes a tool an agent can call. It needs the Kernel reachable, so run it after mdk run:

mdk run mcp

The server listens on spec.mcp.port, or the Gateway port plus 100 when unset. Routes that are not declared read-only stay hidden unless you pass --allow-write. The operator agent guides cover connecting a client.

Troubleshooting

Kernel refuses to start because the policy binds nobody

mdk onboard writes a policy that binds the CLI to admin, so this comes from an mdk.yaml written another way, or one whose spec.rbac lost that binding. Run mdk rbac init to bind the CLI, then mdk run. Without such a binding, the Kernel would boot with nobody able to administer it, so the CLI refuses to start it.

Worker is present but never READY

The same checks as in the testing guide apply. mdk status shows each Worker's state, health and device count, and mdk run worker <name> isolates its boot log.

snapshot or aggregate reads return nothing

A Worker only holds a series when it polls. With no telemetryPollIntervalMs in its contract and none under its spec.workers[].config, the runtime opens no telemetry store and those reads fail with ERR_TELEMETRY_STORE_DISABLED; live per-device reads keep working. Set the key on the Worker's entry in mdk.yaml and restart it. mdk add worker seeds it for a package whose contract is silent, so an entry written by hand, or one carried over from an older project, is the usual cause.

You should not have to wait for a failed read to find out: mdk run warns as each such Worker starts, and mdk validate worker <name> reports it against the entry in mdk.yaml. Both stay quiet when the config sets telemetryPollIntervalMs: null, which says polling is off on purpose.

Worker is READY and HEALTHY but every reading fails

Those two say the Worker process answers the Kernel; they say nothing about its devices, as the device health channel explains. A plugin missing one device option — a password, say — throws on every telemetry channel while the Worker still registers, reports healthy and counts its configured devices, so the first sign is usually an ERR_* inside a snapshot payload rather than anything in mdk status.

Check config.devices[].opts against the keys the package's own smoke.config.js sets. mdk run names the missing ones for you as the Worker starts, and mdk add worker seeds them for a newly added Worker, so the usual cause is an entry written by hand or carried over from an older project.

Dashboard shows no panel for the app

mdk run dashboard reads spec.apps at start, so restart it after mdk create app or mdk add app. A starter UI that failed to build at scaffold time is rebuilt with npm run build:ui inside the app directory.

Next steps

On this page