cups-sane-mcp
# cups-sane-mcp
An MCP server that lets an AI assistant (any MCP client) use a printer and a scanner on your
network: print text and files, scan pages and look at them.
- **Printing** through CUPS: files are handed to CUPS unchanged (PDF, PostScript, PNG, JPEG, TIFF, plain
text), with CUPS options passed through (scaling, resolution, tray, pages per sheet, ...). No conversion
happens here: for formatted documents the client makes a PDF and uploads it.
Optionally prints files from a download link (`print_url`, off by default).
- **Scanning** through SANE: feeder or flatbed, colour/grey/black-and-white, PDF or images. The client gets
small watermarked previews; the full-quality file stays on the machine (or is shared via a short-lived link).
- **Uploads** through any S3-compatible bucket: rather than passing files through the conversation,
`get_upload_link` gives the client a presigned upload URL (used from a code sandbox or any HTTP client),
and `print_file(id)` prints it.
## Tools
| Tool | What it does |
|---|---|
| `list_printers`, `print_queue`, `cancel_job` | Printer status and jobs |
| `printer_options` | The printer's own options and the standard CUPS job options, with allowed values |
| `print_text` | Print plain text (CUPS lays it out) |
| `print_file_data` | Print a small base64 file (tiny files only) |
| `get_upload_link`, `list_uploads`, `print_file` | Upload a file to the bucket, then print it by id |
| `scan`, `list_scan_sources` | Scan; show the scanner's sources and modes |
| `list_scans`, `get_scan`, `print_scan` | Saved scans: list, preview / download link, reprint |
| `print_url` | Download a file from a link (e.g. cloud storage) and print it; only when `MCP_PRINT_URL=1` |
Every print tool takes the same options: `copies`, `duplex`, `page_ranges`, `paper`, `quality`,
`scaling` (`auto`, `fit`, `fill`, `none`), `ppi`, and `options` for anything else CUPS or the printer accepts
(validated against `printer_options`).
## Requirements
Linux with CUPS and cups-filters (`lp`, `lpstat`, `lpoptions`, `cancel`), SANE (`scanimage`),
ImageMagick 7 (`magick`, for scans), Poppler (`pdftoppm`, `pdfinfo`) and the Liberation fonts (scan
preview watermark). Python 3.11+.
## Install
```sh
python -m venv venv && venv/bin/pip install .
venv/bin/cups-sane-mcp # stdio, for local clients
venv/bin/cups-sane-mcp --http # streamable HTTP on 127.0.0.1:8710/mcp
```
For a local client that starts servers itself, the usual configuration looks like:
```json
{
"mcpServers": {
"printer": {
"command": "/path/to/venv/bin/cups-sane-mcp",
"env": { "MCP_PRINTER": "Office_Printer" }
}
}
}
```
To run it permanently over HTTP, see `examples/cups-sane-mcp.service` (systemd, sandboxed) and
"Connecting remote clients" below.
## Settings (environment variables)
| Variable | Default | |
|---|---|---|
| `MCP_PORT`, `MCP_PATH` | `8710`, `/mcp` | HTTP listener (always 127.0.0.1) |
| `MCP_PUBLIC_HOST` | – | Public hostname(s) in front of it, comma-separated (Host/Origin checks) |
| `MCP_ALLOWED_ORIGINS` | – | Extra browser origins allowed to call it, comma-separated, e.g. `https://app.example.com` |
| `MCP_PRINTER` | CUPS default, else first printer | |
| `MCP_SCANNER` | first device `scanimage` finds | SANE device name, e.g. `escl:https://192.0.2.10:443` |
| `MCP_SCAN_SOURCES`, `MCP_SCAN_MODES` | matched by name | JSON maps from tool names to the device's values, e.g. `{"adf": "ADF Front", "flatbed": "Flatbed"}`. The keys become the tool's choices |
| `MCP_SCAN_NOTES` | – | Extra guidance for the client about your scanner's quirks |
| `MCP_SCAN_DIR` | `~/Documents/Scans` | Where scans are saved |
| `MCP_MAX_MB` | `32` | Largest file accepted for printing, in MiB (uploads, links, base64) |
| `S3_ENDPOINT_URL`, `S3_REGION`, `S3_BUCKET`, `S3_ACCESS_KEY_ID`, `S3_SECRET_ACCESS_KEY`, `S3_ADDRESSING_STYLE` | – , `us-east-1`, …, `auto` | Upload storage; see `examples/s3.env.example` |
| `MCP_PRINT_URL` | off | `1` enables `print_url` |
| `MCP_PRINT_URL_HOSTS` | any public host | Comma-separated host suffixes `print_url` may download from, e.g. `dropboxusercontent.com,googleusercontent.com` |
Without a scanner mapping, the server reads the device's `--source` and `--mode` choices and matches them
by name (feeder/flatbed/duplex, colour/grey/lineart). If that picks the wrong one, `list_scan_sources`
shows the device's own names; `scan` accepts those too, or set `MCP_SCAN_SOURCES`.
## Connecting remote clients
Local clients can use stdio. Remote (web) clients need a public HTTPS URL. **The server has no
authentication of its own**: put it behind a reverse proxy or tunnel that does (for example an OAuth-capable
proxy or a zero-trust tunnel), and set `MCP_PUBLIC_HOST` to that hostname. If the client calls it from a
browser page, add that page's origin to `MCP_ALLOWED_ORIGINS`. For uploads, the client's sandbox must be
allowed to reach your bucket's endpoint host.
## Security
- Run it as a dedicated user with no access to anything else; `examples/cups-sane-mcp.service` shows a
systemd sandbox (no home directories, network limited to localhost and the scanner).
- Optional egress filter: route its traffic through a proxy that only allows the S3 endpoint
(`examples/tinyproxy.*`, with `HTTPS_PROXY` in the unit).
- Uploads: use a bucket-scoped key and a short lifecycle rule. Upload links last 15 minutes, scan
download links 5 minutes, and uploads are deleted after printing.
- `print_url` only downloads and prints files: no HTML rendering, no scripts. Each URL and every redirect
must be http(s), on `MCP_PRINT_URL_HOSTS` if set, and resolve only to public addresses, so links to
localhost or your network are refused. Setting `MCP_PRINT_URL_HOSTS` to your storage providers
is recommended. If you use the egress proxy, add those hosts to its filter too.
## Known issues
- **Blank pages from some PDFs.** libcupsfilters 2.2.x with PDFio 1.6.6 or older can't read PDF streams
with chained filters (e.g. `[/ASCII85Decode /FlateDecode]`, which ReportLab uses by default). CUPS logs
`cfFilterPDFToPDF: Unsupported compound stream filter` and prints a blank page while reporting success.
Fixed in PDFio 1.6.7 ([libcupsfilters#301](https://github.com/OpenPrinting/libcupsfilters/issues/301)):
update PDFio. Until then, re-save such PDFs with Flate only (e.g. `qpdf --decode-level=generalized`), or
set `rl_config.useA85 = 0` in ReportLab.
- **Linux only.** It drives the CUPS and SANE command-line tools.
## Licence
MIT, see [LICENSE](LICENSE).
TDQS
Scored across 14 tools
The four print tools (print_text, print_file, print_file_data, print_scan) share nearly identical option blocks, but each description names a distinct input source and explicitly steers callers away from the weaker variants (e.g. 'prefer get_upload_link + print_file for files'). The only genuinely confusable pair is print_file vs print_file_data, which the descriptions mitigate but do not fully eliminate.
Most tools follow a clear verb_noun pattern (list_printers, cancel_job, get_scan, print_text), and the scan/print families are coherently grouped. Two outliers break the pattern: 'printer_options' uses noun-first with no verb, and 'scan' is a bare verb, plus 'print_queue' is really a listing operation.
14 tools sits squarely in the well-scoped range, and each one maps to a distinct step in the print/scan workflow: discovery, option lookup, submission, queue inspection, cancellation, and scan retrieval. Nothing appears redundant or padded.
Core lifecycle is covered: list printers, inspect capabilities, submit from four input types, view and cancel queue jobs, scan, list/retrieve scans, and manage uploads. Minor gaps remain — no hold/release or pause-printer operations, no scan deletion or cleanup, and no way to query a single job's detailed status beyond print_queue.