Skip to content
Operate

Custom adapters

Load your own adapters, built with the adapter SDK, into the engine at startup from a derived image, without rebuilding the engine.

On this page

An adapter you build with the public adapter SDK, @medhook/adapter-sdk, can run in your engine without a new engine release. You add the package to an image built on top of the release image, list it in an allowlist, and the engine loads it when it starts. It then shows up in the adapter list like a built-in one.

To write one, follow Build a custom adapter: Snowflake example. This page covers loading it.

Here is the flow.

sequenceDiagram
    participant You
    participant Registry as Your registry
    participant Engine as MedHook engine
    You->>Registry: Push a derived image (release image + your adapter)
    You->>Engine: Deploy it with the adapter dir and allowlist set
    Engine->>Engine: Load each allowlisted package, check its hash
    Engine->>Engine: Run the SDK contract checks
    Engine-->>You: Registered (audited), or refused (log line + health entry)

What the engine accepts

RuleWhat it means
ESM packageOne folder per adapter under the adapter dir, with a package.json and an ES module entry.
Built with the SDKThe adapter extends BaseAdapter from @medhook/adapter-sdk.
Self-containedBundle every dependency, zod included, into the package (for example with tsup noExternal). The engine does not lend its own node_modules.
ExportThe default export, or a named export adapter, is the adapter instance or its class.
Contract profileThe package also exports contract = { validProfile: { config: { ... } } }: a synthetic profile its configSchema accepts. Never put a real credential in it.
AllowlistedOnly packages named in MEDHOOK_ADAPTERS load. Anything else in the folder is ignored.
Pinned (optional)An entry name@sha256:<64 hex> loads only when the package's hash matches.
New type onlyA package whose adapter type is already a built-in adapter type is refused. A custom adapter can never replace a built-in one.

At startup the engine runs the same contract checks as the SDK's runAdapterContractTests() on each package. An adapter that fails them is not registered: the engine writes one log line naming the package and the failed check, and GET /api/health reports runtimeAdapters as refused (signed in, it lists each package and the reason). The rest of the engine starts normally.

Every adapter that loads is recorded in the audit log as adapter.runtime.loaded, with its package name, version and sha256.

Steps

1. Build the package

Build your adapter with the SDK and its contract tests, then bundle it as ESM. The folder you ship looks like this:

text
demo-lab-results/
├── package.json      # "type": "module", "main" or "exports" pointing at the bundle
└── dist/
    └── index.js      # export default new DemoLabResultsAdapter(); export const contract = { ... }

2. Build a derived image

Start from the engine release image you already mirrored into your registry, at the version you run, and copy each package into /opt/medhook/adapters.

dockerfile
FROM registry.example.test/medhook/medhook-engine:1.4.0

# Owned by root and read-only to the engine's user (nextjs), so the engine
# cannot change its own adapter code.
COPY ./demo-lab-results /opt/medhook/adapters/demo-lab-results

ENV MEDHOOK_ADAPTER_DIR=/opt/medhook/adapters
ENV MEDHOOK_ADAPTERS=demo-lab-results@sha256:<hash>

Push it to the same registry under its own name, for example medhook-engine-adapters:1.4.0-demo.1. Rebuild it for every engine upgrade, from the new release image.

3. Pin the hash

The sha256 is computed over the package folder: every file, as its relative path and its own sha256, in sorted order. The engine also prints it in its startup log and in the adapter.runtime.loaded audit event, so you can load the package once unpinned in a test engine and copy the value.

To compute it before you build, use the tool in the release bundle (tools/adapter-sha256.mjs, the same file as apps/engine/scripts/adapter-sha256.mjs in the repo):

bash
node tools/adapter-sha256.mjs ./demo-lab-results

Without Node, this one line in bash gives the same hash:

bash
(cd ./demo-lab-results && find . -type f | LC_ALL=C sort | while IFS= read -r f; do printf '%s\0%s\n' "${f#./}" "$(sha256sum "$f" | cut -d' ' -f1)"; done | sha256sum | cut -d' ' -f1)

A pinned package whose files change is refused at the next start, until you update the pin.

4. Deploy it

With the Helm chart, set the adapter dir and the allowlist (they render MEDHOOK_ADAPTER_DIR and MEDHOOK_ADAPTERS; set both or neither), and point the engine image at the derived one:

yaml
engine:
  image:
    repository: registry.example.test/medhook/medhook-engine-adapters
    tag: 1.4.0-demo.1
runtimeAdapters:
  dir: /opt/medhook/adapters
  allowlist:
    - demo-lab-results@sha256:<hash>

With the GKE release bundle, ENGINE_IMAGE is the supported way to deploy a derived image. Pass it to deploy.sh as <name>:<tag>, with no registry host and no digest. deploy.sh resolves it in the same registry as the release (<region>-docker.pkg.dev/<project>/<repo>), still refuses any image from outside that registry, deploys yours by digest, and checks the license against it. Leave it unset to deploy the release image.

bash
ENGINE_IMAGE=medhook-engine-adapters:1.4.0-demo.1 MEDHOOK_VERSION=1.4.0 ... ./deploy.sh

Then check GET /api/health: the runtimeAdapters entry lists what loaded and anything refused, with the reason.

Reference