Skip to main content
Glama
README.md
# chainner-agent

**Let AI coding agents build chaiNNer workflows for you.**

`chainner-agent` lets agents such as Claude Code and Codex **create, inspect, edit,
validate and run [chaiNNer](https://github.com/chaiNNer-org/chaiNNer) chains** (`.chn`
files) without touching chaiNNer's graph editor. You describe what you want:

> "Upscale the game textures in this folder, keep transparency, compare three methods
> and save the results."

The agent finds the right nodes, builds the chain, validates it with chaiNNer's own type
checks, runs it with your installed chaiNNer, checks the output, and hands you a `.chn`
file that opens in chaiNNer like any chain you made yourself.

It comes as:

- **`chn`**, a command line tool (JSON in, JSON out), usable by any agent with a shell;
- **`chn-mcp`**, an MCP server exposing the same operations as tools;
- **a skill** for Claude Code and Codex that teaches agents how to use them well.

It is not a fork of chaiNNer and does not modify your chaiNNer installation.

![An agent-built chain opened in chaiNNer](docs/evidence/gui-compare-upscalers.png)

## Contents

- [Requirements](#requirements)
- [1. Install the tool](#1-install-the-tool)
- [2. Connect your agent (MCP)](#2-connect-your-agent-mcp)
- [3. Install the skill](#3-install-the-skill)
- [Try it](#try-it)
- [Command line reference](#command-line-reference)
- [How it works](#how-it-works)
- [Updating chaiNNer](#updating-chainner)
- [Development and tests](#development-and-tests)
- [Limitations](#limitations)
- [License](#license)

## Requirements

- **Windows** with **[chaiNNer](https://chainner.app/)** installed and started at least
  once, so that it has set up its integrated Python. Tested with chaiNNer 0.26.1.
- **[Node.js](https://nodejs.org/) 20 or newer**
- **Git**

Other platforms are untested; see [Limitations](#limitations).

## 1. Install the tool

```powershell
git clone https://github.com/MirkoSon/chainner-agent.git
cd chainner-agent
npm install
```

`npm install` detects your installed chaiNNer version, downloads the matching chaiNNer
source tag into `vendor/` (read-only, git-ignored) and builds the tool. Nothing is
installed globally.

Check that it found your chaiNNer:

```powershell
node bin/chn.js doctor
```

Optional: `npm link` puts the `chn` and `chn-mcp` commands on your PATH.

In the commands below, replace `C:/path/to/chainner-agent` with the folder you cloned
into. Use forward slashes or escaped backslashes in JSON and TOML files.

## 2. Connect your agent (MCP)

The MCP server is a local stdio process that your agent starts on demand. There is no
background service, port or account.

### Claude Code

```powershell
claude mcp add --scope user chainner -- node C:/path/to/chainner-agent/bin/chn-mcp.js
```

`--scope user` makes it available in all your projects. Check it with
`claude mcp get chainner`; it should say *Connected*.

### Codex

```powershell
codex mcp add chainner -- node C:/path/to/chainner-agent/bin/chn-mcp.js
```

Codex asks for approval before each MCP tool call. To let the read-only tools run without
asking (recommended), add this to `~/.codex/config.toml` below the `[mcp_servers.chainner]`
section. Editing and running chains will still ask.

```toml
[mcp_servers.chainner.tools.chainner_search_nodes]
approval_mode = "approve"

[mcp_servers.chainner.tools.chainner_describe_node]
approval_mode = "approve"

[mcp_servers.chainner.tools.chainner_inspect_chain]
approval_mode = "approve"

[mcp_servers.chainner.tools.chainner_validate_chain]
approval_mode = "approve"
```

### Other MCP clients

```json
{
  "mcpServers": {
    "chainner": {
      "command": "node",
      "args": ["C:/path/to/chainner-agent/bin/chn-mcp.js"]
    }
  }
}
```

### The tools

| Tool | What it does |
| --- | --- |
| `chainner_search_nodes` | Search the node types your chaiNNer offers |
| `chainner_describe_node` | Inputs, outputs, allowed values and types of one node type |
| `chainner_edit_chain` | Create or change a chain (add, set, connect, disconnect, remove, layout), atomically |
| `chainner_inspect_chain` | Nodes, values, connections, inferred output types, validation |
| `chainner_validate_chain` | chaiNNer's own validity and type checks |
| `chainner_run_chain` | Execute the chain with your installed chaiNNer |

The MCP server is optional: an agent with a shell can do everything with the `chn` CLI.

## 3. Install the skill

The skill in [`skill/chainner/`](skill/chainner/SKILL.md) gives agents the working
knowledge they would otherwise learn by trial and error: describe every node before using
it, build in one atomic edit, read the validation result, the rules that make a chain run
(it needs an output node, batches use the folder iterator), how values are written, and
what to do when an AI model package is missing. It loads only when a task mentions
chaiNNer or `.chn` files.

Link it into the skill folders so both agents share the one copy in this repository and
it stays up to date with `git pull`. From the `chainner-agent` folder, in PowerShell:

```powershell
# Claude Code
New-Item -ItemType Junction -Path "$HOME\.claude\skills\chainner" -Target "$PWD\skill\chainner"
# Codex
New-Item -ItemType Junction -Path "$HOME\.codex\skills\chainner" -Target "$PWD\skill\chainner"
```

On macOS or Linux, use `ln -s "$PWD/skill/chainner" ~/.claude/skills/chainner` (and the
same for `~/.codex/skills`).

Start a new agent session afterwards so it picks up the MCP server and the skill.

## Try it

Ask your agent, in any folder with some images:

> Build a chaiNNer workflow that resizes every PNG in `./textures` to 50%, keeps
> transparency, saves them as WebP into `./out`, and run it.

Or look at the worked examples:

- [examples/texture-compare](examples/texture-compare/README.md): from a request to a
  finished batch chain that compares three resize methods on a folder of textures.
- [examples/e2e](examples/e2e): Load Image, Resize, Save Image, then modified and run
  again. Regenerated by `npm run e2e`.

## Command line reference

Every command prints JSON. Errors are printed as `{"error": "..."}` on stderr with a
non-zero exit code, and say what is allowed instead.

```text
chn nodes [words...] [--category <id>] [--refresh]   search node types
chn categories                                       list node categories
chn node <type>                                      describe one node type

chn new <file.chn> [--force]                         create an empty chain
chn edit <file> <ops.json | -> [--create] [--overwrite]   apply many operations at once
chn add <file> <type> [--name <n>] [--set <input>=<value>]... [--at <x>,<y>]
chn set <file> <node> [<input>=<value>]... [--name <n>] [--disable | --enable]
chn connect <file> <node>[:<output>] <node>:<input>
chn disconnect <file> <node>:<input>
chn rm <file> <node>
chn layout <file>

chn inspect <file>                                   nodes, values, connections, types, validation
chn validate <file>                                  exit code 1 if invalid
chn run <file> [--app]                               execute; --app uses "chaiNNer.exe run"
chn doctor                                           show the detected chaiNNer installation
```

An `ops.json` for `chn edit` looks like this:

```json
[
  { "op": "add", "type": "chainner:image:load", "as": "load", "set": { "Image File": "C:/work/in.png" } },
  { "op": "add", "type": "chainner:image:resize", "as": "resize",
    "set": { "Image Resize Mode": "Absolute", "Width": 512, "Height": 512 } },
  { "op": "add", "type": "chainner:image:save", "as": "save",
    "set": { "Directory": "C:/work/out", "Image Name": "result", "Image Format": "PNG" } },
  { "op": "connect", "from": "load:Image", "to": "resize:Image" },
  { "op": "connect", "from": "resize:Image", "to": "save:Image" },
  { "op": "layout" }
]
```

The full agent guide is [docs/AGENT_GUIDE.md](docs/AGENT_GUIDE.md).

## How it works

chaiNNer has no plugin or external API for editing graphs, and its own CLI mode can only
*run* an existing `.chn` file. But the code that defines what a valid chain is lives in
plain TypeScript modules of chaiNNer that do not depend on Electron. chainner-agent
reuses those modules unmodified instead of re-implementing them:

| Need | chaiNNer code reused |
| --- | --- |
| Read and write `.chn` files (version, checksum, migrations) | `SaveFile`, `migrations` |
| Node definitions (inputs, outputs, types) | your installed backend's `GET /nodes`, `SchemaMap`, `parseFunctionDefinitions` |
| Create a node with its defaults | `createNode`, the GUI's node factory |
| Check a connection (cycles, types, iterators) | `canConnect` |
| Check the chain | `TypeState`, `ChainLineage`, `checkNodeValidity`, as chaiNNer's CLI does |
| Decide what runs | `optimizeChain`, `toBackendJson` |
| Execute | your installed backend (`POST /run`), or `chaiNNer.exe run` |

[`src/upstream.ts`](src/upstream.ts) is the only file that imports chaiNNer code. What
this project adds is small: references by name instead of UUID, checking values against
the node definitions, the CLI and the MCP adapter.

Node definitions always come from **your** chaiNNer, including the packages you have
installed in it, and are cached per chaiNNer version in `.cache/`. Run
`chn nodes --refresh` after installing a package (PyTorch, NCNN, ONNX) in chaiNNer.

### What happens to your chaiNNer installation

chainner-agent itself writes nothing to it. It reads chaiNNer's `settings.json`, starts
chaiNNer's own Python with chaiNNer's own backend (bytecode writing disabled, logs and
storage redirected into this folder's `.cache/`), and for `chn run --app` starts
`chaiNNer.exe run <file>`.

One side effect belongs to chaiNNer itself: whenever its backend starts, chaiNNer checks
its Python packages and installs or updates missing ones in its own environment, exactly
as it does when you open chaiNNer. On a set-up installation this does nothing.

chaiNNer's CLI mode leaves its Python worker running after it exits. After `chn run --app`
those leftovers are stopped, and only processes that were observed being started by the
chaiNNer process the tool launched.

## Updating chaiNNer

After you update chaiNNer, run this in the chainner-agent folder:

```powershell
npm run setup
```

It fetches the source of the new version and rebuilds. `chn doctor` shows whether the
installed version and the reused source match. Files saved by a newer chaiNNer than the
tool was built for are refused instead of being edited with outdated rules.

chainner-agent depends on about fifteen internal exports of chaiNNer's source (listed in
`src/upstream.ts`) and on the backend's `/nodes` and `/run` endpoints. These are not a
stable public API. If a chaiNNer release changes them, `npm run setup` fails with the
missing import, and `src/upstream.ts` needs a matching update.

## Development and tests

```powershell
npm test            # unit tests on real node definitions and chaiNNer's sample chains (no chaiNNer needed)
npm run typecheck
npm run e2e         # end to end against your installed chaiNNer, about 2 minutes
node scripts/mcp-smoke.mjs                # MCP server over stdio against your installed chaiNNer
powershell -File scripts/gui-snapshot.ps1 -Chain <file.chn> -Out shot.png   # open in the GUI and capture it
```

`npm run e2e` builds Load Image, Resize, Save Image through the CLI, validates it, runs it
on the backend and with `chaiNNer.exe run`, checks the output image, modifies the chain,
runs it again and checks the failure reporting. Project status and the evidence for the
current release are in [docs/STATUS.md](docs/STATUS.md).

## Limitations

- Tested on Windows 11 with chaiNNer 0.26.1 only. On other platforms set
  `CHAINNER_EXE`, `CHAINNER_BACKEND` (chaiNNer's `run.py`) and `CHAINNER_PYTHON`.
- AI upscaling nodes (PyTorch, NCNN, ONNX) use the same code path as every other node, but
  have not been tested end to end with a real model.
- Each `chn run` starts and stops chaiNNer's backend, adding roughly 10 to 20 seconds.
- `chn run --app` only reports an exit code and chaiNNer's log lines, because chaiNNer's
  CLI mode prints nothing to the console. The default engine reports the failing node.
- chaiNNer may show its "unsaved changes" marker shortly after opening an agent-built
  chain. No content is lost.
- Node placement is a simple left-to-right layout.

## License

[GPL-3.0](LICENSE). The built tool bundles GPL-3.0 code from
[chaiNNer](https://github.com/chaiNNer-org/chaiNNer), which is fetched at install time
and is not part of this repository. `test/fixtures/nodes-0.26.1.json` is the unmodified
node list returned by chaiNNer 0.26.1.

This is an independent project, not affiliated with or endorsed by the chaiNNer project.