Commit Graph

143 Commits

Author SHA1 Message Date
mixelpixx
5b380c0f17 Merge pull request #66 from Mehanik/feature/schematic-component-field-inspection
feat: schematic component field inspection and label repositioning
2026-03-16 21:33:51 -04:00
mixelpixx
fbbe5a408d Merge pull request #67 from Mehanik/fix/delete-schematic-component-multiline
fix: delete_schematic_component fails on multi-line KiCAD format
2026-03-16 20:14:44 -04:00
Eugene Mikhantyev
f562035f79 test: add schematic tools tests and apply black formatting
- Add python/tests/conftest.py with MagicMock stubs for pcbnew/skip
- Add python/tests/test_schematic_tools.py with 29 tests covering
  WireManager.delete_wire, WireManager.delete_label (unit + integration),
  and parameter validation for all 11 new _handle_* methods
- Apply black formatting to component_schematic.py, wire_manager.py,
  and kicad_interface.py

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-03-15 22:48:24 +00:00
Eugene Mikhantyev
22a7561f5c style: apply Black formatting to changed files
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-03-15 22:33:50 +00:00
Eugene Mikhantyev
da359513bf fix: delete_schematic_component fails on multi-line KiCAD format
The handler used a line-by-line regex requiring (symbol and (lib_id on
the same line, but KiCAD's file writer places them on separate lines.
Replace with the content-string approach (already used by edit handler)
so \s+ matches across newlines. Add regression tests covering inline,
multi-line, and power symbol (#PWR) cases.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-03-15 21:36:39 +00:00
Eugene Mikhantyev
ccd817531a feat: add get_schematic_component tool and fieldPositions to edit_schematic_component
- New `get_schematic_component` MCP tool returns component position and
  all field values with their label (at x/y/angle) positions
- Extends `edit_schematic_component` with optional `fieldPositions` dict
  so callers can reposition Reference/Value/etc. labels in one call
- Adds 18 tests (6 unit, 12 integration) covering parsing, round-trips,
  and edge cases

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-03-15 15:37:52 +00:00
Eugene Mikhantyev
4dc351660c fix: apply sexpdata round-trip write to delete_schematic_net_label
delete_schematic_net_label was using kicad-skip's write() which silently
discards in-memory _elements mutations. Replaced with WireManager.delete_label()
that uses the same sexpdata round-trip approach already used by delete_schematic_wire.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-03-13 01:08:49 +00:00
Eugene Mikhantyev
9d9234abd0 fix: replace kicad-skip mutation with sexpdata in delete_schematic_wire
kicad-skip's write() serializes from original parsed data, silently
discarding in-memory _elements mutations. Switch to the same sexpdata
approach used by add_wire: parse, find matching wire, delete the entry,
write back. Also adds WireManager.delete_wire() static method.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-03-13 00:39:23 +00:00
Eugene Mikhantyev
2956b4fa9d fix: use _elements.remove() for kicad-skip collections lacking remove method
ElementCollection in kicad-skip doesn't implement remove(), causing
'WireCollection object has no attribute remove' errors. Access the
underlying _elements list directly for wire, label, and symbol deletion.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-13 00:27:17 +00:00
Eugene Mikhantyev
ffe51f0f16 fix: address review issues in schematic tools
- Remove unused imports in move_schematic_component
- Add warning log when mirror attribute missing on rotate
- Move `import re` out of loop in annotate_schematic
- Include global_label in list_schematic_nets
- Fix SVG export to use directory for kicad-cli --output flag
- Return actual SVG data in get_schematic_view TS handler
- Add isError: true to all failure responses in new tools
- Add connect_passthrough to schematic category in registry
- Simplify power symbol filtering control flow in list_labels
- Fix indentation in list_schematic_labels power section

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-13 00:23:26 +00:00
Eugene Mikhantyev
72e5320044 feat: add get_schematic_view tool for rasterized schematic images
Exports schematic to SVG via kicad-cli, then converts to PNG using
cairosvg (same approach as get_board_2d_view). Falls back gracefully
to SVG if cairosvg is not installed. Supports configurable output
size and format (png/svg).

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-12 23:57:39 +00:00
Eugene Mikhantyev
b2a547e624 feat: add 11 missing schematic inspection & editing tools
Add all missing schematic operations identified in the gap analysis:

Inspection (P0/P2):
- list_schematic_components: enumerate all components with refs, values, pins
- list_schematic_nets: list all nets with their connections
- list_schematic_wires: list all wire geometry
- list_schematic_labels: list net labels, global labels, power flags

Editing (P0/P1):
- annotate_schematic: assign ref designators to unannotated components (R? → R1)
- move_schematic_component: reposition placed symbols
- rotate_schematic_component: rotate/mirror placed symbols
- delete_schematic_wire: remove wires by coordinates
- delete_schematic_net_label: remove labels by name/position

Export (P1):
- export_schematic_svg: schematic SVG export via kicad-cli
- export_schematic_pdf: enhanced with file path return & blackAndWhite support

Also registers list_schematic_components and annotate_schematic as
direct tools (always visible) since they're prerequisites for
reference-based workflows.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-12 23:52:11 +00:00
mixelpixx
914ef92c0f Merge pull request #58 from shuofengzhang/fix/xdg-relative-path-handling
Handle relative XDG config/cache paths on Linux
2026-03-11 13:45:37 -04:00
Tom
3d9497ebe5 fix: snapshot saves logs+prompt to logs/ subdir; routing via_x under start pad; router disabled 2026-03-11 11:20:05 +01:00
shuofengzhang
5678a5ee6c Handle relative XDG paths in platform helper 2026-03-10 04:02:38 +08:00
Tom
3e329cbf6b fix: route_pad_to_pad auto-inserts via when pads are on different copper layers
- Detects F.Cu<->B.Cu layer mismatch in route_pad_to_pad
- Splits route into: trace on start layer + via at midpoint + trace on end layer
- route_trace description warns: use route_pad_to_pad for cross-layer routing
- route_pad_to_pad description highlights automatic via insertion
2026-03-07 15:46:54 +01:00
Tom
2e36f96c55 fix: board outline position and rounded corners (3 bugs)
Bug 1 — Rounded corners missing (src/tools/board.ts schema)
  Symptom: Claude sends shape='rectangle' + radius=2.5, but the Zod schema
  only listed 'rectangle', 'circle', 'polygon' as valid shapes. The JS
  handler never passed cornerRadius through the IPC path, and Python only
  generated arcs for shape='rounded_rectangle'. Result: 4 straight lines,
  no arcs.
  Fix: Added 'rounded_rectangle' and 'cornerRadius' to the Zod schema in
  src/tools/board.ts so Claude can send the correct shape directly.
  Also added a Python-side auto-upgrade: if shape='rectangle' and
  cornerRadius>0, silently promote to 'rounded_rectangle' so legacy
  callers still work (python/commands/board/outline.py).

Bug 2 — Board outline placed at (-w/2, -h/2) instead of (0, 0)
  Symptom: A 30x30 mm board outline was placed centred at the origin
  (start -15 -15) ... (end 15 15) instead of the expected top-left at
  (0,0) → (30,30).
  Root cause: src/tools/board.ts extracted x/y from params and renamed
  them to centerX/centerY before forwarding to Python:
      const { x, y, ...otherParams } = params;
      callKicadScript('add_board_outline', { centerX: x, centerY: y, ...otherParams })
  Python received centerX=0, centerY=0 and used that as the board centre,
  placing the outline at (-15,-15)→(+15,+15).
  Fix: Pass x/y directly as top-left corner coordinates. Python already
  contains the correct logic: center = x + width/2, y + height/2. Removed
  the incorrect rename in board.ts:
      callKicadScript('add_board_outline', { shape, ...params })

Bug 3 — print() on stdout corrupted JSON-RPC protocol → Timeout
  Symptom: Claude Desktop reported 'Timeout' for add_board_outline even
  though Python completed the command in <5 ms. KiCAD was falsely launched.
  Root cause: Debug print() statements written to stdout (the same channel
  used for JSON-RPC responses) injected non-JSON lines into the protocol.
  Node.js could not parse the response → timeout → Claude Desktop invented
  a 'KiCAD UI required' workaround.
  Fix: Removed all print() calls from outline.py. Logger.info/debug writes
  to the log file (~/.kicad-mcp/logs/kicad_interface.log) and is safe.

Files changed:
  src/tools/board.ts               — Zod schema + removed centerX/centerY rename
  python/commands/board/outline.py — auto-upgrade rectangle+radius, remove print()
2026-03-07 14:52:45 +01:00
Tom
91fe6a379c feat: dev mode — auto-copy MCP session log to project dir on export_gerber (KICAD_MCP_DEV=1) 2026-03-07 12:45:52 +01:00
Tom
8e4e3176c2 fix: register sync_schematic_to_board + snapshot_project as MCP tools; auto-save board after SWIG mutations; boardPath reload in place_component; add_schematic_connection warns about connect_passthrough; via.GetWidth(F_Cu) for KiCAD 9.0 2026-03-07 11:27:41 +01:00
Tom
0f1bfa3eff fix: search_tools now includes direct tools (snapshot_project etc.)
- searchTools() previously only searched routed tool categories, so
  direct tools like snapshot_project, save_project, create_project
  returned 0 results when Claude called search_tools('snapshot')
- Direct tools are now searched first, with a hint that they must be
  called directly (not via execute_tool)

Also includes routing.py zone-inset fix for rounded board corners
(already staged from previous session)
2026-03-07 09:56:08 +01:00
Tom
b6e1b8b7b1 fix: propagate board reload to project_commands after SVG logo import
save_project uses project_commands.board, which was NOT in the propagation
list after pcbnew.LoadBoard() in _handle_import_svg_logo. As a result,
save_project() wrote the old in-memory board (without logo gr_poly entries)
back to disk, erasing the logo every time.

Fix: call _update_command_handlers() after reload, which updates all
command handler board references including project_commands.
2026-03-07 01:50:03 +01:00
Tom
44c5e7f388 fix: unwrap nested params in add_board_outline (SWIG + IPC paths)
Claude sends {shape:'rectangle', params:{x,y,width,height,...}} but handlers
were reading params.get('width') on the outer dict → None → wrong board size.

outline.py: extract inner = params.get('params', params) at method start,
read all dimensions from inner dict.

kicad_interface.py (_ipc_add_board_outline): delegate 'rectangle' to SWIG
path (same as 'rounded_rectangle') so Claude's shape+dims call is handled
correctly. For polygon IPC path: also unwrap inner params for points/width.
2026-03-07 01:43:43 +01:00
Tom
f4d516f5c3 fix: Flip() after board.Add() to avoid 30s hang in KiCAD 9
FootprintLoad+Flip() on standalone footprint (not yet in board) causes
a 30-second block in KiCAD 9 pcbnew. Solution: add footprint to board
first, then call Flip() with board context available.

Fixes B.Cu placement timeout that killed the Python process.
2026-03-07 00:59:32 +01:00
Tom
f3078b7a02 fix: B.Cu footprint Flip() in SWIG path + J2-on-B.Cu prompt note + pin-1-at-top schema hint 2026-03-06 20:51:52 +01:00
Tom
b43c63edca feat: add snapshot_project tool for step checkpoints (resume from Step2 without full redo) 2026-03-06 20:42:42 +01:00
Tom
ae66001886 fix: reload board into pcbnew after SVG logo import (prevents logo loss on next Save) 2026-03-06 20:38:16 +01:00
Tom
8e92e144b3 fix: board outline 3 bugs (cornerRadius fallback, top-left origin default, IPC rounded_rect arc delegation) 2026-03-06 20:33:25 +01:00
Tom
ec39905702 feat: add sync_schematic_to_board + fix refill_zones subprocess isolation 2026-03-06 20:05:30 +01:00
Tom
373ab35a10 fix: svg_import tokenizer preserves command/number order (fixes V-command crash) 2026-03-06 18:34:27 +01:00
Tom
0688626781 fix: inject_symbol_into_lib_symbols handles sexpdata single-line compaction 2026-03-06 18:10:20 +01:00
Tom
bed65a6da2 fix: true -> True in tool_schemas.py (Python NameError) 2026-03-06 17:32:21 +01:00
Tom
dcecde8d2b fix: remove duplicate comma in tool_schemas.py (SyntaxError) 2026-03-06 17:22:09 +01:00
Tom
93ae23dacc fix: prevent header corruption when sexpdata compacts schematic to single line
create_component_instance used line-based insertion which placed new
symbols BEFORE (kicad_sch ...) header when the file was written as
a single line by sexpdata.dumps(). Switch to rfind()-based string
insertion which is format-independent.

Also remove StreamHandler(sys.stdout) from logging — Python logs now
go only to file (~/.kicad-mcp/logs/kicad_interface.log) to avoid
polluting MCP stderr with INFO/DEBUG entries shown as [error].
2026-03-06 13:17:25 +01:00
Tom
e7a8234ec4 fix: list_library_footprints accepts library_name as alias for library param 2026-03-06 12:45:54 +01:00
Tom
410d02ad8b feat: add import_svg_logo tool — converts SVG to PCB silkscreen polygons 2026-03-06 12:41:11 +01:00
Tom
23946a211a feat: add run_erc tool for schematic electrical rules checking 2026-03-06 12:32:35 +01:00
Tom
11d7e49e74 feat: add connect_passthrough tool for FFC/ribbon cable pin-to-pin wiring 2026-03-06 12:25:25 +01:00
Tom
4c12096886 fix: redirect Python logging from stderr to stdout (prevents false [ERROR] labels in MCP log) 2026-03-06 11:49:31 +01:00
Tom
fec537950f fix: PinLocator resolves pin names (e.g. VCC1, SDA) in addition to pin numbers 2026-03-06 11:38:54 +01:00
Tom
1d390f4fed fix: schematic pin connection reliability
- add_schematic_net_label: warn in description that coords must be exact pin endpoints; recommend connect_to_net instead
- connect_to_net: stub wire direction now follows pin angle (was hardcoded +X)
- pin_locator.py: add get_pin_angle() and _get_lib_id() helpers
- new tool: get_schematic_pin_locations(schematicPath, reference) → returns exact x/y of every pin endpoint, so Claude can place labels correctly
2026-03-06 11:29:52 +01:00
Tom
68fd4509b9 chore: update changelog and schematic template 2026-03-06 09:59:54 +01:00
Tom
2340afe9a4 fix(dynamic-loader): inline extends symbols for lib_symbols compatibility
KiCad 9 refuses to load any schematic whose lib_symbols section contains
an (extends ...) clause, because the extends mechanism is only valid
inside .kicad_sym library files - not inside schematics.

The previous implementation kept the (extends ...) clause and prepended
the parent symbol block with a qualified name, but left the child's
extends referring to the unqualified parent name, causing:
  'Error loading schematic: No parent for extended symbol Q_NMOS_GSD'

Fix: add _iter_top_level_items() and _inline_extends_symbol() helpers.
When a library symbol uses extends, the parent's content (pins, graphics,
sub-symbols) is now merged directly into the child definition:
- Parent properties are overridden by child property values
- Sub-symbol names are renamed from ParentName_X_Y to ChildName_X_Y
- The (extends ...) clause is removed entirely
- Only the fully-resolved child symbol is injected into lib_symbols

Affected symbols include Transistor_FET:2N7002, Transistor_BJT:*,
Regulator_Linear:*, Regulator_Switching:* and many others (~30% of the
KiCad standard library uses extends).

Fixes #52
2026-03-04 15:27:59 +01:00
mixelpixx
d8568fff12 Merge pull request #50 from pasrom/fix/mcp-server-bugs
Fix 6 bugs discovered during end-to-end MCP workflow testing
2026-03-02 08:09:57 -05:00
Tom
53014b9733 fix: remove invalid ;; comments from template_with_symbols_expanded.kicad_sch
PROBLEM
-------
Upstream commit b98c94b added 7 comment lines (;; PASSIVES, ;; SEMICONDUCTORS,
;; INTEGRATED CIRCUITS, ;; CONNECTORS, ;; POWER/REGULATORS, ;; MISC,
;; TEMPLATE INSTANCES) to template_with_symbols_expanded.kicad_sch.

KiCAD's S-expression parser does NOT support any comment syntax.
It expects every non-empty line to begin with '('. These comments caused
KiCAD 9 to refuse opening any schematic generated from this template:

  Expecting '(' in <file>.kicad_sch, line 8, offset 5

AFFECTED USERS
--------------
Any project whose .kicad_sch was generated by the MCP server after upstream
commit b98c94b and before this fix contains the same invalid lines. Those
files must be repaired manually: open the .kicad_sch in a text editor and
delete every line beginning with ';;'.

FIX
---
Remove all 7 ;; lines from the template. No functional change — the comments
provided no information that is not already evident from the symbol names.

ALSO
----
CHANGELOG: add critical bug-fix entry for this regression under v2.2.2-alpha
2026-03-01 21:12:51 +01:00
Roman PASSLER
2dd9de6a52 style: apply black formatting to changed files 2026-03-01 19:15:32 +01:00
Roman PASSLER
61356d42cb fix(schematic): rewrite component lookup to handle single-line files
The previous line-based parser (split("\n") + line-by-line regex)
failed when the MCP server itself generates .kicad_sch files as a
single line. The new implementation works directly on the raw content
string using parenthesis-depth matching to find symbol blocks,
making it independent of formatting/whitespace.
2026-03-01 18:43:20 +01:00
Roman PASSLER
a018575bbc fix(mounting-hole): generate unique references MH1, MH2, etc.
All mounting holes were assigned the same reference "MH", violating
PCB conventions and causing conflicts. Now queries existing footprints
to find the next available MH number.
2026-03-01 18:41:39 +01:00
Roman PASSLER
4e70342eae fix(drc): extract violation coordinates from kicad-cli JSON items
kicad-cli DRC JSON output stores coordinates in items[].pos.x/y,
not at the violation top level. The code was reading violation.x/y
which don't exist, falling back to (0, 0) for every violation.
Now correctly reads the position from the first item in each
violation entry.
2026-03-01 18:40:45 +01:00
Roman PASSLER
ec1939bef4 fix(copper-pour): add outline parameter and fallback to board outline
Two issues fixed:
1. TypeScript schema was missing the outline parameter entirely,
   so MCP clients couldn't send pour boundary points.
2. Python code read "points" key but schema defined "outline" key.

Now accepts "outline" (with "points" as fallback for backwards
compatibility). When no outline is provided, automatically uses
the board edge bounding box as the pour boundary.
2026-03-01 18:40:24 +01:00
Roman PASSLER
246050001a fix(nets): use GetNetClassName() instead of GetClassName() on NETINFO_ITEM
NETINFO_ITEM objects don't have a GetClassName() method, causing
an AttributeError crash when listing nets. The correct method is
GetNetClassName() which returns the net class name string.
2026-03-01 18:37:55 +01:00