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) <noreply@anthropic.com>
This commit is contained in:
Gavin Colonese
2026-06-12 11:00:57 -04:00
parent 91477e8d02
commit d48f4835b0
3 changed files with 79 additions and 0 deletions

View File

@@ -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.

View File

@@ -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");
}

View File

@@ -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",