Skip to main content
Glama
README.md
# Stockflow

**Publication-quality Forrester (stock-and-flow) and causal loop diagrams, built by AI agents.**
MCP server · CLI · live web editor · Claude Code skill — zero dependencies, ~170 kB installed.

[Español](README.es.md) · [License: MIT](LICENSE)

<p align="center"><img src="docs/forrester.svg" alt="Forrester diagram rendered by Stockflow in academic style" width="760"></p>

Stockflow lets an assistant such as Claude turn a system dynamics model into a clean diagram in a single tool call:
stocks, flows, clouds, auxiliaries, constants, exogenous variables, delays, lookup tables, information links with
polarity and R/B feedback loops. It lays everything out automatically, checks the notation, detects the feedback
loops and their polarity, and exports vector PDF/EPS/SVG ready for journals, theses and slides.
It **draws** models; it does not simulate them.

## Features

- **Academic style by default** — monochrome, serif (Times metrics, embedded in the PDF), thin uniform strokes,
  dashed information links, typographic `+`/`−` polarities and classic R/B loop markers. Prints well in grayscale.
- **Figure furniture** — automatic symbol legend (only the symbols in use), `Figure N.` caption and source line,
  in English or Spanish.
- **Exports** — PDF and EPS (vector, for LaTeX/Word), SVG, PNG at 300/600 dpi. A colourful, animated *modern*
  style is available for slides and the web.
- **Automatic layout** — stock chains on a grid, flows and clouds on free sides, parallel pipes, labels that do not
  overlap, polarity signs and loop markers that avoid text, cycle-aware ordering for causal diagrams.
- **Model checks** — feedback loops with computed polarity, plus warnings for notation errors (information links
  into stocks, flows without drivers, constants receiving links, causal links without polarity…).
- **Live editor** — drag nodes and clouds, bend links, create elements with the keyboard, edit captions; it updates
  by itself whenever the agent or the CLI changes the diagram. In academic style the canvas shows the exact page
  that will be exported.
- **Fast and light** — no npm dependencies, CLI start-up ≈ 50 ms, a 30-node diagram renders in ≈ 2 ms.

<p align="center"><img src="docs/causal.svg" alt="Causal loop diagram" width="420"></p>

## Install and connect your agents

Stockflow is **one local program**. Every AI agent talks to it the same way: the agent launches
`stockflow mcp` (an MCP server over stdio) and gets the `diagram_*` tools; all agents share the same diagrams and the
same live editor. The skill (`SKILL.md`) is the "how to model and draw" guide for agents that support skills; agents
without skills get the same guidance from the MCP server's instructions and tool descriptions.

Requires **Node.js ≥ 20**. PDF/EPS/PNG export uses `rsvg-convert` (librsvg: `pacman -S librsvg`,
`apt install librsvg2-bin`, `brew install librsvg`); SVG export needs nothing else.

**1. Install once** (~200 kB, downloads a tarball, no git history):

```bash
curl -fsSL https://raw.githubusercontent.com/Jairodaniel-17/stockflow/master/install.sh | sh
```

The installer puts the `stockflow` CLI in `~/.local/bin` and runs `stockflow connect`, which detects your agents and
registers the MCP server (and the skill, where supported) in each of them.

**2. Connect agents** (any time, e.g. after installing a new agent):

```bash
stockflow connect                 # every agent detected on this machine
stockflow connect codex cursor    # only these
stockflow connect --list          # status
stockflow disconnect              # undo (a .bak-stockflow copy of each config is kept)
```

| Agent | What `stockflow connect` writes | Skill |
|---|---|---|
| Claude Code | `claude mcp add -s user stockflow …` (also extra `~/.claude-*` profiles) | `~/.claude/skills/` |
| Codex CLI | `[mcp_servers.stockflow]` in `~/.codex/config.toml` (every `CODEX_HOME`) | `~/.codex/skills/` |
| Claude Desktop | `mcpServers` in `claude_desktop_config.json` | — |
| Cursor | `mcpServers` in `~/.cursor/mcp.json` | — |
| VS Code (Copilot) | `servers` in the user `mcp.json` | — |
| Gemini CLI | `mcpServers` in `~/.gemini/settings.json` | — |
| Windsurf | `mcpServers` in `~/.codeium/windsurf/mcp_config.json` | — |
| OpenCode | `mcp` in `~/.config/opencode/opencode.json` | — |

Absolute paths are used for `node` and the script, so desktop apps that do not inherit your shell `PATH` work too.
Restart the agent after connecting.

**Any other MCP client**: point it to the stdio command printed by `stockflow connect --list`:

```json
{ "mcpServers": { "stockflow": { "command": "node", "args": ["/path/to/stockflow/bin/stockflow.js", "mcp"] } } }
```

**Agents without MCP** but with a shell can use the CLI directly (`stockflow create model.json`, `stockflow render …`).

