feat: dev mode — auto-copy MCP session log to project dir on export_gerber (KICAD_MCP_DEV=1)

This commit is contained in:
Tom
2026-03-07 12:45:52 +01:00
parent 8e4e3176c2
commit 91fe6a379c
2 changed files with 59 additions and 1 deletions

View File

@@ -7,7 +7,8 @@
"NODE_ENV": "production", "NODE_ENV": "production",
"PYTHONPATH": "C:\\Program Files\\KiCad\\9.0\\bin\\Lib\\site-packages", "PYTHONPATH": "C:\\Program Files\\KiCad\\9.0\\bin\\Lib\\site-packages",
"LOG_LEVEL": "info", "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" "description": "KiCAD PCB Design Assistant - Note: PYTHONPATH auto-detected if venv exists"
} }

View File

@@ -7,6 +7,8 @@ import pcbnew
import logging import logging
from typing import Dict, Any, Optional, List, Tuple from typing import Dict, Any, Optional, List, Tuple
import base64 import base64
import shutil
from datetime import datetime
logger = logging.getLogger("kicad_interface") logger = logging.getLogger("kicad_interface")
@@ -122,6 +124,13 @@ class ExportCommands:
else: else:
logger.warning("kicad-cli not available for drill file generation") 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 { return {
"success": True, "success": True,
"message": "Exported Gerber files", "message": "Exported Gerber files",
@@ -643,3 +652,51 @@ class ExportCommands:
return path return path
return None 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:
<project_dir>/mcp_log_<YYYYMMDD_HHMMSS>.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)")