MDK Logo

Run MDK on a private HyperDHT

The local and air-gapped profile, with private HyperDHT bootstrap nodes, an explicit bind interface and no public hyperdht.org traffic

TL;DR

  • Under mdk run, spec.network.dht starts a private bootstrap and binds every DHT node to the interface you choose; the programmatic and PM2 profiles pass the same bootstrap list to every component
  • Confirm with lsof -nP -iUDP: every UDP socket of mdk run and its app runtimes (pgrep -af 'mdk run|run-app.js') is bound to that interface (for example 127.0.0.1:31737), never * or 0.0.0.0. The port alone proves nothing; the address does. To check one process, use lsof -nP -iUDP -a -p <pid>.

Overview

This page sets up the local and air-gapped profile: every MDK process joins a HyperDHT network you run, binds its DHT node to an interface you choose, and never contacts the public node1/2/3.hyperdht.org bootstrap nodes. It follows HyperDHT's own isolated-DHT pattern: one bootstrap node plus persistent DHT nodes, and every component started with that bootstrap list. The profile is HyperDHT configuration only. There is no proxy, relay or second transport in front of HRPC.

If Kernel, Worker, Gateway or HRPC are unfamiliar, read terminology first.

Discovery and transport are separate

Discovery is how the Kernel learns a Worker's public key. Transport is how it then reaches that key. Every discovery mode, including the same-process case, uses HRPC over HyperDHT, so every mode needs a reachable bootstrap. Moving to a private network changes the bootstrap, not the discovery mode.

ShapeHow the Kernel learns the Worker keyTransportWhat the private profile changes
Single processkernel.registerWorker(rpcKey) in memoryHRPC over HyperDHTBoth DHT nodes join the private bootstrap and bind dhtHost
LocalKey files in <root>/.worker-keys on one machineHRPC over HyperDHTSame
MicroservicesA HyperDHT topic the Kernel and every Worker joinHRPC over HyperDHTSame; the topic announce and lookup also stay on the private network

A same-process Kernel and Worker still open a HyperDHT connection to each other. Without a reachable bootstrap, public or private, that connection never forms and the Worker never reaches READY. The deployment topology concept covers the three shapes in depth.

Choose a bind interface

