Metadata-Version: 2.4
Name: kicad-placement-mcp
Version: 0.1.0a2
Summary: Component placement for KiCad PCBs as an MCP server and CLI: measure, place and orient passives, apply through KiCad's IPC API (no pcbnew)
Author: CValdesS
License-Expression: GPL-3.0-or-later
Project-URL: Homepage, https://github.com/cvaldess/kicad-placement-mcp
Project-URL: Issues, https://github.com/cvaldess/kicad-placement-mcp/issues
Keywords: kicad,pcb,placement,mcp,eda,claude
Classifier: Development Status :: 3 - Alpha
Classifier: Intended Audience :: Science/Research
Classifier: Intended Audience :: Manufacturing
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Programming Language :: Python :: 3.14
Classifier: Topic :: Scientific/Engineering :: Electronic Design Automation (EDA)
Requires-Python: >=3.12
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: shapely>=2.0
Requires-Dist: numpy>=1.26
Requires-Dist: pillow>=10
Requires-Dist: mcp<3,>=2.3
Requires-Dist: kicad-python<0.10,>=0.9.2
Provides-Extra: test
Requires-Dist: pytest>=8; extra == "test"
Dynamic: license-file

# kicad-placement-mcp

<!-- mcp-name: io.github.cvaldess/kicad-placement-mcp -->

Component placement for KiCad boards, as an MCP server for Claude (or any MCP client) and as a command-line tool.

You and the assistant decide the structure of the board: zones, anchors (connectors, ICs, anything you place by hand)
and the design intent that the netlist does not show. The tool does the tedious, error-prone part deterministically:

- `measure` a placement: overlaps, copper too close to the edge, pads on vias or tracks of other nets, parts in
  keepout areas, decoupling capacitors whose own pad blocks the route to their pin, IC power pins without a
  decoupling capacitor, isolation barriers, your constraints, and wire length (HPWL);
- `place` the 2-pad passives (R, C, L, D, LED, ferrite beads, fuses) around the anchors, or, in *conservative* mode,
  fix only what is wrong in a placement done by hand;
- `orient` the passives (90° steps) so that their pads face their nets and do not block the routes;
- `apply` the proposal to the board open in KiCad, through the official IPC API, in one undoable commit.

It never edits a `.kicad_pcb` file: it reads it with its own parser and writes only through KiCad.

