""" Analysis and validation tools for KiCad projects. """ import json import os from typing import Any from mcp.server.fastmcp import FastMCP from kicad_mcp.utils.file_utils import get_project_files def register_analysis_tools(mcp: FastMCP) -> None: """Register analysis and validation tools with the MCP server. Args: mcp: The FastMCP server instance """ @mcp.tool() def validate_project(project_path: str) -> dict[str, Any]: """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 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 = [] 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: issues.append("Missing PCB layout file") if "schematic" not in files: issues.append("Missing schematic file") # Validate project file JSON format try: with open(project_path) as f: json.load(f) 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)}") return { "valid": len(issues) == 0, "path": project_path, "issues": issues if issues else None, "files_found": list(files.keys()), }