diff --git a/config/windows-config.example.json b/config/windows-config.example.json index 78dcd09..7fdb060 100644 --- a/config/windows-config.example.json +++ b/config/windows-config.example.json @@ -7,7 +7,8 @@ "NODE_ENV": "production", "PYTHONPATH": "C:\\Program Files\\KiCad\\9.0\\bin\\Lib\\site-packages", "LOG_LEVEL": "info", - "KICAD_AUTO_LAUNCH": "false" + "KICAD_AUTO_LAUNCH": "false", + "KICAD_MCP_DEV": "0" }, "description": "KiCAD PCB Design Assistant - Note: PYTHONPATH auto-detected if venv exists" } diff --git a/python/commands/export.py b/python/commands/export.py index 5c74a66..2307506 100644 --- a/python/commands/export.py +++ b/python/commands/export.py @@ -7,6 +7,8 @@ import pcbnew import logging from typing import Dict, Any, Optional, List, Tuple import base64 +import shutil +from datetime import datetime logger = logging.getLogger("kicad_interface") @@ -122,6 +124,13 @@ class ExportCommands: else: logger.warning("kicad-cli not available for drill file generation") + # DEV MODE: copy MCP server log into project folder for later analysis + if os.environ.get("KICAD_MCP_DEV") == "1": + try: + self._dev_copy_mcp_log(output_dir) + except Exception as dev_err: + logger.warning(f"[DEV] Could not copy MCP log: {dev_err}") + return { "success": True, "message": "Exported Gerber files", @@ -643,3 +652,51 @@ class ExportCommands: return path return None + + def _dev_copy_mcp_log(self, output_dir: str) -> None: + """DEV MODE: Copy the MCP server log for the current session into the project folder. + + Activated by env var KICAD_MCP_DEV=1. + The log is placed alongside the Gerber output as: + /mcp_log_.txt + + Only lines from the current server session (today's date) are included + to keep the file focused on the relevant run. + """ + import platform + + # Resolve Claude log path per platform + system = platform.system() + if system == "Windows": + log_dir = os.path.join(os.environ.get("APPDATA", ""), "Claude", "logs") + elif system == "Darwin": + log_dir = os.path.expanduser("~/Library/Logs/Claude") + else: + log_dir = os.path.expanduser("~/.config/Claude/logs") + + log_src = os.path.join(log_dir, "mcp-server-kicad.log") + if not os.path.exists(log_src): + logger.warning(f"[DEV] MCP log not found at: {log_src}") + return + + # Project dir = parent of outputDir (the Gerber subfolder) + project_dir = os.path.dirname(output_dir) + + # Extract only lines from the current session start (find last "Initializing server") + with open(log_src, "r", encoding="utf-8", errors="replace") as f: + all_lines = f.readlines() + + # Find last occurrence of server start so we get only the current run + session_start = 0 + for i, line in enumerate(all_lines): + if "Initializing server" in line: + session_start = i + + session_lines = all_lines[session_start:] + + timestamp = datetime.now().strftime("%Y%m%d_%H%M%S") + dest = os.path.join(project_dir, f"mcp_log_{timestamp}.txt") + with open(dest, "w", encoding="utf-8") as f: + f.writelines(session_lines) + + logger.info(f"[DEV] MCP session log saved to: {dest} ({len(session_lines)} lines)")