Build

Write a media plugin

Edit on GitHub

A media plugin adds an image, audio, video, 3D, decision or diarization backend. The host runs the job queue, progress, previews and the HTTP endpoints; you supply the claim, the bills and the job itself. Contract: src/abi/media.zig. Overview: plugins.md. The model-plugin walkthrough (plugin-model.md) covers the kit, install and test flow; this page covers what differs.

mlx-serve plugin new tone --media --kit <kit dir> --dir ~/work

The scaffold is a sine-tone generator: one gain weight, a WAV out. Replace the tone with your model.

The table

abi.MediaTable in plugin.zig:

Field What it does
claims kit.sdk.mediaClaimsByType(&model_types), or by marker for a configless pack (model_index.json, pipeline.json)
required_file A pack with this file missing is invisible until it lands (incomplete downloads)
modality image, audio, video, mesh, decision or diarization; decides the endpoint and the app flow
caps What this pack supports, read from the pack with no MLX (img2img, edit, CFG, LoRA, audio routes)
bill_load, bill_request Bytes the load and each request will allocate, as upper bounds you measured
load / unload Build and free the engine; return a Verdict that refuses by name
step Run one job. Read req.body (the request JSON), report progress, honour the boundary
encode Hand back the finished bytes (WAV, PNG, MP4, GLB...)

Audio packs say which route they serve in caps.routes (speech, music, sound), which map to /v1/audio/speech, /v1/audio/music-generations and /v1/audio/sound-generations.

The job loop

progress.report(progress.ctx, .of("tone"), i + 1, config.chunks);   // stage, step, total
if (progress.boundary(progress.ctx)) return error.ClientLeft;       // chat runs here; true = the client left

Check and try it

cd ~/work/tone && zig build test && zig build
mlx-serve plugin check ./tone
mlx-serve plugin fixture ./tone ~/work/tone-pack      # writes the plugin's tiny pack so you can serve it
mlx-serve plugin install tone/zig-out/lib/libtone.dylib
mlx-serve --serve --port 18082 --model ~/work/tone-pack
curl -s localhost:18082/v1/audio/sound-generations -H 'Content-Type: application/json' \
  -d '{"seconds":1,"frequency":440}' -o tone.wav

Enable plugins in ~/.mlx-serve/plugins.json, same as for model plugins (see the gotchas in plugin-model.md).

In-tree backends with full SDK media blocks, previews, LoRA and ANE offload: recipes/add-a-media-backend.md.

Read these in the kit