| BY1515_NUCLEO as it was routed (`measure`) | The same board after `place` |
|---|---|
| [![BY1515_NUCLEO as routed, drawn by measure](https://raw.githubusercontent.com/cvaldess/kicad-placement-mcp/master/docs/images/by1515-as-routed.png)](https://raw.githubusercontent.com/cvaldess/kicad-placement-mcp/master/docs/images/by1515-as-routed.png) | [![BY1515_NUCLEO after place](https://raw.githubusercontent.com/cvaldess/kicad-placement-mcp/master/docs/images/by1515-place.png)](https://raw.githubusercontent.com/cvaldess/kicad-placement-mcp/master/docs/images/by1515-place.png) |

BY1515_NUCLEO is one of the two public test boards in [tests/data](https://github.com/cvaldess/kicad-placement-mcp/tree/master/tests/data). Left: the board as it was routed.
Right: what `place` proposes when its 47 two-pad passives are placed again around the same anchors, with the board's
[rules file](https://github.com/cvaldess/kicad-placement-mcp/blob/master/tests/data/BY1515_NUCLEO/placement/BY1515_NUCLEO.rules.json). Both images are the PNGs the tool writes,
unedited. Red pads are power, grey ground, yellow signal; thin lines are the signal ratsnest; the magenta ring marks
C8, whose own pad blocks the route to the pin it decouples (U3:16). From the tool's summary: blocked decoupling 1 → 0,
missed `near` rules 2 → 0 (C19 next to D3, R11 next to Q4), signal tree 476 → 489 mm (3 % longer). The board is
routed, so `place` warns that the proposal would leave tracks unconnected and `apply` would refuse it: this only
compares placements. Made with 0.1.0a1:

```
cd tests/data/BY1515_NUCLEO
kicad-placement measure BY1515_NUCLEO.kicad_pcb --rules placement/BY1515_NUCLEO.rules.json --png by1515-as-routed.png
kicad-placement place   BY1515_NUCLEO.kicad_pcb --rules placement/BY1515_NUCLEO.rules.json --png by1515-place.png
```

> Status: alpha (0.1.0a2). It has been used on about ten real boards, two of them public (in
> [tests/data](https://github.com/cvaldess/kicad-placement-mcp/tree/master/tests/data)). Expect wrong placements: please [report them](#reporting-a-failure). Read the
> [limitations](#limitations) before trusting a proposal, and always review the result and run DRC before routing.

## Install

Python 3.12 or newer.

```
pip install --pre kicad-placement-mcp
```

This installs both the MCP server (`kicad-placement-mcp`) and the command-line tool (`kicad-placement`). With
[uv](https://docs.astral.sh/uv/) you can skip the install and register `uvx kicad-placement-mcp` instead.

### Register the MCP server

Claude Code:

```
claude mcp add -s user kicad-placement -- kicad-placement-mcp
```

Claude Desktop or other clients (`claude_desktop_config.json` or equivalent):

```json
{
  "mcpServers": {
    "kicad-placement": { "command": "kicad-placement-mcp" }
  }
}
```

If `kicad-placement-mcp` is not on the client's `PATH`, give the full path of the script, or run the module with the
Python of your environment: `"command": "C:\\path\\to\\.venv\\Scripts\\python.exe", "args": ["-m",
"kicad_placement.mcp_server"]`.

`apply` changes the board open in KiCad. It is marked as destructive, so clients ask before running it; in Claude Code
you can make sure with `"permissions": {"ask": ["mcp__kicad-placement__apply"]}` in `~/.claude/settings.json`.

### Docker (MCP Toolkit)

The server is also published as an image. Mount the folder with your boards; the tools see it as `/boards`:

```
docker run -i --rm -v /path/to/boards:/boards ghcr.io/cvaldess/kicad-placement-mcp:latest
```

In Docker Desktop's MCP Toolkit you can add it to a catalog of your own with
`docker mcp catalog create <your-catalog> --server docker://ghcr.io/cvaldess/kicad-placement-mcp:latest`. It is also
listed in the [MCP Registry](https://registry.modelcontextprotocol.io/) as `io.github.cvaldess/kicad-placement-mcp`.

From a container, `apply` cannot reach KiCad's IPC socket on Windows or macOS: use the container for measure, place
and orient, and a local install (above) to apply.

### Enable the KiCad IPC API (only for `apply`)

KiCad 9 or newer. In KiCad: **Preferences → Plugins → Enable KiCad API**, then open the board in the **PCB editor**
(the project manager alone does not serve it). `apply` connects to the default socket; pass `--socket` (CLI) if yours
is different.

## How to use it

Ask the assistant in plain words, for example *"measure my board"*, *"place the passives around U3 and U5"*, *"fix
what is wrong in my placement without moving the rest"* (conservative), *"apply it"*. The tools:

| Tool | What it does | Touches the board? |
|---|---|---|
| `measure` | metrics and problems of the current placement | no |
| `place` | proposes positions for the movable passives (`movable`, or all 2-pad passives that are not anchors); `conservative` keeps what already complies | no |
| `orient` | proposes rotations only | no |
| `apply` | takes the last proposal to the board open in KiCad, in one commit (one Ctrl+Z); does not save | yes |

Every call writes its result next to the board, in `placement/`: `<board>.measure.json`, `<board>.place.json`,
`<board>.orient.json` and a `.png` view of each (overwritten each time). The JSON of `place` says, for each part, where
it was, where it goes, how far it moves and why (*"decoupling of U3:16"*, *"next to the control pin Q2:1"*,
*"stays: already complies where it is"*…), plus a before/after summary.

Before applying, `apply` checks that the open board matches the file that was measured, part by part; afterwards it
reads the pads back and checks that every footprint is unchanged except for its position. If anything differs it
says so and reports `ok: false`. Parts with tracks connected are refused unless you force it (the routing would be
left behind).

### Command line

The same actions, without an assistant:

```
kicad-placement measure board.kicad_pcb --png view.png --json measure.json [--rules rules.json]
kicad-placement place   board.kicad_pcb --json place.json [--movable C1,C2,R5] [--conservative] [--rules rules.json]
kicad-placement orient  board.kicad_pcb --json orient.json [--only C8,C10]
kicad-placement apply   board.kicad_pcb place.json        # to the board open in KiCad
```

On Windows, run Python with `-X utf8` if the console shows mangled characters.

## Rules

The design intent goes in `placement/<board>.rules.json`, next to the board (or `--rules`). Everything is optional.
Distances are in mm, edge to edge (courtyard ∪ pads); in constraints and exemptions, references accept `*` and `?`
wildcards; `"U2:31"` means pad 31 of U2.

```json
{
  "anchors": ["C1", "R5"],
  "movable": ["TP3"],
  "decoupling": {"C8": "U3:16"},
  "decoupling_exempt": ["U10:5"],
  "overlap_exempt": ["J7", "J2–J3"],
  "edge_exempt": ["J1"],
  "edge_mm": 0.5,
  "constraints": [
    {"type": "near", "a": "C19", "b": "D3", "max": 1.5, "source": "input protection next to the connector"},
    {"type": "separate", "a": ["J7:1", "J7:2"], "b": ["*"], "min": 2.0},
    {"type": "outside", "footprint": "J7", "margin": 0.5, "except": ["J1"]},
    {"type": "outside", "rect": [10, 20, 40, 35], "refs": ["C*", "R*"]},
    {"type": "zone", "refs": ["R1?"], "rect": [50, 10, 70, 30]},
    {"type": "edge", "ref": "J1", "edge": "left", "max": 1.0},
    {"type": "side", "ref": "U4", "side": "F"},
    {"type": "barrier", "dru": "barrera_tc", "min": 3.6, "nets": ["/GND_TC", "/+3V3_TC"], "footprints": ["U2"]}
  ]
}
```

| Key | Meaning |
|---|---|
| `anchors` | 2-pad passives that must not move (exact references). Parts locked in KiCad and everything that is not a 2-pad passive are anchors already |
| `movable` | extra parts that may move (exact references: test points, net ties…) |
| `decoupling` | ties a capacitor to a pin; overrides the automatic assignment (also valid on signal nets: RC filters) |
| `decoupling_exempt` | power pins that do not need a capacitor next to them (the datasheet says so: e.g. an eFuse output) |
| `overlap_exempt` | parts, or pairs `"A–B"` / `["A", "B"]`, whose overlaps are intended |
| `edge_exempt`, `edge_mm` | parts allowed past the edge clearance, and the clearance (0.5 mm) |
| `near` | `a` within `max` of `b` |
| `separate` | copper of `a` at least `min` from copper of `b` |
| `outside` | nothing (or `refs`, minus `except`) inside a rectangle `[x0, y0, x1, y1]` or a footprint's courtyard, plus `margin` |
| `zone` | `refs` inside the rectangle |
| `edge` | `ref` within `max` of the board edge `top`/`bottom`/`left`/`right` |
| `side` | `ref` on side `F` or `B` |
| `barrier` | isolation between two domains: the `nets`, `footprints` and net `classes` of one side keep `min` from the rest; `dru` names the `.kicad_dru` rule it comes from |

`source` (or `_note`) is free text for you. If the `.kicad_dru` has an isolation clearance that no `barrier` covers,
`measure` warns and `place` refuses (unless forced): the proposal would not respect it.

Keepout areas drawn in KiCad, vias, tracks and copper zones are read from the board; they need no rules.

## Limitations

- One side: only parts on their current side are placed; parts are not flipped.
- Anchors are not placed: connectors, ICs, crystals and anything not a 2-pad passive stay where you put them.
- Test points need a rule to move.
- Rotations in 90° steps only.
- Notes that depend on routing (keep away from a trace, Kelvin connections, differential pairs) are not modelled.
- Decoupling capacitors are assigned by geometry and pin types; bulk capacitors also by the schematic. Ambiguous cases
  are reported and need a `decoupling` rule.
- No RF awareness.
- A full placement of a 150-part board takes a few minutes.

See [DESIGN.md](https://github.com/cvaldess/kicad-placement-mcp/blob/master/DESIGN.md) for how it works and why.

## How it's built

I develop this tool with Claude Code, Anthropic's AI coding assistant. That is why the repository has a
[CLAUDE.md](https://github.com/cvaldess/kicad-placement-mcp/blob/master/CLAUDE.md): it is the file Claude Code reads at the start of every session, with the project rules (no
`import pcbnew`, never write a `.kicad_pcb`, never relax a tolerance to make a test pass). I review every change before
it goes in.

The placement criteria come from using the tool on my own boards. Each time it got something wrong (a part where I
would not put it, a check that missed a problem, an `apply` that lost the 3D models), the case became a numbered field
failure, F1 to F27 in [DESIGN.md](https://github.com/cvaldess/kicad-placement-mcp/blob/master/DESIGN.md#field-failures), and the fix came with a test: on one of the public boards,
on the private board where it appeared, or on a small board built inside the test that reproduces it.

- 66 tests in [tests/](https://github.com/cvaldess/kicad-placement-mcp/tree/master/tests), run by CI on Linux and Windows with Python 3.12, 3.13 and 3.14.
- 21 more on private boards, which I run on my machine and cannot publish.
- `apply` is tested against a real KiCad: [tests/test_apply.py](https://github.com/cvaldess/kicad-placement-mcp/blob/master/tests/test_apply.py) starts KiCad 10.99 (the KiCad 11
  nightly) without a GUI in Docker, applies moves and rotations through the IPC API, saves the board and reads it back
  pad by pad. CI skips these tests because it does not build the image; I run them locally.

English is not my first language. The tool and its documentation were first written in Spanish; the English version
was written with Claude and reviewed by me. If a sentence is unclear, please say so in an issue.

## Reporting a failure

Any placement you consider wrong is a failure of the tool or of the rules, and the most useful contribution you can
make. Please open an [issue](https://github.com/cvaldess/kicad-placement-mcp/issues/new/choose) with the
**Placement failure** form and include:

- what you asked (tool and options, or the assistant's call) and the version (`pip show kicad-placement-mcp`);
- the part(s) and what is wrong, and where you would put them and why (a datasheet note, a layout rule of thumb);
- the `placement/<board>.<action>.json` and `.png`, and your `rules.json`;
- the board files if you can share them (`.kicad_pcb`, `.kicad_pro`, `.kicad_dru`, `.kicad_sch`). A failure that comes
  with its board becomes a regression test; if the board is private, a minimal board that shows the same failure
  works just as well.

Crashes and wrong measurements go in a **Bug report**. If you use the assistant, ask it to draft the issue: it has the
JSON at hand.

## Development

```
git clone https://github.com/cvaldess/kicad-placement-mcp
cd kicad-placement-mcp
python -m venv .venv
.venv/bin/pip install -e ".[test]"       # Windows: .venv\Scripts\pip
.venv/bin/python -m pytest -q
```

The tests do not need KiCad, except `tests/test_apply.py`, which runs KiCad 11 nightly in Docker and is skipped if the
image is missing:

```
docker build -t kicad-placement-kicad:nightly-YYYYMMDD -f docker/kicad-nightly.Dockerfile docker
docker build -t kicad-placement-test:nightly-YYYYMMDD -f docker/test.Dockerfile docker
```

(Set `IMAGE` in `tests/test_apply.py` to your tag.) Read [CONTRIBUTING.md](https://github.com/cvaldess/kicad-placement-mcp/blob/master/CONTRIBUTING.md) before a pull request:
every change of criterion comes with a test on a real board.

## License

GPL-3.0-or-later, like KiCad. The test boards in [tests/data](https://github.com/cvaldess/kicad-placement-mcp/tree/master/tests/data) are CERN-OHL-S-2.0
([NOTICE](https://github.com/cvaldess/kicad-placement-mcp/blob/master/tests/data/NOTICE.md)).

This project is not affiliated with or endorsed by the KiCad project.