dhtHost is the interface each DHT node binds (HyperDHT's host option). Without it a node binds every interface.

DeploymentBootstrap reached atdhtHost on every component
One machine127.0.0.1:<port>127.0.0.1
An isolated LAN<bootstrap LAN IP>:<port>That machine's address on the LAN

Every process in the stack needs the same bootstrap list. The Kernel, Workers, Gateway, app runtimes, the MCP server and CLI clients each start a DHT node, and one process left on the defaults joins the public network.

Pass the profile to each component

ComponentProgrammatic optionService environment (worker.js)
BootstrapstartDhtBootstrap({ host, port, nodes })SERVICE=dht-bootstrap, MDK_DHT_HOST, MDK_DHT_PORT, MDK_DHT_NODES
KernelgetKernel({ hrpc: { bootstrap, dhtHost } })Not run by worker.js
Workerbootstrap, dhtHost on every Worker boot function and WorkerRuntimeMDK_DHT_BOOTSTRAP, MDK_DHT_HOST
GatewaystartGateway({ bootstrap, dhtHost })MDK_DHT_BOOTSTRAP, MDK_DHT_HOST
App runtimecreateAppRuntime({ bootstrap, dhtHost, kernelBootstrap, kernelDhtHost })Set by mdk run
MCP server and pluginsconfig.kernelBootstrap, config.kernelDhtHostSet by mdk run
ClientcreateMdkClient({ kernelBootstrap, kernelDhtHost }), or hrpc: { bootstrap, dhtHost }Not applicable

startGateway({ bootstrap, dhtHost }) covers all three DHT nodes the Gateway starts: its net facility (the key the Kernel and Workers admit), its Kernel client and its app runtime client. A bootstrap list that names a public *.hyperdht.org node is refused: startServices(), dhtFromProject() and dhtFromEnv() (which reads MDK_DHT_BOOTSTRAP) throw ERR_MDK_DHT_BOOTSTRAP_PUBLIC, and mdk run stops with an error naming the spec.network.dht.bootstrap entry. A list passed straight to any other programmatic option, such as getKernel(), startGateway(), a Worker boot function, WorkerRuntime, createAppRuntime() or createMdkClient(), is not checked, so pass only your own bootstrap nodes there.

Run the profile

Each subsection is an alternative; pick the one that matches how the stack runs.

A. Programmatic, one machine

Start the bootstrap first and hand its list to every component. Set AVALON_PASSWORD to the miner's password before you run it: export it, or keep it in .env and start the script with node --env-file=.env:

const path = require('path')
const { startDhtBootstrap, getKernel, startGateway, onShutdown, shutdown } = require('@tetherto/mdk-core')
const { WorkerRuntimeV2 } = require('@tetherto/mdk-worker')

const main = async () => {
  const storeDir = '/var/lib/mdk/avalon-1'
  const net = await startDhtBootstrap({ host: '127.0.0.1', port: 31737 })
  const dht = { bootstrap: net.bootstrap, dhtHost: '127.0.0.1' }

  const kernel = await getKernel({ hrpc: dht, discovery: { mode: 'local' } })
  const runtime = new WorkerRuntimeV2(path.dirname(require.resolve('@tetherto/mdk-worker-avalon/package.json')), {
    workerId: 'avalon-1',
    storeDir,
    allowedKeys: [kernel.getClientPublicKey()],
    devices: [{ deviceId: 'AV-001', config: { address: '192.168.1.30', port: 4028, password: process.env.AVALON_PASSWORD } }],
    ...dht
  })
  await runtime.start()
  await kernel.registerWorker(runtime.getPublicKey())
  kernel._cleanup.push(() => runtime.stop())
  await startGateway({ kernel, ...dht })

  onShutdown(async () => {
    await shutdown(kernel) // the Gateway, then the Worker, then the Kernel
    await net.close()
  })
}

main().catch((err) => { console.error(err); process.exit(1) })

Ctrl+C stops every component, then closes the bootstrap with net.close(), so the bootstrap outlives every DHT node that joined it. The signal handler force-exits after three seconds: a Worker still waiting on an unreachable device can hold its stop() past that, and the process then exits before net.close() runs.

B. mdk run

Run from your project folder (holding mdk.yaml). Runtime: mdk run.

Projects created with mdk onboard already have spec.network.dht. Add it to an older mdk.yaml:

spec:
  network:
    dht:
      host: 127.0.0.1   # bind interface, and where the generated bootstrap is reached (default 127.0.0.1)
      port: 31737       # UDP port of the generated bootstrap (default 31737)

The process that runs the Kernel (mdk run or mdk run kernel) also runs the bootstrap and stops it after the Kernel. Every other target (gateway, mcp, worker <name>, app <name>) and every CLI command that calls the Kernel joins host:port. Leave host unset and it defaults to 127.0.0.1, so every component stays on loopback. To join a bootstrap you already run elsewhere, list it and no bootstrap is started; then set host to this machine's LAN address, or leave it unset to bind every interface. A loopback host with a non-loopback bootstrap is refused, because a socket bound to loopback cannot reach it:

spec:
  network:
    dht:
      host: 10.20.0.7
      bootstrap:
        - 10.20.0.5:31737

Without spec.network.dht the stack joins the public DHT. Where outbound UDP to it is blocked, the Kernel, apps and Gateway still start, but no Worker registers: mdk run prints ! worker <name> not registered with the Kernel (ERR_WORKER_IDENTITY_UNAVAILABLE), then a hint naming the public DHT and this setting.

A component you host yourself

A process that starts a component without reading mdk.yaml — for example, a Worker you start yourself alongside mdk run — has to be told the same settings, or it joins the public DHT while the rest of the stack is on the private one. The symptom is narrow and quiet: that one Worker's devices never appear, and every other component is healthy. The process that runs the Kernel (mdk run or mdk run kernel) reports it as warning: the Kernel could not reach the worker at <key prefix>… (ERR_WORKER_IDENTITY_UNAVAILABLE).

Rather than copying the values into a second config, read the ones the stack resolved. The process that runs the Kernel (mdk run or mdk run kernel) publishes them to <projectDir>/.mdk/network.json, and mdk-core reads that back into a programmatic Worker boot:

const { dhtFromProject } = require('@tetherto/mdk-core')

// `{}` on the public DHT, so this is safe to spread either way.
const runtime = new WorkerRuntimeV2(pkgDir, { workerId, storeDir, devices, allowedKeys, ...dhtFromProject(projectDir) })

pkgDir, workerId, storeDir, devices, and allowedKeys are your host script's own values, and projectDir is the folder holding mdk.yaml. Test a third-party Worker builds such a host. Beside mdk run, the host waits for the Kernel key in <projectDir>/.mdk/trusted-keys and publishes the Worker's key to <projectDir>/.mdk/keys.

Start the Kernel before the Worker: the file is written when the Kernel starts, and a host that reads it earlier gets {} and joins the public DHT. dhtFromProject throws on a file it cannot trust — unreadable, unparseable, not an object, a malformed or public bootstrap entry, a dhtHost that is not an IP address — rather than falling back to the public network.

Every component dials the port the bootstrap runs on: 31737, unless the stack sets another. A bootstrap list written by hand, such as a Worker booted with bootstrap: ['127.0.0.1:31737'] or a LAN machine's bootstrap list, keeps its port when the stack's port changes. Both sides then start without an error and never find each other. Read the list with dhtFromProject(projectDir) rather than writing it by hand.

C. PM2 with startServices

await startServices({ runtime: 'pm2', repoRoot, dht: {}, services })

repoRoot is your MDK checkout, and services lists the components to run; a Worker entry also needs its devices, or the call throws ERR_MDK_WORKER_DEVICES_REQUIRED. The @tetherto/mdk-core reference documents both options.

The generated ecosystem.config.js starts mdk-dht-bootstrap first, on 127.0.0.1:31737, and sets MDK_DHT_BOOTSTRAP and MDK_DHT_HOST=127.0.0.1 on every service. For an isolated LAN, pass dht: { host: '<this machine>', bootstrap: ['<lan ip>:31737'] } and no bootstrap app is generated. With a bootstrap list and no host, MDK_DHT_HOST is not set and every service binds all interfaces.

Verify the stack is private

  1. List the UDP sockets the MDK processes hold, and check each is bound to the chosen interface rather than 0.0.0.0 or *. Under mdk run, the processes are mdk run and one app runtime per app (pgrep -af 'mdk run|run-app.js'). On Linux, Node 24 shows in lsof as MainThrea, not node, so list every UDP socket, or filter by process ID:

    lsof -nP -iUDP
    lsof -nP -iUDP -a -p <pid>

    The port alone proves nothing. Only the address, 127.0.0.1 or your chosen interface rather than *, shows the stack is private.

  2. Start the process that runs the bootstrap with DEBUG=mdk:dht-bootstrap set to log the address it serves; other processes log nothing for it.

  3. Block outbound UDP to the public bootstrap addresses at the perimeter, then confirm Workers still reach READY.

The private-dht integration test in backend/tests/integration/ checks the same things for each discovery mode and for the Gateway: sockets bound to 127.0.0.1, and routing tables that hold loopback nodes only.

Next steps

On this page