locmodel-image
# locmodel-image-MCP
> GitHub repository: [jaja1620/locmodel-image-MCP](https://github.com/jaja1620/locmodel-image-MCP) — the installable package, the CLI command and the MCP server are still named `locmodel-image`.
**Turn your local LocModel / stable-diffusion.cpp image engine into a first-class tool for AI agents — offline, free, scriptable.**
`locmodel-image` is a zero-dependency bridge that exposes the image engine bundled inside
[LocModel](https://github.com/LocalAI) (an `sd-server` built on stable-diffusion.cpp) through two
interfaces:
| Interface | Entry point | Use it when |
| --- | --- | --- |
| **MCP server** (recommended) | `scripts/locmodel_mcp.py` — tools `generate_image`, `ref_edit`, `locmodel_status`, `list_models`, `engine_doctor`, `engine_discover` | Your AI client speaks Model Context Protocol (Claude Desktop, Cursor, Marvis, Continue, …). The model gets a typed tool instead of memorising shell commands. |
| **CLI bridge** | `scripts/locmodel_image.py` | Scripts, batch jobs, CI, or clients without MCP support. |
Both share the same engine layer: port auto-discovery, VRAM-aware size guard, failure
classification with retry, cross-process generation lock, structured JSON output and meaningful
exit codes.
> **English** · [简体中文](README.zh-CN.md)
---
## Table of contents
- [Why this exists](#why-this-exists)
- [Requirements](#requirements)
- [Install](#install)
- [Quick start](#quick-start)
- [Connect as an MCP server](#connect-as-an-mcp-server)
- [Install the companion Skill](#install-the-companion-skill)
- [Edit an existing image](#edit-an-existing-image)
- [CLI reference](#cli-reference)
- [Workflow parameters](#workflow-parameters)
- [Configuration reference](#configuration-reference)
- [Sizing and VRAM strategy](#sizing-and-vram-strategy)
- [How it works](#how-it-works)
- [Testing](#testing)
- [Troubleshooting](#troubleshooting)
- [Compatibility and limitations](#compatibility-and-limitations)
- [What changed in 3.2](#what-changed-in-32)
- [What changed in 3.1](#what-changed-in-31)
- [What changed in 3.0](#what-changed-in-30)
- [What changed in 2.0](#what-changed-in-20)
- [License](#license)
---
## Why this exists
LocModel ships a complete local image pipeline but exposes it as a desktop GUI. Agents and
scripts cannot drive a GUI reliably, and hard-coding `http://127.0.0.1:8790` breaks the moment the
port changes, the model changes, or the machine has less VRAM than yours.
This project solves exactly those three problems:
1. **Discovery instead of hard-coded ports** — five fallback layers, including parsing the
`sd-server` process command line (the most reliable source, because LocModel chooses the port
at launch).
2. **Guard instead of mysterious failures** — the engine silently fails on *tiny* images
(512×512 can return `no results`) while succeeding on much larger ones. The guard works on
**total pixel area**, not on a single edge, and rescales proportionally so the requested aspect
ratio survives.
3. **Portability instead of "works on my machine"** — no hard-coded user paths, resolutions,
samplers or Python installs. Every default is discovered or overridable.
---
## Requirements
| Item | Requirement |
| --- | --- |
| Python | 3.8+ (3.10+ recommended). **Standard library only — nothing to `pip install`.** |
| Engine | LocModel desktop app with a diffusion model loaded, **or** any `sd-server` (stable-diffusion.cpp) exposing the `/sdcpp/v1/*` HTTP API |
| OS | Windows 10/11, macOS, Linux |
| GPU | Any GPU the engine itself supports. 6 GB VRAM is usable; 8 GB is comfortable at 1024² |
The bridge does **not** load models and does **not** manage the engine lifecycle — start LocModel
and load a model first. That is a deliberate boundary: engine startup differs per hardware and
per model, and getting it wrong is the number-one cause of "workspace capacity" crashes.
---
## Install
Three ways to install — pick **one**:
- **Installer (single-file exe, no Python needed)** — recommended for normal use. Download
`locmodel-image-setup.exe` from [Releases](https://github.com/jaja1620/locmodel-image-MCP/releases),
double-click it, confirm once, done. It unpacks the runtime into `%LOCALAPPDATA%\locmodel-image`
and registers the MCP server into every supported client it detects. Marvis users then run
`locmodel-image-setup.exe emit marvis` and paste the printed instructions into chat.
- **From the repository (scriptable, hash-verified, no exe, no clone, no pip)** — one command pulls
the MCP server and the companion skill straight from this repo, verifies every file against
`install/manifest.json`, and installs them into `%LOCALAPPDATA%\locmodel-image\repo` (Windows) or
`${XDG_DATA_HOME:-~/.local/share}/locmodel-image/repo` (Linux/macOS) plus your host's skills
directory. Works on Windows, macOS and Linux, and is the route an AI agent can execute on its own.
See [Install from the repository](#install-from-the-repository).
- **From source (needs Python 3.9+, the original way)** — clone and run, as described below.
Detailed, bilingual step-by-step guide (install + usage + troubleshooting):
[docs/INSTALL.md](docs/INSTALL.md) (English) · [docs/INSTALL.zh-CN.md](docs/INSTALL.zh-CN.md) (中文)
### Install from the repository
Nothing to build, nothing to unpack, no admin rights, no registry. Two pieces are deployed — the
**MCP server** (`scripts/*.py`, stdio JSON-RPC) and the **skill** (`SKILL.md`, Markdown only):
```powershell
# Windows
$d = Join-Path $env:TEMP 'locmodel-image-install'
New-Item -ItemType Directory -Force -Path $d | Out-Null
Invoke-WebRequest 'https://raw.githubusercontent.com/jaja1620/locmodel-image-MCP/v4.1.0/install/install-from-repo.ps1' `
-OutFile "$d\install-from-repo.ps1" -UseBasicParsing
powershell -NoProfile -ExecutionPolicy Bypass -File "$d\install-from-repo.ps1"
```
```sh
# Linux / macOS
curl -fsSL https://raw.githubusercontent.com/jaja1620/locmodel-image-MCP/v4.1.0/install/install-from-repo.sh -o /tmp/locmodel-install.sh
sh /tmp/locmodel-install.sh
```
The scripts download each file at the pinned ref, check its SHA-256 against `install/SHA256SUMS`,
deploy it, write `<MCP_ROOT>/install-manifest.json`, then prove the server answers an MCP
`initialize` handshake. `-Register claude|cursor|vscode` also merges the host config entry (keeping a
`.bak-<timestamp>` backup); Marvis and WorkBuddy manage their own config, so a paste-ready snippet is
printed instead.
| Flag (PS / sh) | What it does |
| --- | --- |
| `-Component` / `--component` | `mcp`, `skill`, or `both` (default) |
| `-Host` / `--host` | `auto` (default: every host dir that already exists), or `claude`, `marvis`, `workbuddy`, `cursor`, `vscode`, `generic`, `all` |
| `-Register` / `--register` | write the MCP entry into that host's config, with a backup |
| `-Source` / `--source` | `manifest` (file by file, default), `zip` (release asset), `local` (offline, with `-FromPath`) |
| `-DryRun` / `--dry-run` | print the plan, write nothing |
Offline install: download `locmodel-image-repo-install-v4.1.0.zip` from
[Releases](https://github.com/jaja1620/locmodel-image-MCP/releases), unzip, then
`.\install\install-from-repo.ps1 -Source local -FromPath .`
Full guide, payload tables, exit codes and uninstall steps:
[docs/INSTALL-FROM-REPO.md](docs/INSTALL-FROM-REPO.md) (English) ·
[docs/INSTALL-FROM-REPO.zh-CN.md](docs/INSTALL-FROM-REPO.zh-CN.md) (中文)
**Delegating the install to an AI agent** — hand it this single line:
> Read `https://raw.githubusercontent.com/jaja1620/locmodel-image-MCP/v4.1.0/install/AGENT-INSTALL.md`
> and follow it to install locmodel-image (MCP server + companion skill) on this machine.
[`install/AGENT-INSTALL.md`](install/AGENT-INSTALL.md) is a self-contained spec: exact URLs, the two
destination directories per OS, the hash table, two verification commands, the per-host config key,
and the list of things an agent must not touch. An agent with no shell access can still complete it
by fetching the 8 payload files itself; an agent with no network can hand the human a checklist.
The repo install lives in the `repo\` subdirectory and never modifies an existing
`locmodel-image-setup.exe` install, so the two can coexist.
### From source
Nothing to build. Clone and run:
```bash
git clone https://github.com/jaja1620/locmodel-image-MCP.git
cd locmodel-image-MCP
python scripts/locmodel_image.py doctor
```
`doctor` prints a checklist (Python version, platform, config path, engine port and how it was
found, model, GPU free memory, output directory writability) and exits non-zero if something
essential is missing.
Optional — install as a package so `locmodel-image` is on your `PATH`:
```bash
pip install .
locmodel-image doctor
```
---
## Quick start
```bash
# 1. Is the engine reachable, and where?
python scripts/locmodel_image.py status
# 2. Validate parameters without spending GPU time
python scripts/locmodel_image.py generate --prompt "courtyard at dusk" --aspect 16:9 --dry-run
# 3. Generate
python scripts/locmodel_image.py generate \
--prompt "modern modular building, warm timber facade, dusk, wide angle, architectural photography" \
--aspect 16:9 --out ./output --seed 424242
# 4. Two variants, fixed seed for reproducibility
python scripts/locmodel_image.py generate --prompt "..." --count 2 --seed 424242
```
Every run writes:
```
output/locmodel_20261109_171204_1_seed424242.png
output/locmodel_20261109_171204_meta.json
```
`_meta.json` records the effective prompt, seed, size after guard correction, steps, sampler, CFG,
requested-vs-final size and elapsed seconds — so any image can be reproduced or audited later.
## Edit an existing image
v3.0 adds two verbs on top of text-to-image: **repaint one region** and **redraw the whole frame**.
| You want | Use | Original pixels |
| --- | --- | --- |
| Change one area, keep the rest | `edit_image` (needs a mask) | Preserved outside the mask — enforced, not hoped for |
| Same composition, new style/subject | `redraw_image` | Used as a structural reference only |
| Keep this person, change the hair/clothes/background | `ref_edit` | Anchored on the references, not re-rendered |
### Keep the subject, change one thing (reference channel, 4.1)
`edit_image` repaints the subject from noise inside the mask, so a small identity-preserving change
("same person, shorter hair") tends to return a *different person*. The engine has had a reference
channel for this since 3.6.x; 4.1 exposes it.
```python
ref_edit(prompt="same person, short black bob with a blunt fringe",
ref_images=[r"D:\in\me.png"], # 1-8 paths, data: URIs or bare base64
ref_fit="follow_first", # canvas from the first reference; "image" keeps your width/height
ref_max_pixels=1048576, # budget, default 1024^2, ceiling 4194304
seed=42)
```
Rules that matter:
1. **Describe only the change.** Re-describing the reference invites a re-render.
2. **No `strength`.** The reference channel slices no sigmas, so `strength` does nothing there;
`ref_strength` is rejected with a pointer to `redraw_image`. A stray `init_image` is stripped
and reported in `notes`.
3. **`ref_image` (singular) is an error.** The engine silently ignores it and returns plain
text-to-image; the client now refuses it and asks for `ref_images=[...]`.
4. **Read `engine_trace`.** `fn main(reference_0)` plus `ref_latents=[reference_latent_0]` means the
reference bound; `fn main()` means it did not and the picture is text-to-image. The `engine:` line
in the summary states which one it was.
The canvas follows the first reference's aspect ratio, is capped at `ref_max_pixels`, and is aligned
to the recipe's `size_multiple` (16 here) — the same arithmetic the engine applies.
### Give the mask three ways
**Regions as JSON** — no image editor needed:
```json
{"unit": "norm", "regions": [
{"type": "rect", "x": [0.12, 0.10, 0.48, 0.42]},
{"type": "polygon", "points": [[0.60, 0.62], [0.92, 0.60], [0.78, 0.94]]}
]}
```
`unit` is `norm` (0–1, resolution-independent, default) or `px`. Shapes are unioned.
**A mask PNG** — white = repaint, black = keep. Rescaled automatically if the size differs.
**A colour threshold** — "retexture that solid-colour wall" without drawing anything.
**By hand** — open `annotator/mask_annotator.html` in any browser (single file, no install).
Drag an image in, draw rectangles / polygons / freehand, then export `mask.png` or copy the
normalised JSON straight into `edit_image`.
### Model selection is explicit, never silent
```bash
python scripts/locmodel_image.py edit-models
```
Prints every model, which ones can edit regions, and which one would be picked automatically.
The default priority is `qwen-image-edit` → `flux2-klein` → `qwen-image`.
Discovery cost is bounded and cached, because probing for an engine that is not running used to
be the slowest thing in the tool: process names are checked with `tasklist` before PowerShell is
started to read command lines, `netstat` output is used to skip candidate ports that cannot be
listening, and the resulting snapshot is reused for `edit.snapshot_cache_ttl` seconds (default
120) across processes. A warm call answers in ~0.3 s; pass `--refresh` after loading a model by
hand in the LocModel window:
```bash
python scripts/locmodel_image.py edit-models --refresh
```
- `edit.prefer_loaded` (default `true`) — if an editable model is already in VRAM, use it instead
of swapping to a "better" one. A swap means unload + reload: 30–60 s for a 4B model, longer for
a 20B, and it can OOM on a card that was already tight.
- `edit.auto_load` (default `true`) — load the highest-priority editable model when nothing is
loaded. Without it the priority list would be decorative.
- Pass `model` and it is used **exactly** — an unusable model raises an error listing the
alternatives rather than quietly substituting another one.
- Only the `edit` family (Qwen-Image-Edit, FLUX.2 klein) can repaint a region. Pure generators
(Qwen-Image, Z-Image, SDXL, SD1.5) can only `redraw_image`.
Every response carries a `selection` block (`requested` / `chosen` / `family` / `rank` / `reason` /
`auto_loaded` / `candidates`) so you can explain why a particular model ran.
### Outside the mask cannot change
The engine is not trusted to respect the mask. Results are recomposited on the CPU —
`result = where(mask > 127, edited, original)` — so pixels outside the mask are byte-identical to
the source. `protected_pixels` / `redrawn_pixels` are reported for auditing.
The file at `files[].path` **is** the protected image; the engine's unguarded output is kept
beside it as `*_raw.png` and exposed as `files[].raw` (plus `recomposed[].raw`). `files[].protected`
is `true` when recomposition succeeded. Only hand `files[].path` to the user — `_raw` exists for
"what did the engine actually paint" debugging.
```bash
# repaint a region
python scripts/locmodel_image.py edit --image ./output/v1.png \
--prompt "night-time glass curtain wall, interior lights on" \
--region 0.25,0.18,0.62,0.55 --strength 0.8
# repaint using an exported mask
python scripts/locmodel_image.py edit --image ./output/v1.png --mask ./output/mask.png \
--prompt "oak slat ceiling"
# same composition, watercolour
python scripts/locmodel_image.py redraw --image ./output/v1.png \
--prompt "watercolour architectural sketch, loose ink lines, white paper" --strength 0.65
```
The source image is never overwritten; each edit lands in a new file so variants can be compared.
---
## Connect as an MCP server
The server speaks **newline-delimited JSON-RPC 2.0** over stdio (it also accepts LSP-style
`Content-Length` framing, for clients that prefer it). Protocol versions `2025-06-18`,
`2025-03-26` and `2024-11-05` are negotiated.
### Claude Desktop
`claude_desktop_config.json`:
```json
{
"mcpServers": {
"locmodel-image": {
"command": "python",
"args": ["C:/tools/locmodel-image-MCP/scripts/locmodel_mcp.py"],
"env": { "PYTHONIOENCODING": "utf-8", "PYTHONUTF8": "1" }
}
}
}
```
On Windows use the **absolute path of a real interpreter** (for example
`C:/Users/you/AppData/Local/Programs/Python/Python312/python.exe`); the bare `python` may resolve
to the Microsoft Store stub, which silently exits.
### Marvis
```json
{
"name": "locmodel-image",
"config": {
"command": "C:\\Users\\you\\AppData\\Local\\Programs\\Python\\Python312\\python.exe",
"args": ["C:\\tools\\locmodel-image-MCP\\scripts\\locmodel_mcp.py"],
"env": { "PYTHONIOENCODING": "utf-8", "PYTHONUTF8": "1" }
}
}
```
### Any other client
Point it at `scripts/locmodel_mcp.py` with stdio transport. Pin the port if discovery is
unreliable on your machine:
```json
"env": { "LOCMODEL_SD_PORT": "8790", "PYTHONIOENCODING": "utf-8" }
```
### Tool surface
| Tool | Arguments | Returns |
| --- | --- | --- |
| `generate_image` | `prompt` (required), `negative_prompt`, `width`, `height`, `aspect`, `steps`, `sampler`, `seed`, `count`, `output_dir`, `filename_prefix`, `model`, `timeout_s` | Image paths, effective seed, final size, elapsed time |
| `ref_edit` | `prompt` (required), `ref_images` (required, 1-8), `ref_fit`, `ref_max_pixels`, `negative_prompt`, `width`, `height`, `seed`, `count`, `output_dir`, `filename_prefix`, `model`, `timeout_s`, `params`, `guard`, `dry_run` | Image paths, the reference plan, `engine_trace` (did the reference bind?), effective size |
| `locmodel_status` | — | Engine state, port + how it was found, model, engine defaults, GPU memory, `recipe`, `size_multiple`, `ref_capable` |
| `edit_image` | `init_image` (required), `prompt` (required), `mask_image` \| `regions`, `strength`, `negative_prompt`, `model`, `steps`, `sampler`, `cfg_scale`, `seed`, `mask_blur`, `inpaint_padding`, `channel`, `out_dir`, `filename_prefix`, `verify`, `dry_run` | Edited image paths, mask stats, `selection` (model actually used), `verify` block |
| `redraw_image` | `init_image` (required), `prompt` (required), `strength`, + the same optional set | Redrawn image paths, `selection`, `verify` |
| `list_edit_models` | — | Models with family/rank, which can edit regions, and the automatic pick |
| `load_model` | `name` (required), `timeout` | Loads/switches the engine model, verifies it took effect |
| `list_models` | — | Models reported by the engine |
| `engine_doctor` | — | Full self-check with pass/fail per item |
| `engine_discover` | — | Force a port re-scan and report the result |
| `generate_video` | `prompt` (required), `negative_prompt`, `width`, `height`, `fps`, `video_frames`, `steps`, `flow_shift`, `cfg_scale`, `sampler`, `seed`, `output_format`, `init_image`, `end_image`, `ref_images`, `control_frames`, `wait_seconds`, `dry_run` | `job_id` + `poll_url` immediately (`wait_seconds=0`), or the saved clip path when waiting |
| `job_status` | `job_id` (required), `output_dir`, `filename_prefix`, `save_result` | Status, progress, queue position, saved absolute paths once completed |
| `cancel_job` | `job_id` (required) | Cancellation result |
| `list_recipes` | `kind` (optional, `video`) | Engine recipe list (3.6.x and newer) |
| `lipsync_status` | — | Lip-sync pipeline health: `ok`, effective variant + reason, the 13 required artifacts, `missing`, paths, hardware |
| `lipsync_generate` | `image` (required), `audio` (required), `prompt`, `negative`, `res`, `width`, `height`, `crop`, `steps`, `accel`, `seed`, `max_seconds`, `vocals`, `color_fix`, `crf`, `vae_tiled`, `sage`, `output_dir` | Absolute path of the rendered mp4; fails fast with `lipsync_not_mounted` when the pipeline is not installed |
`generate_image` returns `isError: true` with a human-readable `hint` on failure, so the agent can
act on it instead of retrying blindly.
---
## Install the companion Skill
`SKILL.md` is the prompt-side counterpart of the MCP server: it tells an agent *how to write
prompts*, which aspect ratio suits which subject, which parameters matter, and how to interpret
each failure — knowledge a JSON schema cannot carry.
The two are complementary, not alternatives:
- **MCP** = the hands (typed tool, always available)
- **Skill** = the craft (prompt structure, presets, parameter judgement)
Install by copying `SKILL.md` (that is the whole skill — the rest of the repo is the MCP server) into
your client's skills directory:
```bash
# Claude / generic agent skills directory
mkdir -p ~/.claude/skills/locmodel-image && cp SKILL.md ~/.claude/skills/locmodel-image/
# Marvis (Windows)
xcopy /E /I . "%APPDATA%\Tencent\Marvis\User\<your-id>\skills\custom\locmodel-image"
```
With [Install from the repository](#install-from-the-repository) the skill is already deployed
alongside the MCP server, so no manual copy is needed; `-Host auto` writes one copy into every host
skills directory that already exists.
Skills that support front-matter (`name`, `description`, `version`, `license`) will pick the
metadata up automatically.
---
## CLI reference
```
locmodel_image.py [--config PATH] [--verbose] [--log-file PATH] <command> [options]
```
| Command | Purpose |
| --- | --- |
| `status` | Engine port (and detection source), model, engine defaults, GPU memory |
| `discover` | Force re-discovery, print port + source |
| `models` | List models the engine reports |
| `doctor` | Environment self-check; exit code reflects health |
| `generate` | Submit a job and wait for the image |
| `edit` | Repaint a masked region of an existing image |
| `redraw` | Re-draw a whole image from a new prompt |
| `edit-models` | Models that can edit, their priority, and the automatic pick |
| `load-model` | Load or switch the model inside the engine |
| `config show\|get\|set\|path` | Inspect or modify configuration |
### `generate` options
| Option | Default | Notes |
| --- | --- | --- |
| `--prompt` | required | Non-empty string |
| `--negative` | `""` | Negative prompt |
| `--width` / `--height` | engine default | Positive integers; corrected by the guard |
| `--aspect` | — | `1:1 4:3 3:4 3:2 2:3 16:9 9:16 21:9 2:1 1:2`; mutually exclusive with `--width/--height` |
| `--steps` | engine default | Higher is slower with diminishing returns |
| `--sampler` | engine default | e.g. `euler`, `dpm++2m` |
| `--cfg` | engine default | Turbo/distilled models usually want 1.0–1.5 |
| `--seed` | `-1` (random) | Fix it to reproduce |
| `--count` | `1` | Capped by `generate.max_count` (default 4) |
| `--model` | — | Override model name on multi-model engines |
| `--out` | config `output.dir` | Output directory (created if missing) |
| `--prefix` | `locmodel` | Filename prefix |
| `--timeout` | `generate.timeout` | Seconds per job |
| `--retry` | `generate.retry` | Retries with a new seed on transient failure |
| `--no-guard` | off | Force tier-A sizing (send the requested size verbatim) |
| `--dry-run` | off | Print the HTTP payload and exit — no GPU time spent |
### `edit` options
| Option | Default | Notes |
| --- | --- | --- |
| `--image` | required | Source image (never modified) |
| `--prompt` | required | What should appear *inside* the mask |
| `--mask` | — | Mask PNG, white = repaint. Wins over `--region` |
| `--region` | — | `x1,y1,x2,y2`, repeatable. Coordinates follow `--unit` |
| `--unit` | `norm` | `norm` (0–1) or `px` |
| `--invert` | off | Invert the generated mask |
| `--strength` | `0.75` | Denoising strength; raise to repaint more freely |
| `--mask-blur` | `8` | Feather the mask edge to avoid seams |
| `--padding` | `32` | Grow the mask before the engine sees it |
| `--channel` | `auto` | `img_gen` (async) → `img2img` (sync) fallback |
| `--model` | auto | Exact model; error rather than substitution |
| `--verify` | `agent` | Re-read the output and check it |
| `--dry-run` | off | Print the payload (no engine contact, no GPU time) |
`redraw` takes the same options minus the mask ones, and defaults to `--strength 0.6`.
### Exit codes
| Code | Name | Meaning |
| --- | --- | --- |
| 0 | `ok` | Success |
| 1 | `generic_error` | Unexpected error (network, filesystem, JSON) |
| 2 | `generation_failed` | Engine accepted the job but produced no image |
| 3 | `engine_not_found` | No reachable engine after all discovery layers |
| 4 | `bad_arguments` | Invalid input (empty prompt, `count` over limit, `width=0`, unknown aspect, …) |
Scripts can branch on the code; agents can branch on the `error` field in the JSON body.
---
## Workflow parameters
Anything the engine's `sample_params`, `hires`, `vae_tiling_params`, `high_noise_sample_params`
or `lora` blocks accept can be set from this tool as of 3.2, and the enumerations (samplers,
schedulers, upscalers, output formats) are adopted from the live engine instead of being hard-coded. Parameters travel **inside the HTTP
payload only** — never through `config.json`. LocModel owns that file and rewrites it from its own
panel state, so anything written there is silently reverted; the payload is the only channel that
actually holds.
Three scopes, deliberately different lifetimes:
| Scope | MCP | CLI | Lifetime |
| --- | --- | --- | --- |
| One call | `generate_image(..., params={"cfg": 3.5})` | `generate --param cfg=3.5` | that job only |
| This session | `set_workflow_params(params={"cfg": 3.5, "scheduler": "karras"})` | `params set --param cfg=3.5` | MCP process memory; gone on restart |
| Back to defaults | `reset_workflow_params()` / `reset_workflow_params(keys=["cfg"])` | `params reset` | immediately follows the GUI panel again |
Precedence: **per-call `params` > session parameters > LocModel GUI panel / engine defaults.**
When nobody asks for a parameter, nothing is sent and generation follows the GUI panel exactly as
before — the table is a switch, not a form to fill in.
Read live defaults at any time:
```bash
python scripts/locmodel_image.py params list # 14 core parameters, with live defaults
python scripts/locmodel_image.py params list --all # include folded groups
python scripts/locmodel_image.py params show --param cfg
```
`source: engine` means the value came from `/sdcpp/v1/capabilities`; `source: tool` means the engine
did not advertise it and a built-in fallback was used; `source: session` means it is currently
overridden in this process.
### Core parameters (14)
| Parameter | Aliases | Range | Notes |
| --- | --- | --- | --- |
| `cfg` | `cfg_scale`, `txt_cfg` | 0–30 | prompt adherence vs. highlight blow-out |
| `img_cfg` | — | 0–30 | conditional image branch; empty disables |
| `steps` | `sample_steps` | 1–200 | turbo/flow models peak at 8–12 |
| `sampler` | `sample_method` | 22 values | `euler` is the safe default |
| `scheduler` | — | 19 values | `karras`/`beta` sharper, `sgm_uniform` smoother |
| `flow_shift` | — | 0–100 | flow-matching models, 2–5 is noticeable |
| `shifted_timestep` | — | -1–1000 | leave at 0 |
| `eta` | — | 0–1 | SDE samplers: noise injection |
| `distilled_guidance` | — | 0–30 | distilled/accelerated checkpoints |
| `slg_scale` | — | -10–10 | skip-layer guidance, 0 disables |
| `clip_skip` | — | -64–12 | -1 = engine default |
| `strength` | — | 0–1 | edit/img2img only: 0.35 subtle, 0.75 normal |
| `seed` | — | -1 or int | -1 randomises, fix it for A/B |
| `count` | — | 1–8 | also capped by `generate.max_count` and `limits.max_batch_count` |
### Folded groups
These stay out of the way until a task needs them; `params list --all` prints them with the same
live defaults.
- **hires** — `hires_enabled`, `hires_upscaler` (9 engine names, e.g. `Latent (nearest)`),
`hires_scale` (multiplier), `hires_target_width`, `hires_target_height`, `hires_steps`,
`hires_denoising_strength`, `hires_upscale_tile_size`, `hires_custom_sigmas`
- **vae via tiling** — `vae_tiling`, `vae_tile_size_w`, `vae_tile_size_h`, `vae_rel_size_w`,
`vae_rel_size_h`, `vae_extra_tiling_args`, `vae_target_overlap`, `vae_temporal_tiling`
(18 GB of VRAM not required: turn this on before shrinking the canvas)
- **output** — `output_format` (`png` / `jpeg` / `webp`) and `output_compression` (0–100)
- **high noise** — `high_noise_steps`, `high_noise_cfg`, `high_noise_sampler`
- **slg detail** — `slg_layers`, `slg_layer_start`, `slg_layer_end`
- **raw passthrough** — `custom_sigmas`, `extra_sample_args`, `lora`
(`[{"path": "x.safetensors", "multiplier": 0.8}]`; requires `--lora-model-dir` on the engine)
### Validation is client-side
The engine returns `202 queued` for nonsense (`scheduler="bogus"`, `steps="abc"`) and then hangs the
job. Every value is therefore type- and range-checked here first; a bad value fails fast with
`bad_arguments` and a hint, before any GPU time is spent. Aliases are accepted
(`cfg_scale`→`cfg`, `sample_steps`→`steps`, `Karras`→`karras`, `"7,8,9"`→`[7, 8, 9]`).
---
## Configuration reference
Location (first match wins):
1. `--config <path>`
2. `LOCMODEL_IMAGE_CONFIG` environment variable
3. `~/.locmodel-image/config.json` (created on first `config set`)
Resolution order for the port:
1. `LOCMODEL_SD_PORT` environment variable
2. cached value in `config.json`
3. command line of a running `sd-server` process (most reliable)
4. scan of `port_candidates`
5. scan of local listening ports
Keys (defaults shown):
```jsonc
{
"port": null, // cached port; null = auto-discover every run
"host": "127.0.0.1",
"port_candidates": [8790, 8791, ...],// fallback scan list
"engine": {
"process_names": ["sd-server", "sd-server.exe", "sd.exe", "stable-diffusion"],
"capabilities_path": "/sdcpp/v1/capabilities",
"img_gen_path": "/sdcpp/v1/img_gen",
"jobs_path": "/sdcpp/v1/jobs",
"models_path": "/sdapi/v1/sd-models",
"status_path": "/sdcpp/v1/models/status",
"probe_timeout": 1.5
},
"size": {
"min_area": 589824, // 768x768 — the floor for tool-chosen sizes
"multiple": 8, // align edges to this
"max_side": 1536, // auto-shrinks with VRAM when max_side_auto
"max_side_auto": true,
"hard_max_side": 4096, // engine limits.max_width/height
"explicit_respect": true, // caller-supplied width/height wins over the tiers
"vram_mb_per_mp": 1600 // warning threshold per megapixel
},
"generate": {
"timeout": 900,
"retry": 2,
"poll_interval": 2.0,
"max_count": 8, // engine limits.max_batch_count
"max_download_mb": 64,
"lock": true,
"lock_wait": 900
},
"edit": {
"priority": ["qwen-image-edit", "flux2-klein", "qwen-image"],
"prefer_loaded": true, // keep the loaded model, don't swap for a better rank
"auto_load": true, // load the best editable model when none is loaded
"auto_load_timeout": 240,
"channel": "auto", // img_gen -> img2img fallback
"strength": 0.75, // region edit
"redraw_strength": 0.6, // whole-image redraw
"timeout": 900,
"mask_blur": 8,
"inpaint_padding": 32,
"verify": "agent",
"models_dirs": [], // extra folders to scan when the engine reports none
"load_endpoint": null,
"resolved_channel": null,
"engine_cache_ttl": 20, // seconds; in-process memo
"snapshot_cache_ttl": 120 // seconds; the snapshot survives processes
},
"discover": {
"process_cache_ttl": 20, // reuse the process command lines
"budget_seconds": 20, // hard ceiling for all port probing
"listen_budget_seconds": 4 // ceiling for the netstat leftover sweep
},
"gpu": { "min_free_mb_warn": 500 },
"output": { "dir": "./output" }
}
```
Useful recipes:
```bash
locmodel_image.py config set engine.probe_timeout 3 # slow / remote machine
locmodel_image.py config set generate.max_count 2 # 6 GB VRAM users
locmodel_image.py config set size.max_side_auto false # pin the ceiling yourself
locmodel_image.py config set generate.lock false # single-process only
locmodel_image.py config set edit.prefer_loaded false # always swap to the top-priority model
locmodel_image.py config set edit.priority '["flux2-klein","qwen-image-edit"]'
locmodel_image.py config set edit.models_dirs '["F:/AI-Models"]' # scan when the engine is idle
```
---
## Sizing and VRAM strategy
Two tiers, because the two situations want opposite things: a caller who asked for a specific
canvas should get that canvas, while a tool that picks the canvas itself should stay inside the
smooth band.
**Tier A — the caller supplied `width`/`height` (or `--no-guard` on the CLI).** The numbers are
sent as asked. Only two things can change them:
1. edges are aligned to `size.multiple` (default 8)
2. the engine's own `limits` (64 … 4096 per edge, read live) clamp the result — with a warning,
never silently
Everything else is advice, not action: below `size.min_area` or above the VRAM heuristic you get a
warning in `notes` and the image is rendered anyway.
**Tier B — the tool picked the size (`aspect` preset, or no size at all).** The old guard applies:
1. `width`/`height` (or the aspect preset) is normalised to `size.multiple`
2. total area below `size.min_area` → scaled up proportionally (aspect ratio preserved)
3. longest edge beyond the effective ceiling → scaled down proportionally
4. the effective ceiling is `size.max_side`, lowered automatically from measured VRAM when
`max_side_auto` is on, never above `hard_max_side` (4096, i.e. the engine limit)
The guard exists because the two failure modes produce misleading errors: **too few pixels**
(`generate_image returned no results`) and **too many pixels** (OOM / workspace capacity check,
which on Windows can surface as `ucrtbase.dll 0xc0000409 / BEX64`).
Practical presets:
| VRAM | Suggested | Notes |
| --- | --- | --- |
| 4–6 GB | 768×768, 1024×576, `count`=1 | keep `generate.retry` ≥ 1 |
| 8 GB | 1024×1024, 1280×720, `count`≤2 | the tested sweet spot for turbo models |
| 12 GB+ | 1536×864, 1536×1536 | raise `size.max_side`; explicit sizes are never capped by it |
One more habit that prevents most mysterious failures: **keep exactly one image engine resident**.
Running LocModel plus an LM Studio model plus a second diffusion engine on the same GPU is the
classic cause of `workspace capacity check` crashes. When switching models inside LocModel, restart
LocModel so the previous engine releases VRAM.
---
## How it works
```
┌──────────────┐ MCP stdio ┌──────────────────┐ HTTP ┌──────────────┐
│ AI client │ ────────────► │ locmodel_mcp.py │ ────────► │ │
│ (Claude, │ │ or │ /sdcpp │ sd-server │
│ Cursor, │ ◄──────────── │ locmodel_image.py│ ◄──────── │ (LocModel) │
│ Marvis) │ JSON result └──────────────────┘ polling └──────────────┘
└──────────────┘ │
▼
PNG + _meta.json on disk
```
Job flow: `POST /sdcpp/v1/img_gen` → poll `GET /sdcpp/v1/jobs/<id>` (`queued` → `running` →
`succeeded`) → decode `result.images[].b64_json` (or fetch a loopback-only URL, up to
`max_download_mb`) → write PNG and metadata.
Failures are classified rather than blindly retried:
| Class | Trigger | Action |
| --- | --- | --- |
| `capacity` | `no results`, workspace/allocation errors | retry with the same prompt, shrunk ×0.85 |
| `parameter` | HTTP 400 / invalid argument | no retry — the request itself is wrong |
| `transient` | timeouts, connection resets | retry as-is |
A cross-process lock in `~/.locmodel-image/generate.lock` serialises generations, because two jobs
competing for one GPU is the most common cause of capacity failures. Stale locks from dead PIDs are
reclaimed immediately; locks older than `lock_wait + 600 s` are reclaimed as expired.
---
## Testing
```bash
# Unit tests — no engine required
python -m pytest tests/test_core.py -q
# End-to-end matrix — needs a live engine
python tests/e2e_matrix.py --stage core # status / models / doctor / dry-run
python tests/e2e_matrix.py --stage gen # real generations, aspect presets, batch, guard
python tests/e2e_matrix.py --stage diag # bad arguments, missing engine, config round-trip
python tests/e2e_matrix.py --stage mcp # MCP handshake, framing, tool calls
python tests/e2e_matrix.py --stage all # everything (slow: ~10 min on 8 GB)
```
Each stage writes a JSON report and prints a pass/fail summary. Stages are split so a flaky GPU
session cannot hide which part broke.
---
## Troubleshooting
| Symptom | Likely cause | Fix |
| --- | --- | --- |
| `engine_not_found` (exit 3) | LocModel closed, no model loaded, or unusual port | Start LocModel and load a model → `discover`; or set `LOCMODEL_SD_PORT` |
| `generation_failed` + `no results` | Not enough free VRAM, or two engines competing | Close LM Studio / browser hardware acceleration; one engine only; lower size or `count` |
| Job stuck in `queued` | Engine has a single slot and is busy | Wait; check no orphaned generator process holds the lock |
| Generation > 3 min | Size too large, steps too high, GPU shared | Lower size, restore default steps, free the GPU |
| Prompt seems ignored | CFG too low for a distilled model | Try `--cfg 1.5`; accept that turbo models follow prompts loosely |
| Text renders as gibberish | Diffusion models cannot spell | Do not fight it — overlay text in PPT/PS/Figma |
| MCP client shows no tools | Wrong interpreter, or Windows Store `python` stub | Use the absolute interpreter path; set `PYTHONIOENCODING=utf-8` |
| `workspace capacity check` / `BEX64` crash | Two engines resident; VRAM exhausted | Restart LocModel so a single engine owns the GPU |
Run `doctor` first for anything odd — it reports port source, model, GPU free memory and output
directory writability in one shot.
---
## Compatibility and limitations
- **API surface**: targets the `/sdcpp/v1/*` API of stable-diffusion.cpp servers. It is *not*
Automatic1111-compatible (`/sdapi/v1/txt2img` is not used). Paths are configurable, so a fork
with slightly different routes can be adapted via `config set engine.*_path`.
- **Discovery is heuristic**: on machines where several engines run simultaneously, pin the port
explicitly. Guessing wrong is silent otherwise, which is why `status` always reports *how* the
port was found.
- **No model management**: loading/unloading, model switching and VRAM budgeting remain LocModel's
job. This tool never kills processes or unloads models on your behalf.
- **Not implemented**: standalone upscaling of an existing image. Hires-fix upscaling *during*
generation is supported (`hires_enabled` + `hires_scale` / `hires_upscaler`).
- **Two video sub-panels, never interchangeable**: `generate_video` drives the engine's `vid_gen`
endpoint (text/image-to-video, output webm/webp/avi — no mp4); `lipsync_generate` drives LocModel's
built-in InfiniteTalk + Wan2.1-I2V-14B-480P pipeline (portrait image/video + audio -> mp4), which is
completely isolated from sd-server.
- **Lip-sync hard-depends on weights the user installs in LocModel**: without them the whole task
fails (`lipsync_not_mounted`). There is no fallback — this bridge will not silently switch to
`generate_video` and never goes online. Install via LocModel -> Video -> Lip sync -> one-click
preparation (~24.2 GB of weights plus a ~6 GB CUDA runtime).
- **Mode mutual exclusion**: one engine instance serves one mode at a time, so image generation fails
while a `vid_gen` instance owns the GPU.
- **Text in images**: a limitation of the models, not of the bridge.
---
## What changed in 4.1
- **`ref_edit`, and `ref_images` / `ref_fit` / `ref_max_pixels` on `generate_image`** — the engine's
reference-image channel is now reachable from an agent: pass 1–8 references and a prompt that
names only the change, and identity is anchored on the references instead of re-rendered. Tool
surface: 19.
- **`engine_trace`** — every result now reports what the engine actually built (`fn main(reference_0)`
plus `ref_latents`), so a silent downgrade to text-to-image is visible in the output rather than
discovered by eye.
- **Singular `ref_image` is refused.** The engine drops it without complaint and returns a
text-to-image picture that looks like a successful edit; that trap is now closed client-side.
- **Sizing follows the recipe.** `size_multiple` is read from `/sdcpp/v1/recipes` (16 on
flux-2-klein) instead of a hard-coded 16, `ref_max_pixels` defaults to 1024² with a 4.2 MP ceiling
(the 550k guard was a 3.2-era limit), and `features_by_mode` / `runtime.runtime_recipe` are
honoured when judging support.
- **Workflow surface: 53 keys**, adding `ref_images`, `ref_fit`, `ref_max_pixels`, `cache_mode`,
`cache_option`, `qwen_image_layers`, `scm_mask`, `scm_policy_dynamic`, `control_image`,
`control_strength`, `control_type`; array-valued parameters now validate client-side.
- **CLI**: `--ref-image` (repeatable), `--ref-fit`, `--ref-max-pixels`, plus a `ref-edit`
sub-command.
- Verified here on LocModel 3.6.5 / flux-2-klein-4b: one reference 12.2 s, two references 32.7 s,
no reference 12.6 s with the degradation correctly reported; `pytest tests -q` 150 passed. Video
and lip-sync paths were not exercised (no weights mounted); the new P1 keys are passed through and
feature-gated but were not rendered.
## What changed in 4.0
- **"Generate video" sub-panel added** (Video -> Generate video): `generate_video`, `job_status`,
`cancel_job`, `list_recipes`, talking to `POST /sdcpp/v1/vid_gen`. Asynchronous by default
(`wait_seconds=0` returns a `job_id` right away); `job_status` polls and saves the clip.
- **"Lip sync" sub-panel added** (Video -> Lip sync, its own sub-UI): `lipsync_status` and
`lipsync_generate` drive LocModel's built-in InfiniteTalk + Wan2.1-I2V-14B-480P pipeline. That
pipeline is not an HTTP endpoint at all: its weights and its Python 3.12 venv come from LocModel's
one-click preparation. **Not installed means total failure (`lipsync_not_mounted`), with no
degraded mode.**
- **Sub-panel -> tool mapping is documented**: `SKILL.md` tells the agent to route by what the user
actually asked for, backed by on-machine evidence (LocModel 3.6.1 program strings and the
`lipsync/runner.py` it writes) covering the model list and runtime environment.
- **Embedded `runner.py` copy**: `scripts/lipsync_runner_embed.py` matches what LocModel writes, so
the job still launches on a machine where the GUI never ran the pipeline.
- **Two new CLI entries**: `locmodel_image.py video <status|generate|job|cancel|recipes>` and
`locmodel_image.py lipsync <status|run>`.
## What changed in 3.2
- **Engine capabilities are adopted, not assumed** — `adopt_capabilities()` overwrites the sampler,
scheduler, upscaler, output-format and preview-mode lists from `/sdcpp/v1/capabilities`, reads
`limits` (64 … 4096 per edge, batch 8) and `features`. `locmodel_status` reports all of it.
- **Latent upscalers actually work** — the tool shipped `Latent(nearest)`; the engine spells it
`Latent (nearest)` and rejected the space-less form with `400 invalid generation parameters`.
Fixed, and enum matching is now whitespace-insensitive, so `latent(nearest)` / `JPEG` normalise.
- **Explicit sizes are honoured** — two size tiers (see [Sizing and VRAM
strategy](#sizing-and-vram-strategy)): a caller-supplied `width`/`height` is sent as asked and only
clamped by the engine's own limits, with a warning; tool-chosen sizes keep the area floor and
VRAM-tiered ceiling.
- **Fallback defaults match the engine** — `cfg 1.0 / steps 4 / euler` instead of the old
`7.0 / 20`, and `defaults_by_mode` is read when `defaults` is missing.
- **New parameters** — `hires_scale`, `hires_custom_sigmas`, `output_format`, `output_compression`,
`vae_rel_size_w/h`, `vae_extra_tiling_args` (42 total). JPEG/WebP output works; the extension
follows `output_format`.
- **`guard` / `dry_run` on `generate_image`** — inspect the exact payload and `size_policy` before
spending GPU time.
- **Unsupported knobs are reported** — asking for a parameter the loaded model cannot honour
produces a note instead of a silent no-op.
## What changed in 3.1
- **Workflow parameter channel** — `params` on `generate_image` / `generate --param`, plus
`set_workflow_params` / `get_workflow_params` / `reset_workflow_params`. Values are merged into the
HTTP payload, never into `config.json`, because the LocModel GUI rewrites that file from its own
panel state. Defaults are read live from the engine so the documented numbers cannot drift.
- **`cfg` reachable from MCP** — `generate_image` accepted `steps` and `sampler` before but silently
dropped `cfg`; it is now wired through.
- **Client-side validation** — the engine accepts junk values and hangs the job, so type and range
checks happen before submission.
- **Session scope** — parameters set with `set_workflow_params` apply to every later job in the same
MCP process and vanish on restart; `edit_image` / `redraw_image` honour them too.
## What changed in 3.0
- **`edit_image` / `redraw_image`** — prompt-driven editing on an existing image, without
downloading a dedicated inpainting checkpoint. The engine's existing `init_image` + `mask_image`
path is used; masks are built on the CPU with the standard library only.
- **Mask outside the mask is guaranteed** — results are recomposited on the CPU against the
original, so "keep everything except this area" is a real guarantee rather than a hope.
- **Model priority without silent surprises** — `edit.priority` plus `edit.prefer_loaded` /
`edit.auto_load`, and an explicit error instead of a substitution when the requested model cannot
edit.
- **`list_edit_models` / `load_model`** — see which models can edit, why one was chosen, and load or
switch models without leaving the agent.
- **`annotator/mask_annotator.html`** — a single-file browser mask painter (rect / polygon /
brush) that exports a mask PNG or normalised JSON.
- **Faster probes** — engine discovery is cached (`edit.engine_cache_ttl`), so a closed engine no
longer costs a scan on every call.
- Still **zero dependencies**: standard library only, Python 3.8+.
## What changed in 2.0
See [CHANGELOG.md](docs/CHANGELOG.md) for the full list, and [docs/IMPROVEMENTS.md](docs/IMPROVEMENTS.md) for every hard-coded assumption that had to go before this could run on someone else's machine. Highlights:
- No hard-coded user paths, ports, resolutions, samplers or Python installs
- Area-based size guard with VRAM-aware ceiling and `--aspect` presets
- Failure classification (`capacity` / `parameter` / `transient`) with proportional shrink on retry
- Cross-process generation lock with stale-lock reclamation
- `--dry-run`, `--no-guard`, `--log-file`, `--config`, `config get/set/path`
- Stable exit codes; `expects_error` handling instead of crashes
- MCP: protocol negotiation, dual framing (newline + `Content-Length`), `engine_doctor` /
`engine_discover` tools, `isError` with actionable hints
- Tests: 46 unit tests + staged e2e matrix
---
## License
MIT — see [LICENSE](LICENSE).
TDQS
Scored across 19 tools
Most tools have clearly distinct purposes, and the descriptions actively cross-reference each other (e.g. edit_image vs redraw_image, lipsync_generate vs generate_video, ref_edit vs generate_image). A couple of boundaries are softer: list_models vs list_edit_models vs list_recipes all enumerate models/recipes, and locmodel_status vs engine_doctor are both health/inspection tools, though their scopes are spelled out. ref_edit honestly overlaps generate_image-with-reference, which introduces a minor selection choice.
Many tools follow a clean verb_noun pattern (generate_image, edit_image, load_model, cancel_job, get/set/reset_workflow_params), but ordering is mixed: lipsync_generate and engine_discover are noun-first, while generate_image and edit_image are verb-first, and several are noun_noun status tools (job_status, lipsync_status, engine_doctor). It remains readable but the convention is not uniform across the set.
19 tools is on the heavy side, but the server genuinely spans distinct subsystems (image gen, editing, video jobs, an independent lip-sync pipeline, engine management, workflow params), so most tools earn their place. A few inspection tools (three status tools, three list tools) could theoretically be consolidated, but each targets a different subsystem the descriptions say are not interchangeable.
Coverage is broad and coherent: text-to-image, three distinct editing modes, model listing/loading, engine diagnostics and re-discovery, video generation with job polling/cancel, lip-sync generation with a readiness check, and full set/get/reset of workflow parameters. Minor gaps exist—there's no tool to enumerate or manage all jobs at once (only poll by id), and no output-file cleanup/upscale operations—but core workflows are fully covered.