From d48f4835b073607d1a47a9b813d5d96216f50b44 Mon Sep 17 00:00:00 2001 From: Gavin Colonese Date: Fri, 12 Jun 2026 11:00:57 -0400 Subject: [PATCH] feat: add export_sch_python_bom tool (kicad-cli, legacy Python-BOM XML) New MCP tool `export_sch_python_bom` wrapping `kicad-cli sch export python-bom`: emits the legacy intermediate XML netlist consumed by the schematic editor's Python BOM scripts. Minimal option set (outputPath + schematicPath only, matching the subcommand's --help). schematicPath required and validated. Co-Authored-By: Claude Opus 4.8 (1M context) --- python/kicad_interface.py | 54 +++++++++++++++++++++++++++++++++++++++ src/tools/export.ts | 24 +++++++++++++++++ src/tools/registry.ts | 1 + 3 files changed, 79 insertions(+) diff --git a/python/kicad_interface.py b/python/kicad_interface.py index 8b77697..3273430 100644 --- a/python/kicad_interface.py +++ b/python/kicad_interface.py @@ -605,6 +605,7 @@ class KiCADInterface: "export_sch_dxf": self._handle_export_sch_dxf, "export_sch_hpgl": self._handle_export_sch_hpgl, "export_sch_ps": self._handle_export_sch_ps, + "export_sch_python_bom": self._handle_export_sch_python_bom, "generate_netlist": self._handle_generate_netlist, "sync_schematic_to_board": self._handle_sync_schematic_to_board, "list_schematic_libraries": self._handle_list_schematic_libraries, @@ -6063,6 +6064,59 @@ class KiCADInterface: logger.error(f"Error exporting schematic PostScript: {e}") return {"success": False, "message": str(e)} + def _handle_export_sch_python_bom(self, params: Dict[str, Any]) -> Dict[str, Any]: + """Export the legacy Python-BOM XML from a schematic via kicad-cli + (`sch export python-bom`). + + Emits the legacy intermediate XML netlist consumed by the schematic + editor's Python BOM scripts. Minimal option set (output + input only). + schematicPath is required. + """ + import subprocess + + logger.info("Exporting schematic Python-BOM via kicad-cli") + try: + schematic_path = params.get("schematicPath") + output_path = params.get("outputPath") + + if not schematic_path: + return {"success": False, "message": "schematicPath is required"} + if not os.path.exists(schematic_path): + return {"success": False, "message": f"Schematic not found: {schematic_path}"} + if not output_path: + return {"success": False, "message": "outputPath is required"} + + kicad_cli = self._find_kicad_cli_static() + if not kicad_cli: + return {"success": False, "message": "kicad-cli not found in PATH"} + + output_path = os.path.abspath(os.path.expanduser(output_path)) + parent = os.path.dirname(output_path) + if parent: + os.makedirs(parent, exist_ok=True) + + cmd = [kicad_cli, "sch", "export", "python-bom", "--output", output_path] + cmd.append(schematic_path) + + logger.info(f"Running: {' '.join(cmd)}") + result = subprocess.run(cmd, capture_output=True, text=True, timeout=180) + + if result.returncode != 0: + return { + "success": False, + "message": f"kicad-cli failed (exit {result.returncode}): " + f"{result.stderr.strip()}", + } + return {"success": True, "outputPath": output_path} + + except FileNotFoundError: + return {"success": False, "message": "kicad-cli not found in PATH"} + except subprocess.TimeoutExpired: + return {"success": False, "message": "kicad-cli timed out after 180 seconds"} + except Exception as e: + logger.error(f"Error exporting schematic Python-BOM: {e}") + return {"success": False, "message": str(e)} + def _handle_generate_netlist(self, params: Dict[str, Any]) -> Dict[str, Any]: """Generate netlist from schematic and return structured JSON. diff --git a/src/tools/export.ts b/src/tools/export.ts index d5b5dfd..955d1c2 100644 --- a/src/tools/export.ts +++ b/src/tools/export.ts @@ -1318,5 +1318,29 @@ export function registerExportTools(server: McpServer, callKicadScript: CommandF }, ); + // ------------------------------------------------------ + // Export Schematic Python-BOM Tool (kicad-cli) + // ------------------------------------------------------ + server.tool( + "export_sch_python_bom", + "Export the legacy Python-BOM intermediate XML from a schematic via kicad-cli (`sch export python-bom`). This is the XML netlist consumed by the schematic editor's Python BOM scripts. Minimal option set (output + input only). schematicPath is REQUIRED.", + { + schematicPath: z.string().describe("Path to the .kicad_sch (required)"), + outputPath: z.string().describe("Output XML file path"), + }, + async (args) => { + logger.debug(`Exporting schematic Python-BOM to: ${args.outputPath}`); + const result = await callKicadScript("export_sch_python_bom", args); + return { + content: [ + { + type: "text", + text: JSON.stringify(result), + }, + ], + }; + }, + ); + logger.info("Export tools registered"); } diff --git a/src/tools/registry.ts b/src/tools/registry.ts index 4c8bd19..b0e76ab 100644 --- a/src/tools/registry.ts +++ b/src/tools/registry.ts @@ -76,6 +76,7 @@ export const toolCategories: ToolCategory[] = [ "export_sch_dxf", "export_sch_hpgl", "export_sch_ps", + "export_sch_python_bom", "export_pdf", "export_svg", "export_3d",