**Alternatives**: as a Claude Code plugin (skill + MCP bundled) —
`claude plugin marketplace add Jairodaniel-17/stockflow && claude plugin install stockflow@stockflow` — or with npm —
`npm install -g github:Jairodaniel-17/stockflow && stockflow connect`.

## Using it from an AI agent

Ask for the diagram in plain language, e.g. *"Draw the Forrester diagram of an SIR epidemic for my paper, in
English, as Figure 2"*. The agent will typically call:

| Tool | Purpose |
|---|---|
| `diagram_create` | Create a whole diagram (nodes, links, loops, caption…) in one call; positions are optional |
| `diagram_update` | Add or change elements by id, remove ids, re-layout |
| `diagram_analyze` | Feedback loops (R/B) and notation warnings |
| `diagram_render` | Export SVG/PDF/EPS/PNG; `preview: true` also returns a PNG so the agent can see and fix the drawing |
| `diagram_open` | Open the live editor |
| `diagram_get` / `diagram_list` / `diagram_delete` / `diagram_layout` | Housekeeping |

Example input:

```json
{
  "title": "SIR epidemic", "type": "forrester", "auto_loops": true, "legend": true, "lang": "en",
  "figure": "2", "caption": "Stock-and-flow structure of the SIR model.", "source": "Adapted from Sterman (2000).",
  "nodes": [
    {"id": "s", "type": "stock", "label": "Susceptible"},
    {"id": "i", "type": "stock", "label": "Infected"},
    {"id": "r", "type": "stock", "label": "Recovered"},
    {"id": "infection", "type": "flow", "label": "Infection", "from": "s", "to": "i"},
    {"id": "recovery", "type": "flow", "label": "Recovery", "from": "i", "to": "r"},
    {"id": "contact", "type": "constant", "label": "Contact rate"},
    {"id": "duration", "type": "constant", "label": "Illness duration"}
  ],
  "links": [
    {"from": "s", "to": "infection", "polarity": "+"}, {"from": "i", "to": "infection", "polarity": "+"},
    {"from": "contact", "to": "infection", "polarity": "+"},
    {"from": "i", "to": "recovery", "polarity": "+"}, {"from": "duration", "to": "recovery", "polarity": "-"}
  ]
}
```

## Notation

| Symbol | `type` | Notes |
|---|---|---|
| Stock / level (rectangle) | `stock` | Changes only through flows |
| Flow / rate (valve on a double pipe) | `flow` | `from` / `to`: stock id or `null` for a cloud (source/sink) |
| Auxiliary (circle) | `aux` | |
| Constant / parameter (line with small circle) | `constant` | Receives no links |
| Exogenous variable (double circle) | `exogenous` | Receives no links |
| Delay (box with three cells) | `delay` | |
| Lookup table | `table` | |
| Cloud | `cloud` | Usually implicit through `from: null` / `to: null` |
| Causal variable (text) | `variable` | For `type: "causal"` diagrams |

Links: `polarity` `+`/`-`, `curve` (-1.5…1.5), `delay: true` (‖ mark), `style` `solid`/`dashed`, `label`.
Loops: `type` `R`/`B`, `nodes` (marker is centred automatically), `label`, `dir` `cw`/`ccw`.
`shadow: true` draws a shadow variable as `<Name>`. Spanish type names are accepted too (nivel, flujo, auxiliar…).

## CLI

```bash
stockflow create examples/cacao.json --loops      # full diagram from JSON
stockflow new store --title "Inventory"           # or step by step
stockflow add store stock Inventory
stockflow add store flow Sales --from inventory
stockflow add store aux Demand
stockflow link store demand sales +
stockflow analyze store
stockflow meta store --figure 2 --caption "Inventory structure." --source "Own elaboration" --lang en --legend
stockflow render store -o store.pdf               # .eps .svg .png (--dpi 300) --style modern --theme dark
stockflow open store                              # live editor at http://localhost:4610
stockflow mcp                                     # MCP server on stdio
```

Diagrams are JSON files in `~/.local/share/stockflow/diagrams` (`STOCKFLOW_HOME` changes it, `STOCKFLOW_PORT` the editor port).

## Editor shortcuts

`S` stock · `W` flow (click source then target; empty space = cloud) · `X` auxiliary · `C` constant ·
`E` exogenous · `D` delay · `B` table · `Q` causal variable · `K` link · `O` loop marker · `V` select ·
`L` auto-layout · `F` fit · `A` loops and warnings · `G` figure panel · `T` theme · `M` animation ·
`Ctrl+Z` / `Ctrl+Shift+Z` undo/redo · arrows nudge · drag a link to bend it.

<p align="center"><img src="docs/editor.png" alt="Stockflow live editor" width="900"></p>

## License

[MIT](LICENSE) © Jairo Mendoza