diff --git a/kicad_mcp/tools/analysis_tools.py b/kicad_mcp/tools/analysis_tools.py index 0c6e2b9..73804a4 100644 --- a/kicad_mcp/tools/analysis_tools.py +++ b/kicad_mcp/tools/analysis_tools.py @@ -2,6 +2,7 @@ Analysis and validation tools for KiCad projects. """ +import json import os from typing import Any @@ -19,12 +20,49 @@ def register_analysis_tools(mcp: FastMCP) -> None: @mcp.tool() def validate_project(project_path: str) -> dict[str, Any]: - """Basic validation of a KiCad project.""" + """Basic validation of a KiCad project. + + Args: + project_path: Path to the KiCad project file (.kicad_pro) or directory containing it + + Returns: + Dictionary with validation results + """ + # Handle directory paths by looking for .kicad_pro file + if os.path.isdir(project_path): + # Look for .kicad_pro files in the directory + kicad_pro_files = [f for f in os.listdir(project_path) if f.endswith('.kicad_pro')] + if not kicad_pro_files: + return { + "valid": False, + "error": f"No .kicad_pro file found in directory: {project_path}" + } + elif len(kicad_pro_files) > 1: + return { + "valid": False, + "error": f"Multiple .kicad_pro files found in directory: {project_path}. Please specify the exact file." + } + else: + project_path = os.path.join(project_path, kicad_pro_files[0]) + if not os.path.exists(project_path): - return {"valid": False, "error": f"Project not found: {project_path}"} + return {"valid": False, "error": f"Project file not found: {project_path}"} + + if not project_path.endswith('.kicad_pro'): + return { + "valid": False, + "error": f"Invalid file type. Expected .kicad_pro file, got: {project_path}" + } issues = [] - files = get_project_files(project_path) + + try: + files = get_project_files(project_path) + except Exception as e: + return { + "valid": False, + "error": f"Error analyzing project files: {str(e)}" + } # Check for essential files if "pcb" not in files: @@ -33,14 +71,12 @@ def register_analysis_tools(mcp: FastMCP) -> None: if "schematic" not in files: issues.append("Missing schematic file") - # Validate project file + # Validate project file JSON format try: with open(project_path) as f: - import json - json.load(f) - except json.JSONDecodeError: - issues.append("Invalid project file format (JSON parsing error)") + except json.JSONDecodeError as e: + issues.append(f"Invalid project file format (JSON parsing error): {str(e)}") except Exception as e: issues.append(f"Error reading project file: {str(e)}")