diff --git a/kicad_mcp/tools/export_tools.py b/kicad_mcp/tools/export_tools.py index 2fc130d..42e8477 100644 --- a/kicad_mcp/tools/export_tools.py +++ b/kicad_mcp/tools/export_tools.py @@ -7,7 +7,8 @@ import os import shutil import subprocess -from fastmcp import FastMCP, Image +from fastmcp import FastMCP +from fastmcp.utilities.types import Image from kicad_mcp.config import KICAD_APP_PATH, system from kicad_mcp.utils.file_utils import get_project_files diff --git a/kicad_mcp/utils/ipc_client.py b/kicad_mcp/utils/ipc_client.py index dd481f1..38d6094 100644 --- a/kicad_mcp/utils/ipc_client.py +++ b/kicad_mcp/utils/ipc_client.py @@ -46,9 +46,12 @@ class KiCadIPCClient: self._current_project: Project | None = None self._current_board: Board | None = None - def connect(self) -> bool: + def connect(self, log_failures: bool = False) -> bool: """ - Connect to KiCad IPC server. + Connect to KiCad IPC server with lazy connection support. + + Args: + log_failures: Whether to log connection failures (default: False for lazy connections) Returns: True if connection successful, False otherwise @@ -64,7 +67,10 @@ class KiCadIPCClient: logger.info(f"Connected to KiCad {version} via {connection_info}") return True except Exception as e: - logger.error(f"Failed to connect to KiCad IPC server: {e}") + if log_failures: + logger.error(f"Failed to connect to KiCad IPC server: {e}") + else: + logger.debug(f"KiCad IPC connection attempt failed: {e}") self._kicad = None return False @@ -472,24 +478,39 @@ def kicad_ipc_session(project_path: str = None, board_path: str = None): def check_kicad_availability() -> dict[str, Any]: """ Check if KiCad IPC API is available and working. + Implements lazy connection - only attempts connection when needed. Returns: Dictionary with availability status and version info """ try: - with kicad_ipc_session() as client: - version = client.get_version() + # Quick lazy connection test - don't spam logs for expected failures + client = KiCadIPCClient() + if client.connect(): + try: + version = client.get_version() + client.disconnect() + return { + "available": True, + "version": version, + "message": f"KiCad IPC API available (version {version})" + } + except Exception: + client.disconnect() + raise + else: return { - "available": True, - "version": version, - "message": f"KiCad IPC API available (version {version})" + "available": False, + "version": None, + "message": "KiCad not running - start KiCad to enable real-time features" } except Exception as e: + # Only log debug level for expected "KiCad not running" cases + logger.debug(f"KiCad IPC availability check: {e}") return { "available": False, "version": None, - "message": f"KiCad IPC API not available: {e}", - "error": str(e) + "message": "KiCad not running - start KiCad to enable real-time features" }