kicad-mcp/kicad_mcp/tools/analysis_tools.py
Ryan Malloy 50f17eff35
Some checks failed
CI / Lint and Format (push) Has been cancelled
CI / Test Python 3.11 on macos-latest (push) Has been cancelled
CI / Test Python 3.12 on macos-latest (push) Has been cancelled
CI / Test Python 3.13 on macos-latest (push) Has been cancelled
CI / Test Python 3.10 on ubuntu-latest (push) Has been cancelled
CI / Test Python 3.11 on ubuntu-latest (push) Has been cancelled
CI / Test Python 3.12 on ubuntu-latest (push) Has been cancelled
CI / Test Python 3.13 on ubuntu-latest (push) Has been cancelled
CI / Security Scan (push) Has been cancelled
CI / Build Package (push) Has been cancelled
Fix validate_project tool to handle directory paths and json import error
- Move json import to module level to fix NameError
- Add directory path handling to automatically find .kicad_pro files
- Improve error messages and validation logic
- Add proper file type validation

Fixes IsADirectoryError and 'json' variable access errors.
2025-08-11 17:54:14 -06:00

89 lines
2.8 KiB
Python

"""
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()),
}