Skip to main content
Glama
mcp-server-qidi

mcp-server-qidi

README.md
# mcp-server-qidi

MCP server for a QIDI X-Plus5 (Klipper + Moonraker) 3D printer: status, chamber
webcam, QIDI Box, G-code files and job history, plus gated control (light,
heater targets, pause/resume/cancel, emergency stop, start print).

This is an independent, unofficial project. It is not developed, endorsed or supported by QIDI Technology.

Moonraker on the Plus5 trusts every LAN client by default and has no
credentials. Unless `QIDI_MOONRAKER_API_KEY` is set, the printer's privacy
therefore rests on the network path (for example a VPN tunnel scoped to the
printer's address) and on whatever authenticates callers in front of this server.

## Tools

| Tool | Tier | |
|---|---|---|
| `qidi_server_status` | read | Printer reachable? Which tiers are on, temperature limits. |
| `qidi_printer_status` | read | State, file, progress, layer, time left, nozzle/bed/chamber temps, loaded Box slot. |
| `qidi_snapshot` | read | Chamber webcam picture (`light=true` switches the light on first; needs control). |
| `qidi_box_status` | read | QIDI Box slots: material, colour, inserted, humidity, drying. |
| `qidi_list_files` / `qidi_file_metadata` | read | G-code on the printer; slicer estimate, filament, temps. |
| `qidi_job_history` | read | Recent jobs with result, duration and filament. |
| `qidi_console` | read | Last console lines — read this when a start failed. |
| `qidi_set_light` | control | Chamber light on/off. |
| `qidi_set_temperature` | control | Nozzle/bed/chamber target, capped by `QIDI_MAX_*_TEMP`. |
| `qidi_pause_print` / `qidi_resume_print` | control | |
| `qidi_cancel_print` | control | Must name the running file, so a stale conversation cannot cancel the wrong job. |
| `qidi_emergency_stop` / `qidi_firmware_restart` | control | M112 and recovery. Restart is refused while printing. |
| `qidi_box_set_slot` | control | Vendor, material and colour of a Box slot. Material and vendor must be in QIDI's filament list on the printer (`officiall_filas_list.cfg`). |
| `qidi_box_drying` | control | Start/stop drying a Box. Temperature is capped by the filament in it (see below). |
| `qidi_box_load` / `qidi_box_unload` | control | Load a slot into the toolhead / unload it. Refused while printing or paused. |
| `qidi_upload_file` | control | Put a sliced `.gcode` on the printer (inline text, or https from an allowed host). Never replaces the file being printed; other files only with `overwrite=true`. |
| `qidi_start_print` | print | Start a file already on the printer. See the plate check below. |

There is deliberately **no raw G-code tool**: every command sent to the printer
is built from enumerated input.

### Plate check

Nobody is standing next to the printer when a print is started from an MCP client.
`qidi_start_print` refuses unless:

1. the printer is idle (`standby`, `complete`, `cancelled` or `error`),
2. a `qidi_snapshot` was taken in the last `QIDI_PLATE_CHECK_MAX_AGE_MIN` minutes
   (default 15) **and** after the previous job ended — so whoever starts a print
   has been shown the plate after the last part came off it,
3. the caller passes `plateIsEmpty: true`, and
4. the file exists on the printer.

A snapshot is good for one start.

### Box drying limit

`qidi_box_drying` refuses (does not lower) a temperature above the limit for
that box: the lowest limit among all its slots, by the material each slot is
set up with (the insert sensors are ignored, because spools are ejected before
drying and then read empty), by filament type — 55 °C for PLA, PVA and TPU, 65 °C for PETG, PET, ABS, ASA, PC, PA and
PPS, 50 °C for anything unrecognised — and never above `QIDI_MAX_BOX_DRY_TEMP`.
The limit is only as good as the slot setup, so correct a wrong slot with
`qidi_box_set_slot` first. Drying started from the touchscreen is not covered.

Each Box tool checks Klipper's command list first, so a firmware without the
command fails with a clear message instead of an unknown-command error. The
commands are the ones the Plus5 touchscreen sends (`UPDATE_FILAMENT_INFORMATION`,
`LOAD_/UNLOAD_FILAMENT_CONTROL`, `BOX_START_DRYING BOX= TIME=<hours> TEMP=`,
`BOX_STOP_DRYING`), with the older Box firmware's `E_LOAD`/`E_UNLOAD` and
`ENABLE_BOX_DRY`/`DISABLE_BOX_DRY` as fallback.

## Configuration

| Env var | Default | |
|---|---|---|
| `QIDI_MOONRAKER_URL` | `http://localhost:7125` | Moonraker base URL. |
| `QIDI_SNAPSHOT_URL` | `<host>/webcam/?action=snapshot` | mjpeg-streamer snapshot. |
| `QIDI_MOONRAKER_API_KEY` | — | Sent as `X-Api-Key`. Set it and remove the server's address range from Moonraker's `trusted_clients`, so only this container can drive the printer. |
| `QIDI_ENABLE_CONTROL` | `false` | Light, temperatures, pause/resume/cancel, e-stop, restart, Box tools, upload. |
| `QIDI_ENABLE_PRINT_START` | `false` | `qidi_start_print`. |
| `QIDI_MAX_NOZZLE_TEMP` / `_BED_TEMP` / `_CHAMBER_TEMP` | 300 / 110 / 65 | Remote target caps (°C), integers 0-400. |
| `QIDI_PLATE_CHECK_MAX_AGE_MIN` | `15` | Snapshot freshness for a start, integer minutes 1-1440. |
| `QIDI_MAX_BOX_DRY_TEMP` | `65` | Ceiling for Box drying, integer °C 30-90, on top of the per-filament limit. |
| `QIDI_UPLOAD_ALLOWED_HOSTS` | — | Comma-separated hosts `qidi_upload_file` may download from (https). Empty = URL uploads off. |
| `QIDI_MAX_UPLOAD_MB` | `100` | Largest G-code upload, integer MB 1-1024. Inline uploads are also bounded by `MCP_MAX_BODY_BYTES`. |
| `QIDI_TIMEOUT_MS` | `10000` | Per-request timeout. |
| `QIDI_AUDIT_LOG` | — | JSONL audit log (who did what; Moonraker logs everything as `_TRUSTED_USER_`). |
| `QIDI_TRUST_FORWARDED_USER` | `false` | Trust `X-MCP-User` from an authenticating gateway. Requires `MCP_HTTP_TOKEN`. |

The numeric `QIDI_*` settings are validated at startup: a value that is not an
integer in range stops the server with a message naming the variable. The tools
re-read the limits on every call and refuse to act if they are invalid, so a typo
can never lift a temperature cap or the plate-check age.

### HTTP transport

`npm run dev:http` (or the Docker image) serves `POST /mcp` and an unauthenticated
`GET /healthz`. It binds to `127.0.0.1` unless `MCP_HTTP_HOST`/`HOST` says otherwise.

| Env var | Default | |
|---|---|---|
| `MCP_HTTP_TOKEN` | — | Bearer token callers must present on `/mcp`; at least 32 printable ASCII characters (e.g. `openssl rand -hex 32`). **Required** on a non-loopback bind (e.g. `0.0.0.0` in a container), with `MCP_ALLOW_ANY_ORIGIN=true` or with `QIDI_TRUST_FORWARDED_USER=true`; the server refuses to start otherwise. |
| `MCP_HTTP_ALLOW_UNAUTHENTICATED` | `false` | Serve `/mcp` without a token on a non-loopback bind. Only if access is restricted by other means. |
| `MCP_HTTP_HOST` / `HOST`, `PORT` | `127.0.0.1`, `3000` | Bind address and port. |
| `MCP_MAX_BODY_BYTES` | `10485760` | Request body limit. |
| `MCP_ALLOWED_ORIGINS` / `MCP_ALLOW_ANY_ORIGIN` | localhost origins | Browser origins allowed to call `/mcp`. |
| `MCP_ALLOWED_HOSTS` | localhost names on a loopback bind | Host names accepted in the `Host` header (DNS-rebinding protection). |
| `MCP_AUTH_FAILURE_LIMIT` / `MCP_AUTH_FAILURE_WINDOW_MS` | `20` / `60000` | Failed authentication attempts per client address before `429`; `0` disables. Valid tokens are never throttled. |

Moonraker API requests never follow HTTP redirects. The webcam snapshot follows at
most two redirects, and only on the same origin when an API key is set, so the key
is never sent to another host.

## Security model

- The server drives the printer with **one shared identity**: Moonraker's trusted
  client (or the configured API key). Every MCP client that can call it can use the
  enabled tool tiers.
- Per-user authorization belongs in an identity-aware gateway in front of the
  server. With `QIDI_TRUST_FORWARDED_USER=true` that gateway's user (`X-MCP-User`)
  is written to the audit log; it does not change what the caller may do.
- Tool access is not data access: the tier switches limit the tools, not what
  Moonraker accepts from this server's network address.
- The HTTP server does not terminate TLS. Put it behind a TLS-terminating proxy
  or keep it on a private network.

## Develop

```bash
npm install
npm run typecheck && npm test && npm run build
QIDI_MOONRAKER_URL=http://<printer>:7125 npm run dev        # stdio
```

## License

The code is source-available under the [Functional Source License, Version 1.1, ALv2 Future License](LICENSE) (FSL-1.1-ALv2).

In plain words:

- **Allowed:** reading the code, running it, changing it, using it inside your own organisation (also commercially), using it for non-commercial education and research, and contributing changes back.
- **Not allowed:** making it, or something built from it, available to others as a commercial product or service that competes with it, for example a hosted service with the same functionality.
- **Becomes Apache-2.0:** each version is also licensed under the Apache License 2.0 from two years after it is released.
- **Earlier versions:** versions released before 0.3.0 were licensed under Apache-2.0 and remain so.

The terms in [LICENSE](LICENSE) are what apply; this summary does not replace them. See also [NOTICE](NOTICE).

TDQS

A3.7/5.0

Scored across 16 tools

Disambiguation5/5

The tools split cleanly by purpose: server/tier status vs. live printer state, box filament info, file listing/metadata, history, console diagnostics, and distinct control actions. A minor overlap exists because qidi_snapshot can switch the light on, but the description makes qidi_set_light the dedicated light-control tool.

Naming Consistency5/5

All tools use the same qidi_ prefix and snake_case convention, with predictable verb_noun or noun_status forms such as qidi_set_light, qidi_list_files, and qidi_printer_status. A few noun-only names like qidi_snapshot and qidi_console are still clear and consistent with the overall pattern.

Tool Count4/5

At 16 tools this is slightly above the typical 3-15 well-scoped range, but each tool maps to a distinct printer monitoring or control function and there is little obvious filler. The count is reasonable for a printer control server, though a few tools could be consolidated if simplicity were prioritized.

Completeness3/5

The surface covers monitoring and job lifecycle well: status, snapshot, files, metadata, history, console, temperature, light, start, pause, resume, cancel, emergency stop, and firmware restart. However, notable gaps remain around file lifecycle management (upload/delete) and hardware operations such as filament load/unload or motion/jog/calibration.