Rebuild on FastMCP 3 with src-layout and kicad-sch-api integration

Migrate from FastMCP 2.14.5 to 3.1.0 with complete architectural
overhaul. Adopt src-layout packaging, lazy config functions to
eliminate .env race condition, and decorator-based tool registration.

Consolidate 14 tool modules into 8 focused modules (33 tools total).
Add 9 new schematic tools via kicad-sch-api for creating and
manipulating .kicad_sch files. Drop pandas dependency (BOM uses
stdlib csv). Remove ~17k lines of stubs, over-engineering, and
dead code.

All checks pass: ruff clean, mypy 0 errors, 17/17 tests green.
This commit is contained in:
Ryan Malloy 2026-03-03 18:26:54 -07:00
parent 4ebf8f08e9
commit 4ae38fed59
106 changed files with 4073 additions and 21045 deletions

View File

@ -1,16 +1,20 @@
# Example environment file for KiCad MCP Server
# Copy this file to .env and customize the values
# mckicad Configuration
# Copy to .env and adjust values for your system.
# Additional directories to search for KiCad projects (comma-separated)
# KICAD_SEARCH_PATHS=~/pcb,~/Electronics,~/Projects/KiCad
# Comma-separated paths to search for KiCad projects
# KICAD_SEARCH_PATHS=~/Documents/PCB,~/Electronics,~/Projects/KiCad
# Override the default KiCad user directory
# KiCad user documents directory (auto-detected if not set)
# KICAD_USER_DIR=~/Documents/KiCad
# Override the default KiCad application path
# macOS:
# KICAD_APP_PATH=/Applications/KiCad/KiCad.app
# Windows:
# KICAD_APP_PATH=C:\Program Files\KiCad
# Linux:
# Explicit path to kicad-cli executable (auto-detected if not set)
# KICAD_CLI_PATH=/usr/bin/kicad-cli
# KiCad application path (for opening projects)
# KICAD_APP_PATH=/usr/share/kicad
# Explicit path to FreeRouting JAR for autorouting
# FREEROUTING_JAR_PATH=~/freerouting.jar
# Logging level (DEBUG, INFO, WARNING, ERROR)
# LOG_LEVEL=INFO

405
CLAUDE.md
View File

@ -1,356 +1,135 @@
# CLAUDE.md
This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.
This file provides guidance to Claude Code when working with the mckicad codebase.
## Development Commands
### Essential Commands
- `make install` - Install dependencies using uv (creates .venv automatically)
- `make run` - Start the KiCad MCP server (`uv run python main.py`)
- `make test` - Run all tests with pytest
- `make test <file>` - Run specific test file
- `make lint` - Run linting with ruff and mypy (`uv run ruff check mckicad/ tests/` + `uv run mypy mckicad/`)
- `make format` - Format code with ruff (`uv run ruff format mckicad/ tests/`)
- `make build` - Build package with uv
- `make clean` - Clean build artifacts
- `make install` - Install dependencies with uv (creates .venv)
- `make run` - Start the MCP server (`uv run python main.py`)
- `make test` - Run all tests (`uv run pytest tests/ -v`)
- `make test <file>` - Run a specific test file
- `make lint` - Lint with ruff + mypy (`src/mckicad/` and `tests/`)
- `make format` - Auto-format with ruff
- `make build` - Build package
- `make clean` - Remove build artifacts and caches
### Development Environment
- Uses `uv` for dependency management (Python 3.10+ required)
- Virtual environment is automatically created in `.venv/`
- Configuration via `.env` file (copy from `.env.example`)
Python 3.10+ required. Uses `uv` for everything. Configure via `.env` (copy `.env.example`).
## Architecture
### MCP Server Components
This project implements a Model Context Protocol (MCP) server for KiCad electronic design automation. The architecture follows MCP patterns with three main component types:
**Resources** (read-only data):
- `kicad://projects` - List KiCad projects
- `kicad://project/{project_path}` - Project details
- `kicad://drc_report/{project_path}` - DRC reports
- `kicad://bom/{project_path}` - Bill of materials
- `kicad://netlist/{project_path}` - Circuit netlists
- `kicad://patterns/{project_path}` - Circuit pattern analysis
**Tools** (actions/computations):
- Project management (open projects, analysis)
- DRC checking with KiCad CLI integration
- BOM generation and export
- PCB visualization and thumbnails
- Circuit pattern recognition
- File export operations
**Prompts** (reusable templates):
- PCB debugging assistance
- BOM analysis workflows
- Circuit pattern identification
- DRC troubleshooting
### Key Modules
#### Core Server (`mckicad/server.py`)
- FastMCP server initialization with lifespan management
- Registers all resources, tools, and prompts
- Signal handling for graceful shutdown
- Cleanup handlers for temporary directories
#### Configuration (`mckicad/config.py`)
- Platform-specific KiCad paths (macOS/Windows/Linux)
- Environment variable handling (`KICAD_SEARCH_PATHS`, `KICAD_USER_DIR`)
- Component library mappings and default footprints
- Timeout and display constants
#### Context Management (`mckicad/context.py`)
- Lifespan context with KiCad module availability detection
- Shared cache across requests
- Application state management
#### Security Features
- Path validation utilities in `utils/path_validator.py`
- Secure subprocess execution in `utils/secure_subprocess.py`
- Input sanitization for KiCad CLI operations
- Boundary validation for file operations
### KiCad Integration Strategy
- **Primary**: KiCad CLI (`kicad-cli`) for all operations
- **Fallback**: Direct file parsing for basic operations
- **Detection**: Automatic KiCad installation detection across platforms
- **Isolation**: Subprocess-based execution for security
mckicad is a FastMCP 3 server for KiCad electronic design automation. It uses src-layout packaging with `hatchling` as the build backend.
### Project Structure
```
mckicad/
├── resources/ # MCP resources (data providers)
├── tools/ # MCP tools (action performers)
├── prompts/ # MCP prompt templates
└── utils/ # Utility functions and helpers
├── kicad_utils.py # KiCad-specific operations
├── file_utils.py # File handling utilities
├── path_validator.py # Security path validation
└── secure_subprocess.py # Safe process execution
src/mckicad/
__init__.py # __version__ only
server.py # FastMCP 3 server + lifespan + module imports
config.py # Lazy config functions (no module-level env reads)
tools/
schematic.py # kicad-sch-api: create/edit schematics (9 tools)
project.py # Project discovery and structure (3 tools)
drc.py # DRC checking + manufacturing constraints (4 tools)
bom.py # BOM generation and export (2 tools)
export.py # Gerber, drill, PDF, SVG via kicad-cli (4 tools)
routing.py # FreeRouting autorouter integration (3 tools)
analysis.py # Board validation + real-time analysis (3 tools)
pcb.py # IPC-based PCB manipulation via kipy (5 tools)
resources/
projects.py # kicad://projects resource
files.py # kicad://project/{path} resource
prompts/
templates.py # debug_pcb, analyze_bom, design_circuit, debug_schematic
utils/
kicad_cli.py # KiCad CLI detection and execution
path_validator.py # Path security / directory traversal prevention
secure_subprocess.py # Safe subprocess execution with timeouts
ipc_client.py # kipy IPC wrapper for live KiCad connection
freerouting.py # FreeRouting JAR engine
file_utils.py # Project file discovery
kicad_utils.py # KiCad path detection, project search
tests/
conftest.py # Shared fixtures (tmp dirs, project paths)
test_*.py # Per-module test files
main.py # Entry point: .env loader + server start
```
## Development Notes
### Key Design Decisions
### Adding New Features
1. Identify component type (resource/tool/prompt)
2. Add implementation to appropriate module in `mckicad/`
3. Register in `server.py` create_server() function
4. Use lifespan context for shared state and caching
5. Include progress reporting for long operations
**Lazy config** (`config.py`): All environment-dependent values are accessed via functions (`get_search_paths()`, `get_kicad_user_dir()`) called at runtime, not at import time. Static constants (`KICAD_EXTENSIONS`, `TIMEOUT_CONSTANTS`, `COMMON_LIBRARIES`) remain as module-level dicts since they don't read env vars. This eliminates the .env load-order race condition.
### KiCad CLI Integration
- All KiCad operations use CLI interface for security
- CLI detection in `utils/kicad_cli.py`
- Path validation prevents directory traversal
- Subprocess timeouts prevent hanging operations
**Decorator-based tool registration**: Each tool module imports `mcp` from `server.py` and decorates functions with `@mcp.tool()` at module level. `server.py` imports the modules to trigger registration. No `register_*_tools()` boilerplate.
### Testing
- Unit tests in `tests/unit/`
- Test markers: `unit`, `integration`, `requires_kicad`, `slow`, `performance`
- Coverage target: 80% (configured in pyproject.toml)
- Run with: `pytest` or `make test`
**Schematic abstraction point**: `tools/schematic.py` uses `kicad-sch-api` for file-level schematic manipulation. The `_get_schematic_engine()` helper exists as a swap point for when kipy adds schematic IPC support.
### Configuration
- Environment variables override defaults in `config.py`
- `.env` file support for development
- Platform detection for KiCad paths
- Search path expansion with `~` support
**Dual-mode operation**: PCB tools work via IPC (kipy, requires running KiCad) or CLI (kicad-cli, batch mode). Tools degrade gracefully when KiCad isn't running.
### Entry Point
- `main.py` is the server entry point
- Handles logging setup and .env file loading
- Manages server lifecycle with proper cleanup
- Uses asyncio for MCP server execution
### Tool Registration Pattern
## Revolutionary Features: KiCad IPC + FreeRouting Integration
This project implements groundbreaking real-time PCB automation through two advanced integrations:
### KiCad IPC API Integration (`utils/ipc_client.py`)
- **Real-time design manipulation** via `kicad-python` library (`kipy` package)
- **Live component access**: Move, rotate, analyze footprints in real-time
- **Connectivity monitoring**: Track routing status and net connections
- **Transaction-based operations**: Automatic rollback on errors
- **Lazy connection**: Gracefully degrades when KiCad isn't running
- **Context manager pattern**: Use `kicad_ipc_session()` for automatic cleanup
**Key Usage Pattern:**
```python
from mckicad.utils.ipc_client import kicad_ipc_session
# tools/example.py
from mckicad.server import mcp
with kicad_ipc_session(board_path) as client:
footprints = client.get_footprints()
client.move_footprint("R1", Vector2(100.0, 50.0))
stats = client.get_board_statistics()
@mcp.tool()
def my_tool(param: str) -> dict:
"""Tool description for the calling LLM."""
return {"success": True, "data": "..."}
```
### FreeRouting Integration (`utils/freerouting_engine.py`)
- **Professional autorouting** via FreeRouting JAR execution
- **Complete workflow automation**: DSN export → Route → SES import
- **Multi-strategy routing**: Conservative, balanced, aggressive presets
- **Technology-specific optimization**: Standard, HDI, RF, automotive modes
- **Post-routing optimization**: Via minimization, trace cleanup
- **Configuration system**: Routing parameters, layer preferences, cost functions
### Tool Return Convention
**Routing Workflow:**
1. Export board to DSN format via KiCad CLI
2. Execute FreeRouting with optimized parameters
3. Import routed SES file back via IPC API
4. Validate and optimize routing results
5. Run DRC checks and report statistics
All tools return dicts with at least `success: bool`. On failure, include `error: str`. On success, include relevant data fields.
### Complete Automation Pipeline (`tools/project_automation.py`)
The `automate_complete_design()` tool orchestrates:
1. **AI-driven design analysis** (circuit pattern recognition)
2. **Component placement optimization** (thermal-aware)
3. **Automated PCB routing** (FreeRouting integration)
4. **DRC validation and fixing**
5. **Manufacturing file generation** (Gerber, drill, assembly)
6. **Supply chain optimization** (component availability)
## Adding New Features
## Important Implementation Notes
1. Choose the right module (or create one in `tools/`)
2. Import `mcp` from `mckicad.server`
3. Decorate with `@mcp.tool()` and add a clear docstring
4. If new module: add import in `server.py`
5. Write tests in `tests/test_<module>.py`
### Dual-Mode Operation
This server operates in TWO modes depending on KiCad availability:
## Security
**Mode 1: KiCad Running (IPC Available)**
- Real-time component manipulation
- Live board statistics
- Interactive routing optimization
- Immediate DRC feedback
- All file paths validated via `utils/path_validator.py` before access
- External commands run through `utils/secure_subprocess.py` with timeouts
- KiCad CLI commands sanitized — no shell injection
- `main.py` inline .env loader runs before any mckicad imports
**Mode 2: KiCad CLI Only**
- File-based operations via `kicad-cli`
- Batch processing and exports
- Static analysis and validation
- Manufacturing file generation
## Environment Variables
**Detection Logic:**
```python
# IPC availability checked at runtime
if check_kicad_availability()["available"]:
# Use real-time IPC features
else:
# Fall back to CLI operations
- `KICAD_USER_DIR` - KiCad user config directory
- `KICAD_SEARCH_PATHS` - Comma-separated project search paths
- `KICAD_CLI_PATH` - Explicit kicad-cli path
- `FREEROUTING_JAR_PATH` - Path to FreeRouting JAR
- `LOG_LEVEL` - Logging level (default: INFO)
## Testing
Markers: `unit`, `integration`, `requires_kicad`, `slow`, `performance`
```bash
make test # all tests
make test tests/test_schematic.py # one file
uv run pytest -m "unit" # by marker
```
### FreeRouting Setup
For automated routing to work:
1. Download FreeRouting JAR from https://freerouting.app/
2. Place in one of these locations:
- `~/freerouting.jar`
- `/usr/local/bin/freerouting.jar`
- `/opt/freerouting/freerouting.jar`
3. Ensure Java runtime is installed (`java -version`)
4. Use `check_routing_capability()` tool to verify setup
## Entry Point
### Environment Configuration
Key environment variables in `.env`:
- `KICAD_USER_DIR`: KiCad user documents directory
- `KICAD_SEARCH_PATHS`: Comma-separated project search paths
- `FREEROUTING_JAR_PATH`: Explicit path to FreeRouting JAR
- `KICAD_CLI_PATH`: Explicit path to kicad-cli executable
### Package Entry Point
The package declares a script entry point in `pyproject.toml`:
```toml
[project.scripts]
mckicad = "mckicad.server:main"
```
This enables running via `uvx mckicad` after installation.
### Logging
- All logs written to `mckicad.log` in project root
- Log file overwritten on each server start
- PID included in log messages for process tracking
- Use `logging` module, never `print()` statements
Run via `uvx mckicad`, `uv run mckicad`, or `uv run python main.py`.
## Tool Implementation Patterns
## FreeRouting Setup
### FastMCP Tool Registration
All tools follow this registration pattern in `server.py`:
```python
from mckicad.tools.example_tools import register_example_tools
1. Download JAR from https://freerouting.app/
2. Place at `~/freerouting.jar`, `/usr/local/bin/freerouting.jar`, or `/opt/freerouting/freerouting.jar`
3. Install Java runtime
4. Verify with `check_routing_capability()` tool
5. Or set `FREEROUTING_JAR_PATH` in `.env`
def create_server() -> FastMCP:
mcp = FastMCP("KiCad", lifespan=lifespan_factory)
## Logging
# Register tools
register_example_tools(mcp)
return mcp
```
### Tool Module Structure
Each tool module should:
1. Define a `register_*_tools(mcp: FastMCP)` function
2. Use `@mcp.tool()` decorator for each tool
3. Include comprehensive docstrings with examples
4. Return dictionaries with consistent structure:
```python
{
"success": bool,
"data": Any, # Results on success
"error": str # Error message on failure
}
```
### Progress Reporting
For long operations, use progress constants:
```python
from mckicad.config import PROGRESS_CONSTANTS
progress_callback(PROGRESS_CONSTANTS["start"])
# ... do work ...
progress_callback(PROGRESS_CONSTANTS["processing"])
# ... finish ...
progress_callback(PROGRESS_CONSTANTS["complete"])
```
## Security Architecture
### Path Validation (`utils/path_validator.py`)
- All file paths validated before access
- Directory traversal prevention
- Boundary checking for project directories
- Whitelist-based validation
### Secure Subprocess (`utils/secure_subprocess.py`)
- All external commands executed securely
- Timeouts prevent hanging operations
- Output sanitization
- Error handling with safe error messages
### Always Validate:
1. Project paths exist and are within search paths
2. File extensions match expected types
3. Subprocess commands are sanitized
4. Timeout values are reasonable
## Testing Strategy
### Test Organization
```
tests/
├── unit/ # Fast, isolated tests
│ └── utils/ # Utility function tests
└── integration/ # Full workflow tests (future)
```
### Test Markers
Use pytest markers to categorize tests:
- `@pytest.mark.unit` - Unit tests (fast)
- `@pytest.mark.integration` - Integration tests
- `@pytest.mark.requires_kicad` - Requires KiCad installation
- `@pytest.mark.slow` - Tests taking >5 seconds
- `@pytest.mark.performance` - Performance benchmarks
### Running Specific Tests
```bash
# Single test file
make test tests/unit/utils/test_path_validator.py
# Specific test function
make test tests/unit/utils/test_path_validator.py::test_validate_project_path
# By marker
pytest -m "unit and not slow"
```
## Debugging Tips
### Enable Verbose Logging
Add to `.env`:
```
LOG_LEVEL=DEBUG
```
### Test IPC Connection
```python
from mckicad.utils.ipc_client import check_kicad_availability
print(check_kicad_availability())
```
### Test FreeRouting Setup
```python
from mckicad.utils.freerouting_engine import check_routing_prerequisites
print(check_routing_prerequisites())
```
### Common Issues
**IPC Connection Fails:**
- Ensure KiCad is running and has a project open
- Check that `kicad-python` (kipy) is installed
- Verify no other MCP clients are connected
**FreeRouting Not Found:**
- Verify JAR exists at expected location
- Test Java with: `java -version`
- Set `FREEROUTING_JAR_PATH` explicitly in `.env`
**CLI Operations Timeout:**
- Increase timeout in `config.py` TIMEOUT_CONSTANTS
- Check KiCad CLI is in PATH: `kicad-cli version`
- Verify project files aren't corrupted
Logs go to `mckicad.log` in project root, overwritten each start. Never use `print()` — MCP uses stdin/stdout for JSON-RPC transport.

View File

@ -1,6 +0,0 @@
include README.md
include LICENSE
include requirements.txt
include .env.example
recursive-include kicad_mcp *.py
recursive-include docs *.md

View File

@ -2,44 +2,40 @@
help:
@echo "Available commands:"
@echo " install Install dependencies"
@echo " test Run tests"
@echo " test <file> Run specific test file"
@echo " lint Run linting"
@echo " format Format code"
@echo " clean Clean build artifacts"
@echo " build Build package"
@echo " run Start the KiCad MCP server"
@echo " install Install dependencies"
@echo " test Run tests"
@echo " test <file> Run specific test file"
@echo " lint Run linting"
@echo " format Format code"
@echo " clean Clean build artifacts"
@echo " build Build package"
@echo " run Start the mckicad MCP server"
install:
uv sync --group dev
test:
# Collect extra args; if none, use tests/
@files="$(filter-out $@,$(MAKECMDGOALS))"; \
if [ -z "$$files" ]; then files="tests/"; fi; \
uv run pytest $$files -v
# Prevent “No rule to make target …” errors for the extra args
# Prevent "No rule to make target …" errors for extra args
%::
@:
lint:
uv run ruff check mckicad/ tests/
uv run mypy mckicad/
uv run ruff check src/mckicad/ tests/
uv run mypy src/mckicad/
format:
uv run ruff format mckicad/ tests/
uv run ruff format src/mckicad/ tests/
uv run ruff check --fix src/mckicad/ tests/
clean:
rm -rf dist/
rm -rf build/
rm -rf *.egg-info/
rm -rf .pytest_cache/
rm -rf htmlcov/
rm -rf dist/ build/ *.egg-info/ .pytest_cache/ htmlcov/
rm -f coverage.xml
find . -type d -name __pycache__ -delete
find . -type f -name "*.pyc" -delete
find . -type d -name __pycache__ -exec rm -rf {} + 2>/dev/null || true
find . -type f -name "*.pyc" -delete 2>/dev/null || true
build:
uv build

View File

@ -1,261 +0,0 @@
# Revolutionizing PCB Design: Building the World's Most Advanced EDA Automation Platform
**A Human-AI Collaboration Story**
---
**Metadata:**
- **Date**: August 13, 2025
- **Reading Time**: 10 minutes
- **AI Partner**: Claude Sonnet 4
- **Tools Used**: Claude Code, KiCad, Python, FastMCP, FreeRouting, MCP Protocol
- **Collaboration Type**: technical-revolution
- **Achievement Level**: Revolutionary Platform (100% Success Rate)
- **Tags**: PCB-design, EDA-automation, AI-engineering, KiCad-integration, human-AI-collaboration
---
## The Spark: "Can We Revolutionize PCB Design?"
It started with a simple question that would lead to something extraordinary. As someone passionate about both electronics and AI, I wondered: *What if we could transform KiCad from a design tool into an intelligent, fully-automated EDA platform?*
Traditional PCB design is a fragmented workflow. You design schematics in one tool, route traces manually, run separate DRC checks, export manufacturing files individually, and pray everything works together. Each step requires deep expertise, takes hours, and is prone to human error.
But what if Claude Code could change all that?
## The Vision: Complete Design-to-Manufacturing Automation
Working with Claude Sonnet 4 through Claude Code, we embarked on an ambitious journey to create something that didn't exist: a **Revolutionary EDA Automation Platform** that could:
- **Understand circuit designs** with AI intelligence
- **Manipulate KiCad in real-time** via IPC API with Python bindings
- **Automatically route PCBs** using advanced algorithms
- **Generate manufacturing files instantly** with one command
- **Provide intelligent design feedback** based on pattern recognition
The goal was nothing short of revolutionary: transform the entire PCB development workflow from a manual, multi-hour process into an automated, AI-driven system that works in seconds.
## The Technical Journey: From Concept to Revolutionary Reality
### Phase 1: Foundation Building
Claude and I started by understanding the KiCad ecosystem deeply. We discovered the relatively new KiCad IPC API - a game-changing interface that allows real-time control of KiCad from external applications, with Python bindings providing programmatic access. While the current implementation focuses primarily on PCB editor manipulation (with schematic editor support being expanded in ongoing development), this became our foundation for board-level automation.
```python
# The breakthrough: Real-time KiCad control via Python bindings
class KiCadIPCClient:
def __init__(self, socket_path=None, client_name=None):
# Uses KiCad IPC API with Python bindings
self._kicad = KiCad(
socket_path=socket_path,
client_name=client_name or "KiCad-MCP-Server"
)
```
### Phase 2: The MCP Architecture Breakthrough
The real innovation came when we decided to build this as a **Model Context Protocol (MCP) server**. This meant Claude Code users could access our EDA automation through natural language commands - essentially giving any AI assistant the ability to design PCBs!
We architected the system with three core components:
- **Resources**: Real-time project data, DRC reports, BOMs, netlists
- **Tools**: Actions like component analysis, routing, file generation
- **Prompts**: Reusable templates for common EDA workflows
### Phase 3: AI Circuit Intelligence
One of our proudest achievements was developing AI-powered circuit pattern recognition. The system can analyze any schematic and identify:
```python
# AI recognizes circuit patterns automatically
identified_patterns = {
"power_supply_circuits": identify_power_supplies(components, nets),
"amplifier_circuits": identify_amplifiers(components, nets),
"filter_circuits": identify_filters(components, nets),
"microcontroller_circuits": identify_microcontrollers(components),
"sensor_interface_circuits": identify_sensor_interfaces(components, nets)
}
```
The AI doesn't just see components - it understands **circuit intent** and can provide intelligent design recommendations with 95% confidence.
### Phase 4: The FreeRouting Revolution
Traditional KiCad routing is manual and time-consuming. We integrated **FreeRouting** - an advanced autorouter - to create a complete automated routing pipeline:
1. **Export DSN** from KiCad board
2. **Process with FreeRouting** autorouter
3. **Generate optimized traces**
4. **Import back to KiCad** seamlessly
This eliminated the biggest bottleneck in PCB design: manual routing.
### Phase 5: Manufacturing File Automation
The final piece was one-click manufacturing file generation. Our system can instantly generate:
- **30 Gerber layers** for fabrication
- **Drill files** for holes
- **Pick & place positions** for assembly
- **Bill of Materials (BOM)** for procurement
- **3D models** for mechanical integration
All with a single command.
## The Incredible Results: Perfection Achieved
When we finished development, the testing results were simply stunning:
### **🎯 100% SUCCESS RATE ACROSS ALL TESTS**
- **MCP Server Interface**: 6/6 tests PERFECT
- **Manufacturing Pipeline**: 5/5 tests PERFECT
- **FreeRouting Automation**: 4/4 tests PERFECT
- **Ultimate Comprehensive Demo**: 10/10 capabilities confirmed
### **⚡ Performance That Defies Belief**
- **File analysis**: 0.1ms (sub-millisecond!)
- **IPC connection**: 0.5ms
- **Component analysis**: 6.7ms for 66 components
- **Complete validation**: Under 2 seconds
### **🧠 AI Intelligence Metrics**
- **135 components** analyzed across 13 categories
- **273 wire connections** traced automatically
- **Power network detection** with 100% accuracy
- **Circuit pattern recognition** with 95% confidence
### **🏭 Manufacturing Readiness**
- **30 Gerber layers** generated instantly
- **Complete drill files** for fabrication
- **Pick & place data** for assembly
- **Production-ready files** in seconds
## The Human-AI Collaboration Magic
What made this project extraordinary wasn't just the technical achievements - it was the **creative partnership** between human intuition and AI implementation.
**My role as the human:**
- Provided vision and direction for the revolutionary platform
- Made architectural decisions about MCP integration
- Guided the user experience and workflow design
- Tested real-world scenarios and edge cases
**Claude's role as the AI partner:**
- Implemented complex technical integrations flawlessly
- Created comprehensive testing suites for validation
- Optimized performance to sub-millisecond levels
- Built robust error handling and edge case management
The magic happened in our **iterative collaboration**. I would say "What if we could..." and Claude would respond with "Here's exactly how we can build that..." - then implement it perfectly. When tests failed, Claude would immediately identify the issue, fix it, and improve the system.
## Real-World Impact: A Live Demonstration
To prove the platform worked, we created a live demonstration using a real thermal camera PCB project. We:
1. **Created a new "Smart Sensor Board"** from the existing design
2. **Analyzed 135 components** with AI intelligence
3. **Generated complete manufacturing files** in under 1 second
4. **Demonstrated real-time KiCad control** via the IPC API
5. **Showed automated routing readiness** with FreeRouting integration
The entire workflow - from project analysis to production-ready files - took **0.90 seconds**.
## The Revolutionary Platform Features
What we built is truly revolutionary:
### **🔥 Complete EDA Automation**
From schematic analysis to manufacturing files - fully automated
### **🧠 AI Circuit Intelligence**
Pattern recognition, design recommendations, component analysis
### **⚡ Real-Time Control**
Live KiCad manipulation via IPC API with Python bindings
### **🚀 Sub-Second Performance**
Millisecond response times across all operations
### **🏭 Manufacturing Ready**
One-click generation of all production files
### **🤖 Claude Code Integration**
Natural language interface to professional EDA tools
## The Future: Democratizing PCB Design
This platform represents a fundamental shift in how PCBs will be designed. Instead of requiring years of expertise to navigate complex EDA tools, designers can now:
- **Describe their circuit** in natural language
- **Let AI analyze and optimize** the design automatically
- **Generate manufacturing files** with a single command
- **Go from concept to production** in minutes instead of hours
We've essentially **democratized professional PCB design** by making it accessible through conversational AI.
## Technical Architecture: How We Built the Impossible
For those interested in the technical details, our platform consists of:
```
Revolutionary KiCad MCP Server Architecture:
├── MCP Protocol Integration (FastMCP)
├── KiCad IPC API Client (real-time control)
├── AI Circuit Intelligence Engine
├── FreeRouting Automation Pipeline
├── Manufacturing File Generator
├── Comprehensive Testing Suite
└── Claude Code Integration Layer
```
**Key Technologies:**
- **Python 3.10+** for core implementation
- **KiCad IPC API with Python bindings** for real-time board manipulation
- **FastMCP** for Model Context Protocol server
- **FreeRouting** for automated PCB routing
- **Advanced pattern recognition** for circuit intelligence
## Reflection: What We Learned
This collaboration taught us both invaluable lessons:
**Technical Insights:**
- Real-time EDA automation through IPC APIs is not only possible but can be incredibly fast
- AI pattern recognition can understand circuit intent, not just components
- The MCP protocol opens unlimited possibilities for tool integration
- Python bindings make complex APIs accessible for automation
- Human vision + AI implementation = revolutionary results
**Collaboration Insights:**
- The best innovations come from ambitious "what if" questions
- Iterative development with immediate testing leads to perfection
- Human creativity guides AI technical execution beautifully
- Comprehensive testing is essential for production-ready systems
## The Legacy: Open Source Revolution
This isn't just a technical achievement - it's a **gift to the electronics community**. The entire platform is open source, available on GitHub, ready for engineers worldwide to use and extend.
We've created something that will accelerate innovation in electronics design, making professional PCB development accessible to anyone with Claude Code.
## Conclusion: The Future is Here
In just a few days of intense human-AI collaboration, we built something that seemed impossible: a **complete EDA automation platform** with **100% success rate** and **sub-second performance**.
This project proves that when human creativity meets AI implementation, we can create tools that seemed like science fiction just months ago. We didn't just improve PCB design - we **revolutionized it**.
The future of electronics design is no longer manual, fragmented, and time-consuming. It's **intelligent, automated, and instantaneous**.
And it's available today, through the power of human-AI collaboration.
---
**Want to try it yourself?** The complete KiCad MCP server is available on GitHub, ready to transform your PCB design workflow. Just connect it to Claude Code and experience the future of EDA automation.
*The revolution in PCB design has begun. And it's powered by the incredible partnership between human vision and artificial intelligence.*
---
**About This Collaboration:**
This revolutionary EDA automation platform was built through an intensive human-AI collaboration between Ryan Malloy and Claude Sonnet 4 using Claude Code. The project demonstrates the incredible potential when human creativity and AI technical implementation work together to solve complex engineering challenges.
**GitHub Repository**: [KiCad MCP Server](https://github.com/user/mckicad)
**Performance Metrics**: 100% test success rate, sub-millisecond response times
**Impact**: Democratizes professional PCB design through conversational AI

61
main.py
View File

@ -1,31 +1,21 @@
#!/usr/bin/env python3
"""
KiCad MCP Server - A Model Context Protocol server for KiCad on macOS.
This server allows Claude and other MCP clients to interact with KiCad projects.
"""
import os
import logging
"""mckicad entry point — load .env, start MCP server."""
# --- Setup Logging ---
log_file = os.path.join(os.path.dirname(__file__), 'mckicad.log')
import logging
import os
# --- Logging ---
log_file = os.path.join(os.path.dirname(__file__), "mckicad.log")
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(levelname)s - [PID:%(process)d] - %(message)s',
handlers=[
logging.FileHandler(log_file, mode='w'),
]
format="%(asctime)s - %(levelname)s - [PID:%(process)d] - %(message)s",
handlers=[logging.FileHandler(log_file, mode="w")],
)
# ---------------------
logging.info("--- Server Starting ---")
# Load .env BEFORE any mckicad imports. Importing mckicad.utils.env would
# trigger mckicad/__init__.py which eagerly imports config.py, evaluating
# os.environ.get("KICAD_SEARCH_PATHS") before .env is loaded. So we
# inline the loading here using only stdlib.
_dotenv_path = os.path.join(os.path.dirname(__file__), '.env')
# --- Load .env BEFORE any mckicad imports ---
# This must happen before importing mckicad so config functions see env vars.
_dotenv_path = os.path.join(os.path.dirname(__file__), ".env")
if os.path.exists(_dotenv_path):
logging.info(f"Loading .env from: {_dotenv_path}")
with open(_dotenv_path) as _f:
for _line in _f:
_line = _line.strip()
@ -38,32 +28,9 @@ if os.path.exists(_dotenv_path):
_val.startswith("'") and _val.endswith("'")
):
_val = _val[1:-1]
os.environ[_key] = _val
logging.info(f" {_key}={_val}")
else:
logging.info("No .env file found")
os.environ.setdefault(_key, _val)
# Now safe to import mckicad — config.py will see the .env values
from mckicad.config import KICAD_USER_DIR, ADDITIONAL_SEARCH_PATHS
from mckicad.server import main as server_main
logging.info(f"KICAD_USER_DIR: {KICAD_USER_DIR}")
logging.info(f"ADDITIONAL_SEARCH_PATHS: {ADDITIONAL_SEARCH_PATHS}")
from mckicad.server import main # noqa: E402
if __name__ == "__main__":
try:
logging.info(f"Starting KiCad MCP server process")
# Print search paths from config
logging.info(f"Using KiCad user directory: {KICAD_USER_DIR}") # Changed print to logging
if ADDITIONAL_SEARCH_PATHS:
logging.info(f"Additional search paths: {', '.join(ADDITIONAL_SEARCH_PATHS)}") # Changed print to logging
else:
logging.info(f"No additional search paths configured") # Changed print to logging
# Run server
logging.info(f"Running server with stdio transport") # Changed print to logging
server_main()
except Exception as e:
logging.exception(f"Unhandled exception in main") # Log exception details
raise
main()

View File

@ -1,28 +0,0 @@
"""
KiCad MCP Server.
A Model Context Protocol (MCP) server for KiCad electronic design automation (EDA) files.
"""
from .config import *
from .context import *
from .server import *
__version__ = "0.1.0"
__author__ = "Lama Al Rajih"
__description__ = "Model Context Protocol server for KiCad on Mac, Windows, and Linux"
__all__ = [
# Package metadata
"__version__",
"__author__",
"__description__",
# Server creation / shutdown helpers
"create_server",
"add_cleanup_handler",
"run_cleanup_handlers",
"shutdown_server",
# Lifespan / context helpers
"kicad_lifespan",
"KiCadAppContext",
]

View File

@ -1,208 +0,0 @@
"""
Configuration settings for the KiCad MCP server.
This module provides platform-specific configuration for KiCad integration,
including file paths, extensions, component libraries, and operational constants.
All settings are determined at import time based on the operating system.
Module Variables:
system (str): Operating system name from platform.system()
KICAD_USER_DIR (str): User's KiCad documents directory
KICAD_APP_PATH (str): KiCad application installation path
ADDITIONAL_SEARCH_PATHS (List[str]): Additional project search locations
DEFAULT_PROJECT_LOCATIONS (List[str]): Common project directory patterns
KICAD_PYTHON_BASE (str): KiCad Python framework base path (macOS only)
KICAD_EXTENSIONS (Dict[str, str]): KiCad file extension mappings
DATA_EXTENSIONS (List[str]): Recognized data file extensions
CIRCUIT_DEFAULTS (Dict[str, Union[float, List[float]]]): Default circuit parameters
COMMON_LIBRARIES (Dict[str, Dict[str, Dict[str, str]]]): Component library mappings
DEFAULT_FOOTPRINTS (Dict[str, List[str]]): Default footprint suggestions per component
TIMEOUT_CONSTANTS (Dict[str, float]): Operation timeout values in seconds
PROGRESS_CONSTANTS (Dict[str, int]): Progress reporting percentage values
DISPLAY_CONSTANTS (Dict[str, int]): UI display configuration values
Platform Support:
- macOS (Darwin): Full support with application bundle paths
- Windows: Standard installation paths
- Linux: System package paths
- Unknown: Defaults to macOS paths for compatibility
Dependencies:
- os: File system operations and environment variables
- platform: Operating system detection
"""
import os
import platform
# Determine operating system for platform-specific configuration
# Returns 'Darwin' (macOS), 'Windows', 'Linux', or other
system = platform.system()
# Platform-specific KiCad installation and user directory paths
# These paths are used for finding KiCad resources and user projects
if system == "Darwin": # macOS
KICAD_USER_DIR = os.path.expanduser("~/Documents/KiCad")
KICAD_APP_PATH = "/Applications/KiCad/KiCad.app"
elif system == "Windows":
KICAD_USER_DIR = os.path.expanduser("~/Documents/KiCad")
KICAD_APP_PATH = r"C:\Program Files\KiCad"
elif system == "Linux":
KICAD_USER_DIR = os.path.expanduser("~/KiCad")
KICAD_APP_PATH = "/usr/share/kicad"
else:
# Default to macOS paths if system is unknown for maximum compatibility
# This ensures the server can start even on unrecognized platforms
KICAD_USER_DIR = os.path.expanduser("~/Documents/KiCad")
KICAD_APP_PATH = "/Applications/KiCad/KiCad.app"
# Additional search paths from environment variable KICAD_SEARCH_PATHS
# Users can specify custom project locations as comma-separated paths
ADDITIONAL_SEARCH_PATHS = []
env_search_paths = os.environ.get("KICAD_SEARCH_PATHS", "")
if env_search_paths:
for path in env_search_paths.split(","):
expanded_path = os.path.expanduser(path.strip()) # Expand ~ and variables
if os.path.exists(expanded_path): # Only add existing directories
ADDITIONAL_SEARCH_PATHS.append(expanded_path)
# Auto-detect common project locations for convenient project discovery
# These are typical directory names users create for electronics projects
DEFAULT_PROJECT_LOCATIONS = [
"~/Documents/PCB", # Common Windows/macOS location
"~/PCB", # Simple home directory structure
"~/Electronics", # Generic electronics projects
"~/Projects/Electronics", # Organized project structure
"~/Projects/PCB", # PCB-specific project directory
"~/Projects/KiCad", # KiCad-specific project directory
]
# Add existing default locations to search paths
# Avoids duplicates and only includes directories that actually exist
for location in DEFAULT_PROJECT_LOCATIONS:
expanded_path = os.path.expanduser(location)
if os.path.exists(expanded_path) and expanded_path not in ADDITIONAL_SEARCH_PATHS:
ADDITIONAL_SEARCH_PATHS.append(expanded_path)
# Base path to KiCad's Python framework for API access
# macOS bundles Python framework within the application
if system == "Darwin": # macOS
KICAD_PYTHON_BASE = os.path.join(
KICAD_APP_PATH, "Contents/Frameworks/Python.framework/Versions"
)
else:
# Linux/Windows use system Python or require dynamic detection
KICAD_PYTHON_BASE = "" # Will be determined dynamically in python_path.py
# KiCad file extension mappings for project file identification
# Used by file discovery and validation functions
KICAD_EXTENSIONS = {
"project": ".kicad_pro",
"pcb": ".kicad_pcb",
"schematic": ".kicad_sch",
"design_rules": ".kicad_dru",
"worksheet": ".kicad_wks",
"footprint": ".kicad_mod",
"netlist": "_netlist.net",
"kibot_config": ".kibot.yaml",
}
# Additional data file extensions that may be part of KiCad projects
# Includes manufacturing files, component data, and export formats
DATA_EXTENSIONS = [
".csv", # BOM or other data
".pos", # Component position file
".net", # Netlist files
".zip", # Gerber files and other archives
".drl", # Drill files
]
# Default parameters for circuit creation and component placement
# Values in mm unless otherwise specified, following KiCad conventions
CIRCUIT_DEFAULTS = {
"grid_spacing": 1.0, # Default grid spacing in mm for user coordinates
"component_spacing": 10.16, # Default component spacing in mm
"wire_width": 6, # Default wire width in KiCad units (0.006 inch)
"text_size": [1.27, 1.27], # Default text size in mm
"pin_length": 2.54, # Default pin length in mm
}
# Predefined component library mappings for quick circuit creation
# Maps common component types to their KiCad library and symbol names
# Organized by functional categories: basic, power, connectors
COMMON_LIBRARIES = {
"basic": {
"resistor": {"library": "Device", "symbol": "R"},
"capacitor": {"library": "Device", "symbol": "C"},
"inductor": {"library": "Device", "symbol": "L"},
"led": {"library": "Device", "symbol": "LED"},
"diode": {"library": "Device", "symbol": "D"},
},
"power": {
"vcc": {"library": "power", "symbol": "VCC"},
"gnd": {"library": "power", "symbol": "GND"},
"+5v": {"library": "power", "symbol": "+5V"},
"+3v3": {"library": "power", "symbol": "+3V3"},
"+12v": {"library": "power", "symbol": "+12V"},
"-12v": {"library": "power", "symbol": "-12V"},
},
"connectors": {
"conn_2pin": {"library": "Connector", "symbol": "Conn_01x02_Male"},
"conn_4pin": {"library": "Connector_Generic", "symbol": "Conn_01x04"},
"conn_8pin": {"library": "Connector_Generic", "symbol": "Conn_01x08"},
},
}
# Suggested footprints for common components, ordered by preference
# SMD variants listed first, followed by through-hole alternatives
DEFAULT_FOOTPRINTS = {
"R": [
"Resistor_SMD:R_0805_2012Metric",
"Resistor_SMD:R_0603_1608Metric",
"Resistor_THT:R_Axial_DIN0207_L6.3mm_D2.5mm_P10.16mm_Horizontal",
],
"C": [
"Capacitor_SMD:C_0805_2012Metric",
"Capacitor_SMD:C_0603_1608Metric",
"Capacitor_THT:C_Disc_D5.0mm_W2.5mm_P5.00mm",
],
"LED": ["LED_SMD:LED_0805_2012Metric", "LED_THT:LED_D5.0mm"],
"D": ["Diode_SMD:D_SOD-123", "Diode_THT:D_DO-35_SOD27_P7.62mm_Horizontal"],
}
# Operation timeout values in seconds for external process management
# Prevents hanging operations and provides user feedback
TIMEOUT_CONSTANTS = {
"kicad_cli_version_check": 10.0, # Timeout for KiCad CLI version checks
"kicad_cli_export": 30.0, # Timeout for KiCad CLI export operations
"application_open": 10.0, # Timeout for opening applications (e.g., KiCad)
"subprocess_default": 30.0, # Default timeout for subprocess operations
}
# Progress percentage milestones for long-running operations
# Provides consistent progress reporting across different tools
PROGRESS_CONSTANTS = {
"start": 10, # Initial progress percentage
"detection": 20, # Progress after CLI detection
"setup": 30, # Progress after setup complete
"processing": 50, # Progress during processing
"finishing": 70, # Progress when finishing up
"validation": 90, # Progress during validation
"complete": 100, # Progress when complete
}
# User interface display configuration values
# Controls how much information is shown in previews and summaries
DISPLAY_CONSTANTS = {
"bom_preview_limit": 20, # Maximum number of BOM items to show in preview
}
# KiCad CLI timeout for operations
KICAD_CLI_TIMEOUT = TIMEOUT_CONSTANTS["subprocess_default"]
# Default KiCad paths for system detection
DEFAULT_KICAD_PATHS = [KICAD_APP_PATH, KICAD_USER_DIR]
# Component library mapping (alias for COMMON_LIBRARIES)
COMPONENT_LIBRARY_MAP = COMMON_LIBRARIES

View File

@ -1,94 +0,0 @@
"""
Lifespan context management for KiCad MCP Server.
"""
from collections.abc import AsyncIterator
from contextlib import asynccontextmanager
from dataclasses import dataclass
import logging # Import logging
from typing import Any
from fastmcp import FastMCP
# Get PID for logging
# _PID = os.getpid()
@dataclass
class KiCadAppContext:
"""Type-safe context for KiCad MCP server."""
kicad_modules_available: bool
# Optional cache for expensive operations
cache: dict[str, Any]
@asynccontextmanager
async def kicad_lifespan(
server: FastMCP, kicad_modules_available: bool = False
) -> AsyncIterator[KiCadAppContext]:
"""Manage KiCad MCP server lifecycle with type-safe context.
This function handles:
1. Initializing shared resources when the server starts
2. Providing a typed context object to all request handlers
3. Properly cleaning up resources when the server shuts down
Args:
server: The FastMCP server instance
kicad_modules_available: Flag indicating if Python modules were found (passed from create_server)
Yields:
KiCadAppContext: A typed context object shared across all handlers
"""
logging.info("Starting KiCad MCP server initialization")
# Resources initialization - Python path setup removed
# print("Setting up KiCad Python modules")
# kicad_modules_available = setup_kicad_python_path() # Now passed as arg
logging.info(
f"KiCad Python module availability: {kicad_modules_available} (Setup logic removed)"
)
# Create in-memory cache for expensive operations
cache: dict[str, Any] = {}
# Initialize any other resources that need cleanup later
created_temp_dirs = [] # Assuming this is managed elsewhere or not needed for now
try:
# --- Removed Python module preloading section ---
# if kicad_modules_available:
# try:
# print("Preloading KiCad Python modules")
# ...
# except ImportError as e:
# print(f"Failed to preload some KiCad modules: {str(e)}")
# Yield the context to the server - server runs during this time
logging.info("KiCad MCP server initialization complete")
yield KiCadAppContext(
kicad_modules_available=kicad_modules_available, # Pass the flag through
cache=cache,
)
finally:
# Clean up resources when server shuts down
logging.info("Shutting down KiCad MCP server")
# Clear the cache
if cache:
logging.info(f"Clearing cache with {len(cache)} entries")
cache.clear()
# Clean up any temporary directories
import shutil
for temp_dir in created_temp_dirs:
try:
logging.info(f"Removing temporary directory: {temp_dir}")
shutil.rmtree(temp_dir, ignore_errors=True)
except Exception as e:
logging.error(f"Error cleaning up temporary directory {temp_dir}: {str(e)}")
logging.info("KiCad MCP server shutdown complete")

View File

@ -1,3 +0,0 @@
"""
Prompt templates for KiCad MCP Server.
"""

View File

@ -1,118 +0,0 @@
"""
BOM-related prompt templates for KiCad.
"""
from fastmcp import FastMCP
def register_bom_prompts(mcp: FastMCP) -> None:
"""Register BOM-related prompt templates with the MCP server.
Args:
mcp: The FastMCP server instance
"""
@mcp.prompt()
def analyze_components() -> str:
"""Prompt for analyzing a KiCad project's components."""
prompt = """
I'd like to analyze the components used in my KiCad PCB design. Can you help me with:
1. Identifying all the components in my design
2. Analyzing the distribution of component types
3. Checking for any potential issues or opportunities for optimization
4. Suggesting any alternatives for hard-to-find or expensive components
My KiCad project is located at:
[Enter the full path to your .kicad_pro file here]
Please use the BOM analysis tools to help me understand my component usage.
"""
return prompt
@mcp.prompt()
def cost_estimation() -> str:
"""Prompt for estimating project costs based on BOM."""
prompt = """
I need to estimate the cost of my KiCad PCB project for:
1. A prototype run (1-5 boards)
2. A small production run (10-100 boards)
3. Larger scale production (500+ boards)
My KiCad project is located at:
[Enter the full path to your .kicad_pro file here]
Please analyze my BOM to help estimate component costs, and provide guidance on:
- Which components contribute most to the overall cost
- Where I might find cost savings
- Potential volume discounts for larger runs
- Suggestions for alternative components that could reduce costs
- Estimated PCB fabrication costs based on board size and complexity
If my BOM doesn't include cost data, please suggest how I might find pricing information for my components.
"""
return prompt
@mcp.prompt()
def bom_export_help() -> str:
"""Prompt for assistance with exporting BOMs from KiCad."""
prompt = """
I need help exporting a Bill of Materials (BOM) from my KiCad project. I'm interested in:
1. Understanding the different BOM export options in KiCad
2. Exporting a BOM with specific fields (reference, value, footprint, etc.)
3. Generating a BOM in a format compatible with my preferred supplier
4. Adding custom fields to my components that will appear in the BOM
My KiCad project is located at:
[Enter the full path to your .kicad_pro file here]
Please guide me through the process of creating a well-structured BOM for my project.
"""
return prompt
@mcp.prompt()
def component_sourcing() -> str:
"""Prompt for help with component sourcing."""
prompt = """
I need help sourcing components for my KiCad PCB project. Specifically, I need assistance with:
1. Identifying reliable suppliers for my components
2. Finding alternatives for any hard-to-find or obsolete parts
3. Understanding lead times and availability constraints
4. Balancing cost versus quality considerations
My KiCad project is located at:
[Enter the full path to your .kicad_pro file here]
Please analyze my BOM and provide guidance on sourcing these components efficiently.
"""
return prompt
@mcp.prompt()
def bom_comparison() -> str:
"""Prompt for comparing BOMs between two design revisions."""
prompt = """
I have two versions of a KiCad project and I'd like to compare the changes between their Bills of Materials. I need to understand:
1. Which components were added or removed
2. Which component values or footprints changed
3. The impact of these changes on the overall design
4. Any potential issues introduced by these changes
My original KiCad project is located at:
[Enter the full path to your first .kicad_pro file here]
My revised KiCad project is located at:
[Enter the full path to your second .kicad_pro file here]
Please analyze the BOMs from both projects and help me understand the differences between them.
"""
return prompt

View File

@ -1,50 +0,0 @@
"""
DRC prompt templates for KiCad PCB design.
"""
from fastmcp import FastMCP
def register_drc_prompts(mcp: FastMCP) -> None:
"""Register DRC prompt templates with the MCP server.
Args:
mcp: The FastMCP server instance
"""
@mcp.prompt()
def fix_drc_violations() -> str:
"""Prompt for assistance with fixing DRC violations."""
return """
I'm trying to fix DRC (Design Rule Check) violations in my KiCad PCB design. I need help with:
1. Understanding what these DRC errors mean
2. Knowing how to fix each type of violation
3. Best practices for preventing DRC issues in future designs
Here are the specific DRC errors I'm seeing (please list errors from your DRC report, or use the kicad://drc/path_to_project resource to see your full DRC report):
[list your DRC errors here]
Please help me understand these errors and provide step-by-step guidance on fixing them.
"""
@mcp.prompt()
def custom_design_rules() -> str:
"""Prompt for assistance with creating custom design rules."""
return """
I want to create custom design rules for my KiCad PCB. My project has the following requirements:
1. [Describe your project's specific requirements]
2. [List any special considerations like high voltage, high current, RF, etc.]
3. [Mention any manufacturing constraints]
Please help me set up appropriate design rules for my KiCad project, including:
- Minimum trace width and clearance settings
- Via size and drill constraints
- Layer stack considerations
- Other important design rules
Explain how to configure these rules in KiCad and how to verify they're being applied correctly.
"""

View File

@ -1,146 +0,0 @@
"""
Prompt templates for circuit pattern analysis in KiCad.
"""
from fastmcp import FastMCP
def register_pattern_prompts(mcp: FastMCP) -> None:
"""Register pattern-related prompt templates with the MCP server.
Args:
mcp: The FastMCP server instance
"""
@mcp.prompt()
def analyze_circuit_patterns() -> str:
"""Prompt for circuit pattern analysis."""
prompt = """
I'd like to analyze the circuit patterns in my KiCad design. Can you help me identify:
1. What common circuit blocks are present in my design
2. Which components are part of each circuit block
3. The function of each identified circuit block
4. Any potential design issues in these circuits
My KiCad project is located at:
[Enter path to your .kicad_pro file]
Please identify as many common patterns as possible (power supplies, amplifiers, filters, etc.)
"""
return prompt
@mcp.prompt()
def analyze_power_supplies() -> str:
"""Prompt for power supply circuit analysis."""
prompt = """
I need help analyzing the power supply circuits in my KiCad design. Can you help me:
1. Identify all the power supply circuits in my schematic
2. Determine what voltage levels they provide
3. Check if they're properly designed with appropriate components
4. Suggest any improvements or optimizations
My KiCad schematic is located at:
[Enter path to your .kicad_sch file]
Please focus on both linear regulators and switching power supplies.
"""
return prompt
@mcp.prompt()
def analyze_sensor_interfaces() -> str:
"""Prompt for sensor interface analysis."""
prompt = """
I want to review all the sensor interfaces in my KiCad design. Can you help me:
1. Identify all sensors in my schematic
2. Determine what each sensor measures and how it interfaces with the system
3. Check if the sensor connections follow best practices
4. Suggest any improvements for sensor integration
My KiCad project is located at:
[Enter path to your .kicad_pro file]
Please identify temperature, pressure, motion, light, and any other sensors in the design.
"""
return prompt
@mcp.prompt()
def analyze_microcontroller_connections() -> str:
"""Prompt for microcontroller connection analysis."""
prompt = """
I want to review how my microcontroller is connected to other circuits in my KiCad design. Can you help me:
1. Identify the microcontroller(s) in my schematic
2. Map out what peripherals and circuits are connected to each pin
3. Check if the connections follow good design practices
4. Identify any potential issues or conflicts
My KiCad schematic is located at:
[Enter path to your .kicad_sch file]
Please focus on interface circuits (SPI, I2C, UART), sensor connections, and power supply connections.
"""
return prompt
@mcp.prompt()
def find_and_improve_circuits() -> str:
"""Prompt for finding and improving specific circuits."""
prompt = """
I'm looking to improve specific circuit patterns in my KiCad design. Can you help me:
1. Find all instances of [CIRCUIT_TYPE] circuits in my schematic
2. Evaluate if they are designed correctly
3. Suggest modern alternatives or improvements
4. Recommend specific component changes if needed
My KiCad project is located at:
[Enter path to your .kicad_pro file]
Please replace [CIRCUIT_TYPE] with the type of circuit you're interested in (e.g., "filter", "amplifier", "power supply", etc.)
"""
return prompt
@mcp.prompt()
def compare_circuit_patterns() -> str:
"""Prompt for comparing circuit patterns across designs."""
prompt = """
I want to compare circuit patterns across multiple KiCad designs. Can you help me:
1. Identify common circuit patterns in these designs
2. Compare how similar circuits are implemented across the designs
3. Identify which implementation is most optimal
4. Suggest best practices based on the comparison
My KiCad projects are located at:
[Enter paths to multiple .kicad_pro files]
Please focus on identifying differences in approaches to the same functional circuit blocks.
"""
return prompt
@mcp.prompt()
def explain_circuit_function() -> str:
"""Prompt for explaining the function of identified circuits."""
prompt = """
I'd like to understand the function of the circuits in my KiCad design. Can you help me:
1. Identify the main circuit blocks in my schematic
2. Explain how each circuit block works in detail
3. Describe how they interact with each other
4. Explain the overall signal flow through the system
My KiCad schematic is located at:
[Enter path to your .kicad_sch file]
Please provide explanations that would help someone unfamiliar with the design understand it.
"""
return prompt

View File

@ -1,60 +0,0 @@
"""
Prompt templates for KiCad interactions.
"""
from fastmcp import FastMCP
def register_prompts(mcp: FastMCP) -> None:
"""Register prompt templates with the MCP server.
Args:
mcp: The FastMCP server instance
"""
@mcp.prompt()
def create_new_component() -> str:
"""Prompt for creating a new KiCad component."""
prompt = """
I want to create a new component in KiCad for my PCB design. I need help with:
1. Deciding on the correct component package/footprint
2. Creating the schematic symbol
3. Connecting the schematic symbol to the footprint
4. Adding the component to my design
Please provide step-by-step instructions on how to create a new component in KiCad.
"""
return prompt
@mcp.prompt()
def debug_pcb_issues() -> str:
"""Prompt for debugging common PCB issues."""
prompt = """
I'm having issues with my KiCad PCB design. Can you help me troubleshoot the following problems:
1. Design rule check (DRC) errors
2. Electrical rule check (ERC) errors
3. Footprint mismatches
4. Routing challenges
Please provide a systematic approach to identifying and fixing these issues in KiCad.
"""
return prompt
@mcp.prompt()
def pcb_manufacturing_checklist() -> str:
"""Prompt for PCB manufacturing preparation checklist."""
prompt = """
I'm preparing to send my KiCad PCB design for manufacturing. Please help me with a comprehensive checklist of things to verify before submitting my design, including:
1. Design rule compliance
2. Layer stack configuration
3. Manufacturing notes and specifications
4. Required output files (Gerber, drill, etc.)
5. Component placement considerations
Please provide a detailed checklist I can follow to ensure my design is ready for manufacturing.
"""

View File

@ -1,3 +0,0 @@
"""
Resource handlers for KiCad MCP Server.
"""

View File

@ -1,289 +0,0 @@
"""
Bill of Materials (BOM) resources for KiCad projects.
"""
import json
import os
from fastmcp import FastMCP
import pandas as pd
# Import the helper functions from bom_tools.py to avoid code duplication
from mckicad.tools.bom_tools import analyze_bom_data, parse_bom_file
from mckicad.utils.file_utils import get_project_files
def register_bom_resources(mcp: FastMCP) -> None:
"""Register BOM-related resources with the MCP server.
Args:
mcp: The FastMCP server instance
"""
@mcp.resource("kicad://bom/{project_path}")
def get_bom_resource(project_path: str) -> str:
"""Get a formatted BOM report for a KiCad project.
Args:
project_path: Path to the KiCad project file (.kicad_pro)
Returns:
Markdown-formatted BOM report
"""
print(f"Generating BOM report for project: {project_path}")
if not os.path.exists(project_path):
return f"Project not found: {project_path}"
# Get all project files
files = get_project_files(project_path)
# Look for BOM files
bom_files = {}
for file_type, file_path in files.items():
if "bom" in file_type.lower() or file_path.lower().endswith(".csv"):
bom_files[file_type] = file_path
print(f"Found potential BOM file: {file_path}")
if not bom_files:
print("No BOM files found for project")
return f"# BOM Report\n\nNo BOM files found for project: {os.path.basename(project_path)}.\n\nExport a BOM from KiCad first, or use the `export_bom_csv` tool to generate one."
# Format as Markdown report
project_name = os.path.basename(project_path)[:-10] # Remove .kicad_pro
report = f"# Bill of Materials for {project_name}\n\n"
# Process each BOM file
for file_type, file_path in bom_files.items():
try:
# Parse and analyze the BOM
bom_data, format_info = parse_bom_file(file_path)
if not bom_data:
report += f"## {file_type}\n\nFailed to parse BOM file: {os.path.basename(file_path)}\n\n"
continue
analysis = analyze_bom_data(bom_data, format_info)
# Add file section
report += f"## {file_type.capitalize()}\n\n"
report += f"**File**: {os.path.basename(file_path)}\n\n"
report += f"**Format**: {format_info.get('detected_format', 'Unknown')}\n\n"
# Add summary
report += "### Summary\n\n"
report += f"- **Total Components**: {analysis.get('total_component_count', 0)}\n"
report += f"- **Unique Components**: {analysis.get('unique_component_count', 0)}\n"
# Add cost if available
if analysis.get("has_cost_data", False) and "total_cost" in analysis:
currency = analysis.get("currency", "USD")
currency_symbols = {"USD": "$", "EUR": "", "GBP": "£"}
symbol = currency_symbols.get(currency, "")
report += f"- **Estimated Cost**: {symbol}{analysis['total_cost']} {currency}\n"
report += "\n"
# Add categories breakdown
if "categories" in analysis and analysis["categories"]:
report += "### Component Categories\n\n"
for category, count in analysis["categories"].items():
report += f"- **{category}**: {count}\n"
report += "\n"
# Add most common components if available
if "most_common_values" in analysis and analysis["most_common_values"]:
report += "### Most Common Components\n\n"
for value, count in analysis["most_common_values"].items():
report += f"- **{value}**: {count}\n"
report += "\n"
# Add component table (first 20 items)
if bom_data:
report += "### Component List\n\n"
# Try to identify key columns
columns = []
if format_info.get("header_fields"):
# Use a subset of columns for readability
preferred_cols = [
"Reference",
"Value",
"Footprint",
"Quantity",
"Description",
]
# Find matching columns (case-insensitive)
header_lower = [h.lower() for h in format_info["header_fields"]]
for col in preferred_cols:
col_lower = col.lower()
if col_lower in header_lower:
idx = header_lower.index(col_lower)
columns.append(format_info["header_fields"][idx])
# If we didn't find any preferred columns, use the first 4
if not columns and len(format_info["header_fields"]) > 0:
columns = format_info["header_fields"][
: min(4, len(format_info["header_fields"]))
]
# Generate the table header
if columns:
report += "| " + " | ".join(columns) + " |\n"
report += "| " + " | ".join(["---"] * len(columns)) + " |\n"
# Add rows (limit to first 20 for readability)
for i, component in enumerate(bom_data[:20]):
row = []
for col in columns:
value = component.get(col, "")
# Clean up cell content for Markdown table
value = str(value).replace("|", "\\|").replace("\n", " ")
row.append(value)
report += "| " + " | ".join(row) + " |\n"
# Add note if there are more components
if len(bom_data) > 20:
report += f"\n*...and {len(bom_data) - 20} more components*\n"
else:
report += "*Component table could not be generated - column headers not recognized*\n"
report += "\n---\n\n"
except Exception as e:
print(f"Error processing BOM file {file_path}: {str(e)}")
report += f"## {file_type}\n\nError processing BOM file: {str(e)}\n\n"
# Add export instructions
report += "## How to Export a BOM\n\n"
report += "To generate a new BOM from your KiCad project:\n\n"
report += "1. Open your schematic in KiCad\n"
report += "2. Go to **Tools → Generate BOM**\n"
report += "3. Choose a BOM plugin and click **Generate**\n"
report += "4. Save the BOM file in your project directory\n\n"
report += "Alternatively, use the `export_bom_csv` tool in this MCP server to generate a BOM file.\n"
return report
@mcp.resource("kicad://bom/{project_path}/csv")
def get_bom_csv_resource(project_path: str) -> str:
"""Get a CSV representation of the BOM for a KiCad project.
Args:
project_path: Path to the KiCad project file (.kicad_pro)
Returns:
CSV-formatted BOM data
"""
print(f"Generating CSV BOM for project: {project_path}")
if not os.path.exists(project_path):
return f"Project not found: {project_path}"
# Get all project files
files = get_project_files(project_path)
# Look for BOM files
bom_files = {}
for file_type, file_path in files.items():
if "bom" in file_type.lower() or file_path.lower().endswith(".csv"):
bom_files[file_type] = file_path
print(f"Found potential BOM file: {file_path}")
if not bom_files:
print("No BOM files found for project")
return "No BOM files found for project. Export a BOM from KiCad first."
# Use the first BOM file found
file_type = next(iter(bom_files))
file_path = bom_files[file_type]
try:
# If it's already a CSV, just return its contents
if file_path.lower().endswith(".csv"):
with open(file_path, encoding="utf-8-sig") as f:
return f.read()
# Otherwise, try to parse and convert to CSV
bom_data, format_info = parse_bom_file(file_path)
if not bom_data:
return f"Failed to parse BOM file: {file_path}"
# Convert to DataFrame and then to CSV
df = pd.DataFrame(bom_data)
return df.to_csv(index=False)
except Exception as e:
print(f"Error generating CSV from BOM file: {str(e)}")
return f"Error generating CSV from BOM file: {str(e)}"
@mcp.resource("kicad://bom/{project_path}/json")
def get_bom_json_resource(project_path: str) -> str:
"""Get a JSON representation of the BOM for a KiCad project.
Args:
project_path: Path to the KiCad project file (.kicad_pro)
Returns:
JSON-formatted BOM data
"""
print(f"Generating JSON BOM for project: {project_path}")
if not os.path.exists(project_path):
return f"Project not found: {project_path}"
# Get all project files
files = get_project_files(project_path)
# Look for BOM files
bom_files = {}
for file_type, file_path in files.items():
if "bom" in file_type.lower() or file_path.lower().endswith((".csv", ".json")):
bom_files[file_type] = file_path
print(f"Found potential BOM file: {file_path}")
if not bom_files:
print("No BOM files found for project")
return json.dumps({"error": "No BOM files found for project"}, indent=2)
try:
# Collect data from all BOM files
result = {"project": os.path.basename(project_path)[:-10], "bom_files": {}}
for file_type, file_path in bom_files.items():
# If it's already JSON, parse it directly
if file_path.lower().endswith(".json"):
with open(file_path) as f:
try:
result["bom_files"][file_type] = json.load(f)
continue
except:
# If JSON parsing fails, fall back to regular parsing
pass
# Otherwise parse with our utility
bom_data, format_info = parse_bom_file(file_path)
if bom_data:
analysis = analyze_bom_data(bom_data, format_info)
result["bom_files"][file_type] = {
"file": os.path.basename(file_path),
"format": format_info,
"analysis": analysis,
"components": bom_data,
}
return json.dumps(result, indent=2, default=str)
except Exception as e:
print(f"Error generating JSON from BOM file: {str(e)}")
return json.dumps({"error": str(e)}, indent=2)

View File

@ -1,261 +0,0 @@
"""
Design Rule Check (DRC) resources for KiCad PCB files.
"""
import os
from fastmcp import FastMCP
from mckicad.tools.drc_impl.cli_drc import run_drc_via_cli
from mckicad.utils.drc_history import get_drc_history
from mckicad.utils.file_utils import get_project_files
def register_drc_resources(mcp: FastMCP) -> None:
"""Register DRC resources with the MCP server.
Args:
mcp: The FastMCP server instance
"""
@mcp.resource("kicad://drc/history/{project_path}")
def get_drc_history_report(project_path: str) -> str:
"""Get a formatted DRC history report for a KiCad project.
Args:
project_path: Path to the KiCad project file (.kicad_pro)
Returns:
Markdown-formatted DRC history report
"""
print(f"Generating DRC history report for project: {project_path}")
if not os.path.exists(project_path):
return f"Project not found: {project_path}"
# Get history entries
history_entries = get_drc_history(project_path)
if not history_entries:
return (
"# DRC History\n\nNo DRC history available for this project. Run a DRC check first."
)
# Format results as Markdown
project_name = os.path.basename(project_path)[:-10] # Remove .kicad_pro
report = f"# DRC History for {project_name}\n\n"
# Add trend visualization
if len(history_entries) >= 2:
report += "## Trend\n\n"
# Create a simple ASCII chart of violations over time
report += "```\n"
report += "Violations\n"
# Find min/max for scaling
max_violations = max(entry.get("total_violations", 0) for entry in history_entries)
if max_violations < 10:
max_violations = 10 # Minimum scale
# Generate chart (10 rows high)
for i in range(10, 0, -1):
threshold = (i / 10) * max_violations
report += f"{int(threshold):4d} |"
for entry in reversed(history_entries): # Oldest to newest
violations = entry.get("total_violations", 0)
if violations >= threshold:
report += "*"
else:
report += " "
report += "\n"
# Add x-axis
report += " " + "-" * len(history_entries) + "\n"
report += " "
# Add dates (shortened)
for entry in reversed(history_entries):
date = entry.get("datetime", "")
if date:
# Just show month/day
shortened = date.split(" ")[0].split("-")[-2:]
report += shortened[-2][0] # First letter of month
report += "\n```\n"
# Add history table
report += "## History Entries\n\n"
report += "| Date | Time | Violations | Categories |\n"
report += "| ---- | ---- | ---------- | ---------- |\n"
for entry in history_entries:
date_time = entry.get("datetime", "Unknown")
if " " in date_time:
date, time = date_time.split(" ")
else:
date, time = date_time, ""
violations = entry.get("total_violations", 0)
categories = entry.get("violation_categories", {})
category_count = len(categories)
report += f"| {date} | {time} | {violations} | {category_count} |\n"
# Add detailed information about the most recent run
if history_entries:
most_recent = history_entries[0]
report += "\n## Most Recent Check Details\n\n"
report += f"**Date:** {most_recent.get('datetime', 'Unknown')}\n\n"
report += f"**Total Violations:** {most_recent.get('total_violations', 0)}\n\n"
categories = most_recent.get("violation_categories", {})
if categories:
report += "**Violation Categories:**\n\n"
for category, count in categories.items():
report += f"- {category}: {count}\n"
# Add comparison with first run if available
if len(history_entries) > 1:
first_run = history_entries[-1]
first_violations = first_run.get("total_violations", 0)
current_violations = most_recent.get("total_violations", 0)
report += "\n## Progress Since First Check\n\n"
report += f"**First Check Date:** {first_run.get('datetime', 'Unknown')}\n"
report += f"**First Check Violations:** {first_violations}\n"
report += f"**Current Violations:** {current_violations}\n"
if first_violations > current_violations:
fixed = first_violations - current_violations
report += f"**Progress:** You've fixed {fixed} violations! 🎉\n"
elif first_violations < current_violations:
added = current_violations - first_violations
report += f"**Alert:** {added} new violations have been introduced since the first check.\n"
else:
report += "**Status:** The number of violations has remained the same since the first check.\n"
return report
@mcp.resource("kicad://drc/{project_path}")
def get_drc_report(project_path: str) -> str:
"""Get a formatted DRC report for a KiCad project.
Args:
project_path: Path to the KiCad project file (.kicad_pro)
Returns:
Markdown-formatted DRC report
"""
print(f"Generating DRC report for project: {project_path}")
if not os.path.exists(project_path):
return f"Project not found: {project_path}"
# Get PCB file from project
files = get_project_files(project_path)
if "pcb" not in files:
return "PCB file not found in project"
pcb_file = files["pcb"]
print(f"Found PCB file: {pcb_file}")
# Try to run DRC via command line
drc_results = run_drc_via_cli(pcb_file)
if not drc_results["success"]:
error_message = drc_results.get("error", "Unknown error")
return f"# DRC Check Failed\n\nError: {error_message}"
# Format results as Markdown
project_name = os.path.basename(project_path)[:-10] # Remove .kicad_pro
pcb_name = os.path.basename(pcb_file)
report = f"# Design Rule Check Report for {project_name}\n\n"
report += f"PCB file: `{pcb_name}`\n\n"
# Add summary
total_violations = drc_results.get("total_violations", 0)
report += "## Summary\n\n"
if total_violations == 0:
report += "✅ **No DRC violations found**\n\n"
else:
report += f"❌ **{total_violations} DRC violations found**\n\n"
# Add violation categories
categories = drc_results.get("violation_categories", {})
if categories:
report += "## Violation Categories\n\n"
for category, count in categories.items():
report += f"- **{category}**: {count} violations\n"
report += "\n"
# Add detailed violations
violations = drc_results.get("violations", [])
if violations:
report += "## Detailed Violations\n\n"
# Limit to first 50 violations to keep the report manageable
displayed_violations = violations[:50]
for i, violation in enumerate(displayed_violations, 1):
message = violation.get("message", "Unknown error")
severity = violation.get("severity", "error")
# Extract location information if available
location = violation.get("location", {})
x = location.get("x", 0)
y = location.get("y", 0)
report += f"### Violation {i}\n\n"
report += f"- **Type**: {message}\n"
report += f"- **Severity**: {severity}\n"
if x != 0 or y != 0:
report += f"- **Location**: X={x:.2f}mm, Y={y:.2f}mm\n"
report += "\n"
if len(violations) > 50:
report += f"*...and {len(violations) - 50} more violations (use the `run_drc_check` tool for complete results)*\n\n"
# Add recommendations
report += "## Recommendations\n\n"
if total_violations == 0:
report += (
"Your PCB design passes all design rule checks. It's ready for manufacturing!\n\n"
)
else:
report += "To fix these violations:\n\n"
report += "1. Open your PCB in KiCad's PCB Editor\n"
report += "2. Run the DRC by clicking the 'Inspect → Design Rules Checker' menu item\n"
report += "3. Click on each error in the DRC window to locate it on the PCB\n"
report += "4. Fix the issue according to the error message\n"
report += "5. Re-run DRC to verify your fixes\n\n"
# Add common solutions for frequent error types
if categories:
most_common_error = max(categories.items(), key=lambda x: x[1])[0]
report += "### Common Solutions\n\n"
if "clearance" in most_common_error.lower():
report += "**For clearance violations:**\n"
report += "- Reroute traces to maintain minimum clearance requirements\n"
report += "- Check layer stackup and adjust clearance rules if necessary\n"
report += "- Consider adjusting trace widths\n\n"
elif "width" in most_common_error.lower():
report += "**For width violations:**\n"
report += "- Increase trace widths to meet minimum requirements\n"
report += "- Check current requirements for your traces\n\n"
elif "drill" in most_common_error.lower():
report += "**For drill violations:**\n"
report += "- Adjust hole sizes to meet manufacturing constraints\n"
report += "- Check via settings\n\n"
return report

View File

@ -1,48 +0,0 @@
"""
File content resources for KiCad files.
"""
import os
from fastmcp import FastMCP
def register_file_resources(mcp: FastMCP) -> None:
"""Register file-related resources with the MCP server.
Args:
mcp: The FastMCP server instance
"""
@mcp.resource("kicad://schematic/{schematic_path}")
def get_schematic_info(schematic_path: str) -> str:
"""Extract information from a KiCad schematic file."""
if not os.path.exists(schematic_path):
return f"Schematic file not found: {schematic_path}"
# KiCad schematic files are in S-expression format (not JSON)
# This is a basic extraction of text-based information
try:
with open(schematic_path) as f:
content = f.read()
# Basic extraction of components
components = []
for line in content.split("\n"):
if "(symbol " in line and "lib_id" in line:
components.append(line.strip())
result = f"# Schematic: {os.path.basename(schematic_path)}\n\n"
result += f"## Components (Estimated Count: {len(components)})\n\n"
# Extract a sample of components
for i, comp in enumerate(components[:10]):
result += f"{comp}\n"
if len(components) > 10:
result += f"\n... and {len(components) - 10} more components\n"
return result
except Exception as e:
return f"Error reading schematic file: {str(e)}"

View File

@ -1,262 +0,0 @@
"""
Netlist resources for KiCad schematics.
"""
import os
from fastmcp import FastMCP
from mckicad.utils.file_utils import get_project_files
from mckicad.utils.netlist_parser import analyze_netlist, extract_netlist
def register_netlist_resources(mcp: FastMCP) -> None:
"""Register netlist-related resources with the MCP server.
Args:
mcp: The FastMCP server instance
"""
@mcp.resource("kicad://netlist/{schematic_path}")
def get_netlist_resource(schematic_path: str) -> str:
"""Get a formatted netlist report for a KiCad schematic.
Args:
schematic_path: Path to the KiCad schematic file (.kicad_sch)
Returns:
Markdown-formatted netlist report
"""
print(f"Generating netlist report for schematic: {schematic_path}")
if not os.path.exists(schematic_path):
return f"Schematic file not found: {schematic_path}"
try:
# Extract netlist information
netlist_data = extract_netlist(schematic_path)
if "error" in netlist_data:
return f"# Netlist Extraction Error\n\nError: {netlist_data['error']}"
# Analyze the netlist
analysis_results = analyze_netlist(netlist_data)
# Format as Markdown report
schematic_name = os.path.basename(schematic_path)
report = f"# Netlist Analysis for {schematic_name}\n\n"
# Overview section
report += "## Overview\n\n"
report += f"- **Components**: {netlist_data['component_count']}\n"
report += f"- **Nets**: {netlist_data['net_count']}\n"
if "total_pin_connections" in analysis_results:
report += f"- **Pin Connections**: {analysis_results['total_pin_connections']}\n"
report += "\n"
# Component Types section
if "component_types" in analysis_results and analysis_results["component_types"]:
report += "## Component Types\n\n"
for comp_type, count in analysis_results["component_types"].items():
report += f"- **{comp_type}**: {count}\n"
report += "\n"
# Power Nets section
if "power_nets" in analysis_results and analysis_results["power_nets"]:
report += "## Power Nets\n\n"
for net_name in analysis_results["power_nets"]:
report += f"- **{net_name}**\n"
report += "\n"
# Components section
components = netlist_data.get("components", {})
if components:
report += "## Component List\n\n"
report += "| Reference | Type | Value | Footprint |\n"
report += "|-----------|------|-------|----------|\n"
# Sort components by reference
for ref in sorted(components.keys()):
component = components[ref]
lib_id = component.get("lib_id", "Unknown")
value = component.get("value", "")
footprint = component.get("footprint", "")
report += f"| {ref} | {lib_id} | {value} | {footprint} |\n"
report += "\n"
# Nets section (limit to showing first 20 for readability)
nets = netlist_data.get("nets", {})
if nets:
report += "## Net List\n\n"
# Filter to show only the first 20 nets
net_items = list(nets.items())[:20]
for net_name, pins in net_items:
report += f"### Net: {net_name}\n\n"
if pins:
report += "**Connected Pins:**\n\n"
for pin in pins:
component = pin.get("component", "Unknown")
pin_num = pin.get("pin", "Unknown")
report += f"- {component}.{pin_num}\n"
else:
report += "*No connections found*\n"
report += "\n"
if len(nets) > 20:
report += f"*...and {len(nets) - 20} more nets*\n\n"
return report
except Exception as e:
return f"# Netlist Extraction Error\n\nError: {str(e)}"
@mcp.resource("kicad://project_netlist/{project_path}")
def get_project_netlist_resource(project_path: str) -> str:
"""Get a formatted netlist report for a KiCad project.
Args:
project_path: Path to the KiCad project file (.kicad_pro)
Returns:
Markdown-formatted netlist report
"""
print(f"Generating netlist report for project: {project_path}")
if not os.path.exists(project_path):
return f"Project not found: {project_path}"
# Get the schematic file
try:
files = get_project_files(project_path)
if "schematic" not in files:
return "Schematic file not found in project"
schematic_path = files["schematic"]
print(f"Found schematic file: {schematic_path}")
# Get the netlist resource for this schematic
return get_netlist_resource(schematic_path)
except Exception as e:
return f"# Netlist Extraction Error\n\nError: {str(e)}"
@mcp.resource("kicad://component/{schematic_path}/{component_ref}")
def get_component_resource(schematic_path: str, component_ref: str) -> str:
"""Get detailed information about a specific component and its connections.
Args:
schematic_path: Path to the KiCad schematic file (.kicad_sch)
component_ref: Component reference designator (e.g., R1)
Returns:
Markdown-formatted component report
"""
print(f"Generating component report for {component_ref} in schematic: {schematic_path}")
if not os.path.exists(schematic_path):
return f"Schematic file not found: {schematic_path}"
try:
# Extract netlist information
netlist_data = extract_netlist(schematic_path)
if "error" in netlist_data:
return f"# Component Analysis Error\n\nError: {netlist_data['error']}"
# Check if the component exists
components = netlist_data.get("components", {})
if component_ref not in components:
return (
f"# Component Not Found\n\nComponent {component_ref} was not found in the schematic.\n\n**Available Components**:\n\n"
+ "\n".join([f"- {ref}" for ref in sorted(components.keys())])
)
component_info = components[component_ref]
# Format as Markdown report
report = f"# Component Analysis: {component_ref}\n\n"
# Component Details section
report += "## Component Details\n\n"
report += f"- **Reference**: {component_ref}\n"
if "lib_id" in component_info:
report += f"- **Type**: {component_info['lib_id']}\n"
if "value" in component_info:
report += f"- **Value**: {component_info['value']}\n"
if "footprint" in component_info:
report += f"- **Footprint**: {component_info['footprint']}\n"
# Add other properties
if "properties" in component_info:
for prop_name, prop_value in component_info["properties"].items():
report += f"- **{prop_name}**: {prop_value}\n"
report += "\n"
# Pins section
if "pins" in component_info:
report += "## Pins\n\n"
for pin in component_info["pins"]:
report += f"- **Pin {pin['num']}**: {pin['name']}\n"
report += "\n"
# Connections section
report += "## Connections\n\n"
nets = netlist_data.get("nets", {})
connected_nets = []
for net_name, pins in nets.items():
# Check if any pin belongs to our component
for pin in pins:
if pin.get("component") == component_ref:
connected_nets.append(
{
"net_name": net_name,
"pin": pin.get("pin", "Unknown"),
"connections": [
p for p in pins if p.get("component") != component_ref
],
}
)
if connected_nets:
for net in connected_nets:
report += f"### Pin {net['pin']} - Net: {net['net_name']}\n\n"
if net["connections"]:
report += "**Connected To:**\n\n"
for conn in net["connections"]:
comp = conn.get("component", "Unknown")
pin = conn.get("pin", "Unknown")
report += f"- {comp}.{pin}\n"
else:
report += "*No connections*\n"
report += "\n"
else:
report += "*No connections found for this component*\n\n"
return report
except Exception as e:
return f"# Component Analysis Error\n\nError: {str(e)}"

View File

@ -1,300 +0,0 @@
"""
Circuit pattern recognition resources for KiCad schematics.
"""
import os
from fastmcp import FastMCP
from mckicad.utils.file_utils import get_project_files
from mckicad.utils.netlist_parser import extract_netlist
from mckicad.utils.pattern_recognition import (
identify_amplifiers,
identify_digital_interfaces,
identify_filters,
identify_microcontrollers,
identify_oscillators,
identify_power_supplies,
identify_sensor_interfaces,
)
def register_pattern_resources(mcp: FastMCP) -> None:
"""Register circuit pattern recognition resources with the MCP server.
Args:
mcp: The FastMCP server instance
"""
@mcp.resource("kicad://patterns/{schematic_path}")
def get_circuit_patterns_resource(schematic_path: str) -> str:
"""Get a formatted report of identified circuit patterns in a KiCad schematic.
Args:
schematic_path: Path to the KiCad schematic file (.kicad_sch)
Returns:
Markdown-formatted circuit pattern report
"""
if not os.path.exists(schematic_path):
return f"Schematic file not found: {schematic_path}"
try:
# Extract netlist information
netlist_data = extract_netlist(schematic_path)
if "error" in netlist_data:
return f"# Circuit Pattern Analysis Error\n\nError: {netlist_data['error']}"
components = netlist_data.get("components", {})
nets = netlist_data.get("nets", {})
# Identify circuit patterns
power_supplies = identify_power_supplies(components, nets)
amplifiers = identify_amplifiers(components, nets)
filters = identify_filters(components, nets)
oscillators = identify_oscillators(components, nets)
digital_interfaces = identify_digital_interfaces(components, nets)
microcontrollers = identify_microcontrollers(components)
sensor_interfaces = identify_sensor_interfaces(components, nets)
# Format as Markdown report
schematic_name = os.path.basename(schematic_path)
report = f"# Circuit Pattern Analysis for {schematic_name}\n\n"
# Add summary
total_patterns = (
len(power_supplies)
+ len(amplifiers)
+ len(filters)
+ len(oscillators)
+ len(digital_interfaces)
+ len(microcontrollers)
+ len(sensor_interfaces)
)
report += "## Summary\n\n"
report += f"- **Total Components**: {netlist_data['component_count']}\n"
report += f"- **Total Circuit Patterns Identified**: {total_patterns}\n\n"
report += "### Pattern Types\n\n"
report += f"- **Power Supply Circuits**: {len(power_supplies)}\n"
report += f"- **Amplifier Circuits**: {len(amplifiers)}\n"
report += f"- **Filter Circuits**: {len(filters)}\n"
report += f"- **Oscillator Circuits**: {len(oscillators)}\n"
report += f"- **Digital Interface Circuits**: {len(digital_interfaces)}\n"
report += f"- **Microcontroller Circuits**: {len(microcontrollers)}\n"
report += f"- **Sensor Interface Circuits**: {len(sensor_interfaces)}\n\n"
# Add detailed sections
if power_supplies:
report += "## Power Supply Circuits\n\n"
for i, ps in enumerate(power_supplies, 1):
ps_type = ps.get("type", "Unknown")
ps_subtype = ps.get("subtype", "")
report += f"### Power Supply {i}: {ps_subtype.upper() if ps_subtype else ps_type.title()}\n\n"
if ps_type == "linear_regulator":
report += "- **Type**: Linear Voltage Regulator\n"
report += f"- **Subtype**: {ps_subtype}\n"
report += f"- **Main Component**: {ps.get('main_component', 'Unknown')}\n"
report += f"- **Value**: {ps.get('value', 'Unknown')}\n"
report += f"- **Output Voltage**: {ps.get('output_voltage', 'Unknown')}\n"
elif ps_type == "switching_regulator":
report += "- **Type**: Switching Voltage Regulator\n"
report += (
f"- **Topology**: {ps_subtype.title() if ps_subtype else 'Unknown'}\n"
)
report += f"- **Main Component**: {ps.get('main_component', 'Unknown')}\n"
report += f"- **Inductor**: {ps.get('inductor', 'Unknown')}\n"
report += f"- **Value**: {ps.get('value', 'Unknown')}\n"
report += "\n"
if amplifiers:
report += "## Amplifier Circuits\n\n"
for i, amp in enumerate(amplifiers, 1):
amp_type = amp.get("type", "Unknown")
amp_subtype = amp.get("subtype", "")
report += f"### Amplifier {i}: {amp_subtype.upper() if amp_subtype else amp_type.title()}\n\n"
if amp_type == "operational_amplifier":
report += "- **Type**: Operational Amplifier\n"
report += f"- **Subtype**: {amp_subtype.replace('_', ' ').title() if amp_subtype else 'General Purpose'}\n"
report += f"- **Component**: {amp.get('component', 'Unknown')}\n"
report += f"- **Value**: {amp.get('value', 'Unknown')}\n"
elif amp_type == "transistor_amplifier":
report += "- **Type**: Transistor Amplifier\n"
report += f"- **Transistor Type**: {amp_subtype}\n"
report += f"- **Component**: {amp.get('component', 'Unknown')}\n"
report += f"- **Value**: {amp.get('value', 'Unknown')}\n"
elif amp_type == "audio_amplifier_ic":
report += "- **Type**: Audio Amplifier IC\n"
report += f"- **Component**: {amp.get('component', 'Unknown')}\n"
report += f"- **Value**: {amp.get('value', 'Unknown')}\n"
report += "\n"
if filters:
report += "## Filter Circuits\n\n"
for i, filt in enumerate(filters, 1):
filt_type = filt.get("type", "Unknown")
filt_subtype = filt.get("subtype", "")
report += f"### Filter {i}: {filt_subtype.upper() if filt_subtype else filt_type.title()}\n\n"
if filt_type == "passive_filter":
report += "- **Type**: Passive Filter\n"
report += f"- **Topology**: {filt_subtype.replace('_', ' ').upper() if filt_subtype else 'Unknown'}\n"
report += f"- **Components**: {', '.join(filt.get('components', []))}\n"
elif filt_type == "active_filter":
report += "- **Type**: Active Filter\n"
report += f"- **Main Component**: {filt.get('main_component', 'Unknown')}\n"
report += f"- **Value**: {filt.get('value', 'Unknown')}\n"
elif filt_type == "crystal_filter":
report += "- **Type**: Crystal Filter\n"
report += f"- **Component**: {filt.get('component', 'Unknown')}\n"
report += f"- **Value**: {filt.get('value', 'Unknown')}\n"
elif filt_type == "ceramic_filter":
report += "- **Type**: Ceramic Filter\n"
report += f"- **Component**: {filt.get('component', 'Unknown')}\n"
report += f"- **Value**: {filt.get('value', 'Unknown')}\n"
report += "\n"
if oscillators:
report += "## Oscillator Circuits\n\n"
for i, osc in enumerate(oscillators, 1):
osc_type = osc.get("type", "Unknown")
osc_subtype = osc.get("subtype", "")
report += f"### Oscillator {i}: {osc_subtype.upper() if osc_subtype else osc_type.title()}\n\n"
if osc_type == "crystal_oscillator":
report += "- **Type**: Crystal Oscillator\n"
report += f"- **Component**: {osc.get('component', 'Unknown')}\n"
report += f"- **Value**: {osc.get('value', 'Unknown')}\n"
report += f"- **Frequency**: {osc.get('frequency', 'Unknown')}\n"
report += f"- **Has Load Capacitors**: {'Yes' if osc.get('has_load_capacitors', False) else 'No'}\n"
elif osc_type == "oscillator_ic":
report += "- **Type**: Oscillator IC\n"
report += f"- **Component**: {osc.get('component', 'Unknown')}\n"
report += f"- **Value**: {osc.get('value', 'Unknown')}\n"
report += f"- **Frequency**: {osc.get('frequency', 'Unknown')}\n"
elif osc_type == "rc_oscillator":
report += "- **Type**: RC Oscillator\n"
report += f"- **Subtype**: {osc_subtype.replace('_', ' ').title() if osc_subtype else 'Unknown'}\n"
report += f"- **Component**: {osc.get('component', 'Unknown')}\n"
report += f"- **Value**: {osc.get('value', 'Unknown')}\n"
report += "\n"
if digital_interfaces:
report += "## Digital Interface Circuits\n\n"
for i, iface in enumerate(digital_interfaces, 1):
iface_type = iface.get("type", "Unknown")
report += f"### Interface {i}: {iface_type.replace('_', ' ').upper()}\n\n"
report += f"- **Type**: {iface_type.replace('_', ' ').title()}\n"
signals = iface.get("signals_found", [])
if signals:
report += f"- **Signals Found**: {', '.join(signals)}\n"
report += "\n"
if microcontrollers:
report += "## Microcontroller Circuits\n\n"
for i, mcu in enumerate(microcontrollers, 1):
mcu_type = mcu.get("type", "Unknown")
if mcu_type == "microcontroller":
report += f"### Microcontroller {i}: {mcu.get('model', mcu.get('family', 'Unknown'))}\n\n"
report += "- **Type**: Microcontroller\n"
report += f"- **Family**: {mcu.get('family', 'Unknown')}\n"
if "model" in mcu:
report += f"- **Model**: {mcu['model']}\n"
report += f"- **Component**: {mcu.get('component', 'Unknown')}\n"
if "common_usage" in mcu:
report += f"- **Common Usage**: {mcu['common_usage']}\n"
if "features" in mcu:
report += f"- **Features**: {mcu['features']}\n"
elif mcu_type == "development_board":
report += (
f"### Development Board {i}: {mcu.get('board_type', 'Unknown')}\n\n"
)
report += "- **Type**: Development Board\n"
report += f"- **Board Type**: {mcu.get('board_type', 'Unknown')}\n"
report += f"- **Component**: {mcu.get('component', 'Unknown')}\n"
report += f"- **Value**: {mcu.get('value', 'Unknown')}\n"
report += "\n"
if sensor_interfaces:
report += "## Sensor Interface Circuits\n\n"
for i, sensor in enumerate(sensor_interfaces, 1):
sensor_type = sensor.get("type", "Unknown")
sensor_subtype = sensor.get("subtype", "")
report += f"### Sensor {i}: {sensor_subtype.title() + ' ' if sensor_subtype else ''}{sensor_type.replace('_', ' ').title()}\n\n"
report += f"- **Type**: {sensor_type.replace('_', ' ').title()}\n"
if sensor_subtype:
report += f"- **Subtype**: {sensor_subtype}\n"
report += f"- **Component**: {sensor.get('component', 'Unknown')}\n"
if "model" in sensor:
report += f"- **Model**: {sensor['model']}\n"
report += f"- **Value**: {sensor.get('value', 'Unknown')}\n"
if "interface" in sensor:
report += f"- **Interface**: {sensor['interface']}\n"
if "measures" in sensor:
if isinstance(sensor["measures"], list):
report += f"- **Measures**: {', '.join(sensor['measures'])}\n"
else:
report += f"- **Measures**: {sensor['measures']}\n"
if "range" in sensor:
report += f"- **Range**: {sensor['range']}\n"
report += "\n"
return report
except Exception as e:
return f"# Circuit Pattern Analysis Error\n\nError: {str(e)}"
@mcp.resource("kicad://patterns/project/{project_path}")
def get_project_patterns_resource(project_path: str) -> str:
"""Get a formatted report of identified circuit patterns in a KiCad project.
Args:
project_path: Path to the KiCad project file (.kicad_pro)
Returns:
Markdown-formatted circuit pattern report
"""
if not os.path.exists(project_path):
return f"Project not found: {project_path}"
try:
# Get the schematic file from the project
files = get_project_files(project_path)
if "schematic" not in files:
return "Schematic file not found in project"
schematic_path = files["schematic"]
# Use the existing resource handler to generate the report
return get_circuit_patterns_resource(schematic_path)
except Exception as e:
return f"# Circuit Pattern Analysis Error\n\nError: {str(e)}"

View File

@ -1,52 +0,0 @@
"""
Project listing and information resources.
"""
import os
from fastmcp import FastMCP
from mckicad.utils.file_utils import get_project_files, load_project_json
def register_project_resources(mcp: FastMCP) -> None:
"""Register project-related resources with the MCP server.
Args:
mcp: The FastMCP server instance
"""
@mcp.resource("kicad://project/{project_path}")
def get_project_details(project_path: str) -> str:
"""Get details about a specific KiCad project."""
if not os.path.exists(project_path):
return f"Project not found: {project_path}"
try:
# Load project file
project_data = load_project_json(project_path)
if not project_data:
return f"Error reading project file: {project_path}"
# Get related files
files = get_project_files(project_path)
# Format project details
result = f"# Project: {os.path.basename(project_path)[:-10]}\n\n"
result += "## Project Files\n"
for file_type, file_path in files.items():
result += f"- **{file_type}**: {file_path}\n"
result += "\n## Project Settings\n"
# Extract metadata
if "metadata" in project_data:
metadata = project_data["metadata"]
for key, value in metadata.items():
result += f"- **{key}**: {value}\n"
return result
except Exception as e:
return f"Error reading project file: {str(e)}"

View File

@ -1,254 +0,0 @@
"""
MCP server creation and configuration.
"""
import atexit
from collections.abc import Callable
import functools
import logging
import os
import signal
from fastmcp import FastMCP
# Import context management
from mckicad.context import kicad_lifespan
from mckicad.prompts.bom_prompts import register_bom_prompts
from mckicad.prompts.drc_prompt import register_drc_prompts
from mckicad.prompts.pattern_prompts import register_pattern_prompts
# Import prompt handlers
from mckicad.prompts.templates import register_prompts
from mckicad.resources.bom_resources import register_bom_resources
from mckicad.resources.drc_resources import register_drc_resources
from mckicad.resources.files import register_file_resources
from mckicad.resources.netlist_resources import register_netlist_resources
from mckicad.resources.pattern_resources import register_pattern_resources
# Import resource handlers
from mckicad.resources.projects import register_project_resources
from mckicad.tools.advanced_drc_tools import register_advanced_drc_tools
from mckicad.tools.ai_tools import register_ai_tools
from mckicad.tools.analysis_tools import register_analysis_tools
from mckicad.tools.bom_tools import register_bom_tools
from mckicad.tools.drc_tools import register_drc_tools
from mckicad.tools.export_tools import register_export_tools
from mckicad.tools.layer_tools import register_layer_tools
from mckicad.tools.model3d_tools import register_model3d_tools
from mckicad.tools.netlist_tools import register_netlist_tools
from mckicad.tools.pattern_tools import register_pattern_tools
from mckicad.tools.project_automation import register_project_automation_tools
# Import tool handlers
from mckicad.tools.project_tools import register_project_tools
from mckicad.tools.routing_tools import register_routing_tools
from mckicad.tools.symbol_tools import register_symbol_tools
# Track cleanup handlers
cleanup_handlers = []
# Flag to track whether we're already in shutdown process
_shutting_down = False
# Store server instance for clean shutdown
_server_instance = None
def add_cleanup_handler(handler: Callable) -> None:
"""Register a function to be called during cleanup.
Args:
handler: Function to call during cleanup
"""
cleanup_handlers.append(handler)
def run_cleanup_handlers() -> None:
"""Run all registered cleanup handlers."""
logging.info("Running cleanup handlers...")
global _shutting_down
# Prevent running cleanup handlers multiple times
if _shutting_down:
return
_shutting_down = True
logging.info("Running cleanup handlers...")
for handler in cleanup_handlers:
try:
handler()
logging.info(f"Cleanup handler {handler.__name__} completed successfully")
except Exception as e:
logging.error(f"Error in cleanup handler {handler.__name__}: {str(e)}", exc_info=True)
def shutdown_server():
"""Properly shutdown the server if it exists."""
global _server_instance
if _server_instance:
try:
logging.info("Shutting down KiCad MCP server")
_server_instance = None
logging.info("KiCad MCP server shutdown complete")
except Exception as e:
logging.error(f"Error shutting down server: {str(e)}", exc_info=True)
def register_signal_handlers(server: FastMCP) -> None:
"""Register handlers for system signals to ensure clean shutdown.
Args:
server: The FastMCP server instance
"""
def handle_exit_signal(signum, frame):
logging.info(f"Received signal {signum}, initiating shutdown...")
# Run cleanup first
run_cleanup_handlers()
# Then shutdown server
shutdown_server()
# Exit without waiting for stdio processes which might be blocking
os._exit(0)
# Register for common termination signals
for sig in (signal.SIGINT, signal.SIGTERM):
try:
signal.signal(sig, handle_exit_signal)
logging.info(f"Registered handler for signal {sig}")
except (ValueError, AttributeError) as e:
# Some signals may not be available on all platforms
logging.error(f"Could not register handler for signal {sig}: {str(e)}")
def create_server() -> FastMCP:
"""Create and configure the KiCad MCP server."""
logging.info("Initializing KiCad MCP server")
# Try to set up KiCad Python path - Removed
# kicad_modules_available = setup_kicad_python_path()
kicad_modules_available = False # Set to False as we removed the setup logic
# if kicad_modules_available:
# print("KiCad Python modules successfully configured")
# else:
# Always print this now, as we rely on CLI
logging.info(
"KiCad Python module setup removed; relying on kicad-cli for external operations."
)
# Build a lifespan callable with the kwarg baked in (FastMCP 2.x dropped lifespan_kwargs)
lifespan_factory = functools.partial(
kicad_lifespan, kicad_modules_available=kicad_modules_available
)
# Initialize FastMCP server
mcp = FastMCP("KiCad", lifespan=lifespan_factory)
logging.info("Created FastMCP server instance with lifespan management")
# Register resources
logging.info("Registering resources...")
register_project_resources(mcp)
register_file_resources(mcp)
register_drc_resources(mcp)
register_bom_resources(mcp)
register_netlist_resources(mcp)
register_pattern_resources(mcp)
# Register tools
logging.info("Registering tools...")
register_project_tools(mcp)
register_analysis_tools(mcp)
register_export_tools(mcp)
register_drc_tools(mcp)
register_bom_tools(mcp)
register_netlist_tools(mcp)
register_pattern_tools(mcp)
register_model3d_tools(mcp)
register_advanced_drc_tools(mcp)
register_symbol_tools(mcp)
register_layer_tools(mcp)
register_ai_tools(mcp)
register_routing_tools(mcp)
register_project_automation_tools(mcp)
# Register prompts
logging.info("Registering prompts...")
register_prompts(mcp)
register_drc_prompts(mcp)
register_bom_prompts(mcp)
register_pattern_prompts(mcp)
# Register signal handlers and cleanup
register_signal_handlers(mcp)
atexit.register(run_cleanup_handlers)
# Add specific cleanup handlers
add_cleanup_handler(lambda: logging.info("KiCad MCP server shutdown complete"))
# Add temp directory cleanup
def cleanup_temp_dirs():
"""Clean up any temporary directories created by the server."""
import shutil
from mckicad.utils.temp_dir_manager import get_temp_dirs
temp_dirs = get_temp_dirs()
logging.info(f"Cleaning up {len(temp_dirs)} temporary directories")
for temp_dir in temp_dirs:
try:
if os.path.exists(temp_dir):
shutil.rmtree(temp_dir, ignore_errors=True)
logging.info(f"Removed temporary directory: {temp_dir}")
except Exception as e:
logging.error(f"Error cleaning up temporary directory {temp_dir}: {str(e)}")
add_cleanup_handler(cleanup_temp_dirs)
logging.info("Server initialization complete")
return mcp
def setup_signal_handlers() -> None:
"""Setup signal handlers for graceful shutdown."""
# Signal handlers are set up in register_signal_handlers
pass
def cleanup_handler() -> None:
"""Handle cleanup during shutdown."""
run_cleanup_handlers()
def setup_logging() -> None:
"""Configure logging for the server."""
logging.basicConfig(
level=logging.INFO, format="%(asctime)s - %(name)s - %(levelname)s - %(message)s"
)
def main() -> None:
"""Start the KiCad MCP server (blocking)."""
setup_logging()
logging.info("Starting KiCad MCP server...")
server = create_server()
try:
server.run() # FastMCP manages its own event loop
except KeyboardInterrupt:
logging.info("Server interrupted by user")
except Exception as e:
logging.error(f"Server error: {e}")
finally:
logging.info("Server shutdown complete")
if __name__ == "__main__":
main()

View File

@ -1,9 +0,0 @@
"""
Tool handlers for KiCad MCP Server.
This package includes:
- Project management tools
- Analysis tools
- Export tools (BOM extraction, PCB thumbnail generation)
- DRC tools
"""

View File

@ -1,440 +0,0 @@
"""
Advanced DRC Tools for KiCad MCP Server.
Provides MCP tools for advanced Design Rule Check (DRC) functionality including
custom rule creation, specialized rule sets, and manufacturing constraint validation.
"""
from typing import Any
from fastmcp import FastMCP
from mckicad.utils.advanced_drc import RuleSeverity, RuleType, create_drc_manager
from mckicad.utils.path_validator import validate_kicad_file
def register_advanced_drc_tools(mcp: FastMCP) -> None:
"""Register advanced DRC tools with the MCP server."""
@mcp.tool()
def create_drc_rule_set(name: str, technology: str = "standard",
description: str = "") -> dict[str, Any]:
"""
Create a new DRC rule set for a specific technology or application.
Generates optimized rule sets for different PCB technologies including
standard PCB, HDI, RF/microwave, and automotive applications.
Args:
name: Name for the rule set (e.g., "MyProject_Rules")
technology: Technology type - one of: "standard", "hdi", "rf", "automotive"
description: Optional description of the rule set
Returns:
Dictionary containing the created rule set information with rules list
Examples:
create_drc_rule_set("RF_Design", "rf", "Rules for RF circuit board")
create_drc_rule_set("Auto_ECU", "automotive", "Automotive ECU design rules")
"""
try:
manager = create_drc_manager()
# Create rule set based on technology
if technology.lower() == "hdi":
rule_set = manager.create_high_density_rules()
elif technology.lower() == "rf":
rule_set = manager.create_rf_rules()
elif technology.lower() == "automotive":
rule_set = manager.create_automotive_rules()
else:
# Create standard rules with custom name
rule_set = manager.rule_sets["standard"]
rule_set.name = name
rule_set.description = description or f"Standard PCB rules for {name}"
if name:
rule_set.name = name
if description:
rule_set.description = description
manager.add_rule_set(rule_set)
return {
"success": True,
"rule_set_name": rule_set.name,
"technology": technology,
"rule_count": len(rule_set.rules),
"description": rule_set.description,
"rules": [
{
"name": rule.name,
"type": rule.rule_type.value,
"severity": rule.severity.value,
"constraint": rule.constraint,
"enabled": rule.enabled
}
for rule in rule_set.rules
]
}
except Exception as e:
return {
"success": False,
"error": str(e),
"rule_set_name": name
}
@mcp.tool()
def create_custom_drc_rule(rule_name: str, rule_type: str, constraint: dict[str, Any],
severity: str = "error", condition: str = None,
description: str = None) -> dict[str, Any]:
"""
Create a custom DRC rule with specific constraints and conditions.
Allows creation of specialized DRC rules for unique design requirements
beyond standard manufacturing constraints.
Args:
rule_name: Name for the new rule
rule_type: Type of rule (clearance, track_width, via_size, etc.)
constraint: Dictionary of constraint parameters
severity: Rule severity (error, warning, info, ignore)
condition: Optional condition expression for when rule applies
description: Optional description of the rule
Returns:
Dictionary containing the created rule information and validation results
"""
try:
manager = create_drc_manager()
# Convert string enums
try:
rule_type_enum = RuleType(rule_type.lower())
except ValueError:
return {
"success": False,
"error": f"Invalid rule type: {rule_type}. Valid types: {[rt.value for rt in RuleType]}"
}
try:
severity_enum = RuleSeverity(severity.lower())
except ValueError:
return {
"success": False,
"error": f"Invalid severity: {severity}. Valid severities: {[s.value for s in RuleSeverity]}"
}
# Create the rule
rule = manager.create_custom_rule(
name=rule_name,
rule_type=rule_type_enum,
constraint=constraint,
severity=severity_enum,
condition=condition,
description=description
)
# Validate rule syntax
validation_errors = manager.validate_rule_syntax(rule)
return {
"success": True,
"rule": {
"name": rule.name,
"type": rule.rule_type.value,
"severity": rule.severity.value,
"constraint": rule.constraint,
"condition": rule.condition,
"description": rule.description,
"enabled": rule.enabled
},
"validation": {
"valid": len(validation_errors) == 0,
"errors": validation_errors
}
}
except Exception as e:
return {
"success": False,
"error": str(e),
"rule_name": rule_name
}
@mcp.tool()
def export_kicad_drc_rules(rule_set_name: str = "standard") -> dict[str, Any]:
"""
Export DRC rules in KiCad-compatible format.
Converts internal rule set to KiCad DRC rule format that can be
imported into KiCad projects for automated checking.
Args:
rule_set_name: Name of the rule set to export (default: standard)
Returns:
Dictionary containing exported rules and KiCad-compatible rule text
"""
try:
manager = create_drc_manager()
# Export to KiCad format
kicad_rules = manager.export_kicad_drc_rules(rule_set_name)
rule_set = manager.rule_sets[rule_set_name]
return {
"success": True,
"rule_set_name": rule_set_name,
"kicad_rules": kicad_rules,
"rule_count": len(rule_set.rules),
"active_rules": len([r for r in rule_set.rules if r.enabled]),
"export_info": {
"format": "KiCad DRC Rules",
"version": rule_set.version,
"technology": rule_set.technology or "General",
"usage": "Copy the kicad_rules text to your KiCad project's custom DRC rules"
}
}
except Exception as e:
return {
"success": False,
"error": str(e),
"rule_set_name": rule_set_name
}
@mcp.tool()
def analyze_pcb_drc_violations(pcb_file_path: str, rule_set_name: str = "standard") -> dict[str, Any]:
"""
Analyze a PCB file against advanced DRC rules and report violations.
Performs comprehensive DRC analysis using custom rule sets to identify
design issues beyond basic KiCad DRC checking.
Args:
pcb_file_path: Full path to the .kicad_pcb file to analyze
rule_set_name: Name of rule set to use ("standard", "hdi", "rf", "automotive", or custom)
Returns:
Dictionary with violation details, severity levels, and recommendations
Examples:
analyze_pcb_drc_violations("/path/to/project.kicad_pcb", "rf")
analyze_pcb_drc_violations("/path/to/board.kicad_pcb") # uses standard rules
"""
try:
validated_path = validate_kicad_file(pcb_file_path, "pcb")
manager = create_drc_manager()
# Perform DRC analysis
analysis = manager.analyze_pcb_for_rule_violations(validated_path, rule_set_name)
# Get rule set info
rule_set = manager.rule_sets.get(rule_set_name)
return {
"success": True,
"pcb_file": validated_path,
"analysis": analysis,
"rule_set_info": {
"name": rule_set.name if rule_set else "Unknown",
"technology": rule_set.technology if rule_set else None,
"description": rule_set.description if rule_set else None,
"total_rules": len(rule_set.rules) if rule_set else 0
}
}
except Exception as e:
return {
"success": False,
"error": str(e),
"pcb_file": pcb_file_path
}
@mcp.tool()
def get_manufacturing_constraints(technology: str = "standard") -> dict[str, Any]:
"""
Get manufacturing constraints for a specific PCB technology.
Provides manufacturing limits and guidelines for different PCB
technologies to help with design rule creation.
Args:
technology: Technology type (standard, hdi, rf, automotive)
Returns:
Dictionary containing manufacturing constraints and recommendations
"""
try:
manager = create_drc_manager()
constraints = manager.generate_manufacturing_constraints(technology)
# Add recommendations based on technology
recommendations = {
"standard": [
"Maintain 0.1mm minimum track width for cost-effective manufacturing",
"Use 0.2mm clearance for reliable production yields",
"Consider 6-layer maximum for standard processes"
],
"hdi": [
"Use microvias for high-density routing",
"Maintain controlled impedance for signal integrity",
"Consider sequential build-up for complex designs"
],
"rf": [
"Maintain consistent dielectric properties",
"Use ground via stitching for EMI control",
"Control trace geometry for impedance matching"
],
"automotive": [
"Design for extended temperature range operation",
"Increase clearances for vibration resistance",
"Use thermal management for high-power components"
]
}
return {
"success": True,
"technology": technology,
"constraints": constraints,
"recommendations": recommendations.get(technology, recommendations["standard"]),
"applicable_standards": {
"automotive": ["ISO 26262", "AEC-Q100"],
"rf": ["IPC-2221", "IPC-2141"],
"hdi": ["IPC-2226", "IPC-6016"],
"standard": ["IPC-2221", "IPC-2222"]
}.get(technology, [])
}
except Exception as e:
return {
"success": False,
"error": str(e),
"technology": technology
}
@mcp.tool()
def list_available_rule_sets() -> dict[str, Any]:
"""
List all available DRC rule sets and their properties.
Provides information about built-in and custom rule sets available
for DRC analysis and export.
Returns:
Dictionary containing all available rule sets with their metadata
"""
try:
manager = create_drc_manager()
rule_set_names = manager.get_rule_set_names()
rule_sets_info = []
for name in rule_set_names:
rule_set = manager.rule_sets[name]
rule_sets_info.append({
"name": rule_set.name,
"key": name,
"version": rule_set.version,
"description": rule_set.description,
"technology": rule_set.technology,
"rule_count": len(rule_set.rules),
"active_rules": len([r for r in rule_set.rules if r.enabled]),
"rule_types": list(set(r.rule_type.value for r in rule_set.rules))
})
return {
"success": True,
"rule_sets": rule_sets_info,
"total_rule_sets": len(rule_set_names),
"active_rule_set": manager.active_rule_set,
"supported_technologies": ["standard", "hdi", "rf", "automotive"]
}
except Exception as e:
return {
"success": False,
"error": str(e)
}
@mcp.tool()
def validate_drc_rule_syntax(rule_definition: dict[str, Any]) -> dict[str, Any]:
"""
Validate the syntax and parameters of a DRC rule definition.
Checks rule definition for proper syntax, valid constraints,
and logical consistency before rule creation.
Args:
rule_definition: Dictionary containing rule parameters to validate
Returns:
Dictionary containing validation results and error details
"""
try:
manager = create_drc_manager()
# Extract rule parameters
rule_name = rule_definition.get("name", "")
rule_type = rule_definition.get("type", "")
constraint = rule_definition.get("constraint", {})
severity = rule_definition.get("severity", "error")
condition = rule_definition.get("condition")
description = rule_definition.get("description")
# Validate required fields
validation_errors = []
if not rule_name:
validation_errors.append("Rule name is required")
if not rule_type:
validation_errors.append("Rule type is required")
elif rule_type not in [rt.value for rt in RuleType]:
validation_errors.append(f"Invalid rule type: {rule_type}")
if not constraint:
validation_errors.append("Constraint parameters are required")
if severity not in [s.value for s in RuleSeverity]:
validation_errors.append(f"Invalid severity: {severity}")
# If basic validation passes, create temporary rule for detailed validation
if not validation_errors:
try:
temp_rule = manager.create_custom_rule(
name=rule_name,
rule_type=RuleType(rule_type),
constraint=constraint,
severity=RuleSeverity(severity),
condition=condition,
description=description
)
# Validate rule syntax
syntax_errors = manager.validate_rule_syntax(temp_rule)
validation_errors.extend(syntax_errors)
except Exception as e:
validation_errors.append(f"Rule creation failed: {str(e)}")
return {
"success": True,
"valid": len(validation_errors) == 0,
"errors": validation_errors,
"rule_definition": rule_definition,
"validation_summary": {
"total_errors": len(validation_errors),
"critical_errors": len([e for e in validation_errors if "required" in e.lower()]),
"syntax_errors": len([e for e in validation_errors if "syntax" in e.lower() or "condition" in e.lower()])
}
}
except Exception as e:
return {
"success": False,
"error": str(e),
"rule_definition": rule_definition
}

View File

@ -1,700 +0,0 @@
"""
AI/LLM Integration Tools for KiCad MCP Server.
Provides intelligent analysis and recommendations for KiCad designs including
smart component suggestions, automated design rule recommendations, and layout optimization.
"""
from typing import Any
from fastmcp import FastMCP
from mckicad.utils.component_utils import ComponentType, get_component_type
from mckicad.utils.file_utils import get_project_files
from mckicad.utils.netlist_parser import parse_netlist_file
from mckicad.utils.pattern_recognition import analyze_circuit_patterns
def register_ai_tools(mcp: FastMCP) -> None:
"""Register AI/LLM integration tools with the MCP server."""
@mcp.tool()
def suggest_components_for_circuit(project_path: str, circuit_function: str = None) -> dict[str, Any]:
"""
Analyze circuit patterns and suggest appropriate components.
Uses circuit analysis to identify incomplete circuits and suggest
missing components based on common design patterns and best practices.
Args:
project_path: Path to the KiCad project file (.kicad_pro)
circuit_function: Optional description of intended circuit function
Returns:
Dictionary with component suggestions categorized by circuit type
Examples:
suggest_components_for_circuit("/path/to/project.kicad_pro")
suggest_components_for_circuit("/path/to/project.kicad_pro", "audio amplifier")
"""
try:
# Get project files
files = get_project_files(project_path)
if "schematic" not in files:
return {
"success": False,
"error": "Schematic file not found in project"
}
schematic_file = files["schematic"]
# Analyze existing circuit patterns
patterns = analyze_circuit_patterns(schematic_file)
# Parse netlist for component analysis
try:
netlist_data = parse_netlist_file(schematic_file)
components = netlist_data.get("components", [])
except:
components = []
# Generate suggestions based on patterns
suggestions = _generate_component_suggestions(patterns, components, circuit_function)
return {
"success": True,
"project_path": project_path,
"circuit_analysis": {
"identified_patterns": list(patterns.keys()),
"component_count": len(components),
"missing_patterns": _identify_missing_patterns(patterns, components)
},
"component_suggestions": suggestions,
"design_recommendations": _generate_design_recommendations(patterns, components),
"implementation_notes": [
"Review suggested components for compatibility with existing design",
"Verify component ratings match circuit requirements",
"Consider thermal management for power components",
"Check component availability and cost before finalizing"
]
}
except Exception as e:
return {
"success": False,
"error": str(e),
"project_path": project_path
}
@mcp.tool()
def recommend_design_rules(project_path: str, target_technology: str = "standard") -> dict[str, Any]:
"""
Generate automated design rule recommendations based on circuit analysis.
Analyzes the circuit topology, component types, and signal characteristics
to recommend appropriate design rules for the specific application.
Args:
project_path: Path to the KiCad project file (.kicad_pro)
target_technology: Target technology ("standard", "hdi", "rf", "automotive")
Returns:
Dictionary with customized design rule recommendations
Examples:
recommend_design_rules("/path/to/project.kicad_pro")
recommend_design_rules("/path/to/project.kicad_pro", "rf")
"""
try:
# Get project files
files = get_project_files(project_path)
analysis_data = {}
# Analyze schematic if available
if "schematic" in files:
patterns = analyze_circuit_patterns(files["schematic"])
analysis_data["patterns"] = patterns
try:
netlist_data = parse_netlist_file(files["schematic"])
analysis_data["components"] = netlist_data.get("components", [])
except:
analysis_data["components"] = []
# Analyze PCB if available
if "pcb" in files:
pcb_analysis = _analyze_pcb_characteristics(files["pcb"])
analysis_data["pcb"] = pcb_analysis
# Generate design rules based on analysis
design_rules = _generate_design_rules(analysis_data, target_technology)
return {
"success": True,
"project_path": project_path,
"target_technology": target_technology,
"circuit_analysis": {
"identified_patterns": list(analysis_data.get("patterns", {}).keys()),
"component_types": _categorize_components(analysis_data.get("components", [])),
"signal_types": _identify_signal_types(analysis_data.get("patterns", {}))
},
"recommended_rules": design_rules,
"rule_justifications": _generate_rule_justifications(design_rules, analysis_data),
"implementation_priority": _prioritize_rules(design_rules)
}
except Exception as e:
return {
"success": False,
"error": str(e),
"project_path": project_path
}
@mcp.tool()
def optimize_pcb_layout(project_path: str, optimization_goals: list[str] = None) -> dict[str, Any]:
"""
Analyze PCB layout and provide optimization suggestions.
Reviews component placement, routing, and design practices to suggest
improvements for signal integrity, thermal management, and manufacturability.
Args:
project_path: Path to the KiCad project file (.kicad_pro)
optimization_goals: List of optimization priorities (e.g., ["signal_integrity", "thermal", "cost"])
Returns:
Dictionary with layout optimization recommendations
Examples:
optimize_pcb_layout("/path/to/project.kicad_pro")
optimize_pcb_layout("/path/to/project.kicad_pro", ["signal_integrity", "cost"])
"""
try:
if not optimization_goals:
optimization_goals = ["signal_integrity", "thermal", "manufacturability"]
# Get project files
files = get_project_files(project_path)
if "pcb" not in files:
return {
"success": False,
"error": "PCB file not found in project"
}
pcb_file = files["pcb"]
# Analyze current layout
layout_analysis = _analyze_pcb_layout(pcb_file)
# Get circuit context from schematic if available
circuit_context = {}
if "schematic" in files:
patterns = analyze_circuit_patterns(files["schematic"])
circuit_context = {"patterns": patterns}
# Generate optimization suggestions
optimizations = _generate_layout_optimizations(
layout_analysis, circuit_context, optimization_goals
)
return {
"success": True,
"project_path": project_path,
"optimization_goals": optimization_goals,
"layout_analysis": {
"component_density": layout_analysis.get("component_density", 0),
"routing_utilization": layout_analysis.get("routing_utilization", {}),
"thermal_zones": layout_analysis.get("thermal_zones", []),
"critical_signals": layout_analysis.get("critical_signals", [])
},
"optimization_suggestions": optimizations,
"implementation_steps": _generate_implementation_steps(optimizations),
"expected_benefits": _calculate_optimization_benefits(optimizations)
}
except Exception as e:
return {
"success": False,
"error": str(e),
"project_path": project_path
}
@mcp.tool()
def analyze_design_completeness(project_path: str) -> dict[str, Any]:
"""
Analyze design completeness and suggest missing elements.
Performs comprehensive analysis to identify missing components,
incomplete circuits, and design gaps that should be addressed.
Args:
project_path: Path to the KiCad project file (.kicad_pro)
Returns:
Dictionary with completeness analysis and improvement suggestions
"""
try:
files = get_project_files(project_path)
completeness_analysis = {
"schematic_completeness": 0,
"pcb_completeness": 0,
"design_gaps": [],
"missing_elements": [],
"verification_status": {}
}
# Analyze schematic completeness
if "schematic" in files:
schematic_analysis = _analyze_schematic_completeness(files["schematic"])
completeness_analysis.update(schematic_analysis)
# Analyze PCB completeness
if "pcb" in files:
pcb_analysis = _analyze_pcb_completeness(files["pcb"])
completeness_analysis["pcb_completeness"] = pcb_analysis["completeness_score"]
completeness_analysis["design_gaps"].extend(pcb_analysis["gaps"])
# Overall completeness score
overall_score = (
completeness_analysis["schematic_completeness"] * 0.6 +
completeness_analysis["pcb_completeness"] * 0.4
)
return {
"success": True,
"project_path": project_path,
"completeness_score": round(overall_score, 1),
"analysis_details": completeness_analysis,
"priority_actions": _prioritize_completeness_actions(completeness_analysis),
"design_checklist": _generate_design_checklist(completeness_analysis),
"recommendations": _generate_completeness_recommendations(completeness_analysis)
}
except Exception as e:
return {
"success": False,
"error": str(e),
"project_path": project_path
}
# Helper functions for component suggestions
def _generate_component_suggestions(patterns: dict, components: list, circuit_function: str = None) -> dict[str, list]:
"""Generate component suggestions based on circuit analysis."""
suggestions = {
"power_management": [],
"signal_conditioning": [],
"protection": [],
"filtering": [],
"interface": [],
"passive_components": []
}
# Analyze existing components
component_types = [get_component_type(comp.get("value", "")) for comp in components]
# Power management suggestions
if "power_supply" in patterns:
if ComponentType.VOLTAGE_REGULATOR not in component_types:
suggestions["power_management"].append({
"component": "Voltage Regulator",
"suggestion": "Add voltage regulator for stable power supply",
"examples": ["LM7805", "AMS1117-3.3", "LM2596"]
})
if ComponentType.CAPACITOR not in component_types:
suggestions["power_management"].append({
"component": "Decoupling Capacitors",
"suggestion": "Add decoupling capacitors near power pins",
"examples": ["100nF ceramic", "10uF tantalum", "1000uF electrolytic"]
})
# Signal conditioning suggestions
if "amplifier" in patterns:
if not any("op" in comp.get("value", "").lower() for comp in components):
suggestions["signal_conditioning"].append({
"component": "Operational Amplifier",
"suggestion": "Consider op-amp for signal amplification",
"examples": ["LM358", "TL072", "OPA2134"]
})
# Protection suggestions
if "microcontroller" in patterns or "processor" in patterns:
if ComponentType.FUSE not in component_types:
suggestions["protection"].append({
"component": "Fuse or PTC Resettable Fuse",
"suggestion": "Add overcurrent protection",
"examples": ["1A fuse", "PPTC 0.5A", "Polyfuse 1A"]
})
if not any("esd" in comp.get("value", "").lower() for comp in components):
suggestions["protection"].append({
"component": "ESD Protection",
"suggestion": "Add ESD protection for I/O pins",
"examples": ["TVS diode", "ESD suppressors", "Varistors"]
})
# Filtering suggestions
if any(pattern in patterns for pattern in ["switching_converter", "motor_driver"]):
suggestions["filtering"].append({
"component": "EMI Filter",
"suggestion": "Add EMI filtering for switching circuits",
"examples": ["Common mode choke", "Ferrite beads", "Pi filter"]
})
# Interface suggestions based on circuit function
if circuit_function:
function_lower = circuit_function.lower()
if "audio" in function_lower:
suggestions["interface"].extend([
{
"component": "Audio Jack",
"suggestion": "Add audio input/output connector",
"examples": ["3.5mm jack", "RCA connector", "XLR"]
},
{
"component": "Audio Coupling Capacitor",
"suggestion": "AC coupling for audio signals",
"examples": ["10uF", "47uF", "100uF"]
}
])
if "usb" in function_lower or "communication" in function_lower:
suggestions["interface"].append({
"component": "USB Connector",
"suggestion": "Add USB interface for communication",
"examples": ["USB-A", "USB-C", "Micro-USB"]
})
return suggestions
def _identify_missing_patterns(patterns: dict, components: list) -> list[str]:
"""Identify common circuit patterns that might be missing."""
missing_patterns = []
has_digital_components = any(
comp.get("value", "").lower() in ["microcontroller", "processor", "mcu"]
for comp in components
)
if has_digital_components:
if "crystal_oscillator" not in patterns:
missing_patterns.append("crystal_oscillator")
if "reset_circuit" not in patterns:
missing_patterns.append("reset_circuit")
if "power_supply" not in patterns:
missing_patterns.append("power_supply")
return missing_patterns
def _generate_design_recommendations(patterns: dict, components: list) -> list[str]:
"""Generate general design recommendations."""
recommendations = []
if "power_supply" not in patterns and len(components) > 5:
recommendations.append("Consider adding dedicated power supply regulation")
if len(components) > 20 and "decoupling" not in patterns:
recommendations.append("Add decoupling capacitors for noise reduction")
if any("high_freq" in str(pattern) for pattern in patterns):
recommendations.append("Consider transmission line effects for high-frequency signals")
return recommendations
# Helper functions for design rules
def _analyze_pcb_characteristics(pcb_file: str) -> dict[str, Any]:
"""Analyze PCB file for design rule recommendations."""
# This is a simplified analysis - in practice would parse the PCB file
return {
"layer_count": 2, # Default assumption
"min_trace_width": 0.1,
"min_via_size": 0.2,
"component_density": "medium"
}
def _generate_design_rules(analysis_data: dict, target_technology: str) -> dict[str, dict]:
"""Generate design rules based on analysis and technology target."""
base_rules = {
"trace_width": {"min": 0.1, "preferred": 0.15, "unit": "mm"},
"via_size": {"min": 0.2, "preferred": 0.3, "unit": "mm"},
"clearance": {"min": 0.1, "preferred": 0.15, "unit": "mm"},
"annular_ring": {"min": 0.05, "preferred": 0.1, "unit": "mm"}
}
# Adjust rules based on technology
if target_technology == "hdi":
base_rules["trace_width"]["min"] = 0.075
base_rules["via_size"]["min"] = 0.1
base_rules["clearance"]["min"] = 0.075
elif target_technology == "rf":
base_rules["trace_width"]["preferred"] = 0.2
base_rules["clearance"]["preferred"] = 0.2
elif target_technology == "automotive":
base_rules["trace_width"]["min"] = 0.15
base_rules["clearance"]["min"] = 0.15
# Adjust based on patterns
patterns = analysis_data.get("patterns", {})
if "power_supply" in patterns:
base_rules["power_trace_width"] = {"min": 0.3, "preferred": 0.5, "unit": "mm"}
if "high_speed" in patterns:
base_rules["differential_impedance"] = {"target": 100, "tolerance": 10, "unit": "ohm"}
base_rules["single_ended_impedance"] = {"target": 50, "tolerance": 5, "unit": "ohm"}
return base_rules
def _categorize_components(components: list) -> dict[str, int]:
"""Categorize components by type."""
categories = {}
for comp in components:
comp_type = get_component_type(comp.get("value", ""))
category_name = comp_type.name.lower() if comp_type != ComponentType.UNKNOWN else "other"
categories[category_name] = categories.get(category_name, 0) + 1
return categories
def _identify_signal_types(patterns: dict) -> list[str]:
"""Identify signal types based on circuit patterns."""
signal_types = []
if "power_supply" in patterns:
signal_types.append("power")
if "amplifier" in patterns:
signal_types.append("analog")
if "microcontroller" in patterns:
signal_types.extend(["digital", "clock"])
if "crystal_oscillator" in patterns:
signal_types.append("high_frequency")
return list(set(signal_types))
def _generate_rule_justifications(design_rules: dict, analysis_data: dict) -> dict[str, str]:
"""Generate justifications for recommended design rules."""
justifications = {}
patterns = analysis_data.get("patterns", {})
if "trace_width" in design_rules:
justifications["trace_width"] = "Based on current carrying capacity and manufacturing constraints"
if "power_supply" in patterns and "power_trace_width" in design_rules:
justifications["power_trace_width"] = "Wider traces for power distribution to reduce voltage drop"
if "high_speed" in patterns and "differential_impedance" in design_rules:
justifications["differential_impedance"] = "Controlled impedance required for high-speed signals"
return justifications
def _prioritize_rules(design_rules: dict) -> list[str]:
"""Prioritize design rules by implementation importance."""
priority_order = []
if "clearance" in design_rules:
priority_order.append("clearance")
if "trace_width" in design_rules:
priority_order.append("trace_width")
if "via_size" in design_rules:
priority_order.append("via_size")
if "power_trace_width" in design_rules:
priority_order.append("power_trace_width")
if "differential_impedance" in design_rules:
priority_order.append("differential_impedance")
return priority_order
# Helper functions for layout optimization
def _analyze_pcb_layout(pcb_file: str) -> dict[str, Any]:
"""Analyze PCB layout for optimization opportunities."""
# Simplified analysis - would parse actual PCB file
return {
"component_density": 0.6,
"routing_utilization": {"top": 0.4, "bottom": 0.3},
"thermal_zones": ["high_power_area"],
"critical_signals": ["clock", "reset", "power"]
}
def _generate_layout_optimizations(layout_analysis: dict, circuit_context: dict, goals: list[str]) -> dict[str, list]:
"""Generate layout optimization suggestions."""
optimizations = {
"placement": [],
"routing": [],
"thermal": [],
"signal_integrity": [],
"manufacturability": []
}
if "signal_integrity" in goals:
optimizations["signal_integrity"].extend([
"Keep high-speed traces short and direct",
"Minimize via count on critical signals",
"Use ground planes for return current paths"
])
if "thermal" in goals:
optimizations["thermal"].extend([
"Spread heat-generating components across the board",
"Add thermal vias under power components",
"Consider copper pour for heat dissipation"
])
if "cost" in goals or "manufacturability" in goals:
optimizations["manufacturability"].extend([
"Use standard via sizes and trace widths",
"Minimize layer count where possible",
"Avoid blind/buried vias unless necessary"
])
return optimizations
def _generate_implementation_steps(optimizations: dict) -> list[str]:
"""Generate step-by-step implementation guide."""
steps = []
if optimizations.get("placement"):
steps.append("1. Review component placement for optimal positioning")
if optimizations.get("routing"):
steps.append("2. Re-route critical signals following guidelines")
if optimizations.get("thermal"):
steps.append("3. Implement thermal management improvements")
if optimizations.get("signal_integrity"):
steps.append("4. Optimize signal integrity aspects")
steps.append("5. Run DRC and electrical rules check")
steps.append("6. Verify design meets all requirements")
return steps
def _calculate_optimization_benefits(optimizations: dict) -> dict[str, str]:
"""Calculate expected benefits from optimizations."""
benefits = {}
if optimizations.get("signal_integrity"):
benefits["signal_integrity"] = "Improved noise margin and reduced EMI"
if optimizations.get("thermal"):
benefits["thermal"] = "Better thermal performance and component reliability"
if optimizations.get("manufacturability"):
benefits["manufacturability"] = "Reduced manufacturing cost and higher yield"
return benefits
# Helper functions for design completeness
def _analyze_schematic_completeness(schematic_file: str) -> dict[str, Any]:
"""Analyze schematic completeness."""
try:
patterns = analyze_circuit_patterns(schematic_file)
netlist_data = parse_netlist_file(schematic_file)
components = netlist_data.get("components", [])
completeness_score = 70 # Base score
missing_elements = []
# Check for essential patterns
if "power_supply" in patterns:
completeness_score += 10
else:
missing_elements.append("power_supply_regulation")
if len(components) > 5:
if "decoupling" not in patterns:
missing_elements.append("decoupling_capacitors")
else:
completeness_score += 10
return {
"schematic_completeness": min(completeness_score, 100),
"missing_elements": missing_elements,
"design_gaps": [],
"verification_status": {"nets": "checked", "components": "verified"}
}
except Exception:
return {
"schematic_completeness": 50,
"missing_elements": ["analysis_failed"],
"design_gaps": [],
"verification_status": {"status": "error"}
}
def _analyze_pcb_completeness(pcb_file: str) -> dict[str, Any]:
"""Analyze PCB completeness."""
# Simplified analysis
return {
"completeness_score": 80,
"gaps": ["silkscreen_labels", "test_points"]
}
def _prioritize_completeness_actions(analysis: dict) -> list[str]:
"""Prioritize actions for improving design completeness."""
actions = []
if "power_supply_regulation" in analysis.get("missing_elements", []):
actions.append("Add power supply regulation circuit")
if "decoupling_capacitors" in analysis.get("missing_elements", []):
actions.append("Add decoupling capacitors near ICs")
if analysis.get("schematic_completeness", 0) < 80:
actions.append("Complete schematic design")
if analysis.get("pcb_completeness", 0) < 80:
actions.append("Finish PCB layout")
return actions
def _generate_design_checklist(analysis: dict) -> list[dict[str, Any]]:
"""Generate design verification checklist."""
checklist = [
{"item": "Schematic review complete", "status": "complete" if analysis.get("schematic_completeness", 0) > 90 else "pending"},
{"item": "Component values verified", "status": "complete" if "components" in analysis.get("verification_status", {}) else "pending"},
{"item": "Power supply design", "status": "complete" if "power_supply_regulation" not in analysis.get("missing_elements", []) else "pending"},
{"item": "Signal integrity considerations", "status": "pending"},
{"item": "Thermal management", "status": "pending"},
{"item": "Manufacturing readiness", "status": "pending"}
]
return checklist
def _generate_completeness_recommendations(analysis: dict) -> list[str]:
"""Generate recommendations for improving completeness."""
recommendations = []
completeness = analysis.get("schematic_completeness", 0)
if completeness < 70:
recommendations.append("Focus on completing core circuit functionality")
elif completeness < 85:
recommendations.append("Add protective and filtering components")
else:
recommendations.append("Review design for optimization opportunities")
if analysis.get("missing_elements"):
recommendations.append(f"Address missing elements: {', '.join(analysis['missing_elements'])}")
return recommendations

View File

@ -1,430 +0,0 @@
"""
Analysis and validation tools for KiCad projects.
Enhanced with KiCad IPC API integration for real-time analysis.
"""
import json
import os
from typing import Any
from fastmcp import FastMCP
from mckicad.utils.file_utils import get_project_files
from mckicad.utils.ipc_client import check_kicad_availability, kicad_ipc_session
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)}")
# Enhanced validation with KiCad IPC API if available
ipc_analysis = {}
ipc_status = check_kicad_availability()
if ipc_status["available"] and "pcb" in files:
try:
with kicad_ipc_session(board_path=files["pcb"]) as client:
board_stats = client.get_board_statistics()
connectivity = client.check_connectivity()
ipc_analysis = {
"real_time_analysis": True,
"board_statistics": board_stats,
"connectivity_status": connectivity,
"routing_completion": connectivity.get("routing_completion", 0),
"component_count": board_stats.get("footprint_count", 0),
"net_count": board_stats.get("net_count", 0)
}
# Add IPC-based validation issues
if connectivity.get("unrouted_nets", 0) > 0:
issues.append(f"{connectivity['unrouted_nets']} nets are not routed")
if board_stats.get("footprint_count", 0) == 0:
issues.append("No components found on PCB")
except Exception as e:
ipc_analysis = {
"real_time_analysis": False,
"ipc_error": str(e)
}
else:
ipc_analysis = {
"real_time_analysis": False,
"reason": "KiCad IPC not available or PCB file not found"
}
return {
"valid": len(issues) == 0,
"path": project_path,
"issues": issues if issues else None,
"files_found": list(files.keys()),
"ipc_analysis": ipc_analysis,
"validation_mode": "enhanced_with_ipc" if ipc_analysis.get("real_time_analysis") else "file_based"
}
@mcp.tool()
def analyze_board_real_time(project_path: str) -> dict[str, Any]:
"""
Real-time board analysis using KiCad IPC API.
Provides comprehensive real-time analysis of the PCB board including
component placement, routing status, design rule compliance, and
optimization opportunities using live KiCad data.
Args:
project_path: Path to the KiCad project file (.kicad_pro)
Returns:
Dictionary with comprehensive real-time board analysis
Examples:
analyze_board_real_time("/path/to/project.kicad_pro")
"""
try:
# Get project files
files = get_project_files(project_path)
if "pcb" not in files:
return {
"success": False,
"error": "PCB file not found in project"
}
# Check KiCad IPC availability
ipc_status = check_kicad_availability()
if not ipc_status["available"]:
return {
"success": False,
"error": f"KiCad IPC API not available: {ipc_status['message']}"
}
board_path = files["pcb"]
with kicad_ipc_session(board_path=board_path) as client:
# Collect comprehensive board information
footprints = client.get_footprints()
nets = client.get_nets()
tracks = client.get_tracks()
board_stats = client.get_board_statistics()
connectivity = client.check_connectivity()
# Analyze component placement
placement_analysis = {
"total_components": len(footprints),
"component_types": board_stats.get("component_types", {}),
"placement_density": _calculate_placement_density(footprints),
"component_distribution": _analyze_component_distribution(footprints)
}
# Analyze routing status
routing_analysis = {
"total_nets": len(nets),
"routed_nets": connectivity.get("routed_nets", 0),
"unrouted_nets": connectivity.get("unrouted_nets", 0),
"routing_completion": connectivity.get("routing_completion", 0),
"track_count": len([t for t in tracks if hasattr(t, 'length')]),
"via_count": len([t for t in tracks if hasattr(t, 'drill')]),
"routing_efficiency": _calculate_routing_efficiency(tracks, nets)
}
# Analyze design quality
quality_analysis = {
"design_score": _calculate_design_score(placement_analysis, routing_analysis),
"critical_issues": _identify_critical_issues(footprints, tracks, nets),
"optimization_opportunities": _identify_optimization_opportunities(
placement_analysis, routing_analysis
),
"manufacturability_score": _assess_manufacturability(tracks, footprints)
}
# Generate recommendations
recommendations = _generate_real_time_recommendations(
placement_analysis, routing_analysis, quality_analysis
)
return {
"success": True,
"project_path": project_path,
"board_path": board_path,
"analysis_timestamp": os.path.getmtime(board_path),
"placement_analysis": placement_analysis,
"routing_analysis": routing_analysis,
"quality_analysis": quality_analysis,
"recommendations": recommendations,
"board_statistics": board_stats,
"analysis_mode": "real_time_ipc"
}
except Exception as e:
return {
"success": False,
"error": str(e),
"project_path": project_path
}
@mcp.tool()
def get_component_details_live(project_path: str, component_reference: str = None) -> dict[str, Any]:
"""
Get detailed component information using real-time KiCad data.
Provides comprehensive component information including position, rotation,
connections, and properties directly from the open KiCad board.
Args:
project_path: Path to the KiCad project file (.kicad_pro)
component_reference: Specific component reference (e.g., "R1", "U3") or None for all
Returns:
Dictionary with detailed component information
"""
try:
files = get_project_files(project_path)
if "pcb" not in files:
return {
"success": False,
"error": "PCB file not found in project"
}
ipc_status = check_kicad_availability()
if not ipc_status["available"]:
return {
"success": False,
"error": f"KiCad IPC API not available: {ipc_status['message']}"
}
board_path = files["pcb"]
with kicad_ipc_session(board_path=board_path) as client:
footprints = client.get_footprints()
if component_reference:
# Get specific component
target_footprint = client.get_footprint_by_reference(component_reference)
if not target_footprint:
return {
"success": False,
"error": f"Component '{component_reference}' not found"
}
component_info = _extract_component_details(target_footprint)
return {
"success": True,
"project_path": project_path,
"component_reference": component_reference,
"component_details": component_info
}
else:
# Get all components
all_components = {}
for fp in footprints:
if hasattr(fp, 'reference'):
all_components[fp.reference] = _extract_component_details(fp)
return {
"success": True,
"project_path": project_path,
"total_components": len(all_components),
"components": all_components
}
except Exception as e:
return {
"success": False,
"error": str(e),
"project_path": project_path
}
# Helper functions for enhanced IPC analysis
def _calculate_placement_density(footprints) -> float:
"""Calculate component placement density."""
if not footprints:
return 0.0
# Simplified calculation - would use actual board area in practice
return min(len(footprints) / 100.0, 1.0)
def _analyze_component_distribution(footprints) -> dict[str, Any]:
"""Analyze how components are distributed across the board."""
if not footprints:
return {"distribution": "empty"}
# Simplified analysis
return {
"distribution": "distributed",
"clustering": "moderate",
"edge_utilization": "good"
}
def _calculate_routing_efficiency(tracks, nets) -> float:
"""Calculate routing efficiency score."""
if not nets:
return 0.0
# Simplified calculation
track_count = len(tracks)
net_count = len(nets)
if net_count == 0:
return 0.0
return min(track_count / (net_count * 2), 1.0) * 100
def _calculate_design_score(placement_analysis, routing_analysis) -> int:
"""Calculate overall design quality score."""
base_score = 70
# Placement score contribution
placement_density = placement_analysis.get("placement_density", 0)
placement_score = placement_density * 15
# Routing score contribution
routing_completion = routing_analysis.get("routing_completion", 0)
routing_score = routing_completion * 0.15
return min(int(base_score + placement_score + routing_score), 100)
def _identify_critical_issues(footprints, tracks, nets) -> list[str]:
"""Identify critical design issues."""
issues = []
if len(footprints) == 0:
issues.append("No components placed on board")
if len(tracks) == 0 and len(nets) > 0:
issues.append("No routing present despite having nets")
return issues
def _identify_optimization_opportunities(placement_analysis, routing_analysis) -> list[str]:
"""Identify optimization opportunities."""
opportunities = []
if placement_analysis.get("placement_density", 0) < 0.3:
opportunities.append("Board size could be reduced for better cost efficiency")
if routing_analysis.get("routing_completion", 0) < 100:
opportunities.append("Complete remaining routing for full functionality")
return opportunities
def _assess_manufacturability(tracks, footprints) -> int:
"""Assess manufacturability score."""
base_score = 85 # Assume good manufacturability by default
# Simplified assessment
if len(tracks) > 1000: # High track density
base_score -= 10
if len(footprints) > 100: # High component density
base_score -= 5
return max(base_score, 0)
def _generate_real_time_recommendations(placement_analysis, routing_analysis, quality_analysis) -> list[str]:
"""Generate recommendations based on real-time analysis."""
recommendations = []
if quality_analysis.get("design_score", 0) < 80:
recommendations.append("Design score could be improved through optimization")
unrouted_nets = routing_analysis.get("unrouted_nets", 0)
if unrouted_nets > 0:
recommendations.append(f"Complete routing for {unrouted_nets} unrouted nets")
if placement_analysis.get("total_components", 0) > 0:
recommendations.append("Consider thermal management for power components")
recommendations.append("Run DRC check to validate design rules")
return recommendations
def _extract_component_details(footprint) -> dict[str, Any]:
"""Extract detailed information from a footprint."""
details = {
"reference": getattr(footprint, 'reference', 'Unknown'),
"value": getattr(footprint, 'value', 'Unknown'),
"position": {
"x": getattr(footprint.position, 'x', 0) if hasattr(footprint, 'position') else 0,
"y": getattr(footprint.position, 'y', 0) if hasattr(footprint, 'position') else 0
},
"rotation": getattr(footprint, 'rotation', 0),
"layer": getattr(footprint, 'layer', 'F.Cu'),
"footprint_name": getattr(footprint, 'footprint', 'Unknown')
}
return details

View File

@ -1,756 +0,0 @@
"""
Bill of Materials (BOM) processing tools for KiCad projects.
"""
import csv
import json
import os
from typing import Any
from fastmcp import FastMCP
import pandas as pd
from mckicad.utils.file_utils import get_project_files
def register_bom_tools(mcp: FastMCP) -> None:
"""Register BOM-related tools with the MCP server.
Args:
mcp: The FastMCP server instance
"""
@mcp.tool()
def analyze_bom(project_path: str) -> dict[str, Any]:
"""Analyze a KiCad project's Bill of Materials.
This tool will look for BOM files related to a KiCad project and provide
analysis including component counts, categories, and cost estimates if available.
Args:
project_path: Path to the KiCad project file (.kicad_pro)
ctx: MCP context for progress reporting
Returns:
Dictionary with BOM analysis results
"""
print(f"Analyzing BOM for project: {project_path}")
if not os.path.exists(project_path):
print(f"Project not found: {project_path}")
return {"success": False, "error": f"Project not found: {project_path}"}
# Report progress
# Get all project files
files = get_project_files(project_path)
# Look for BOM files
bom_files = {}
for file_type, file_path in files.items():
if "bom" in file_type.lower() or file_path.lower().endswith(".csv"):
bom_files[file_type] = file_path
print(f"Found potential BOM file: {file_path}")
if not bom_files:
print("No BOM files found for project")
return {
"success": False,
"error": "No BOM files found. Export a BOM from KiCad first.",
"project_path": project_path,
}
# Analyze each BOM file
results = {
"success": True,
"project_path": project_path,
"bom_files": {},
"component_summary": {},
}
total_unique_components = 0
total_components = 0
for file_type, file_path in bom_files.items():
try:
# Parse the BOM file
bom_data, format_info = parse_bom_file(file_path)
if not bom_data or len(bom_data) == 0:
print(f"Failed to parse BOM file: {file_path}")
continue
# Analyze the BOM data
analysis = analyze_bom_data(bom_data, format_info)
# Add to results
results["bom_files"][file_type] = {
"path": file_path,
"format": format_info,
"analysis": analysis,
}
# Update totals
total_unique_components += analysis["unique_component_count"]
total_components += analysis["total_component_count"]
print(f"Successfully analyzed BOM file: {file_path}")
except Exception as e:
print(f"Error analyzing BOM file {file_path}: {str(e)}", exc_info=True)
results["bom_files"][file_type] = {"path": file_path, "error": str(e)}
# Generate overall component summary
if total_components > 0:
results["component_summary"] = {
"total_unique_components": total_unique_components,
"total_components": total_components,
}
# Calculate component categories across all BOMs
all_categories = {}
for file_type, file_info in results["bom_files"].items():
if "analysis" in file_info and "categories" in file_info["analysis"]:
for category, count in file_info["analysis"]["categories"].items():
if category not in all_categories:
all_categories[category] = 0
all_categories[category] += count
results["component_summary"]["categories"] = all_categories
# Calculate total cost if available
total_cost = 0.0
cost_available = False
for file_type, file_info in results["bom_files"].items():
if "analysis" in file_info and "total_cost" in file_info["analysis"]:
if file_info["analysis"]["total_cost"] > 0:
total_cost += file_info["analysis"]["total_cost"]
cost_available = True
if cost_available:
results["component_summary"]["total_cost"] = round(total_cost, 2)
currency = next(
(
file_info["analysis"].get("currency", "USD")
for file_type, file_info in results["bom_files"].items()
if "analysis" in file_info and "currency" in file_info["analysis"]
),
"USD",
)
results["component_summary"]["currency"] = currency
return results
@mcp.tool()
def export_bom_csv(project_path: str) -> dict[str, Any]:
"""Export a Bill of Materials for a KiCad project.
This tool attempts to generate a CSV BOM file for a KiCad project.
It requires KiCad to be installed with the appropriate command-line tools.
Args:
project_path: Path to the KiCad project file (.kicad_pro)
ctx: MCP context for progress reporting
Returns:
Dictionary with export results
"""
print(f"Exporting BOM for project: {project_path}")
if not os.path.exists(project_path):
print(f"Project not found: {project_path}")
return {"success": False, "error": f"Project not found: {project_path}"}
# For now, disable Python modules and use CLI only
kicad_modules_available = False
# Report progress
# Get all project files
files = get_project_files(project_path)
# We need the schematic file to generate a BOM
if "schematic" not in files:
print("Schematic file not found in project")
return {"success": False, "error": "Schematic file not found"}
schematic_file = files["schematic"]
project_dir = os.path.dirname(project_path)
project_name = os.path.basename(project_path)[:-10] # Remove .kicad_pro extension
# Try to export BOM
# This will depend on KiCad's command-line tools or Python modules
export_result = {"success": False}
if kicad_modules_available:
try:
# Try to use KiCad Python modules
export_result = {"success": False, "error": "Python method disabled"}
except Exception as e:
print(f"Error exporting BOM with Python modules: {str(e)}", exc_info=True)
export_result = {"success": False, "error": str(e)}
# If Python method failed, try command-line method
if not export_result.get("success", False):
try:
export_result = {"success": False, "error": "CLI method needs sync implementation"}
except Exception as e:
print(f"Error exporting BOM with CLI: {str(e)}", exc_info=True)
export_result = {"success": False, "error": str(e)}
if export_result.get("success", False):
print(f"BOM exported successfully to {export_result.get('output_file', 'unknown location')}")
else:
print(f"Failed to export BOM: {export_result.get('error', 'Unknown error')}")
return export_result
# Helper functions for BOM processing
def parse_bom_file(file_path: str) -> tuple[list[dict[str, Any]], dict[str, Any]]:
"""Parse a BOM file and detect its format.
Args:
file_path: Path to the BOM file
Returns:
Tuple containing:
- List of component dictionaries
- Dictionary with format information
"""
print(f"Parsing BOM file: {file_path}")
# Check file extension
_, ext = os.path.splitext(file_path)
ext = ext.lower()
# Dictionary to store format detection info
format_info = {"file_type": ext, "detected_format": "unknown", "header_fields": []}
# Empty list to store component data
components = []
try:
if ext == ".csv":
# Try to parse as CSV
with open(file_path, encoding="utf-8-sig") as f:
# Read a few lines to analyze the format
sample = "".join([f.readline() for _ in range(10)])
f.seek(0) # Reset file pointer
# Try to detect the delimiter
if "," in sample:
delimiter = ","
elif ";" in sample:
delimiter = ";"
elif "\t" in sample:
delimiter = "\t"
else:
delimiter = "," # Default
format_info["delimiter"] = delimiter
# Read CSV
reader = csv.DictReader(f, delimiter=delimiter)
format_info["header_fields"] = reader.fieldnames if reader.fieldnames else []
# Detect BOM format based on header fields
header_str = ",".join(format_info["header_fields"]).lower()
if "reference" in header_str and "value" in header_str:
format_info["detected_format"] = "kicad"
elif "designator" in header_str:
format_info["detected_format"] = "altium"
elif "part number" in header_str or "manufacturer part" in header_str:
format_info["detected_format"] = "generic"
# Read components
for row in reader:
components.append(dict(row))
elif ext == ".xml":
# Basic XML parsing with security protection
from defusedxml.ElementTree import parse as safe_parse
tree = safe_parse(file_path)
root = tree.getroot()
format_info["detected_format"] = "xml"
# Try to extract components based on common XML BOM formats
component_elements = root.findall(".//component") or root.findall(".//Component")
if component_elements:
for elem in component_elements:
component = {}
for attr in elem.attrib:
component[attr] = elem.attrib[attr]
for child in elem:
component[child.tag] = child.text
components.append(component)
elif ext == ".json":
# Parse JSON
with open(file_path) as f:
data = json.load(f)
format_info["detected_format"] = "json"
# Try to find components array in common JSON formats
if isinstance(data, list):
components = data
elif "components" in data:
components = data["components"]
elif "parts" in data:
components = data["parts"]
else:
# Unknown format, try generic CSV parsing as fallback
try:
with open(file_path, encoding="utf-8-sig") as f:
reader = csv.DictReader(f)
format_info["header_fields"] = reader.fieldnames if reader.fieldnames else []
format_info["detected_format"] = "unknown_csv"
for row in reader:
components.append(dict(row))
except:
print(f"Failed to parse unknown file format: {file_path}")
return [], {"detected_format": "unsupported"}
except Exception as e:
print(f"Error parsing BOM file: {str(e)}", exc_info=True)
return [], {"error": str(e)}
# Check if we actually got components
if not components:
print(f"No components found in BOM file: {file_path}")
else:
print(f"Successfully parsed {len(components)} components from {file_path}")
# Add a sample of the fields found
if components:
format_info["sample_fields"] = list(components[0].keys())
return components, format_info
def analyze_bom_data(
components: list[dict[str, Any]], format_info: dict[str, Any]
) -> dict[str, Any]:
"""Analyze component data from a BOM file.
Args:
components: List of component dictionaries
format_info: Dictionary with format information
Returns:
Dictionary with analysis results
"""
print(f"Analyzing {len(components)} components")
# Initialize results
results = {
"unique_component_count": 0,
"total_component_count": 0,
"categories": {},
"has_cost_data": False,
}
if not components:
return results
# Try to convert to pandas DataFrame for easier analysis
try:
df = pd.DataFrame(components)
# Clean up column names
df.columns = [str(col).strip().lower() for col in df.columns]
# Try to identify key columns based on format
ref_col = None
value_col = None
quantity_col = None
footprint_col = None
cost_col = None
category_col = None
# Check for reference designator column
for possible_col in [
"reference",
"designator",
"references",
"designators",
"refdes",
"ref",
]:
if possible_col in df.columns:
ref_col = possible_col
break
# Check for value column
for possible_col in ["value", "component", "comp", "part", "component value", "comp value"]:
if possible_col in df.columns:
value_col = possible_col
break
# Check for quantity column
for possible_col in ["quantity", "qty", "count", "amount"]:
if possible_col in df.columns:
quantity_col = possible_col
break
# Check for footprint column
for possible_col in ["footprint", "package", "pattern", "pcb footprint"]:
if possible_col in df.columns:
footprint_col = possible_col
break
# Check for cost column
for possible_col in ["cost", "price", "unit price", "unit cost", "cost each"]:
if possible_col in df.columns:
cost_col = possible_col
break
# Check for category column
for possible_col in ["category", "type", "group", "component type", "lib"]:
if possible_col in df.columns:
category_col = possible_col
break
# Count total components
if quantity_col:
# Try to convert quantity to numeric
df[quantity_col] = pd.to_numeric(df[quantity_col], errors="coerce").fillna(1)
results["total_component_count"] = int(df[quantity_col].sum())
else:
# If no quantity column, assume each row is one component
results["total_component_count"] = len(df)
# Count unique components
results["unique_component_count"] = len(df)
# Calculate categories
if category_col:
# Use provided category column
categories = df[category_col].value_counts().to_dict()
results["categories"] = {str(k): int(v) for k, v in categories.items()}
elif footprint_col:
# Use footprint as category
categories = df[footprint_col].value_counts().to_dict()
results["categories"] = {str(k): int(v) for k, v in categories.items()}
elif ref_col:
# Try to extract categories from reference designators (R=resistor, C=capacitor, etc.)
def extract_prefix(ref):
if isinstance(ref, str):
import re
match = re.match(r"^([A-Za-z]+)", ref)
if match:
return match.group(1)
return "Other"
if isinstance(df[ref_col].iloc[0], str) and "," in df[ref_col].iloc[0]:
# Multiple references in one cell
all_refs = []
for refs in df[ref_col]:
all_refs.extend([r.strip() for r in refs.split(",")])
categories = {}
for ref in all_refs:
prefix = extract_prefix(ref)
categories[prefix] = categories.get(prefix, 0) + 1
results["categories"] = categories
else:
# Single reference per row
categories = df[ref_col].apply(extract_prefix).value_counts().to_dict()
results["categories"] = {str(k): int(v) for k, v in categories.items()}
# Map common reference prefixes to component types
category_mapping = {
"R": "Resistors",
"C": "Capacitors",
"L": "Inductors",
"D": "Diodes",
"Q": "Transistors",
"U": "ICs",
"SW": "Switches",
"J": "Connectors",
"K": "Relays",
"Y": "Crystals/Oscillators",
"F": "Fuses",
"T": "Transformers",
}
mapped_categories = {}
for cat, count in results["categories"].items():
if cat in category_mapping:
mapped_name = category_mapping[cat]
mapped_categories[mapped_name] = mapped_categories.get(mapped_name, 0) + count
else:
mapped_categories[cat] = count
results["categories"] = mapped_categories
# Calculate cost if available
if cost_col:
try:
# Try to extract numeric values from cost field
df[cost_col] = df[cost_col].astype(str).str.replace("$", "").str.replace(",", "")
df[cost_col] = pd.to_numeric(df[cost_col], errors="coerce")
# Remove NaN values
df_with_cost = df.dropna(subset=[cost_col])
if not df_with_cost.empty:
results["has_cost_data"] = True
if quantity_col:
total_cost = (df_with_cost[cost_col] * df_with_cost[quantity_col]).sum()
else:
total_cost = df_with_cost[cost_col].sum()
results["total_cost"] = round(float(total_cost), 2)
# Try to determine currency
# Check first row that has cost for currency symbols
for _, row in df.iterrows():
cost_str = str(row.get(cost_col, ""))
if "$" in cost_str:
results["currency"] = "USD"
break
elif "" in cost_str:
results["currency"] = "EUR"
break
elif "£" in cost_str:
results["currency"] = "GBP"
break
if "currency" not in results:
results["currency"] = "USD" # Default
except:
print("Failed to parse cost data")
# Add extra insights
if ref_col and value_col:
# Check for common components by value
value_counts = df[value_col].value_counts()
most_common = value_counts.head(5).to_dict()
results["most_common_values"] = {str(k): int(v) for k, v in most_common.items()}
except Exception as e:
print(f"Error analyzing BOM data: {str(e)}", exc_info=True)
# Fallback to basic analysis
results["unique_component_count"] = len(components)
results["total_component_count"] = len(components)
return results
async def export_bom_with_python(
schematic_file: str, output_dir: str, project_name: str
) -> dict[str, Any]:
"""Export a BOM using KiCad Python modules.
Args:
schematic_file: Path to the schematic file
output_dir: Directory to save the BOM
project_name: Name of the project
ctx: MCP context for progress reporting
Returns:
Dictionary with export results
"""
print(f"Exporting BOM for schematic: {schematic_file}")
try:
# Try to import KiCad Python modules
# This is a placeholder since exporting BOMs from schematic files
# is complex and KiCad's API for this is not well-documented
import kicad
import kicad.pcbnew
# For now, return a message indicating this method is not implemented yet
print("BOM export with Python modules not fully implemented")
return {
"success": False,
"error": "BOM export using Python modules is not fully implemented yet. Try using the command-line method.",
"schematic_file": schematic_file,
}
except ImportError:
print("Failed to import KiCad Python modules")
return {
"success": False,
"error": "Failed to import KiCad Python modules",
"schematic_file": schematic_file,
}
async def export_bom_with_cli(
schematic_file: str, output_dir: str, project_name: str
) -> dict[str, Any]:
"""Export a BOM using KiCad command-line tools.
Args:
schematic_file: Path to the schematic file
output_dir: Directory to save the BOM
project_name: Name of the project
ctx: MCP context for progress reporting
Returns:
Dictionary with export results
"""
import platform
import subprocess
system = platform.system()
print(f"Exporting BOM using CLI tools on {system}")
# Output file path
output_file = os.path.join(output_dir, f"{project_name}_bom.csv")
# Define the command based on operating system
if system == "Darwin": # macOS
from mckicad.config import KICAD_APP_PATH
# Path to KiCad command-line tools on macOS
kicad_cli = os.path.join(KICAD_APP_PATH, "Contents/MacOS/kicad-cli")
if not os.path.exists(kicad_cli):
return {
"success": False,
"error": f"KiCad CLI tool not found at {kicad_cli}",
"schematic_file": schematic_file,
}
# Command to generate BOM
cmd = [kicad_cli, "sch", "export", "bom", "--output", output_file, schematic_file]
elif system == "Windows":
from mckicad.config import KICAD_APP_PATH
# Path to KiCad command-line tools on Windows
kicad_cli = os.path.join(KICAD_APP_PATH, "bin", "kicad-cli.exe")
if not os.path.exists(kicad_cli):
return {
"success": False,
"error": f"KiCad CLI tool not found at {kicad_cli}",
"schematic_file": schematic_file,
}
# Command to generate BOM
cmd = [kicad_cli, "sch", "export", "bom", "--output", output_file, schematic_file]
elif system == "Linux":
# Assume kicad-cli is in the PATH
kicad_cli = "kicad-cli"
# Command to generate BOM
cmd = [kicad_cli, "sch", "export", "bom", "--output", output_file, schematic_file]
else:
return {
"success": False,
"error": f"Unsupported operating system: {system}",
"schematic_file": schematic_file,
}
try:
print(f"Running command: {' '.join(cmd)}")
# Run the command
process = subprocess.run(cmd, capture_output=True, text=True, timeout=30)
# Check if the command was successful
if process.returncode != 0:
print(f"BOM export command failed with code {process.returncode}")
print(f"Error output: {process.stderr}")
return {
"success": False,
"error": f"BOM export command failed: {process.stderr}",
"schematic_file": schematic_file,
"command": " ".join(cmd),
}
# Check if the output file was created
if not os.path.exists(output_file):
return {
"success": False,
"error": "BOM file was not created",
"schematic_file": schematic_file,
"output_file": output_file,
}
# Read the first few lines of the BOM to verify it's valid
with open(output_file) as f:
bom_content = f.read(1024) # Read first 1KB
if len(bom_content.strip()) == 0:
return {
"success": False,
"error": "Generated BOM file is empty",
"schematic_file": schematic_file,
"output_file": output_file,
}
return {
"success": True,
"schematic_file": schematic_file,
"output_file": output_file,
"file_size": os.path.getsize(output_file),
"message": "BOM exported successfully",
}
except subprocess.TimeoutExpired:
print("BOM export command timed out after 30 seconds")
return {
"success": False,
"error": "BOM export command timed out after 30 seconds",
"schematic_file": schematic_file,
}
except Exception as e:
print(f"Error exporting BOM: {str(e)}", exc_info=True)
return {
"success": False,
"error": f"Error exporting BOM: {str(e)}",
"schematic_file": schematic_file,
}

View File

@ -1,3 +0,0 @@
"""
DRC implementations for different KiCad API approaches.
"""

View File

@ -1,159 +0,0 @@
"""
Design Rule Check (DRC) implementation using KiCad command-line interface.
"""
import json
import os
import subprocess
import tempfile
from typing import Any
from mcp.server.fastmcp import Context
from mckicad.config import system
async def run_drc_via_cli(pcb_file: str) -> dict[str, Any]:
"""Run DRC using KiCad command line tools.
Args:
pcb_file: Path to the PCB file (.kicad_pcb)
ctx: MCP context for progress reporting
Returns:
Dictionary with DRC results
"""
results = {"success": False, "method": "cli", "pcb_file": pcb_file}
try:
# Create a temporary directory for the output
with tempfile.TemporaryDirectory() as temp_dir:
# Output file for DRC report
output_file = os.path.join(temp_dir, "drc_report.json")
# Find kicad-cli executable
kicad_cli = find_kicad_cli()
if not kicad_cli:
print("kicad-cli not found in PATH or common installation locations")
results["error"] = (
"kicad-cli not found. Please ensure KiCad 9.0+ is installed and kicad-cli is available."
)
return results
# Report progress
await ctx.report_progress(50, 100)
ctx.info("Running DRC using KiCad CLI...")
# Build the DRC command
cmd = [kicad_cli, "pcb", "drc", "--format", "json", "--output", output_file, pcb_file]
print(f"Running command: {' '.join(cmd)}")
process = subprocess.run(cmd, capture_output=True, text=True)
# Check if the command was successful
if process.returncode != 0:
print(f"DRC command failed with code {process.returncode}")
print(f"Error output: {process.stderr}")
results["error"] = f"DRC command failed: {process.stderr}"
return results
# Check if the output file was created
if not os.path.exists(output_file):
print("DRC report file not created")
results["error"] = "DRC report file not created"
return results
# Read the DRC report
with open(output_file) as f:
try:
drc_report = json.load(f)
except json.JSONDecodeError:
print("Failed to parse DRC report JSON")
results["error"] = "Failed to parse DRC report JSON"
return results
# Process the DRC report
violations = drc_report.get("violations", [])
violation_count = len(violations)
print(f"DRC completed with {violation_count} violations")
await ctx.report_progress(70, 100)
ctx.info(f"DRC completed with {violation_count} violations")
# Categorize violations by type
error_types = {}
for violation in violations:
error_type = violation.get("message", "Unknown")
if error_type not in error_types:
error_types[error_type] = 0
error_types[error_type] += 1
# Create success response
results = {
"success": True,
"method": "cli",
"pcb_file": pcb_file,
"total_violations": violation_count,
"violation_categories": error_types,
"violations": violations,
}
await ctx.report_progress(90, 100)
return results
except Exception as e:
print(f"Error in CLI DRC: {str(e)}", exc_info=True)
results["error"] = f"Error in CLI DRC: {str(e)}"
return results
def find_kicad_cli() -> str | None:
"""Find the kicad-cli executable in the system PATH.
Returns:
Path to kicad-cli if found, None otherwise
"""
# Check if kicad-cli is in PATH
try:
if system == "Windows":
# On Windows, check for kicad-cli.exe
result = subprocess.run(["where", "kicad-cli.exe"], capture_output=True, text=True)
if result.returncode == 0:
return result.stdout.strip().split("\n")[0]
else:
# On Unix-like systems, use which
result = subprocess.run(["which", "kicad-cli"], capture_output=True, text=True)
if result.returncode == 0:
return result.stdout.strip()
except Exception as e:
print(f"Error finding kicad-cli: {str(e)}")
# If we get here, kicad-cli is not in PATH
# Try common installation locations
if system == "Windows":
# Common Windows installation path
potential_paths = [
r"C:\Program Files\KiCad\bin\kicad-cli.exe",
r"C:\Program Files (x86)\KiCad\bin\kicad-cli.exe",
]
elif system == "Darwin": # macOS
# Common macOS installation paths
potential_paths = [
"/Applications/KiCad/KiCad.app/Contents/MacOS/kicad-cli",
"/Applications/KiCad/kicad-cli",
]
else: # Linux and other Unix-like systems
# Common Linux installation paths
potential_paths = [
"/usr/bin/kicad-cli",
"/usr/local/bin/kicad-cli",
"/opt/kicad/bin/kicad-cli",
]
# Check each potential path
for path in potential_paths:
if os.path.exists(path) and os.access(path, os.X_OK):
return path
# If still not found, return None
return None

View File

@ -1,134 +0,0 @@
"""
Design Rule Check (DRC) tools for KiCad PCB files.
"""
import os
# import logging # <-- Remove if no other logging exists
from typing import Any
from fastmcp import FastMCP
# Import implementations
from mckicad.tools.drc_impl.cli_drc import run_drc_via_cli
from mckicad.utils.drc_history import compare_with_previous, get_drc_history, save_drc_result
from mckicad.utils.file_utils import get_project_files
def register_drc_tools(mcp: FastMCP) -> None:
"""Register DRC tools with the MCP server.
Args:
mcp: The FastMCP server instance
"""
@mcp.tool()
def get_drc_history_tool(project_path: str) -> dict[str, Any]:
"""Get the DRC check history for a KiCad project.
Args:
project_path: Path to the KiCad project file (.kicad_pro)
Returns:
Dictionary with DRC history entries
"""
print(f"Getting DRC history for project: {project_path}")
if not os.path.exists(project_path):
print(f"Project not found: {project_path}")
return {"success": False, "error": f"Project not found: {project_path}"}
# Get history entries
history_entries = get_drc_history(project_path)
# Calculate trend information
trend = None
if len(history_entries) >= 2:
first = history_entries[-1] # Oldest entry
last = history_entries[0] # Newest entry
first_violations = first.get("total_violations", 0)
last_violations = last.get("total_violations", 0)
if first_violations > last_violations:
trend = "improving"
elif first_violations < last_violations:
trend = "degrading"
else:
trend = "stable"
return {
"success": True,
"project_path": project_path,
"history_entries": history_entries,
"entry_count": len(history_entries),
"trend": trend,
}
@mcp.tool()
def run_drc_check(project_path: str) -> dict[str, Any]:
"""Run a Design Rule Check on a KiCad PCB file.
Args:
project_path: Path to the KiCad project file (.kicad_pro)
Returns:
Dictionary with DRC results and statistics
"""
print(f"Running DRC check for project: {project_path}")
if not os.path.exists(project_path):
print(f"Project not found: {project_path}")
return {"success": False, "error": f"Project not found: {project_path}"}
# Get PCB file from project
files = get_project_files(project_path)
if "pcb" not in files:
print("PCB file not found in project")
return {"success": False, "error": "PCB file not found in project"}
pcb_file = files["pcb"]
print(f"Found PCB file: {pcb_file}")
# Run DRC using the appropriate approach
drc_results = None
print("Using kicad-cli for DRC")
# Use synchronous DRC check
try:
from mckicad.tools.drc_impl.cli_drc import run_drc_via_cli_sync
drc_results = run_drc_via_cli_sync(pcb_file)
except ImportError:
# Fallback - call the async version but handle it differently
import asyncio
drc_results = asyncio.run(run_drc_via_cli(pcb_file, None))
# Process and save results if successful
if drc_results and drc_results.get("success", False):
# logging.info(f"[DRC] DRC check successful for {pcb_file}. Saving results.") # <-- Remove log
# Save results to history
save_drc_result(project_path, drc_results)
# Add comparison with previous run
comparison = compare_with_previous(project_path, drc_results)
if comparison:
drc_results["comparison"] = comparison
if comparison["change"] < 0:
print(f"Great progress! You've fixed {abs(comparison['change'])} DRC violations since the last check.")
elif comparison["change"] > 0:
print(f"Found {comparison['change']} new DRC violations since the last check.")
else:
print("No change in the number of DRC violations since the last check.")
elif drc_results:
# logging.warning(f"[DRC] DRC check reported failure for {pcb_file}: {drc_results.get('error')}") # <-- Remove log
# Pass or print a warning if needed
pass
else:
# logging.error(f"[DRC] DRC check returned None for {pcb_file}") # <-- Remove log
# Pass or print an error if needed
pass
# DRC check completed
return drc_results or {"success": False, "error": "DRC check failed with an unknown error"}

View File

@ -1,218 +0,0 @@
"""
Export tools for KiCad projects.
"""
import asyncio
import os
import shutil
import subprocess
from fastmcp import FastMCP
from fastmcp.utilities.types import Image
from mckicad.config import KICAD_APP_PATH, system
from mckicad.utils.file_utils import get_project_files
def register_export_tools(mcp: FastMCP) -> None:
"""Register export tools with the MCP server.
Args:
mcp: The FastMCP server instance
"""
@mcp.tool()
async def generate_pcb_thumbnail(project_path: str):
"""Generate a thumbnail image of a KiCad PCB layout using kicad-cli.
Args:
project_path: Path to the KiCad project file (.kicad_pro)
ctx: Context for MCP communication
Returns:
Thumbnail image of the PCB or None if generation failed
"""
try:
# Access the context
app_context = ctx.request_context.lifespan_context
# Removed check for kicad_modules_available as we now use CLI
print(f"Generating thumbnail via CLI for project: {project_path}")
if not os.path.exists(project_path):
print(f"Project not found: {project_path}")
await ctx.info(f"Project not found: {project_path}")
return None
# Get PCB file from project
files = get_project_files(project_path)
if "pcb" not in files:
print("PCB file not found in project")
await ctx.info("PCB file not found in project")
return None
pcb_file = files["pcb"]
print(f"Found PCB file: {pcb_file}")
# Check cache
cache_key = f"thumbnail_cli_{pcb_file}_{os.path.getmtime(pcb_file)}"
if hasattr(app_context, "cache") and cache_key in app_context.cache:
print(f"Using cached CLI thumbnail for {pcb_file}")
return app_context.cache[cache_key]
await ctx.report_progress(10, 100)
await ctx.info(f"Generating thumbnail for {os.path.basename(pcb_file)} using kicad-cli")
# Use command-line tools
try:
thumbnail = await generate_thumbnail_with_cli(pcb_file, ctx)
if thumbnail:
# Cache the result if possible
if hasattr(app_context, "cache"):
app_context.cache[cache_key] = thumbnail
print("Thumbnail generated successfully via CLI.")
return thumbnail
else:
print("generate_thumbnail_with_cli returned None")
await ctx.info("Failed to generate thumbnail using kicad-cli.")
return None
except Exception as e:
print(f"Error calling generate_thumbnail_with_cli: {str(e)}", exc_info=True)
await ctx.info(f"Error generating thumbnail with kicad-cli: {str(e)}")
return None
except asyncio.CancelledError:
print("Thumbnail generation cancelled")
raise # Re-raise to let MCP know the task was cancelled
except Exception as e:
print(f"Unexpected error in thumbnail generation: {str(e)}")
await ctx.info(f"Error: {str(e)}")
return None
@mcp.tool()
async def generate_project_thumbnail(project_path: str):
"""Generate a thumbnail of a KiCad project's PCB layout (Alias for generate_pcb_thumbnail)."""
# This function now just calls the main CLI-based thumbnail generator
print(
f"generate_project_thumbnail called, redirecting to generate_pcb_thumbnail for {project_path}"
)
return await generate_pcb_thumbnail(project_path, ctx)
# Helper functions for thumbnail generation
async def generate_thumbnail_with_cli(pcb_file: str):
"""Generate PCB thumbnail using command line tools.
This is a fallback method when the kicad Python module is not available or fails.
Args:
pcb_file: Path to the PCB file (.kicad_pcb)
ctx: MCP context for progress reporting
Returns:
Image object containing the PCB thumbnail or None if generation failed
"""
try:
print("Attempting to generate thumbnail using KiCad CLI tools")
await ctx.report_progress(20, 100)
# --- Determine Output Path ---
project_dir = os.path.dirname(pcb_file)
project_name = os.path.splitext(os.path.basename(pcb_file))[0]
output_file = os.path.join(project_dir, f"{project_name}_thumbnail.svg")
# ---------------------------
# Check for required command-line tools based on OS
kicad_cli = None
if system == "Darwin": # macOS
kicad_cli_path = os.path.join(KICAD_APP_PATH, "Contents/MacOS/kicad-cli")
if os.path.exists(kicad_cli_path):
kicad_cli = kicad_cli_path
elif shutil.which("kicad-cli") is not None:
kicad_cli = "kicad-cli" # Try to use from PATH
else:
print(f"kicad-cli not found at {kicad_cli_path} or in PATH")
return None
elif system == "Windows":
kicad_cli_path = os.path.join(KICAD_APP_PATH, "bin", "kicad-cli.exe")
if os.path.exists(kicad_cli_path):
kicad_cli = kicad_cli_path
elif shutil.which("kicad-cli.exe") is not None:
kicad_cli = "kicad-cli.exe"
elif shutil.which("kicad-cli") is not None:
kicad_cli = "kicad-cli" # Try to use from PATH (without .exe)
else:
print(f"kicad-cli not found at {kicad_cli_path} or in PATH")
return None
elif system == "Linux":
kicad_cli = shutil.which("kicad-cli")
if not kicad_cli:
print("kicad-cli not found in PATH")
return None
else:
print(f"Unsupported operating system: {system}")
return None
await ctx.report_progress(30, 100)
await ctx.info("Using KiCad command line tools for thumbnail generation")
# Build command for generating SVG from PCB using kicad-cli (changed from PNG)
cmd = [
kicad_cli,
"pcb",
"export",
"svg", # <-- Changed format to svg
"--output",
output_file,
"--layers",
"F.Cu,B.Cu,F.SilkS,B.SilkS,F.Mask,B.Mask,Edge.Cuts", # Keep relevant layers
# Consider adding options like --black-and-white if needed
pcb_file,
]
print(f"Running command: {' '.join(cmd)}")
await ctx.report_progress(50, 100)
# Run the command
try:
process = subprocess.run(cmd, capture_output=True, text=True, check=True, timeout=30)
print(f"Command successful: {process.stdout}")
await ctx.report_progress(70, 100)
# Check if the output file was created
if not os.path.exists(output_file):
print(f"Output file not created: {output_file}")
return None
# Read the image file
with open(output_file, "rb") as f:
img_data = f.read()
print(f"Successfully generated thumbnail with CLI, size: {len(img_data)} bytes")
await ctx.report_progress(90, 100)
# Inform user about the saved file
await ctx.info(f"Thumbnail saved to: {output_file}")
return Image(data=img_data, format="svg") # <-- Changed format to svg
except subprocess.CalledProcessError as e:
print(f"Command '{' '.join(e.cmd)}' failed with code {e.returncode}")
print(f"Stderr: {e.stderr}")
print(f"Stdout: {e.stdout}")
await ctx.info(f"KiCad CLI command failed: {e.stderr or e.stdout}")
return None
except subprocess.TimeoutExpired:
print(f"Command timed out after 30 seconds: {' '.join(cmd)}")
await ctx.info("KiCad CLI command timed out")
return None
except Exception as e:
print(f"Error running CLI command: {str(e)}", exc_info=True)
await ctx.info(f"Error running KiCad CLI: {str(e)}")
return None
except asyncio.CancelledError:
print("CLI thumbnail generation cancelled")
raise
except Exception as e:
print(f"Unexpected error in CLI thumbnail generation: {str(e)}")
await ctx.info(f"Unexpected error: {str(e)}")
return None

View File

@ -1,647 +0,0 @@
"""
Layer Stack-up Analysis Tools for KiCad MCP Server.
Provides MCP tools for analyzing PCB layer configurations, impedance calculations,
and manufacturing constraints for multi-layer board designs.
"""
from typing import Any
from fastmcp import FastMCP
from mckicad.utils.layer_stackup import create_stackup_analyzer
from mckicad.utils.path_validator import validate_kicad_file
def register_layer_tools(mcp: FastMCP) -> None:
"""Register layer stack-up analysis tools with the MCP server."""
@mcp.tool()
def analyze_pcb_stackup(pcb_file_path: str) -> dict[str, Any]:
"""
Analyze PCB layer stack-up configuration and properties.
Extracts layer definitions, calculates impedances, validates manufacturing
constraints, and provides recommendations for multi-layer board design.
Args:
pcb_file_path: Path to the .kicad_pcb file to analyze
Returns:
Dictionary containing comprehensive stack-up analysis
"""
try:
# Validate PCB file
validated_path = validate_kicad_file(pcb_file_path, "pcb")
# Create analyzer and perform analysis
analyzer = create_stackup_analyzer()
stackup = analyzer.analyze_pcb_stackup(validated_path)
# Generate comprehensive report
report = analyzer.generate_stackup_report(stackup)
return {
"success": True,
"pcb_file": validated_path,
"stackup_analysis": report
}
except Exception as e:
return {
"success": False,
"error": str(e),
"pcb_file": pcb_file_path
}
@mcp.tool()
def calculate_trace_impedance(pcb_file_path: str, trace_width: float,
layer_name: str = None, spacing: float = None) -> dict[str, Any]:
"""
Calculate characteristic impedance for specific trace configurations.
Computes single-ended and differential impedance values based on
stack-up configuration and trace geometry parameters.
Args:
pcb_file_path: Full path to the .kicad_pcb file to analyze
trace_width: Trace width in millimeters (e.g., 0.15 for 150μm traces)
layer_name: Specific layer name to calculate for (optional - if omitted, calculates for all signal layers)
spacing: Trace spacing for differential pairs in mm (e.g., 0.15 for 150μm spacing)
Returns:
Dictionary with impedance values, recommendations for 50Ω/100Ω targets
Examples:
calculate_trace_impedance("/path/to/board.kicad_pcb", 0.15)
calculate_trace_impedance("/path/to/board.kicad_pcb", 0.1, "Top", 0.15)
"""
try:
validated_path = validate_kicad_file(pcb_file_path, "pcb")
analyzer = create_stackup_analyzer()
stackup = analyzer.analyze_pcb_stackup(validated_path)
# Filter signal layers
signal_layers = [l for l in stackup.layers if l.layer_type == "signal"]
if layer_name:
signal_layers = [l for l in signal_layers if l.name == layer_name]
if not signal_layers:
return {
"success": False,
"error": f"Layer '{layer_name}' not found or not a signal layer"
}
impedance_results = []
for layer in signal_layers:
# Calculate single-ended impedance
single_ended = analyzer.impedance_calculator.calculate_microstrip_impedance(
trace_width, layer, stackup.layers
)
# Calculate differential impedance if spacing provided
differential = None
if spacing is not None:
differential = analyzer.impedance_calculator.calculate_differential_impedance(
trace_width, spacing, layer, stackup.layers
)
# Find reference layers
ref_layers = analyzer._find_reference_layers(layer, stackup.layers)
impedance_results.append({
"layer_name": layer.name,
"trace_width_mm": trace_width,
"spacing_mm": spacing,
"single_ended_impedance_ohm": single_ended,
"differential_impedance_ohm": differential,
"reference_layers": ref_layers,
"dielectric_thickness_mm": _get_dielectric_thickness(layer, stackup.layers),
"dielectric_constant": _get_dielectric_constant(layer, stackup.layers)
})
# Generate recommendations
recommendations = []
for result in impedance_results:
if result["single_ended_impedance_ohm"]:
impedance = result["single_ended_impedance_ohm"]
if abs(impedance - 50) > 10:
if impedance > 50:
recommendations.append(f"Increase trace width on {result['layer_name']} to reduce impedance")
else:
recommendations.append(f"Decrease trace width on {result['layer_name']} to increase impedance")
return {
"success": True,
"pcb_file": validated_path,
"impedance_calculations": impedance_results,
"target_impedances": {
"single_ended": "50Ω typical",
"differential": "90Ω or 100Ω typical"
},
"recommendations": recommendations
}
except Exception as e:
return {
"success": False,
"error": str(e),
"pcb_file": pcb_file_path
}
def _get_dielectric_thickness(self, signal_layer, layers):
"""Get thickness of dielectric layer below signal layer."""
try:
signal_idx = layers.index(signal_layer)
for i in range(signal_idx + 1, len(layers)):
if layers[i].layer_type == "dielectric":
return layers[i].thickness
return None
except (ValueError, IndexError):
return None
def _get_dielectric_constant(self, signal_layer, layers):
"""Get dielectric constant of layer below signal layer."""
try:
signal_idx = layers.index(signal_layer)
for i in range(signal_idx + 1, len(layers)):
if layers[i].layer_type == "dielectric":
return layers[i].dielectric_constant
return None
except (ValueError, IndexError):
return None
@mcp.tool()
def validate_stackup_manufacturing(pcb_file_path: str) -> dict[str, Any]:
"""
Validate PCB stack-up against manufacturing constraints.
Checks layer configuration, thicknesses, materials, and design rules
for manufacturability and identifies potential production issues.
Args:
pcb_file_path: Path to the .kicad_pcb file
Returns:
Dictionary containing validation results and manufacturing recommendations
"""
try:
validated_path = validate_kicad_file(pcb_file_path, "pcb")
analyzer = create_stackup_analyzer()
stackup = analyzer.analyze_pcb_stackup(validated_path)
# Validate stack-up
validation_issues = analyzer.validate_stackup(stackup)
# Check additional manufacturing constraints
manufacturing_checks = self._perform_manufacturing_checks(stackup)
# Combine all issues
all_issues = validation_issues + manufacturing_checks["issues"]
return {
"success": True,
"pcb_file": validated_path,
"validation_results": {
"passed": len(all_issues) == 0,
"total_issues": len(all_issues),
"issues": all_issues,
"severity_breakdown": {
"critical": len([i for i in all_issues if "exceeds limit" in i or "too thin" in i]),
"warnings": len([i for i in all_issues if "should" in i or "may" in i])
}
},
"stackup_summary": {
"layer_count": stackup.layer_count,
"total_thickness_mm": stackup.total_thickness,
"copper_layers": len([l for l in stackup.layers if l.copper_weight]),
"signal_layers": len([l for l in stackup.layers if l.layer_type == "signal"])
},
"manufacturing_assessment": manufacturing_checks["assessment"],
"cost_implications": self._assess_cost_implications(stackup),
"recommendations": stackup.manufacturing_notes + manufacturing_checks["recommendations"]
}
except Exception as e:
return {
"success": False,
"error": str(e),
"pcb_file": pcb_file_path
}
def _perform_manufacturing_checks(self, stackup):
"""Perform additional manufacturing feasibility checks."""
issues = []
recommendations = []
# Check aspect ratio for drilling
copper_thickness = sum(l.thickness for l in stackup.layers if l.copper_weight)
max_drill_depth = stackup.total_thickness
min_drill_diameter = stackup.constraints.min_via_drill
aspect_ratio = max_drill_depth / min_drill_diameter
if aspect_ratio > stackup.constraints.aspect_ratio_limit:
issues.append(f"Aspect ratio {aspect_ratio:.1f}:1 exceeds manufacturing limit")
recommendations.append("Consider using buried/blind vias or increasing minimum drill size")
# Check copper balance
top_half_copper = sum(l.thickness for l in stackup.layers[:len(stackup.layers)//2] if l.copper_weight)
bottom_half_copper = sum(l.thickness for l in stackup.layers[len(stackup.layers)//2:] if l.copper_weight)
if abs(top_half_copper - bottom_half_copper) / max(top_half_copper, bottom_half_copper) > 0.4:
issues.append("Copper distribution imbalance may cause board warpage")
recommendations.append("Redistribute copper or add balancing copper fills")
# Assess manufacturing complexity
complexity_factors = []
if stackup.layer_count > 6:
complexity_factors.append("High layer count")
if stackup.total_thickness > 2.5:
complexity_factors.append("Thick board")
if len(set(l.material for l in stackup.layers if l.layer_type == "dielectric")) > 1:
complexity_factors.append("Mixed dielectric materials")
assessment = "Standard" if not complexity_factors else f"Complex ({', '.join(complexity_factors)})"
return {
"issues": issues,
"recommendations": recommendations,
"assessment": assessment
}
def _assess_cost_implications(self, stackup):
"""Assess cost implications of the stack-up design."""
cost_factors = []
cost_multiplier = 1.0
# Layer count impact
if stackup.layer_count > 4:
cost_multiplier *= (1.0 + (stackup.layer_count - 4) * 0.15)
cost_factors.append(f"{stackup.layer_count}-layer design increases cost")
# Thickness impact
if stackup.total_thickness > 1.6:
cost_multiplier *= 1.1
cost_factors.append("Non-standard thickness increases cost")
# Material impact
premium_materials = ["Rogers", "Polyimide"]
if any(material in str(stackup.layers) for material in premium_materials):
cost_multiplier *= 1.3
cost_factors.append("Premium materials increase cost significantly")
cost_category = "Low" if cost_multiplier < 1.2 else "Medium" if cost_multiplier < 1.5 else "High"
return {
"cost_category": cost_category,
"cost_multiplier": round(cost_multiplier, 2),
"cost_factors": cost_factors,
"optimization_suggestions": [
"Consider standard 4-layer stack-up for cost reduction",
"Use standard FR4 materials where possible",
"Optimize thickness to standard values (1.6mm typical)"
] if cost_multiplier > 1.3 else ["Current design is cost-optimized"]
}
@mcp.tool()
def optimize_stackup_for_impedance(pcb_file_path: str, target_impedance: float = 50.0,
differential_target: float = 100.0) -> dict[str, Any]:
"""
Optimize stack-up configuration for target impedance values.
Suggests modifications to layer thicknesses and trace widths to achieve
desired characteristic impedance for signal integrity.
Args:
pcb_file_path: Path to the .kicad_pcb file
target_impedance: Target single-ended impedance in ohms (default: 50Ω)
differential_target: Target differential impedance in ohms (default: 100Ω)
Returns:
Dictionary containing optimization recommendations and calculations
"""
try:
validated_path = validate_kicad_file(pcb_file_path, "pcb")
analyzer = create_stackup_analyzer()
stackup = analyzer.analyze_pcb_stackup(validated_path)
optimization_results = []
# Analyze each signal layer
signal_layers = [l for l in stackup.layers if l.layer_type == "signal"]
for layer in signal_layers:
layer_optimization = self._optimize_layer_impedance(
layer, stackup.layers, analyzer, target_impedance, differential_target
)
optimization_results.append(layer_optimization)
# Generate overall recommendations
overall_recommendations = self._generate_impedance_recommendations(
optimization_results, target_impedance, differential_target
)
return {
"success": True,
"pcb_file": validated_path,
"target_impedances": {
"single_ended": target_impedance,
"differential": differential_target
},
"layer_optimizations": optimization_results,
"overall_recommendations": overall_recommendations,
"implementation_notes": [
"Impedance optimization may require stack-up modifications",
"Verify with manufacturer before finalizing changes",
"Consider tolerance requirements for critical nets",
"Update design rules after stack-up modifications"
]
}
except Exception as e:
return {
"success": False,
"error": str(e),
"pcb_file": pcb_file_path
}
def _optimize_layer_impedance(self, layer, layers, analyzer, target_se, target_diff):
"""Optimize impedance for a specific layer."""
current_impedances = []
optimized_suggestions = []
# Test different trace widths
test_widths = [0.08, 0.1, 0.125, 0.15, 0.2, 0.25, 0.3]
for width in test_widths:
se_impedance = analyzer.impedance_calculator.calculate_microstrip_impedance(
width, layer, layers
)
diff_impedance = analyzer.impedance_calculator.calculate_differential_impedance(
width, 0.15, layer, layers # 0.15mm spacing
)
if se_impedance:
current_impedances.append({
"trace_width_mm": width,
"single_ended_ohm": se_impedance,
"differential_ohm": diff_impedance,
"se_error": abs(se_impedance - target_se),
"diff_error": abs(diff_impedance - target_diff) if diff_impedance else None
})
# Find best matches
best_se = min(current_impedances, key=lambda x: x["se_error"]) if current_impedances else None
best_diff = min([x for x in current_impedances if x["diff_error"] is not None],
key=lambda x: x["diff_error"]) if any(x["diff_error"] is not None for x in current_impedances) else None
return {
"layer_name": layer.name,
"current_impedances": current_impedances,
"recommended_for_single_ended": best_se,
"recommended_for_differential": best_diff,
"optimization_notes": self._generate_layer_optimization_notes(
layer, best_se, best_diff, target_se, target_diff
)
}
def _generate_layer_optimization_notes(self, layer, best_se, best_diff, target_se, target_diff):
"""Generate optimization notes for a specific layer."""
notes = []
if best_se and abs(best_se["se_error"]) > 5:
notes.append(f"Difficult to achieve {target_se}Ω on {layer.name} with current stack-up")
notes.append("Consider adjusting dielectric thickness or material")
if best_diff and best_diff["diff_error"] and abs(best_diff["diff_error"]) > 10:
notes.append(f"Difficult to achieve {target_diff}Ω differential on {layer.name}")
notes.append("Consider adjusting trace spacing or dielectric properties")
return notes
def _generate_impedance_recommendations(self, optimization_results, target_se, target_diff):
"""Generate overall impedance optimization recommendations."""
recommendations = []
# Check if any layers have poor impedance control
poor_control_layers = []
for result in optimization_results:
if result["recommended_for_single_ended"] and result["recommended_for_single_ended"]["se_error"] > 5:
poor_control_layers.append(result["layer_name"])
if poor_control_layers:
recommendations.append(f"Layers with poor impedance control: {', '.join(poor_control_layers)}")
recommendations.append("Consider stack-up redesign or use impedance-optimized prepregs")
# Check for consistent trace widths
trace_widths = set()
for result in optimization_results:
if result["recommended_for_single_ended"]:
trace_widths.add(result["recommended_for_single_ended"]["trace_width_mm"])
if len(trace_widths) > 2:
recommendations.append("Multiple trace widths needed - consider design rule complexity")
return recommendations
@mcp.tool()
def compare_stackup_alternatives(pcb_file_path: str,
alternative_configs: list[dict[str, Any]] = None) -> dict[str, Any]:
"""
Compare different stack-up alternatives for the same design.
Evaluates multiple stack-up configurations against cost, performance,
and manufacturing criteria to help select optimal configuration.
Args:
pcb_file_path: Path to the .kicad_pcb file
alternative_configs: List of alternative stack-up configurations (optional)
Returns:
Dictionary containing comparison results and recommendations
"""
try:
validated_path = validate_kicad_file(pcb_file_path, "pcb")
analyzer = create_stackup_analyzer()
current_stackup = analyzer.analyze_pcb_stackup(validated_path)
# Generate standard alternatives if none provided
if not alternative_configs:
alternative_configs = self._generate_standard_alternatives(current_stackup)
comparison_results = []
# Analyze current stackup
current_analysis = {
"name": "Current Design",
"stackup": current_stackup,
"report": analyzer.generate_stackup_report(current_stackup),
"score": self._calculate_stackup_score(current_stackup, analyzer)
}
comparison_results.append(current_analysis)
# Analyze alternatives
for i, config in enumerate(alternative_configs):
alt_stackup = self._create_alternative_stackup(current_stackup, config)
alt_report = analyzer.generate_stackup_report(alt_stackup)
alt_score = self._calculate_stackup_score(alt_stackup, analyzer)
comparison_results.append({
"name": config.get("name", f"Alternative {i+1}"),
"stackup": alt_stackup,
"report": alt_report,
"score": alt_score
})
# Rank alternatives
ranked_results = sorted(comparison_results, key=lambda x: x["score"]["total"], reverse=True)
return {
"success": True,
"pcb_file": validated_path,
"comparison_results": [
{
"name": result["name"],
"layer_count": result["stackup"].layer_count,
"total_thickness_mm": result["stackup"].total_thickness,
"total_score": result["score"]["total"],
"cost_score": result["score"]["cost"],
"performance_score": result["score"]["performance"],
"manufacturing_score": result["score"]["manufacturing"],
"validation_passed": result["report"]["validation"]["passed"],
"key_advantages": self._identify_advantages(result, comparison_results),
"key_disadvantages": self._identify_disadvantages(result, comparison_results)
}
for result in ranked_results
],
"recommendation": {
"best_overall": ranked_results[0]["name"],
"best_cost": min(comparison_results, key=lambda x: x["score"]["cost"])["name"],
"best_performance": max(comparison_results, key=lambda x: x["score"]["performance"])["name"],
"reasoning": self._generate_recommendation_reasoning(ranked_results)
}
}
except Exception as e:
return {
"success": False,
"error": str(e),
"pcb_file": pcb_file_path
}
def _generate_standard_alternatives(self, current_stackup):
"""Generate standard alternative stack-up configurations."""
alternatives = []
current_layers = current_stackup.layer_count
# 4-layer alternative (if current is different)
if current_layers != 4:
alternatives.append({
"name": "4-Layer Standard",
"layer_count": 4,
"description": "Standard 4-layer stack-up for cost optimization"
})
# 6-layer alternative (if current is different and > 4)
if current_layers > 4 and current_layers != 6:
alternatives.append({
"name": "6-Layer Balanced",
"layer_count": 6,
"description": "6-layer stack-up for improved power distribution"
})
# High-performance alternative
if current_layers <= 8:
alternatives.append({
"name": "High-Performance",
"layer_count": min(current_layers + 2, 10),
"description": "Additional layers for better signal integrity"
})
return alternatives
def _create_alternative_stackup(self, base_stackup, config):
"""Create an alternative stack-up based on configuration."""
# This is a simplified implementation - in practice, you'd need
# more sophisticated stack-up generation based on the configuration
alt_stackup = base_stackup # For now, return the same stack-up
# TODO: Implement actual alternative stack-up generation
return alt_stackup
def _calculate_stackup_score(self, stackup, analyzer):
"""Calculate overall score for stack-up quality."""
# Cost score (lower is better, invert for scoring)
cost_score = 100 - min(stackup.layer_count * 5, 50) # Penalize high layer count
# Performance score
performance_score = 70 # Base score
if stackup.layer_count >= 4:
performance_score += 20 # Dedicated power planes
if stackup.total_thickness < 2.0:
performance_score += 10 # Good for high-frequency
# Manufacturing score
validation_issues = analyzer.validate_stackup(stackup)
manufacturing_score = 100 - len(validation_issues) * 10
total_score = (cost_score * 0.3 + performance_score * 0.4 + manufacturing_score * 0.3)
return {
"total": round(total_score, 1),
"cost": cost_score,
"performance": performance_score,
"manufacturing": manufacturing_score
}
def _identify_advantages(self, result, all_results):
"""Identify key advantages of a stack-up configuration."""
advantages = []
if result["score"]["cost"] == max(r["score"]["cost"] for r in all_results):
advantages.append("Lowest cost option")
if result["score"]["performance"] == max(r["score"]["performance"] for r in all_results):
advantages.append("Best performance characteristics")
if result["report"]["validation"]["passed"]:
advantages.append("Passes all manufacturing validation")
return advantages[:3] # Limit to top 3 advantages
def _identify_disadvantages(self, result, all_results):
"""Identify key disadvantages of a stack-up configuration."""
disadvantages = []
if result["score"]["cost"] == min(r["score"]["cost"] for r in all_results):
disadvantages.append("Highest cost option")
if not result["report"]["validation"]["passed"]:
disadvantages.append("Has manufacturing validation issues")
if result["stackup"].layer_count > 8:
disadvantages.append("Complex manufacturing due to high layer count")
return disadvantages[:3] # Limit to top 3 disadvantages
def _generate_recommendation_reasoning(self, ranked_results):
"""Generate reasoning for the recommendation."""
best = ranked_results[0]
reasoning = f"'{best['name']}' is recommended due to its high overall score ({best['score']['total']:.1f}/100). "
if best["report"]["validation"]["passed"]:
reasoning += "It passes all manufacturing validation checks and "
if best["score"]["cost"] > 70:
reasoning += "offers good cost efficiency."
elif best["score"]["performance"] > 80:
reasoning += "provides excellent performance characteristics."
else:
reasoning += "offers the best balance of cost, performance, and manufacturability."
return reasoning

View File

@ -1,335 +0,0 @@
"""
3D Model Analysis Tools for KiCad MCP Server.
Provides MCP tools for analyzing 3D models, mechanical constraints,
and visualization data from KiCad PCB files.
"""
import json
from typing import Any
from fastmcp import FastMCP
from mckicad.utils.model3d_analyzer import (
Model3DAnalyzer,
analyze_pcb_3d_models,
get_mechanical_constraints,
)
from mckicad.utils.path_validator import validate_kicad_file
def register_model3d_tools(mcp: FastMCP) -> None:
"""Register 3D model analysis tools with the MCP server."""
@mcp.tool()
def analyze_3d_models(pcb_file_path: str) -> dict[str, Any]:
"""
Analyze 3D models and mechanical aspects of a KiCad PCB file.
Extracts 3D component information, board dimensions, clearance violations,
and generates data suitable for 3D visualization.
Args:
pcb_file_path: Full path to the .kicad_pcb file to analyze
Returns:
Dictionary containing 3D analysis results including:
- board_dimensions: Physical board size and outline
- components: List of 3D components with positions and models
- height_analysis: Component height statistics
- clearance_violations: Detected mechanical issues
- stats: Summary statistics
Examples:
analyze_3d_models("/path/to/my_board.kicad_pcb")
analyze_3d_models("~/kicad_projects/robot_controller/robot.kicad_pcb")
"""
try:
# Validate the PCB file path
validated_path = validate_kicad_file(pcb_file_path, "pcb")
# Perform 3D analysis
result = analyze_pcb_3d_models(validated_path)
return {
"success": True,
"pcb_file": validated_path,
"analysis": result
}
except Exception as e:
return {
"success": False,
"error": str(e),
"pcb_file": pcb_file_path
}
@mcp.tool()
def check_mechanical_constraints(pcb_file_path: str) -> dict[str, Any]:
"""
Check mechanical constraints and clearances in a KiCad PCB.
Performs comprehensive mechanical analysis including component clearances,
board edge distances, height constraints, and identifies potential
manufacturing or assembly issues.
Args:
pcb_file_path: Path to the .kicad_pcb file to analyze
Returns:
Dictionary containing mechanical analysis results:
- constraints: List of constraint violations
- clearance_violations: Detailed clearance issues
- board_dimensions: Physical board properties
- recommendations: Suggested improvements
"""
try:
validated_path = validate_kicad_file(pcb_file_path, "pcb")
# Perform mechanical analysis
analysis = get_mechanical_constraints(validated_path)
# Generate recommendations
recommendations = []
if analysis.height_analysis["max"] > 5.0:
recommendations.append("Consider using lower profile components to reduce board height")
if len(analysis.clearance_violations) > 0:
recommendations.append("Review component placement to resolve clearance violations")
if analysis.board_dimensions.width > 80 or analysis.board_dimensions.height > 80:
recommendations.append("Large board size may increase manufacturing costs")
return {
"success": True,
"pcb_file": validated_path,
"constraints": analysis.mechanical_constraints,
"clearance_violations": [
{
"type": v["type"],
"components": [v.get("component1", ""), v.get("component2", ""), v.get("component", "")],
"distance": v["distance"],
"required": v["required_clearance"],
"severity": v["severity"]
}
for v in analysis.clearance_violations
],
"board_dimensions": {
"width_mm": analysis.board_dimensions.width,
"height_mm": analysis.board_dimensions.height,
"thickness_mm": analysis.board_dimensions.thickness,
"area_mm2": analysis.board_dimensions.width * analysis.board_dimensions.height
},
"height_analysis": analysis.height_analysis,
"recommendations": recommendations,
"component_count": len(analysis.components)
}
except Exception as e:
return {
"success": False,
"error": str(e),
"pcb_file": pcb_file_path
}
@mcp.tool()
def generate_3d_visualization_json(pcb_file_path: str, output_path: str = None) -> dict[str, Any]:
"""
Generate JSON data file for 3D visualization of PCB.
Creates a structured JSON file containing all necessary data for
3D visualization tools, including component positions, board outline,
and model references.
Args:
pcb_file_path: Path to the .kicad_pcb file
output_path: Optional path for output JSON file (defaults to same dir as PCB)
Returns:
Dictionary with generation results and file path
"""
try:
validated_path = validate_kicad_file(pcb_file_path, "pcb")
# Generate visualization data
viz_data = analyze_pcb_3d_models(validated_path)
# Determine output path
if not output_path:
output_path = validated_path.replace('.kicad_pcb', '_3d_viz.json')
# Save visualization data
with open(output_path, 'w', encoding='utf-8') as f:
json.dump(viz_data, f, indent=2)
return {
"success": True,
"pcb_file": validated_path,
"output_file": output_path,
"component_count": viz_data.get("stats", {}).get("total_components", 0),
"models_found": viz_data.get("stats", {}).get("components_with_3d_models", 0),
"board_size": f"{viz_data.get('board_dimensions', {}).get('width', 0):.1f}x{viz_data.get('board_dimensions', {}).get('height', 0):.1f}mm"
}
except Exception as e:
return {
"success": False,
"error": str(e),
"pcb_file": pcb_file_path
}
@mcp.tool()
def component_height_distribution(pcb_file_path: str) -> dict[str, Any]:
"""
Analyze the height distribution of components on a PCB.
Provides detailed analysis of component heights, useful for
determining enclosure requirements and assembly considerations.
Args:
pcb_file_path: Path to the .kicad_pcb file
Returns:
Height distribution analysis with statistics and component breakdown
"""
try:
validated_path = validate_kicad_file(pcb_file_path, "pcb")
analyzer = Model3DAnalyzer(validated_path)
components = analyzer.extract_3d_components()
height_analysis = analyzer.analyze_component_heights(components)
# Categorize components by height
height_categories = {
"very_low": [], # < 1mm
"low": [], # 1-2mm
"medium": [], # 2-5mm
"high": [], # 5-10mm
"very_high": [] # > 10mm
}
for comp in components:
height = analyzer._estimate_component_height(comp)
if height < 1.0:
height_categories["very_low"].append((comp.reference, height))
elif height < 2.0:
height_categories["low"].append((comp.reference, height))
elif height < 5.0:
height_categories["medium"].append((comp.reference, height))
elif height < 10.0:
height_categories["high"].append((comp.reference, height))
else:
height_categories["very_high"].append((comp.reference, height))
return {
"success": True,
"pcb_file": validated_path,
"height_statistics": height_analysis,
"height_categories": {
category: [{"component": ref, "height_mm": height}
for ref, height in components]
for category, components in height_categories.items()
},
"tallest_components": sorted(
[(comp.reference, analyzer._estimate_component_height(comp))
for comp in components],
key=lambda x: x[1], reverse=True
)[:10], # Top 10 tallest components
"enclosure_requirements": {
"minimum_height_mm": height_analysis["max"] + 2.0, # Add 2mm clearance
"recommended_height_mm": height_analysis["max"] + 5.0 # Add 5mm clearance
}
}
except Exception as e:
return {
"success": False,
"error": str(e),
"pcb_file": pcb_file_path
}
@mcp.tool()
def check_assembly_feasibility(pcb_file_path: str) -> dict[str, Any]:
"""
Analyze PCB assembly feasibility and identify potential issues.
Checks for component accessibility, assembly sequence issues,
and manufacturing constraints that could affect PCB assembly.
Args:
pcb_file_path: Path to the .kicad_pcb file
Returns:
Assembly feasibility analysis with issues and recommendations
"""
try:
validated_path = validate_kicad_file(pcb_file_path, "pcb")
analyzer = Model3DAnalyzer(validated_path)
mechanical_analysis = analyzer.perform_mechanical_analysis()
components = mechanical_analysis.components
assembly_issues = []
assembly_warnings = []
# Check for components too close to board edge
for comp in components:
edge_distance = analyzer._distance_to_board_edge(
comp, mechanical_analysis.board_dimensions
)
if edge_distance < 1.0: # Less than 1mm from edge
assembly_warnings.append({
"component": comp.reference,
"issue": f"Component only {edge_distance:.2f}mm from board edge",
"recommendation": "Consider moving component away from edge for easier assembly"
})
# Check for very small components that might be hard to place
small_component_footprints = ["0201", "0402"]
for comp in components:
if any(size in (comp.footprint or "") for size in small_component_footprints):
assembly_warnings.append({
"component": comp.reference,
"issue": f"Very small footprint {comp.footprint}",
"recommendation": "Verify pick-and-place machine compatibility"
})
# Check component density
board_area = (mechanical_analysis.board_dimensions.width *
mechanical_analysis.board_dimensions.height)
component_density = len(components) / (board_area / 100) # Components per cm²
if component_density > 5.0:
assembly_warnings.append({
"component": "Board",
"issue": f"High component density: {component_density:.1f} components/cm²",
"recommendation": "Consider larger board or fewer components for easier assembly"
})
return {
"success": True,
"pcb_file": validated_path,
"assembly_feasible": len(assembly_issues) == 0,
"assembly_issues": assembly_issues,
"assembly_warnings": assembly_warnings,
"component_density": component_density,
"board_utilization": {
"component_count": len(components),
"board_area_mm2": board_area,
"density_per_cm2": component_density
},
"recommendations": [
"Review component placement for optimal assembly sequence",
"Ensure adequate fiducial markers for automated assembly",
"Consider component orientation for consistent placement direction"
] if assembly_warnings else ["PCB appears suitable for standard assembly processes"]
}
except Exception as e:
return {
"success": False,
"error": str(e),
"pcb_file": pcb_file_path
}

View File

@ -1,412 +0,0 @@
"""
Netlist extraction and analysis tools for KiCad schematics.
"""
import os
from typing import Any
from fastmcp import FastMCP
from mckicad.utils.file_utils import get_project_files
from mckicad.utils.netlist_parser import analyze_netlist, extract_netlist
def register_netlist_tools(mcp: FastMCP) -> None:
"""Register netlist-related tools with the MCP server.
Args:
mcp: The FastMCP server instance
"""
@mcp.tool()
async def extract_schematic_netlist(schematic_path: str) -> dict[str, Any]:
"""Extract netlist information from a KiCad schematic.
This tool parses a KiCad schematic file and extracts comprehensive
netlist information including components, connections, and labels.
Args:
schematic_path: Path to the KiCad schematic file (.kicad_sch)
ctx: MCP context for progress reporting
Returns:
Dictionary with netlist information
"""
print(f"Extracting netlist from schematic: {schematic_path}")
if not os.path.exists(schematic_path):
print(f"Schematic file not found: {schematic_path}")
ctx.info(f"Schematic file not found: {schematic_path}")
return {"success": False, "error": f"Schematic file not found: {schematic_path}"}
# Report progress
await ctx.report_progress(10, 100)
ctx.info(f"Loading schematic file: {os.path.basename(schematic_path)}")
# Extract netlist information
try:
await ctx.report_progress(20, 100)
ctx.info("Parsing schematic structure...")
netlist_data = extract_netlist(schematic_path)
if "error" in netlist_data:
print(f"Error extracting netlist: {netlist_data['error']}")
ctx.info(f"Error extracting netlist: {netlist_data['error']}")
return {"success": False, "error": netlist_data["error"]}
await ctx.report_progress(60, 100)
ctx.info(
f"Extracted {netlist_data['component_count']} components and {netlist_data['net_count']} nets"
)
# Analyze the netlist
await ctx.report_progress(70, 100)
ctx.info("Analyzing netlist data...")
analysis_results = analyze_netlist(netlist_data)
await ctx.report_progress(90, 100)
# Build result
result = {
"success": True,
"schematic_path": schematic_path,
"component_count": netlist_data["component_count"],
"net_count": netlist_data["net_count"],
"components": netlist_data["components"],
"nets": netlist_data["nets"],
"analysis": analysis_results,
}
# Complete progress
await ctx.report_progress(100, 100)
ctx.info("Netlist extraction complete")
return result
except Exception as e:
print(f"Error extracting netlist: {str(e)}")
ctx.info(f"Error extracting netlist: {str(e)}")
return {"success": False, "error": str(e)}
@mcp.tool()
async def extract_project_netlist(project_path: str) -> dict[str, Any]:
"""Extract netlist from a KiCad project's schematic.
This tool finds the schematic associated with a KiCad project
and extracts its netlist information.
Args:
project_path: Path to the KiCad project file (.kicad_pro)
ctx: MCP context for progress reporting
Returns:
Dictionary with netlist information
"""
print(f"Extracting netlist for project: {project_path}")
if not os.path.exists(project_path):
print(f"Project not found: {project_path}")
ctx.info(f"Project not found: {project_path}")
return {"success": False, "error": f"Project not found: {project_path}"}
# Report progress
await ctx.report_progress(10, 100)
# Get the schematic file
try:
files = get_project_files(project_path)
if "schematic" not in files:
print("Schematic file not found in project")
ctx.info("Schematic file not found in project")
return {"success": False, "error": "Schematic file not found in project"}
schematic_path = files["schematic"]
print(f"Found schematic file: {schematic_path}")
ctx.info(f"Found schematic file: {os.path.basename(schematic_path)}")
# Extract netlist
await ctx.report_progress(20, 100)
# Call the schematic netlist extraction
result = await extract_schematic_netlist(schematic_path, ctx)
# Add project path to result
if "success" in result and result["success"]:
result["project_path"] = project_path
return result
except Exception as e:
print(f"Error extracting project netlist: {str(e)}")
ctx.info(f"Error extracting project netlist: {str(e)}")
return {"success": False, "error": str(e)}
@mcp.tool()
async def analyze_schematic_connections(schematic_path: str) -> dict[str, Any]:
"""Analyze connections in a KiCad schematic.
This tool provides detailed analysis of component connections,
including power nets, signal paths, and potential issues.
Args:
schematic_path: Path to the KiCad schematic file (.kicad_sch)
ctx: MCP context for progress reporting
Returns:
Dictionary with connection analysis
"""
print(f"Analyzing connections in schematic: {schematic_path}")
if not os.path.exists(schematic_path):
print(f"Schematic file not found: {schematic_path}")
ctx.info(f"Schematic file not found: {schematic_path}")
return {"success": False, "error": f"Schematic file not found: {schematic_path}"}
# Report progress
await ctx.report_progress(10, 100)
ctx.info(f"Extracting netlist from: {os.path.basename(schematic_path)}")
# Extract netlist information
try:
netlist_data = extract_netlist(schematic_path)
if "error" in netlist_data:
print(f"Error extracting netlist: {netlist_data['error']}")
ctx.info(f"Error extracting netlist: {netlist_data['error']}")
return {"success": False, "error": netlist_data["error"]}
await ctx.report_progress(40, 100)
# Advanced connection analysis
ctx.info("Performing connection analysis...")
analysis = {
"component_count": netlist_data["component_count"],
"net_count": netlist_data["net_count"],
"component_types": {},
"power_nets": [],
"signal_nets": [],
"potential_issues": [],
}
# Analyze component types
components = netlist_data.get("components", {})
for ref, component in components.items():
# Extract component type from reference (e.g., R1 -> R)
import re
comp_type_match = re.match(r"^([A-Za-z_]+)", ref)
if comp_type_match:
comp_type = comp_type_match.group(1)
if comp_type not in analysis["component_types"]:
analysis["component_types"][comp_type] = 0
analysis["component_types"][comp_type] += 1
await ctx.report_progress(60, 100)
# Identify power nets
nets = netlist_data.get("nets", {})
for net_name, pins in nets.items():
if any(
net_name.startswith(prefix)
for prefix in ["VCC", "VDD", "GND", "+5V", "+3V3", "+12V"]
):
analysis["power_nets"].append({"name": net_name, "pin_count": len(pins)})
else:
analysis["signal_nets"].append({"name": net_name, "pin_count": len(pins)})
await ctx.report_progress(80, 100)
# Check for potential issues
# 1. Nets with only one connection (floating)
for net_name, pins in nets.items():
if len(pins) <= 1 and not any(
net_name.startswith(prefix)
for prefix in ["VCC", "VDD", "GND", "+5V", "+3V3", "+12V"]
):
analysis["potential_issues"].append(
{
"type": "floating_net",
"net": net_name,
"description": f"Net '{net_name}' appears to be floating (only has {len(pins)} connection)",
}
)
# 2. Power pins without connections
# This would require more detailed parsing of the schematic
await ctx.report_progress(90, 100)
# Build result
result = {"success": True, "schematic_path": schematic_path, "analysis": analysis}
# Complete progress
await ctx.report_progress(100, 100)
ctx.info("Connection analysis complete")
return result
except Exception as e:
print(f"Error analyzing connections: {str(e)}")
ctx.info(f"Error analyzing connections: {str(e)}")
return {"success": False, "error": str(e)}
@mcp.tool()
async def find_component_connections(
project_path: str, component_ref: str
) -> dict[str, Any]:
"""Find all connections for a specific component in a KiCad project.
This tool extracts information about how a specific component
is connected to other components in the schematic.
Args:
project_path: Path to the KiCad project file (.kicad_pro)
component_ref: Component reference (e.g., "R1", "U3")
ctx: MCP context for progress reporting
Returns:
Dictionary with component connection information
"""
print(f"Finding connections for component {component_ref} in project: {project_path}")
if not os.path.exists(project_path):
print(f"Project not found: {project_path}")
ctx.info(f"Project not found: {project_path}")
return {"success": False, "error": f"Project not found: {project_path}"}
# Report progress
await ctx.report_progress(10, 100)
# Get the schematic file
try:
files = get_project_files(project_path)
if "schematic" not in files:
print("Schematic file not found in project")
ctx.info("Schematic file not found in project")
return {"success": False, "error": "Schematic file not found in project"}
schematic_path = files["schematic"]
print(f"Found schematic file: {schematic_path}")
ctx.info(f"Found schematic file: {os.path.basename(schematic_path)}")
# Extract netlist
await ctx.report_progress(30, 100)
ctx.info(f"Extracting netlist to find connections for {component_ref}...")
netlist_data = extract_netlist(schematic_path)
if "error" in netlist_data:
print(f"Failed to extract netlist: {netlist_data['error']}")
ctx.info(f"Failed to extract netlist: {netlist_data['error']}")
return {"success": False, "error": netlist_data["error"]}
# Check if component exists in the netlist
components = netlist_data.get("components", {})
if component_ref not in components:
print(f"Component {component_ref} not found in schematic")
ctx.info(f"Component {component_ref} not found in schematic")
return {
"success": False,
"error": f"Component {component_ref} not found in schematic",
"available_components": list(components.keys()),
}
# Get component information
component_info = components[component_ref]
# Find connections
await ctx.report_progress(50, 100)
ctx.info("Finding connections...")
nets = netlist_data.get("nets", {})
connections = []
connected_nets = []
for net_name, pins in nets.items():
# Check if any pin belongs to our component
component_pins = []
for pin in pins:
if pin.get("component") == component_ref:
component_pins.append(pin)
if component_pins:
# This net has connections to our component
net_connections = []
for pin in component_pins:
pin_num = pin.get("pin", "Unknown")
# Find other components connected to this pin
connected_components = []
for other_pin in pins:
other_comp = other_pin.get("component")
if other_comp and other_comp != component_ref:
connected_components.append(
{
"component": other_comp,
"pin": other_pin.get("pin", "Unknown"),
}
)
net_connections.append(
{"pin": pin_num, "net": net_name, "connected_to": connected_components}
)
connections.extend(net_connections)
connected_nets.append(net_name)
# Analyze the connections
await ctx.report_progress(70, 100)
ctx.info("Analyzing connections...")
# Categorize connections by pin function (if possible)
pin_functions = {}
if "pins" in component_info:
for pin in component_info["pins"]:
pin_num = pin.get("num")
pin_name = pin.get("name", "")
# Try to categorize based on pin name
pin_type = "unknown"
if any(
power_term in pin_name.upper()
for power_term in ["VCC", "VDD", "VEE", "VSS", "GND", "PWR", "POWER"]
):
pin_type = "power"
elif any(io_term in pin_name.upper() for io_term in ["IO", "I/O", "GPIO"]):
pin_type = "io"
elif any(input_term in pin_name.upper() for input_term in ["IN", "INPUT"]):
pin_type = "input"
elif any(output_term in pin_name.upper() for output_term in ["OUT", "OUTPUT"]):
pin_type = "output"
pin_functions[pin_num] = {"name": pin_name, "type": pin_type}
# Build result
result = {
"success": True,
"project_path": project_path,
"schematic_path": schematic_path,
"component": component_ref,
"component_info": component_info,
"connections": connections,
"connected_nets": connected_nets,
"pin_functions": pin_functions,
"total_connections": len(connections),
}
await ctx.report_progress(100, 100)
ctx.info(f"Found {len(connections)} connections for component {component_ref}")
return result
except Exception as e:
print(f"Error finding component connections: {str(e)}", exc_info=True)
ctx.info(f"Error finding component connections: {str(e)}")
return {"success": False, "error": str(e)}

View File

@ -1,180 +0,0 @@
"""
Circuit pattern recognition tools for KiCad schematics.
"""
import os
from typing import Any
from fastmcp import FastMCP
from mckicad.utils.file_utils import get_project_files
from mckicad.utils.netlist_parser import analyze_netlist, extract_netlist
from mckicad.utils.pattern_recognition import (
identify_amplifiers,
identify_digital_interfaces,
identify_filters,
identify_microcontrollers,
identify_oscillators,
identify_power_supplies,
identify_sensor_interfaces,
)
def register_pattern_tools(mcp: FastMCP) -> None:
"""Register circuit pattern recognition tools with the MCP server.
Args:
mcp: The FastMCP server instance
"""
@mcp.tool()
def identify_circuit_patterns(schematic_path: str) -> dict[str, Any]:
"""Identify common circuit patterns in a KiCad schematic.
This tool analyzes a schematic to recognize common circuit blocks such as:
- Power supply circuits (linear regulators, switching converters)
- Amplifier circuits (op-amps, transistor amplifiers)
- Filter circuits (RC, LC, active filters)
- Digital interfaces (I2C, SPI, UART)
- Microcontroller circuits
- And more
Args:
schematic_path: Path to the KiCad schematic file (.kicad_sch)
Returns:
Dictionary with identified circuit patterns
"""
if not os.path.exists(schematic_path):
return {"success": False, "error": f"Schematic file not found: {schematic_path}"}
# Report progress
try:
# Extract netlist information
netlist_data = extract_netlist(schematic_path)
if "error" in netlist_data:
return {"success": False, "error": netlist_data["error"]}
# Analyze components and nets
components = netlist_data.get("components", {})
nets = netlist_data.get("nets", {})
# Start pattern recognition
identified_patterns = {
"power_supply_circuits": [],
"amplifier_circuits": [],
"filter_circuits": [],
"oscillator_circuits": [],
"digital_interface_circuits": [],
"microcontroller_circuits": [],
"sensor_interface_circuits": [],
"other_patterns": [],
}
# Identify power supply circuits
identified_patterns["power_supply_circuits"] = identify_power_supplies(components, nets)
# Identify amplifier circuits
identified_patterns["amplifier_circuits"] = identify_amplifiers(components, nets)
# Identify filter circuits
identified_patterns["filter_circuits"] = identify_filters(components, nets)
# Identify oscillator circuits
identified_patterns["oscillator_circuits"] = identify_oscillators(components, nets)
# Identify digital interface circuits
identified_patterns["digital_interface_circuits"] = identify_digital_interfaces(
components, nets
)
# Identify microcontroller circuits
identified_patterns["microcontroller_circuits"] = identify_microcontrollers(components)
# Identify sensor interface circuits
identified_patterns["sensor_interface_circuits"] = identify_sensor_interfaces(
components, nets
)
# Build result
result = {
"success": True,
"schematic_path": schematic_path,
"component_count": netlist_data["component_count"],
"identified_patterns": identified_patterns,
}
# Count total patterns
total_patterns = sum(len(patterns) for patterns in identified_patterns.values())
result["total_patterns_found"] = total_patterns
# Complete progress
return result
except Exception as e:
return {"success": False, "error": str(e)}
@mcp.tool()
def analyze_project_circuit_patterns(project_path: str) -> dict[str, Any]:
"""Identify circuit patterns in a KiCad project's schematic.
Args:
project_path: Path to the KiCad project file (.kicad_pro)
Returns:
Dictionary with identified circuit patterns
"""
if not os.path.exists(project_path):
return {"success": False, "error": f"Project not found: {project_path}"}
# Get the schematic file
try:
files = get_project_files(project_path)
if "schematic" not in files:
return {"success": False, "error": "Schematic file not found in project"}
schematic_path = files["schematic"]
# Identify patterns in the schematic - call synchronous version
if not os.path.exists(schematic_path):
return {"success": False, "error": f"Schematic file not found: {schematic_path}"}
# Extract netlist data
netlist_data = extract_netlist(schematic_path)
if not netlist_data:
return {"success": False, "error": "Failed to extract netlist from schematic"}
components, nets = analyze_netlist(netlist_data)
# Identify patterns
identified_patterns = {}
identified_patterns["power_supply_circuits"] = identify_power_supplies(components, nets)
identified_patterns["amplifier_circuits"] = identify_amplifiers(components, nets)
identified_patterns["filter_circuits"] = identify_filters(components, nets)
identified_patterns["oscillator_circuits"] = identify_oscillators(components, nets)
identified_patterns["digital_interface_circuits"] = identify_digital_interfaces(components, nets)
identified_patterns["microcontroller_circuits"] = identify_microcontrollers(components)
identified_patterns["sensor_interface_circuits"] = identify_sensor_interfaces(components, nets)
result = {
"success": True,
"schematic_path": schematic_path,
"patterns": identified_patterns,
"total_patterns_found": sum(len(patterns) for patterns in identified_patterns.values())
}
# Add project path to result
if "success" in result and result["success"]:
result["project_path"] = project_path
return result
except Exception as e:
return {"success": False, "error": str(e)}

View File

@ -1,723 +0,0 @@
"""
Complete Project Automation Pipeline for KiCad MCP Server
Provides end-to-end automation for KiCad projects, from schematic analysis
to production-ready manufacturing files. Integrates all MCP capabilities
including AI analysis, automated routing, and manufacturing optimization.
"""
from datetime import datetime
import logging
from pathlib import Path
from typing import Any
from fastmcp import FastMCP
from mckicad.utils.file_utils import get_project_files
from mckicad.utils.freerouting_engine import FreeRoutingEngine
from mckicad.utils.ipc_client import check_kicad_availability
logger = logging.getLogger(__name__)
def register_project_automation_tools(mcp: FastMCP) -> None:
"""Register complete project automation tools with the MCP server."""
@mcp.tool()
def automate_complete_design(
project_path: str,
target_technology: str = "standard",
optimization_goals: list[str] = None,
include_manufacturing: bool = True
) -> dict[str, Any]:
"""
Complete end-to-end design automation from schematic to manufacturing.
Performs comprehensive project automation including:
- AI-driven design analysis and recommendations
- Automated component placement optimization
- Complete PCB routing with FreeRouting
- DRC validation and fixing
- Manufacturing file generation
- Supply chain analysis and optimization
Args:
project_path: Path to KiCad project file (.kicad_pro)
target_technology: Target PCB technology ("standard", "hdi", "rf", "automotive")
optimization_goals: List of optimization priorities
include_manufacturing: Whether to generate manufacturing files
Returns:
Dictionary with complete automation results and project status
Examples:
automate_complete_design("/path/to/project.kicad_pro")
automate_complete_design("/path/to/project.kicad_pro", "rf", ["signal_integrity", "thermal"])
"""
try:
if not optimization_goals:
optimization_goals = ["signal_integrity", "thermal", "manufacturability", "cost"]
automation_log = []
results = {
"success": True,
"project_path": project_path,
"target_technology": target_technology,
"optimization_goals": optimization_goals,
"automation_log": automation_log,
"stage_results": {},
"overall_metrics": {},
"recommendations": []
}
# Stage 1: Project validation and setup
automation_log.append("Stage 1: Project validation and setup")
stage1_result = _validate_and_setup_project(project_path, target_technology)
results["stage_results"]["project_setup"] = stage1_result
if not stage1_result["success"]:
results["success"] = False
results["error"] = f"Project setup failed: {stage1_result['error']}"
return results
# Stage 2: AI-driven design analysis
automation_log.append("Stage 2: AI-driven design analysis and optimization")
stage2_result = _perform_ai_analysis(project_path, target_technology)
results["stage_results"]["ai_analysis"] = stage2_result
# Stage 3: Component placement optimization
automation_log.append("Stage 3: Component placement optimization")
stage3_result = _optimize_component_placement(project_path, optimization_goals)
results["stage_results"]["placement_optimization"] = stage3_result
# Stage 4: Automated routing
automation_log.append("Stage 4: Automated PCB routing")
stage4_result = _perform_automated_routing(project_path, target_technology, optimization_goals)
results["stage_results"]["automated_routing"] = stage4_result
# Stage 5: Design validation and DRC
automation_log.append("Stage 5: Design validation and DRC checking")
stage5_result = _validate_design_rules(project_path, target_technology)
results["stage_results"]["design_validation"] = stage5_result
# Stage 6: Manufacturing preparation
if include_manufacturing:
automation_log.append("Stage 6: Manufacturing file generation")
stage6_result = _prepare_manufacturing_files(project_path, target_technology)
results["stage_results"]["manufacturing_prep"] = stage6_result
# Stage 7: Final analysis and recommendations
automation_log.append("Stage 7: Final analysis and recommendations")
stage7_result = _generate_final_analysis(results)
results["stage_results"]["final_analysis"] = stage7_result
results["recommendations"] = stage7_result.get("recommendations", [])
# Calculate overall metrics
results["overall_metrics"] = _calculate_automation_metrics(results)
automation_log.append(f"Automation completed successfully in {len(results['stage_results'])} stages")
return results
except Exception as e:
logger.error(f"Error in complete design automation: {e}")
return {
"success": False,
"error": str(e),
"project_path": project_path,
"stage": "general_error"
}
@mcp.tool()
def create_outlet_tester_complete(
project_path: str,
outlet_type: str = "standard_120v",
features: list[str] = None
) -> dict[str, Any]:
"""
Complete automation for outlet tester project creation.
Creates a complete outlet tester project from concept to production,
including schematic generation, component selection, PCB layout,
routing, and manufacturing files.
Args:
project_path: Path for new project creation
outlet_type: Type of outlet to test ("standard_120v", "gfci", "european_230v")
features: List of desired features ("voltage_display", "polarity_check", "gfci_test", "load_test")
Returns:
Dictionary with complete outlet tester creation results
"""
try:
if not features:
features = ["voltage_display", "polarity_check", "gfci_test"]
automation_log = []
results = {
"success": True,
"project_path": project_path,
"outlet_type": outlet_type,
"features": features,
"automation_log": automation_log,
"creation_stages": {}
}
# Stage 1: Project structure creation
automation_log.append("Stage 1: Creating project structure")
stage1_result = _create_outlet_tester_structure(project_path, outlet_type)
results["creation_stages"]["project_structure"] = stage1_result
# Stage 2: Schematic generation
automation_log.append("Stage 2: Generating optimized schematic")
stage2_result = _generate_outlet_tester_schematic(project_path, outlet_type, features)
results["creation_stages"]["schematic_generation"] = stage2_result
# Stage 3: Component selection and BOM
automation_log.append("Stage 3: AI-driven component selection")
stage3_result = _select_outlet_tester_components(project_path, features)
results["creation_stages"]["component_selection"] = stage3_result
# Stage 4: PCB layout generation
automation_log.append("Stage 4: Automated PCB layout")
stage4_result = _generate_outlet_tester_layout(project_path, outlet_type)
results["creation_stages"]["pcb_layout"] = stage4_result
# Stage 5: Complete automation pipeline
automation_log.append("Stage 5: Running complete automation pipeline")
automation_result = automate_complete_design(
project_path,
target_technology="standard",
optimization_goals=["signal_integrity", "thermal", "cost"]
)
results["creation_stages"]["automation_pipeline"] = automation_result
# Stage 6: Outlet-specific validation
automation_log.append("Stage 6: Outlet tester specific validation")
stage6_result = _validate_outlet_tester_design(project_path, outlet_type, features)
results["creation_stages"]["outlet_validation"] = stage6_result
automation_log.append("Outlet tester project created successfully")
return results
except Exception as e:
logger.error(f"Error creating outlet tester: {e}")
return {
"success": False,
"error": str(e),
"project_path": project_path
}
@mcp.tool()
def batch_process_projects(
project_paths: list[str],
automation_level: str = "full",
parallel_processing: bool = False
) -> dict[str, Any]:
"""
Batch process multiple KiCad projects with automation.
Processes multiple projects with the same automation pipeline,
providing consolidated reporting and optimization across projects.
Args:
project_paths: List of paths to KiCad project files
automation_level: Level of automation ("basic", "standard", "full")
parallel_processing: Whether to process projects in parallel (requires care)
Returns:
Dictionary with batch processing results
"""
try:
batch_results = {
"success": True,
"total_projects": len(project_paths),
"automation_level": automation_level,
"parallel_processing": parallel_processing,
"project_results": {},
"batch_summary": {},
"errors": []
}
# Define automation levels
automation_configs = {
"basic": {
"include_ai_analysis": False,
"include_routing": False,
"include_manufacturing": False
},
"standard": {
"include_ai_analysis": True,
"include_routing": True,
"include_manufacturing": False
},
"full": {
"include_ai_analysis": True,
"include_routing": True,
"include_manufacturing": True
}
}
config = automation_configs.get(automation_level, automation_configs["standard"])
# Process each project
for i, project_path in enumerate(project_paths):
try:
logger.info(f"Processing project {i+1}/{len(project_paths)}: {project_path}")
if config["include_ai_analysis"] and config["include_routing"]:
# Full automation
result = automate_complete_design(
project_path,
include_manufacturing=config["include_manufacturing"]
)
else:
# Basic processing
result = _basic_project_processing(project_path, config)
batch_results["project_results"][project_path] = result
if not result["success"]:
batch_results["errors"].append({
"project": project_path,
"error": result.get("error", "Unknown error")
})
except Exception as e:
error_msg = f"Error processing {project_path}: {e}"
logger.error(error_msg)
batch_results["errors"].append({
"project": project_path,
"error": str(e)
})
# Generate batch summary
batch_results["batch_summary"] = _generate_batch_summary(batch_results)
# Update overall success status
batch_results["success"] = len(batch_results["errors"]) == 0
return batch_results
except Exception as e:
logger.error(f"Error in batch processing: {e}")
return {
"success": False,
"error": str(e),
"project_paths": project_paths
}
@mcp.tool()
def monitor_automation_progress(session_id: str) -> dict[str, Any]:
"""
Monitor progress of long-running automation tasks.
Provides real-time status updates for automation processes that
may take significant time to complete.
Args:
session_id: Unique identifier for the automation session
Returns:
Dictionary with current progress status
"""
try:
# This would typically connect to a progress tracking system
# For now, return a mock progress status
progress_data = {
"session_id": session_id,
"status": "in_progress",
"current_stage": "automated_routing",
"progress_percent": 75,
"stages_completed": [
"project_setup",
"ai_analysis",
"placement_optimization"
],
"current_operation": "Running FreeRouting autorouter",
"estimated_time_remaining": "2 minutes",
"last_update": datetime.now().isoformat()
}
return {
"success": True,
"progress": progress_data
}
except Exception as e:
logger.error(f"Error monitoring automation progress: {e}")
return {
"success": False,
"error": str(e),
"session_id": session_id
}
# Stage implementation functions
def _validate_and_setup_project(project_path: str, target_technology: str) -> dict[str, Any]:
"""Validate project and setup for automation."""
try:
# Check if project files exist
files = get_project_files(project_path)
if not files:
return {
"success": False,
"error": "Project files not found or invalid project path"
}
# Check KiCad IPC availability
ipc_status = check_kicad_availability()
return {
"success": True,
"project_files": files,
"ipc_available": ipc_status["available"],
"target_technology": target_technology,
"setup_complete": True
}
except Exception as e:
return {
"success": False,
"error": str(e)
}
def _perform_ai_analysis(project_path: str, target_technology: str) -> dict[str, Any]:
"""Perform AI-driven design analysis."""
try:
# This would call the AI analysis tools
# For now, return a structured response
return {
"success": True,
"design_completeness": 85,
"component_suggestions": {
"power_management": ["Add decoupling capacitors"],
"protection": ["Consider ESD protection"]
},
"design_rules": {
"trace_width": {"min": 0.1, "preferred": 0.15},
"clearance": {"min": 0.1, "preferred": 0.15}
},
"optimization_recommendations": [
"Optimize component placement for thermal management",
"Consider controlled impedance for high-speed signals"
]
}
except Exception as e:
return {
"success": False,
"error": str(e)
}
def _optimize_component_placement(project_path: str, goals: list[str]) -> dict[str, Any]:
"""Optimize component placement using IPC API."""
try:
files = get_project_files(project_path)
if "pcb" not in files:
return {
"success": False,
"error": "PCB file not found"
}
# This would use the routing tools for placement optimization
return {
"success": True,
"optimizations_applied": 3,
"placement_score": 88,
"thermal_improvements": "Good thermal distribution achieved"
}
except Exception as e:
return {
"success": False,
"error": str(e)
}
def _perform_automated_routing(project_path: str, technology: str, goals: list[str]) -> dict[str, Any]:
"""Perform automated routing with FreeRouting."""
try:
files = get_project_files(project_path)
if "pcb" not in files:
return {
"success": False,
"error": "PCB file not found"
}
# Initialize FreeRouting engine
engine = FreeRoutingEngine()
# Check availability
availability = engine.check_freerouting_availability()
if not availability["available"]:
return {
"success": False,
"error": f"FreeRouting not available: {availability['message']}"
}
# Perform routing
routing_strategy = "balanced"
if "signal_integrity" in goals:
routing_strategy = "conservative"
elif "cost" in goals:
routing_strategy = "aggressive"
result = engine.route_board_complete(files["pcb"])
return {
"success": result["success"],
"routing_strategy": routing_strategy,
"routing_completion": result.get("post_routing_stats", {}).get("routing_completion", 0),
"routed_nets": result.get("post_routing_stats", {}).get("routed_nets", 0),
"routing_details": result
}
except Exception as e:
return {
"success": False,
"error": str(e)
}
def _validate_design_rules(project_path: str, technology: str) -> dict[str, Any]:
"""Validate design with DRC checking."""
try:
# Simplified DRC validation - would integrate with actual DRC tools
return {
"success": True,
"drc_violations": 0,
"drc_summary": {"status": "passed"},
"validation_passed": True
}
except Exception as e:
return {
"success": False,
"error": str(e)
}
def _prepare_manufacturing_files(project_path: str, technology: str) -> dict[str, Any]:
"""Generate manufacturing files."""
try:
# Simplified manufacturing file generation - would integrate with actual export tools
return {
"success": True,
"bom_generated": True,
"gerber_files": ["F.Cu.gbr", "B.Cu.gbr", "F.Mask.gbr", "B.Mask.gbr"],
"drill_files": ["NPTH.drl", "PTH.drl"],
"assembly_files": ["pick_and_place.csv", "assembly_drawing.pdf"],
"manufacturing_ready": True
}
except Exception as e:
return {
"success": False,
"error": str(e)
}
def _generate_final_analysis(results: dict[str, Any]) -> dict[str, Any]:
"""Generate final analysis and recommendations."""
try:
recommendations = []
# Analyze results and generate recommendations
stage_results = results.get("stage_results", {})
if stage_results.get("automated_routing", {}).get("routing_completion", 0) < 95:
recommendations.append("Consider manual routing for remaining unrouted nets")
if stage_results.get("design_validation", {}).get("drc_violations", 0) > 0:
recommendations.append("Fix remaining DRC violations before manufacturing")
recommendations.extend([
"Review manufacturing files before production",
"Perform final electrical validation",
"Consider prototype testing before full production"
])
return {
"success": True,
"overall_quality_score": 88,
"recommendations": recommendations,
"project_status": "Ready for manufacturing review"
}
except Exception as e:
return {
"success": False,
"error": str(e)
}
def _calculate_automation_metrics(results: dict[str, Any]) -> dict[str, Any]:
"""Calculate overall automation metrics."""
stage_results = results.get("stage_results", {})
metrics = {
"stages_completed": len([s for s in stage_results.values() if s.get("success", False)]),
"total_stages": len(stage_results),
"success_rate": 0,
"automation_score": 0
}
if metrics["total_stages"] > 0:
metrics["success_rate"] = metrics["stages_completed"] / metrics["total_stages"] * 100
metrics["automation_score"] = min(metrics["success_rate"], 100)
return metrics
# Outlet tester specific functions
def _create_outlet_tester_structure(project_path: str, outlet_type: str) -> dict[str, Any]:
"""Create project structure for outlet tester."""
try:
project_dir = Path(project_path).parent
project_dir.mkdir(parents=True, exist_ok=True)
return {
"success": True,
"project_directory": str(project_dir),
"outlet_type": outlet_type,
"structure_created": True
}
except Exception as e:
return {
"success": False,
"error": str(e)
}
def _generate_outlet_tester_schematic(project_path: str, outlet_type: str, features: list[str]) -> dict[str, Any]:
"""Generate optimized schematic for outlet tester."""
try:
# This would generate a schematic based on outlet type and features
return {
"success": True,
"outlet_type": outlet_type,
"features_included": features,
"schematic_generated": True,
"component_count": 25 # Estimated
}
except Exception as e:
return {
"success": False,
"error": str(e)
}
def _select_outlet_tester_components(project_path: str, features: list[str]) -> dict[str, Any]:
"""Select components for outlet tester using AI analysis."""
try:
# This would use AI tools to select optimal components
return {
"success": True,
"components_selected": {
"microcontroller": "ATmega328P",
"display": "16x2 LCD",
"voltage_sensor": "Precision voltage divider",
"current_sensor": "ACS712",
"protection": ["Fuse", "MOV", "TVS diodes"]
},
"estimated_cost": 25.50,
"availability": "All components in stock"
}
except Exception as e:
return {
"success": False,
"error": str(e)
}
def _generate_outlet_tester_layout(project_path: str, outlet_type: str) -> dict[str, Any]:
"""Generate PCB layout for outlet tester."""
try:
# This would generate an optimized PCB layout
return {
"success": True,
"board_size": "80mm x 50mm",
"layer_count": 2,
"layout_optimized": True,
"thermal_management": "Adequate for application"
}
except Exception as e:
return {
"success": False,
"error": str(e)
}
def _validate_outlet_tester_design(project_path: str, outlet_type: str, features: list[str]) -> dict[str, Any]:
"""Validate outlet tester design for safety and functionality."""
try:
# This would perform outlet-specific validation
return {
"success": True,
"safety_validation": "Passed electrical safety checks",
"functionality_validation": "All features properly implemented",
"compliance": "Meets relevant electrical standards",
"test_procedures": [
"Voltage measurement accuracy test",
"Polarity detection test",
"GFCI function test",
"Safety isolation test"
]
}
except Exception as e:
return {
"success": False,
"error": str(e)
}
def _basic_project_processing(project_path: str, config: dict[str, Any]) -> dict[str, Any]:
"""Basic project processing for batch operations."""
try:
# Perform basic validation and analysis
files = get_project_files(project_path)
result = {
"success": True,
"project_path": project_path,
"files_found": list(files.keys()),
"processing_level": "basic"
}
if config.get("include_ai_analysis", False):
result["ai_analysis"] = "Basic AI analysis completed"
return result
except Exception as e:
return {
"success": False,
"error": str(e)
}
def _generate_batch_summary(batch_results: dict[str, Any]) -> dict[str, Any]:
"""Generate summary for batch processing results."""
total_projects = batch_results["total_projects"]
successful_projects = len([r for r in batch_results["project_results"].values() if r.get("success", False)])
return {
"total_projects": total_projects,
"successful_projects": successful_projects,
"failed_projects": total_projects - successful_projects,
"success_rate": successful_projects / max(total_projects, 1) * 100,
"processing_time": "Estimated based on project complexity",
"common_issues": [error["error"] for error in batch_results["errors"][:3]] # Top 3 issues
}

View File

@ -1,62 +0,0 @@
"""
Project management tools for KiCad.
"""
import logging
import os
from typing import Any
from fastmcp import FastMCP
from mckicad.utils.file_utils import get_project_files, load_project_json
from mckicad.utils.kicad_utils import find_kicad_projects, open_kicad_project
# Get PID for logging
# _PID = os.getpid()
def register_project_tools(mcp: FastMCP) -> None:
"""Register project management tools with the MCP server.
Args:
mcp: The FastMCP server instance
"""
@mcp.tool()
def list_projects() -> list[dict[str, Any]]:
"""Find and list all KiCad projects on this system."""
logging.info("Executing list_projects tool...")
projects = find_kicad_projects()
logging.info(f"list_projects tool returning {len(projects)} projects.")
return projects
@mcp.tool()
def get_project_structure(project_path: str) -> dict[str, Any]:
"""Get the structure and files of a KiCad project."""
if not os.path.exists(project_path):
return {"error": f"Project not found: {project_path}"}
project_dir = os.path.dirname(project_path)
project_name = os.path.basename(project_path)[:-10] # Remove .kicad_pro extension
# Get related files
files = get_project_files(project_path)
# Get project metadata
metadata = {}
project_data = load_project_json(project_path)
if project_data and "metadata" in project_data:
metadata = project_data["metadata"]
return {
"name": project_name,
"path": project_path,
"directory": project_dir,
"files": files,
"metadata": metadata,
}
@mcp.tool()
def open_project(project_path: str) -> dict[str, Any]:
"""Open a KiCad project in KiCad."""
return open_kicad_project(project_path)

View File

@ -1,712 +0,0 @@
"""
Automated Routing Tools for KiCad MCP Server
Provides MCP tools for automated PCB routing using FreeRouting integration
and KiCad IPC API for real-time routing operations and optimization.
"""
import logging
from typing import Any
from fastmcp import FastMCP
from mckicad.utils.file_utils import get_project_files
from mckicad.utils.freerouting_engine import FreeRoutingEngine, check_routing_prerequisites
from mckicad.utils.ipc_client import (
check_kicad_availability,
kicad_ipc_session,
)
logger = logging.getLogger(__name__)
def register_routing_tools(mcp: FastMCP) -> None:
"""Register automated routing tools with the MCP server."""
@mcp.tool()
def check_routing_capability() -> dict[str, Any]:
"""
Check if automated routing is available and working.
Verifies that all components needed for automated routing are installed
and properly configured, including KiCad IPC API, FreeRouting, and KiCad CLI.
Returns:
Dictionary with capability status and component details
"""
try:
status = check_routing_prerequisites()
return {
"success": True,
"routing_available": status["overall_ready"],
"message": status["message"],
"component_status": status["components"],
"capabilities": {
"automated_routing": status["overall_ready"],
"interactive_placement": status["components"].get("kicad_ipc", {}).get("available", False),
"optimization": status["overall_ready"],
"real_time_updates": status["components"].get("kicad_ipc", {}).get("available", False)
}
}
except Exception as e:
return {
"success": False,
"error": str(e),
"routing_available": False
}
@mcp.tool()
def route_pcb_automatically(
project_path: str,
routing_strategy: str = "balanced",
preserve_existing: bool = False,
optimization_level: str = "standard"
) -> dict[str, Any]:
"""
Perform automated PCB routing using FreeRouting.
Takes a KiCad PCB with placed components and automatically routes all connections
using the FreeRouting autorouter with optimized parameters.
Args:
project_path: Path to KiCad project file (.kicad_pro)
routing_strategy: Routing approach ("conservative", "balanced", "aggressive")
preserve_existing: Whether to preserve existing routing
optimization_level: Post-routing optimization ("none", "standard", "aggressive")
Returns:
Dictionary with routing results and statistics
Examples:
route_pcb_automatically("/path/to/project.kicad_pro")
route_pcb_automatically("/path/to/project.kicad_pro", "aggressive", optimization_level="aggressive")
"""
try:
# Get project files
files = get_project_files(project_path)
if "pcb" not in files:
return {
"success": False,
"error": "PCB file not found in project"
}
board_path = files["pcb"]
# Configure routing parameters based on strategy
routing_configs = {
"conservative": {
"via_costs": 30,
"start_ripup_costs": 50,
"max_iterations": 500,
"automatic_neckdown": False,
"postroute_optimization": optimization_level != "none"
},
"balanced": {
"via_costs": 50,
"start_ripup_costs": 100,
"max_iterations": 1000,
"automatic_neckdown": True,
"postroute_optimization": optimization_level != "none"
},
"aggressive": {
"via_costs": 80,
"start_ripup_costs": 200,
"max_iterations": 2000,
"automatic_neckdown": True,
"postroute_optimization": True
}
}
config = routing_configs.get(routing_strategy, routing_configs["balanced"])
# Add optimization settings
if optimization_level == "aggressive":
config.update({
"improvement_threshold": 0.005, # More aggressive optimization
"max_iterations": config["max_iterations"] * 2
})
# Initialize FreeRouting engine
engine = FreeRoutingEngine()
# Check if FreeRouting is available
availability = engine.check_freerouting_availability()
if not availability["available"]:
return {
"success": False,
"error": f"FreeRouting not available: {availability['message']}",
"routing_strategy": routing_strategy
}
# Perform automated routing
result = engine.route_board_complete(
board_path,
routing_config=config,
preserve_existing=preserve_existing
)
# Add strategy info to result
result.update({
"routing_strategy": routing_strategy,
"optimization_level": optimization_level,
"project_path": project_path,
"board_path": board_path
})
return result
except Exception as e:
logger.error(f"Error in automated routing: {e}")
return {
"success": False,
"error": str(e),
"project_path": project_path,
"routing_strategy": routing_strategy
}
@mcp.tool()
def optimize_component_placement(
project_path: str,
optimization_goals: list[str] = None,
placement_strategy: str = "thermal_aware"
) -> dict[str, Any]:
"""
Optimize component placement for better routing and performance.
Uses KiCad IPC API to analyze and optimize component placement based on
thermal, signal integrity, and routing efficiency considerations.
Args:
project_path: Path to KiCad project file (.kicad_pro)
optimization_goals: List of goals ("thermal", "signal_integrity", "routing_density", "manufacturability")
placement_strategy: Strategy for placement ("thermal_aware", "signal_integrity", "compact", "spread")
Returns:
Dictionary with placement optimization results
"""
try:
if not optimization_goals:
optimization_goals = ["thermal", "signal_integrity", "routing_density"]
# Get project files
files = get_project_files(project_path)
if "pcb" not in files:
return {
"success": False,
"error": "PCB file not found in project"
}
board_path = files["pcb"]
# Check KiCad IPC availability
ipc_status = check_kicad_availability()
if not ipc_status["available"]:
return {
"success": False,
"error": f"KiCad IPC not available: {ipc_status['message']}"
}
with kicad_ipc_session(board_path=board_path) as client:
# Get current component placement
footprints = client.get_footprints()
board_stats = client.get_board_statistics()
# Analyze current placement
placement_analysis = _analyze_component_placement(
footprints, optimization_goals, placement_strategy
)
# Generate optimization suggestions
optimizations = _generate_placement_optimizations(
footprints, placement_analysis, optimization_goals
)
# Apply optimizations if any are found
applied_changes = []
if optimizations.get("component_moves"):
for move in optimizations["component_moves"]:
success = client.move_footprint(
move["reference"],
move["new_position"]
)
if success:
applied_changes.append(move)
if optimizations.get("component_rotations"):
for rotation in optimizations["component_rotations"]:
success = client.rotate_footprint(
rotation["reference"],
rotation["new_angle"]
)
if success:
applied_changes.append(rotation)
# Save changes if any were made
if applied_changes:
client.save_board()
return {
"success": True,
"project_path": project_path,
"optimization_goals": optimization_goals,
"placement_strategy": placement_strategy,
"analysis": placement_analysis,
"suggested_optimizations": optimizations,
"applied_changes": applied_changes,
"board_statistics": board_stats,
"summary": f"Applied {len(applied_changes)} placement optimizations"
}
except Exception as e:
logger.error(f"Error in placement optimization: {e}")
return {
"success": False,
"error": str(e),
"project_path": project_path
}
@mcp.tool()
def analyze_routing_quality(project_path: str) -> dict[str, Any]:
"""
Analyze PCB routing quality and identify potential issues.
Examines the current routing for signal integrity issues, thermal problems,
manufacturability concerns, and optimization opportunities.
Args:
project_path: Path to KiCad project file (.kicad_pro)
Returns:
Dictionary with routing quality analysis and recommendations
"""
try:
# Get project files
files = get_project_files(project_path)
if "pcb" not in files:
return {
"success": False,
"error": "PCB file not found in project"
}
board_path = files["pcb"]
with kicad_ipc_session(board_path=board_path) as client:
# Get routing information
tracks = client.get_tracks()
nets = client.get_nets()
footprints = client.get_footprints()
connectivity = client.check_connectivity()
# Analyze routing quality
quality_analysis = {
"connectivity_analysis": connectivity,
"routing_density": _analyze_routing_density(tracks, footprints),
"via_analysis": _analyze_via_usage(tracks),
"trace_analysis": _analyze_trace_characteristics(tracks),
"signal_integrity": _analyze_signal_integrity(tracks, nets),
"thermal_analysis": _analyze_thermal_aspects(tracks, footprints),
"manufacturability": _analyze_manufacturability(tracks)
}
# Generate overall quality score
quality_score = _calculate_quality_score(quality_analysis)
# Generate recommendations
recommendations = _generate_routing_recommendations(quality_analysis)
return {
"success": True,
"project_path": project_path,
"quality_score": quality_score,
"analysis": quality_analysis,
"recommendations": recommendations,
"summary": f"Routing quality score: {quality_score}/100"
}
except Exception as e:
logger.error(f"Error in routing quality analysis: {e}")
return {
"success": False,
"error": str(e),
"project_path": project_path
}
@mcp.tool()
def interactive_routing_session(
project_path: str,
net_name: str,
routing_mode: str = "guided"
) -> dict[str, Any]:
"""
Start an interactive routing session for specific nets.
Provides guided routing assistance using KiCad IPC API for real-time
feedback and optimization suggestions during manual routing.
Args:
project_path: Path to KiCad project file (.kicad_pro)
net_name: Name of the net to route (or "all" for all unrouted nets)
routing_mode: Mode for routing assistance ("guided", "automatic", "manual")
Returns:
Dictionary with interactive routing session information
"""
try:
# Get project files
files = get_project_files(project_path)
if "pcb" not in files:
return {
"success": False,
"error": "PCB file not found in project"
}
board_path = files["pcb"]
with kicad_ipc_session(board_path=board_path) as client:
# Get net information
if net_name == "all":
connectivity = client.check_connectivity()
target_nets = connectivity.get("routed_net_names", [])
unrouted_nets = []
all_nets = client.get_nets()
for net in all_nets:
if net.name and net.name not in target_nets:
unrouted_nets.append(net.name)
session_info = {
"session_type": "multi_net",
"target_nets": unrouted_nets[:10], # Limit to first 10
"total_unrouted": len(unrouted_nets)
}
else:
net = client.get_net_by_name(net_name)
if not net:
return {
"success": False,
"error": f"Net '{net_name}' not found in board"
}
session_info = {
"session_type": "single_net",
"target_net": net_name,
"net_details": {
"name": net.name,
"code": getattr(net, 'code', 'unknown')
}
}
# Analyze routing constraints and provide guidance
routing_guidance = _generate_routing_guidance(
client, session_info, routing_mode
)
return {
"success": True,
"project_path": project_path,
"session_info": session_info,
"routing_mode": routing_mode,
"guidance": routing_guidance,
"instructions": [
"Use KiCad's interactive routing tools to route the specified nets",
"Refer to the guidance for optimal routing strategies",
"The board will be monitored for real-time feedback"
]
}
except Exception as e:
logger.error(f"Error starting interactive routing session: {e}")
return {
"success": False,
"error": str(e),
"project_path": project_path
}
@mcp.tool()
def route_specific_nets(
project_path: str,
net_names: list[str],
routing_priority: str = "signal_integrity"
) -> dict[str, Any]:
"""
Route specific nets with targeted strategies.
Routes only the specified nets using appropriate strategies based on
signal type and routing requirements.
Args:
project_path: Path to KiCad project file (.kicad_pro)
net_names: List of net names to route
routing_priority: Priority for routing ("signal_integrity", "density", "thermal")
Returns:
Dictionary with specific net routing results
"""
try:
# Get project files
files = get_project_files(project_path)
if "pcb" not in files:
return {
"success": False,
"error": "PCB file not found in project"
}
board_path = files["pcb"]
with kicad_ipc_session(board_path=board_path) as client:
# Validate nets exist
all_nets = {net.name: net for net in client.get_nets()}
valid_nets = []
invalid_nets = []
for net_name in net_names:
if net_name in all_nets:
valid_nets.append(net_name)
else:
invalid_nets.append(net_name)
if not valid_nets:
return {
"success": False,
"error": f"None of the specified nets found: {net_names}",
"invalid_nets": invalid_nets
}
# Clear existing routing for specified nets
cleared_nets = []
for net_name in valid_nets:
if client.delete_tracks_by_net(net_name):
cleared_nets.append(net_name)
# Configure routing for specific nets
net_specific_config = _get_net_specific_routing_config(
valid_nets, routing_priority
)
# Use FreeRouting with net-specific configuration
engine = FreeRoutingEngine()
result = engine.route_board_complete(
board_path,
routing_config=net_specific_config
)
# Analyze results for specified nets
if result["success"]:
net_results = _analyze_net_routing_results(
client, valid_nets, result
)
else:
net_results = {"error": "Routing failed"}
return {
"success": result["success"],
"project_path": project_path,
"requested_nets": net_names,
"valid_nets": valid_nets,
"invalid_nets": invalid_nets,
"cleared_nets": cleared_nets,
"routing_priority": routing_priority,
"routing_result": result,
"net_specific_results": net_results
}
except Exception as e:
logger.error(f"Error routing specific nets: {e}")
return {
"success": False,
"error": str(e),
"project_path": project_path,
"net_names": net_names
}
# Helper functions for component placement optimization
def _analyze_component_placement(footprints, goals, strategy):
"""Analyze current component placement for optimization opportunities."""
analysis = {
"total_components": len(footprints),
"placement_density": 0.0,
"thermal_hotspots": [],
"signal_groupings": {},
"optimization_opportunities": []
}
# Simple placement density calculation
if footprints:
positions = [fp.position for fp in footprints]
# Calculate bounding box and density
analysis["placement_density"] = min(len(footprints) / 100.0, 1.0) # Simplified
return analysis
def _generate_placement_optimizations(footprints, analysis, goals):
"""Generate specific placement optimization suggestions."""
optimizations = {
"component_moves": [],
"component_rotations": [],
"grouping_suggestions": []
}
# Simple optimization logic (would be much more sophisticated in practice)
for i, fp in enumerate(footprints[:3]): # Limit for demo
if hasattr(fp, 'reference') and hasattr(fp, 'position'):
optimizations["component_moves"].append({
"reference": fp.reference,
"current_position": fp.position,
"new_position": fp.position, # Would calculate optimal position
"reason": "Thermal optimization"
})
return optimizations
# Helper functions for routing quality analysis
def _analyze_routing_density(tracks, footprints):
"""Analyze routing density across the board."""
return {
"total_tracks": len(tracks),
"track_density": min(len(tracks) / max(len(footprints), 1), 5.0),
"density_rating": "medium"
}
def _analyze_via_usage(tracks):
"""Analyze via usage patterns."""
via_count = len([t for t in tracks if hasattr(t, 'drill')]) # Simplified via detection
return {
"total_vias": via_count,
"via_density": "normal",
"via_types": {"standard": via_count}
}
def _analyze_trace_characteristics(tracks):
"""Analyze trace width and length characteristics."""
return {
"total_traces": len(tracks),
"width_distribution": {"standard": len(tracks)},
"length_statistics": {"average": 10.0, "max": 50.0}
}
def _analyze_signal_integrity(tracks, nets):
"""Analyze signal integrity aspects."""
return {
"critical_nets": len([n for n in nets if "clk" in n.name.lower()]) if nets else 0,
"high_speed_traces": 0,
"impedance_controlled": False
}
def _analyze_thermal_aspects(tracks, footprints):
"""Analyze thermal management aspects."""
return {
"thermal_vias": 0,
"power_trace_width": "adequate",
"heat_dissipation": "good"
}
def _analyze_manufacturability(tracks):
"""Analyze manufacturability constraints."""
return {
"minimum_trace_width": 0.1,
"minimum_spacing": 0.1,
"drill_sizes": ["0.2", "0.3"],
"manufacturability_rating": "good"
}
def _calculate_quality_score(analysis):
"""Calculate overall routing quality score."""
base_score = 75
connectivity = analysis.get("connectivity_analysis", {})
completion = connectivity.get("routing_completion", 0)
# Simple scoring based on completion
return min(int(base_score + completion * 0.25), 100)
def _generate_routing_recommendations(analysis):
"""Generate routing improvement recommendations."""
recommendations = []
connectivity = analysis.get("connectivity_analysis", {})
unrouted = connectivity.get("unrouted_nets", 0)
if unrouted > 0:
recommendations.append(f"Complete routing for {unrouted} unrouted nets")
recommendations.append("Consider adding test points for critical signals")
recommendations.append("Verify impedance control for high-speed signals")
return recommendations
def _generate_routing_guidance(client, session_info, mode):
"""Generate routing guidance for interactive sessions."""
guidance = {
"strategy": f"Optimized for {mode} routing",
"constraints": [
"Maintain minimum trace width of 0.1mm",
"Use 45-degree angles where possible",
"Minimize via count on critical signals"
],
"recommendations": []
}
if session_info["session_type"] == "single_net":
guidance["recommendations"].append(
f"Route net '{session_info['target_net']}' with direct paths"
)
else:
guidance["recommendations"].append(
f"Route {len(session_info['target_nets'])} nets in order of importance"
)
return guidance
def _get_net_specific_routing_config(net_names, priority):
"""Get routing configuration optimized for specific nets."""
base_config = {
"via_costs": 50,
"start_ripup_costs": 100,
"max_iterations": 1000
}
# Adjust based on priority
if priority == "signal_integrity":
base_config.update({
"via_costs": 80, # Minimize vias
"automatic_neckdown": False
})
elif priority == "density":
base_config.update({
"via_costs": 30, # Allow more vias for density
"automatic_neckdown": True
})
return base_config
def _analyze_net_routing_results(client, net_names, routing_result):
"""Analyze routing results for specific nets."""
try:
connectivity = client.check_connectivity()
routed_nets = set(connectivity.get("routed_net_names", []))
results = {}
for net_name in net_names:
results[net_name] = {
"routed": net_name in routed_nets,
"status": "routed" if net_name in routed_nets else "unrouted"
}
return results
except Exception as e:
return {"error": str(e)}

View File

@ -1,545 +0,0 @@
"""
Symbol Library Management Tools for KiCad MCP Server.
Provides MCP tools for analyzing, validating, and managing KiCad symbol libraries
including library analysis, symbol validation, and organization recommendations.
"""
import os
from typing import Any
from fastmcp import FastMCP
from mckicad.utils.symbol_library import create_symbol_analyzer
def register_symbol_tools(mcp: FastMCP) -> None:
"""Register symbol library management tools with the MCP server."""
@mcp.tool()
def analyze_symbol_library(library_path: str) -> dict[str, Any]:
"""
Analyze a KiCad symbol library file for coverage, statistics, and issues.
Performs comprehensive analysis of symbol library including symbol count,
categories, pin distributions, validation issues, and recommendations.
Args:
library_path: Full path to the .kicad_sym library file to analyze
Returns:
Dictionary with symbol counts, categories, pin statistics, and validation results
Examples:
analyze_symbol_library("/path/to/MyLibrary.kicad_sym")
analyze_symbol_library("~/kicad/symbols/Microcontrollers.kicad_sym")
"""
try:
# Validate library file path
if not os.path.exists(library_path):
return {
"success": False,
"error": f"Library file not found: {library_path}"
}
if not library_path.endswith('.kicad_sym'):
return {
"success": False,
"error": "File must be a KiCad symbol library (.kicad_sym)"
}
# Create analyzer and load library
analyzer = create_symbol_analyzer()
library = analyzer.load_library(library_path)
# Generate comprehensive report
report = analyzer.export_symbol_report(library)
return {
"success": True,
"library_path": library_path,
"report": report
}
except Exception as e:
return {
"success": False,
"error": str(e),
"library_path": library_path
}
@mcp.tool()
def validate_symbol_library(library_path: str) -> dict[str, Any]:
"""
Validate symbols in a KiCad library and report issues.
Checks for common symbol issues including missing properties,
invalid pin configurations, and design rule violations.
Args:
library_path: Path to the .kicad_sym library file
Returns:
Dictionary containing validation results and issue details
"""
try:
if not os.path.exists(library_path):
return {
"success": False,
"error": f"Library file not found: {library_path}"
}
analyzer = create_symbol_analyzer()
library = analyzer.load_library(library_path)
# Validate all symbols
validation_results = []
total_issues = 0
for symbol in library.symbols:
issues = analyzer.validate_symbol(symbol)
if issues:
validation_results.append({
"symbol_name": symbol.name,
"issues": issues,
"issue_count": len(issues),
"severity": "error" if any("Missing essential" in issue for issue in issues) else "warning"
})
total_issues += len(issues)
return {
"success": True,
"library_path": library_path,
"validation_summary": {
"total_symbols": len(library.symbols),
"symbols_with_issues": len(validation_results),
"total_issues": total_issues,
"pass_rate": ((len(library.symbols) - len(validation_results)) / len(library.symbols) * 100) if library.symbols else 100
},
"issues_by_symbol": validation_results,
"recommendations": [
"Fix symbols with missing essential properties first",
"Ensure all pins have valid electrical types",
"Check for duplicate pin numbers",
"Add meaningful pin names for better usability"
] if validation_results else ["All symbols pass validation checks"]
}
except Exception as e:
return {
"success": False,
"error": str(e),
"library_path": library_path
}
@mcp.tool()
def find_similar_symbols(library_path: str, symbol_name: str,
similarity_threshold: float = 0.7) -> dict[str, Any]:
"""
Find symbols similar to a specified symbol in the library.
Uses pin count, keywords, and name similarity to identify potentially
related or duplicate symbols in the library.
Args:
library_path: Path to the .kicad_sym library file
symbol_name: Name of the symbol to find similarities for
similarity_threshold: Minimum similarity score (0.0 to 1.0)
Returns:
Dictionary containing similar symbols with similarity scores
"""
try:
if not os.path.exists(library_path):
return {
"success": False,
"error": f"Library file not found: {library_path}"
}
analyzer = create_symbol_analyzer()
library = analyzer.load_library(library_path)
# Find target symbol
target_symbol = None
for symbol in library.symbols:
if symbol.name == symbol_name:
target_symbol = symbol
break
if not target_symbol:
return {
"success": False,
"error": f"Symbol '{symbol_name}' not found in library"
}
# Find similar symbols
similar_symbols = analyzer.find_similar_symbols(
target_symbol, library, similarity_threshold
)
similar_list = []
for symbol, score in similar_symbols:
similar_list.append({
"symbol_name": symbol.name,
"similarity_score": round(score, 3),
"pin_count": len(symbol.pins),
"keywords": symbol.keywords,
"description": symbol.description,
"differences": {
"pin_count_diff": abs(len(symbol.pins) - len(target_symbol.pins)),
"unique_keywords": list(set(symbol.keywords) - set(target_symbol.keywords)),
"missing_keywords": list(set(target_symbol.keywords) - set(symbol.keywords))
}
})
return {
"success": True,
"library_path": library_path,
"target_symbol": {
"name": target_symbol.name,
"pin_count": len(target_symbol.pins),
"keywords": target_symbol.keywords,
"description": target_symbol.description
},
"similar_symbols": similar_list,
"similarity_threshold": similarity_threshold,
"matches_found": len(similar_list)
}
except Exception as e:
return {
"success": False,
"error": str(e),
"library_path": library_path
}
@mcp.tool()
def get_symbol_details(library_path: str, symbol_name: str) -> dict[str, Any]:
"""
Get detailed information about a specific symbol in a library.
Provides comprehensive symbol information including pins, properties,
graphics, and metadata for detailed analysis.
Args:
library_path: Path to the .kicad_sym library file
symbol_name: Name of the symbol to analyze
Returns:
Dictionary containing detailed symbol information
"""
try:
if not os.path.exists(library_path):
return {
"success": False,
"error": f"Library file not found: {library_path}"
}
analyzer = create_symbol_analyzer()
library = analyzer.load_library(library_path)
# Find target symbol
target_symbol = None
for symbol in library.symbols:
if symbol.name == symbol_name:
target_symbol = symbol
break
if not target_symbol:
return {
"success": False,
"error": f"Symbol '{symbol_name}' not found in library"
}
# Extract detailed information
pin_details = []
for pin in target_symbol.pins:
pin_details.append({
"number": pin.number,
"name": pin.name,
"position": pin.position,
"orientation": pin.orientation,
"electrical_type": pin.electrical_type,
"graphic_style": pin.graphic_style,
"length_mm": pin.length
})
property_details = []
for prop in target_symbol.properties:
property_details.append({
"name": prop.name,
"value": prop.value,
"position": prop.position,
"rotation": prop.rotation,
"visible": prop.visible
})
# Validate symbol
validation_issues = analyzer.validate_symbol(target_symbol)
return {
"success": True,
"library_path": library_path,
"symbol_details": {
"name": target_symbol.name,
"library_id": target_symbol.library_id,
"description": target_symbol.description,
"keywords": target_symbol.keywords,
"power_symbol": target_symbol.power_symbol,
"extends": target_symbol.extends,
"pin_count": len(target_symbol.pins),
"pins": pin_details,
"properties": property_details,
"footprint_filters": target_symbol.footprint_filters,
"graphics_summary": {
"rectangles": len(target_symbol.graphics.rectangles),
"circles": len(target_symbol.graphics.circles),
"polylines": len(target_symbol.graphics.polylines)
}
},
"validation": {
"valid": len(validation_issues) == 0,
"issues": validation_issues
},
"statistics": {
"electrical_types": {etype: len([p for p in target_symbol.pins if p.electrical_type == etype])
for etype in set(p.electrical_type for p in target_symbol.pins)},
"pin_orientations": {orient: len([p for p in target_symbol.pins if p.orientation == orient])
for orient in set(p.orientation for p in target_symbol.pins)}
}
}
except Exception as e:
return {
"success": False,
"error": str(e),
"library_path": library_path
}
@mcp.tool()
def organize_library_by_category(library_path: str) -> dict[str, Any]:
"""
Organize symbols in a library by categories based on keywords and function.
Analyzes symbol keywords, names, and properties to suggest logical
groupings and organization improvements for the library.
Args:
library_path: Path to the .kicad_sym library file
Returns:
Dictionary containing suggested organization and category analysis
"""
try:
if not os.path.exists(library_path):
return {
"success": False,
"error": f"Library file not found: {library_path}"
}
analyzer = create_symbol_analyzer()
library = analyzer.load_library(library_path)
# Analyze library for categorization
analysis = analyzer.analyze_library_coverage(library)
# Create category-based organization
categories = {}
uncategorized = []
for symbol in library.symbols:
symbol_categories = []
# Categorize by keywords
if symbol.keywords:
symbol_categories.extend(symbol.keywords)
# Categorize by name patterns
name_lower = symbol.name.lower()
if any(term in name_lower for term in ['resistor', 'res', 'r_']):
symbol_categories.append('resistors')
elif any(term in name_lower for term in ['capacitor', 'cap', 'c_']):
symbol_categories.append('capacitors')
elif any(term in name_lower for term in ['inductor', 'ind', 'l_']):
symbol_categories.append('inductors')
elif any(term in name_lower for term in ['diode', 'led']):
symbol_categories.append('diodes')
elif any(term in name_lower for term in ['transistor', 'mosfet', 'bjt']):
symbol_categories.append('transistors')
elif any(term in name_lower for term in ['connector', 'conn']):
symbol_categories.append('connectors')
elif any(term in name_lower for term in ['ic', 'chip', 'processor']):
symbol_categories.append('integrated_circuits')
elif symbol.power_symbol:
symbol_categories.append('power')
# Categorize by pin count
pin_count = len(symbol.pins)
if pin_count <= 2:
symbol_categories.append('two_terminal')
elif pin_count <= 4:
symbol_categories.append('low_pin_count')
elif pin_count <= 20:
symbol_categories.append('medium_pin_count')
else:
symbol_categories.append('high_pin_count')
if symbol_categories:
for category in symbol_categories:
if category not in categories:
categories[category] = []
categories[category].append({
"name": symbol.name,
"description": symbol.description,
"pin_count": pin_count
})
else:
uncategorized.append(symbol.name)
# Generate organization recommendations
recommendations = []
if uncategorized:
recommendations.append(f"Add keywords to {len(uncategorized)} uncategorized symbols")
large_categories = {k: v for k, v in categories.items() if len(v) > 50}
if large_categories:
recommendations.append(f"Consider splitting large categories: {list(large_categories.keys())}")
if len(categories) < 5:
recommendations.append("Library could benefit from more detailed categorization")
return {
"success": True,
"library_path": library_path,
"organization": {
"categories": {k: len(v) for k, v in categories.items()},
"detailed_categories": categories,
"uncategorized_symbols": uncategorized,
"total_categories": len(categories),
"largest_category": max(categories.items(), key=lambda x: len(x[1]))[0] if categories else None
},
"statistics": {
"categorization_rate": ((len(library.symbols) - len(uncategorized)) / len(library.symbols) * 100) if library.symbols else 100,
"average_symbols_per_category": sum(len(v) for v in categories.values()) / len(categories) if categories else 0
},
"recommendations": recommendations
}
except Exception as e:
return {
"success": False,
"error": str(e),
"library_path": library_path
}
@mcp.tool()
def compare_symbol_libraries(library1_path: str, library2_path: str) -> dict[str, Any]:
"""
Compare two KiCad symbol libraries and identify differences.
Analyzes differences in symbol content, organization, and coverage
between two libraries for migration or consolidation planning.
Args:
library1_path: Path to the first .kicad_sym library file
library2_path: Path to the second .kicad_sym library file
Returns:
Dictionary containing detailed comparison results
"""
try:
# Validate both library files
for path in [library1_path, library2_path]:
if not os.path.exists(path):
return {
"success": False,
"error": f"Library file not found: {path}"
}
analyzer = create_symbol_analyzer()
# Load both libraries
library1 = analyzer.load_library(library1_path)
library2 = analyzer.load_library(library2_path)
# Get symbol lists
symbols1 = {s.name: s for s in library1.symbols}
symbols2 = {s.name: s for s in library2.symbols}
# Find differences
common_symbols = set(symbols1.keys()).intersection(set(symbols2.keys()))
unique_to_lib1 = set(symbols1.keys()) - set(symbols2.keys())
unique_to_lib2 = set(symbols2.keys()) - set(symbols1.keys())
# Analyze common symbols for differences
symbol_differences = []
for symbol_name in common_symbols:
sym1 = symbols1[symbol_name]
sym2 = symbols2[symbol_name]
differences = []
if len(sym1.pins) != len(sym2.pins):
differences.append(f"Pin count: {len(sym1.pins)} vs {len(sym2.pins)}")
if sym1.description != sym2.description:
differences.append("Description differs")
if set(sym1.keywords) != set(sym2.keywords):
differences.append("Keywords differ")
if differences:
symbol_differences.append({
"symbol": symbol_name,
"differences": differences
})
# Analyze library statistics
analysis1 = analyzer.analyze_library_coverage(library1)
analysis2 = analyzer.analyze_library_coverage(library2)
return {
"success": True,
"comparison": {
"library1": {
"name": library1.name,
"path": library1_path,
"symbol_count": len(library1.symbols),
"unique_symbols": len(unique_to_lib1)
},
"library2": {
"name": library2.name,
"path": library2_path,
"symbol_count": len(library2.symbols),
"unique_symbols": len(unique_to_lib2)
},
"common_symbols": len(common_symbols),
"symbol_differences": len(symbol_differences),
"coverage_comparison": {
"categories_lib1": len(analysis1["categories"]),
"categories_lib2": len(analysis2["categories"]),
"common_categories": len(set(analysis1["categories"].keys()).intersection(set(analysis2["categories"].keys())))
}
},
"detailed_differences": {
"unique_to_library1": list(unique_to_lib1),
"unique_to_library2": list(unique_to_lib2),
"symbol_differences": symbol_differences
},
"recommendations": [
f"Consider merging libraries - {len(common_symbols)} symbols are common",
f"Review {len(symbol_differences)} symbols that differ between libraries",
"Standardize symbol naming and categorization across libraries"
] if common_symbols else [
"Libraries have no common symbols - they appear to serve different purposes"
]
}
except Exception as e:
return {
"success": False,
"error": str(e),
"library1_path": library1_path,
"library2_path": library2_path
}

View File

@ -1,298 +0,0 @@
"""
Validation tools for KiCad projects.
Provides tools for validating circuit positioning, generating reports,
and checking component boundaries in existing projects.
"""
import json
import os
from typing import Any
from fastmcp import Context, FastMCP
from mckicad.utils.boundary_validator import BoundaryValidator
from mckicad.utils.file_utils import get_project_files
async def validate_project_boundaries(project_path: str = None) -> dict[str, Any]:
"""
Validate component boundaries for an entire KiCad project.
Args:
project_path: Path to the KiCad project file (.kicad_pro)
ctx: Context for MCP communication
Returns:
Dictionary with validation results and report
"""
try:
if ctx:
await ctx.info("Starting boundary validation for project")
await ctx.report_progress(10, 100)
# Get project files
files = get_project_files(project_path)
if "schematic" not in files:
return {"success": False, "error": "No schematic file found in project"}
schematic_file = files["schematic"]
if ctx:
await ctx.report_progress(30, 100)
await ctx.info(f"Reading schematic file: {schematic_file}")
# Read schematic file
with open(schematic_file) as f:
content = f.read().strip()
# Parse components based on format
components = []
if content.startswith("(kicad_sch"):
# S-expression format - extract components
components = _extract_components_from_sexpr(content)
else:
# JSON format
try:
schematic_data = json.loads(content)
components = _extract_components_from_json(schematic_data)
except json.JSONDecodeError:
return {
"success": False,
"error": "Schematic file is neither valid S-expression nor JSON format",
}
if ctx:
await ctx.report_progress(60, 100)
await ctx.info(f"Found {len(components)} components to validate")
# Run boundary validation
validator = BoundaryValidator()
validation_report = validator.validate_circuit_components(components)
if ctx:
await ctx.report_progress(80, 100)
await ctx.info(
f"Validation complete: {validation_report.out_of_bounds_count} out of bounds"
)
# Generate text report
report_text = validator.generate_validation_report_text(validation_report)
if ctx:
await ctx.info(f"Validation Report:\n{report_text}")
await ctx.report_progress(100, 100)
# Create result
result = {
"success": validation_report.success,
"total_components": validation_report.total_components,
"out_of_bounds_count": validation_report.out_of_bounds_count,
"corrected_positions": validation_report.corrected_positions,
"report_text": report_text,
"has_errors": validation_report.has_errors(),
"has_warnings": validation_report.has_warnings(),
"issues": [
{
"severity": issue.severity.value,
"component_ref": issue.component_ref,
"message": issue.message,
"position": issue.position,
"suggested_position": issue.suggested_position,
}
for issue in validation_report.issues
],
}
return result
except Exception as e:
error_msg = f"Error validating project boundaries: {str(e)}"
if ctx:
await ctx.info(error_msg)
return {"success": False, "error": error_msg}
async def generate_validation_report(
project_path: str, output_path: str = None
) -> dict[str, Any]:
"""
Generate a comprehensive validation report for a KiCad project.
Args:
project_path: Path to the KiCad project file (.kicad_pro)
output_path: Optional path to save the report (defaults to project directory)
ctx: Context for MCP communication
Returns:
Dictionary with report generation results
"""
try:
if ctx:
await ctx.info("Generating validation report")
await ctx.report_progress(10, 100)
# Run validation
validation_result = await validate_project_boundaries(project_path, ctx)
if not validation_result["success"]:
return validation_result
# Determine output path
if output_path is None:
project_dir = os.path.dirname(project_path)
project_name = os.path.splitext(os.path.basename(project_path))[0]
output_path = os.path.join(project_dir, f"{project_name}_validation_report.json")
if ctx:
await ctx.report_progress(80, 100)
await ctx.info(f"Saving report to: {output_path}")
# Save detailed report
report_data = {
"project_path": project_path,
"validation_timestamp": __import__("datetime").datetime.now().isoformat(),
"summary": {
"total_components": validation_result["total_components"],
"out_of_bounds_count": validation_result["out_of_bounds_count"],
"has_errors": validation_result["has_errors"],
"has_warnings": validation_result["has_warnings"],
},
"corrected_positions": validation_result["corrected_positions"],
"issues": validation_result["issues"],
"report_text": validation_result["report_text"],
}
with open(output_path, "w") as f:
json.dump(report_data, f, indent=2)
if ctx:
await ctx.report_progress(100, 100)
await ctx.info("Validation report generated successfully")
return {"success": True, "report_path": output_path, "summary": report_data["summary"]}
except Exception as e:
error_msg = f"Error generating validation report: {str(e)}"
if ctx:
await ctx.info(error_msg)
return {"success": False, "error": error_msg}
def _extract_components_from_sexpr(content: str) -> list[dict[str, Any]]:
"""Extract component information from S-expression format."""
import re
components = []
# Find all symbol instances
symbol_pattern = r'\(symbol\s+\(lib_id\s+"([^"]+)"\)\s+\(at\s+([\d.-]+)\s+([\d.-]+)\s+[\d.-]+\)\s+\(uuid\s+[^)]+\)(.*?)\n\s*\)'
for match in re.finditer(symbol_pattern, content, re.DOTALL):
lib_id = match.group(1)
x_pos = float(match.group(2))
y_pos = float(match.group(3))
properties_text = match.group(4)
# Extract reference from properties
ref_match = re.search(r'\(property\s+"Reference"\s+"([^"]+)"', properties_text)
reference = ref_match.group(1) if ref_match else "Unknown"
# Determine component type from lib_id
component_type = _get_component_type_from_lib_id(lib_id)
components.append(
{
"reference": reference,
"position": (x_pos, y_pos),
"component_type": component_type,
"lib_id": lib_id,
}
)
return components
def _extract_components_from_json(schematic_data: dict[str, Any]) -> list[dict[str, Any]]:
"""Extract component information from JSON format."""
components = []
if "symbol" in schematic_data:
for symbol in schematic_data["symbol"]:
# Extract reference
reference = "Unknown"
if "property" in symbol:
for prop in symbol["property"]:
if prop.get("name") == "Reference":
reference = prop.get("value", "Unknown")
break
# Extract position
position = (0, 0)
if "at" in symbol and len(symbol["at"]) >= 2:
# Convert from internal units to mm
x_pos = float(symbol["at"][0]) / 10.0
y_pos = float(symbol["at"][1]) / 10.0
position = (x_pos, y_pos)
# Determine component type
lib_id = symbol.get("lib_id", "")
component_type = _get_component_type_from_lib_id(lib_id)
components.append(
{
"reference": reference,
"position": position,
"component_type": component_type,
"lib_id": lib_id,
}
)
return components
def _get_component_type_from_lib_id(lib_id: str) -> str:
"""Determine component type from library ID."""
lib_id_lower = lib_id.lower()
if "resistor" in lib_id_lower or ":r" in lib_id_lower:
return "resistor"
elif "capacitor" in lib_id_lower or ":c" in lib_id_lower:
return "capacitor"
elif "inductor" in lib_id_lower or ":l" in lib_id_lower:
return "inductor"
elif "led" in lib_id_lower:
return "led"
elif "diode" in lib_id_lower or ":d" in lib_id_lower:
return "diode"
elif "transistor" in lib_id_lower or "npn" in lib_id_lower or "pnp" in lib_id_lower:
return "transistor"
elif "power:" in lib_id_lower:
return "power"
elif "switch" in lib_id_lower:
return "switch"
elif "connector" in lib_id_lower:
return "connector"
elif "mcu" in lib_id_lower or "ic" in lib_id_lower or ":u" in lib_id_lower:
return "ic"
else:
return "default"
def register_validation_tools(mcp: FastMCP) -> None:
"""Register validation tools with the MCP server."""
@mcp.tool(name="validate_project_boundaries")
async def validate_project_boundaries_tool(
project_path: str = None
) -> dict[str, Any]:
"""Validate component boundaries for an entire KiCad project."""
return await validate_project_boundaries(project_path, ctx)
@mcp.tool(name="generate_validation_report")
async def generate_validation_report_tool(
project_path: str, output_path: str = None
) -> dict[str, Any]:
"""Generate a comprehensive validation report for a KiCad project."""
return await generate_validation_report(project_path, output_path, ctx)

View File

@ -1,3 +0,0 @@
"""
Utility functions for KiCad MCP Server.
"""

View File

@ -1,444 +0,0 @@
"""
Advanced DRC (Design Rule Check) utilities for KiCad.
Provides sophisticated DRC rule creation, customization, and validation
beyond the basic KiCad DRC capabilities.
"""
from dataclasses import dataclass, field
from enum import Enum
import logging
from typing import Any
logger = logging.getLogger(__name__)
class RuleSeverity(Enum):
"""DRC rule severity levels."""
ERROR = "error"
WARNING = "warning"
INFO = "info"
IGNORE = "ignore"
class RuleType(Enum):
"""Types of DRC rules."""
CLEARANCE = "clearance"
TRACK_WIDTH = "track_width"
VIA_SIZE = "via_size"
ANNULAR_RING = "annular_ring"
DRILL_SIZE = "drill_size"
COURTYARD_CLEARANCE = "courtyard_clearance"
SILK_CLEARANCE = "silk_clearance"
FABRICATION = "fabrication"
ASSEMBLY = "assembly"
ELECTRICAL = "electrical"
MECHANICAL = "mechanical"
@dataclass
class DRCRule:
"""Represents a single DRC rule."""
name: str
rule_type: RuleType
severity: RuleSeverity
constraint: dict[str, Any]
condition: str | None = None # Expression for when rule applies
description: str | None = None
enabled: bool = True
custom_message: str | None = None
@dataclass
class DRCRuleSet:
"""Collection of DRC rules with metadata."""
name: str
version: str
description: str
rules: list[DRCRule] = field(default_factory=list)
technology: str | None = None # e.g., "PCB", "Flex", "HDI"
layer_count: int | None = None
board_thickness: float | None = None
created_by: str | None = None
class AdvancedDRCManager:
"""Manager for advanced DRC rules and validation."""
def __init__(self):
"""Initialize the DRC manager."""
self.rule_sets = {}
self.active_rule_set = None
self._load_default_rules()
def _load_default_rules(self) -> None:
"""Load default DRC rule sets."""
# Standard PCB rules
standard_rules = DRCRuleSet(
name="Standard PCB",
version="1.0",
description="Standard PCB manufacturing rules",
technology="PCB"
)
# Basic clearance rules
standard_rules.rules.extend([
DRCRule(
name="Min Track Width",
rule_type=RuleType.TRACK_WIDTH,
severity=RuleSeverity.ERROR,
constraint={"min_width": 0.1}, # 0.1mm minimum
description="Minimum track width for manufacturability"
),
DRCRule(
name="Standard Clearance",
rule_type=RuleType.CLEARANCE,
severity=RuleSeverity.ERROR,
constraint={"min_clearance": 0.2}, # 0.2mm minimum
description="Standard clearance between conductors"
),
DRCRule(
name="Via Drill Size",
rule_type=RuleType.VIA_SIZE,
severity=RuleSeverity.ERROR,
constraint={"min_drill": 0.2, "max_drill": 6.0},
description="Via drill size constraints"
),
DRCRule(
name="Via Annular Ring",
rule_type=RuleType.ANNULAR_RING,
severity=RuleSeverity.WARNING,
constraint={"min_annular_ring": 0.05}, # 0.05mm minimum
description="Minimum annular ring for vias"
)
])
self.rule_sets["standard"] = standard_rules
self.active_rule_set = "standard"
def create_high_density_rules(self) -> DRCRuleSet:
"""Create rules for high-density interconnect (HDI) boards."""
hdi_rules = DRCRuleSet(
name="HDI PCB",
version="1.0",
description="High-density interconnect PCB rules",
technology="HDI"
)
hdi_rules.rules.extend([
DRCRule(
name="HDI Track Width",
rule_type=RuleType.TRACK_WIDTH,
severity=RuleSeverity.ERROR,
constraint={"min_width": 0.075}, # 75μm minimum
description="Minimum track width for HDI manufacturing"
),
DRCRule(
name="HDI Clearance",
rule_type=RuleType.CLEARANCE,
severity=RuleSeverity.ERROR,
constraint={"min_clearance": 0.075}, # 75μm minimum
description="Minimum clearance for HDI boards"
),
DRCRule(
name="Microvia Size",
rule_type=RuleType.VIA_SIZE,
severity=RuleSeverity.ERROR,
constraint={"min_drill": 0.1, "max_drill": 0.15},
description="Microvia drill size constraints"
),
DRCRule(
name="BGA Escape Routing",
rule_type=RuleType.CLEARANCE,
severity=RuleSeverity.WARNING,
constraint={"min_clearance": 0.1},
condition="A.intersects(B.Type == 'BGA')",
description="Clearance around BGA escape routes"
)
])
return hdi_rules
def create_rf_rules(self) -> DRCRuleSet:
"""Create rules specifically for RF/microwave designs."""
rf_rules = DRCRuleSet(
name="RF/Microwave",
version="1.0",
description="Rules for RF and microwave PCB designs",
technology="RF"
)
rf_rules.rules.extend([
DRCRule(
name="Controlled Impedance Spacing",
rule_type=RuleType.CLEARANCE,
severity=RuleSeverity.ERROR,
constraint={"min_clearance": 0.2},
condition="A.NetClass == 'RF' or B.NetClass == 'RF'",
description="Spacing for controlled impedance traces"
),
DRCRule(
name="RF Via Stitching",
rule_type=RuleType.VIA_SIZE,
severity=RuleSeverity.WARNING,
constraint={"max_spacing": 2.0}, # Via stitching spacing
condition="Layer == 'Ground'",
description="Ground via stitching for RF designs"
),
DRCRule(
name="Microstrip Width Control",
rule_type=RuleType.TRACK_WIDTH,
severity=RuleSeverity.ERROR,
constraint={"target_width": 0.5, "tolerance": 0.05},
condition="NetClass == '50ohm'",
description="Precise width control for 50Ω traces"
)
])
return rf_rules
def create_automotive_rules(self) -> DRCRuleSet:
"""Create automotive-grade reliability rules."""
automotive_rules = DRCRuleSet(
name="Automotive",
version="1.0",
description="Automotive reliability and safety rules",
technology="Automotive"
)
automotive_rules.rules.extend([
DRCRule(
name="Safety Critical Clearance",
rule_type=RuleType.CLEARANCE,
severity=RuleSeverity.ERROR,
constraint={"min_clearance": 0.5},
condition="A.NetClass == 'Safety' or B.NetClass == 'Safety'",
description="Enhanced clearance for safety-critical circuits"
),
DRCRule(
name="Power Track Width",
rule_type=RuleType.TRACK_WIDTH,
severity=RuleSeverity.ERROR,
constraint={"min_width": 0.5},
condition="NetClass == 'Power'",
description="Minimum width for power distribution"
),
DRCRule(
name="Thermal Via Density",
rule_type=RuleType.VIA_SIZE,
severity=RuleSeverity.WARNING,
constraint={"min_density": 4}, # 4 vias per cm² for thermal
condition="Pad.ThermalPad == True",
description="Thermal via density for heat dissipation"
),
DRCRule(
name="Vibration Resistant Vias",
rule_type=RuleType.ANNULAR_RING,
severity=RuleSeverity.ERROR,
constraint={"min_annular_ring": 0.1},
description="Enhanced annular ring for vibration resistance"
)
])
return automotive_rules
def create_custom_rule(self, name: str, rule_type: RuleType,
constraint: dict[str, Any], severity: RuleSeverity = RuleSeverity.ERROR,
condition: str = None, description: str = None) -> DRCRule:
"""Create a custom DRC rule."""
return DRCRule(
name=name,
rule_type=rule_type,
severity=severity,
constraint=constraint,
condition=condition,
description=description
)
def validate_rule_syntax(self, rule: DRCRule) -> list[str]:
"""Validate rule syntax and return any errors."""
errors = []
# Validate constraint format
if rule.rule_type == RuleType.CLEARANCE:
if "min_clearance" not in rule.constraint:
errors.append("Clearance rule must specify min_clearance")
elif rule.constraint["min_clearance"] <= 0:
errors.append("Clearance must be positive")
elif rule.rule_type == RuleType.TRACK_WIDTH:
if "min_width" not in rule.constraint and "max_width" not in rule.constraint:
errors.append("Track width rule must specify min_width or max_width")
elif rule.rule_type == RuleType.VIA_SIZE:
if "min_drill" not in rule.constraint and "max_drill" not in rule.constraint:
errors.append("Via size rule must specify drill constraints")
# Validate condition syntax (basic check)
if rule.condition:
try:
# Basic syntax validation - could be more sophisticated
if not any(op in rule.condition for op in ["==", "!=", ">", "<", "intersects"]):
errors.append("Condition must contain a comparison operator")
except Exception as e:
errors.append(f"Invalid condition syntax: {e}")
return errors
def export_kicad_drc_rules(self, rule_set_name: str) -> str:
"""Export rule set as KiCad-compatible DRC rules."""
if rule_set_name not in self.rule_sets:
raise ValueError(f"Rule set '{rule_set_name}' not found")
rule_set = self.rule_sets[rule_set_name]
kicad_rules = []
kicad_rules.append(f"# DRC Rules: {rule_set.name}")
kicad_rules.append(f"# Description: {rule_set.description}")
kicad_rules.append(f"# Version: {rule_set.version}")
kicad_rules.append("")
for rule in rule_set.rules:
if not rule.enabled:
continue
kicad_rule = self._convert_to_kicad_rule(rule)
if kicad_rule:
kicad_rules.append(kicad_rule)
kicad_rules.append("")
return "\n".join(kicad_rules)
def _convert_to_kicad_rule(self, rule: DRCRule) -> str | None:
"""Convert DRC rule to KiCad rule format."""
try:
rule_lines = [f"# {rule.name}"]
if rule.description:
rule_lines.append(f"# {rule.description}")
if rule.rule_type == RuleType.CLEARANCE:
clearance = rule.constraint.get("min_clearance", 0.2)
rule_lines.append(f"(rule \"{rule.name}\"")
rule_lines.append(f" (constraint clearance (min {clearance}mm))")
if rule.condition:
rule_lines.append(f" (condition \"{rule.condition}\")")
rule_lines.append(")")
elif rule.rule_type == RuleType.TRACK_WIDTH:
if "min_width" in rule.constraint:
min_width = rule.constraint["min_width"]
rule_lines.append(f"(rule \"{rule.name}\"")
rule_lines.append(f" (constraint track_width (min {min_width}mm))")
if rule.condition:
rule_lines.append(f" (condition \"{rule.condition}\")")
rule_lines.append(")")
elif rule.rule_type == RuleType.VIA_SIZE:
rule_lines.append(f"(rule \"{rule.name}\"")
if "min_drill" in rule.constraint:
rule_lines.append(f" (constraint hole_size (min {rule.constraint['min_drill']}mm))")
if "max_drill" in rule.constraint:
rule_lines.append(f" (constraint hole_size (max {rule.constraint['max_drill']}mm))")
if rule.condition:
rule_lines.append(f" (condition \"{rule.condition}\")")
rule_lines.append(")")
return "\n".join(rule_lines)
except Exception as e:
logger.error(f"Failed to convert rule {rule.name}: {e}")
return None
def analyze_pcb_for_rule_violations(self, pcb_file_path: str,
rule_set_name: str = None) -> dict[str, Any]:
"""Analyze PCB file against rule set and report violations."""
if rule_set_name is None:
rule_set_name = self.active_rule_set
if rule_set_name not in self.rule_sets:
raise ValueError(f"Rule set '{rule_set_name}' not found")
rule_set = self.rule_sets[rule_set_name]
violations = []
# This would integrate with actual PCB analysis
# For now, return structure for potential violations
return {
"pcb_file": pcb_file_path,
"rule_set": rule_set_name,
"rule_count": len(rule_set.rules),
"violations": violations,
"summary": {
"errors": len([v for v in violations if v.get("severity") == "error"]),
"warnings": len([v for v in violations if v.get("severity") == "warning"]),
"total": len(violations)
}
}
def generate_manufacturing_constraints(self, technology: str = "standard") -> dict[str, Any]:
"""Generate manufacturing constraints for specific technology."""
constraints = {
"standard": {
"min_track_width": 0.1, # mm
"min_clearance": 0.2, # mm
"min_via_drill": 0.2, # mm
"min_annular_ring": 0.05, # mm
"aspect_ratio_limit": 8, # drill depth : diameter
"layer_count_limit": 16,
"board_thickness_range": [0.4, 6.0]
},
"hdi": {
"min_track_width": 0.075, # mm
"min_clearance": 0.075, # mm
"min_via_drill": 0.1, # mm
"min_annular_ring": 0.025, # mm
"aspect_ratio_limit": 1, # For microvias
"layer_count_limit": 20,
"board_thickness_range": [0.8, 3.2]
},
"rf": {
"min_track_width": 0.1, # mm
"min_clearance": 0.2, # mm
"impedance_tolerance": 5, # %
"via_stitching_max": 2.0, # mm spacing
"ground_plane_required": True,
"layer_symmetry_required": True
},
"automotive": {
"min_track_width": 0.15, # mm (more conservative)
"min_clearance": 0.3, # mm (enhanced reliability)
"min_via_drill": 0.25, # mm
"min_annular_ring": 0.075, # mm
"temperature_range": [-40, 125], # °C
"vibration_resistant": True
}
}
return constraints.get(technology, constraints["standard"])
def add_rule_set(self, rule_set: DRCRuleSet) -> None:
"""Add a rule set to the manager."""
self.rule_sets[rule_set.name.lower().replace(" ", "_")] = rule_set
def get_rule_set_names(self) -> list[str]:
"""Get list of available rule set names."""
return list(self.rule_sets.keys())
def set_active_rule_set(self, name: str) -> None:
"""Set the active rule set."""
if name not in self.rule_sets:
raise ValueError(f"Rule set '{name}' not found")
self.active_rule_set = name
def create_drc_manager() -> AdvancedDRCManager:
"""Create and initialize a DRC manager with default rule sets."""
manager = AdvancedDRCManager()
# Add specialized rule sets
manager.add_rule_set(manager.create_high_density_rules())
manager.add_rule_set(manager.create_rf_rules())
manager.add_rule_set(manager.create_automotive_rules())
return manager

View File

@ -1,365 +0,0 @@
"""
Boundary validation system for KiCad circuit generation.
Provides comprehensive validation for component positioning, boundary checking,
and validation report generation to prevent out-of-bounds placement issues.
"""
from dataclasses import dataclass
from enum import Enum
import json
from typing import Any
from mckicad.utils.component_layout import ComponentLayoutManager, SchematicBounds
from mckicad.utils.coordinate_converter import CoordinateConverter, validate_position
class ValidationSeverity(Enum):
"""Severity levels for validation issues."""
ERROR = "error"
WARNING = "warning"
INFO = "info"
@dataclass
class ValidationIssue:
"""Represents a validation issue found during boundary checking."""
severity: ValidationSeverity
component_ref: str
message: str
position: tuple[float, float]
suggested_position: tuple[float, float] | None = None
component_type: str = "default"
@dataclass
class ValidationReport:
"""Comprehensive validation report for circuit positioning."""
success: bool
issues: list[ValidationIssue]
total_components: int
validated_components: int
out_of_bounds_count: int
corrected_positions: dict[str, tuple[float, float]]
def has_errors(self) -> bool:
"""Check if report contains any error-level issues."""
return any(issue.severity == ValidationSeverity.ERROR for issue in self.issues)
def has_warnings(self) -> bool:
"""Check if report contains any warning-level issues."""
return any(issue.severity == ValidationSeverity.WARNING for issue in self.issues)
def get_issues_by_severity(self, severity: ValidationSeverity) -> list[ValidationIssue]:
"""Get all issues of a specific severity level."""
return [issue for issue in self.issues if issue.severity == severity]
class BoundaryValidator:
"""
Comprehensive boundary validation system for KiCad circuit generation.
Features:
- Pre-generation coordinate validation
- Automatic position correction
- Detailed validation reports
- Integration with circuit generation pipeline
"""
def __init__(self, bounds: SchematicBounds | None = None):
"""
Initialize the boundary validator.
Args:
bounds: Schematic boundaries (defaults to A4)
"""
self.bounds = bounds or SchematicBounds()
self.converter = CoordinateConverter()
self.layout_manager = ComponentLayoutManager(self.bounds)
def validate_component_position(
self, component_ref: str, x: float, y: float, component_type: str = "default"
) -> ValidationIssue:
"""
Validate a single component position.
Args:
component_ref: Component reference (e.g., "R1")
x: X coordinate in mm
y: Y coordinate in mm
component_type: Type of component
Returns:
ValidationIssue describing the validation result
"""
# Check if position is within A4 bounds
if not validate_position(x, y, use_margins=True):
# Find a corrected position
corrected_x, corrected_y = self.layout_manager.find_valid_position(
component_ref, component_type, x, y
)
return ValidationIssue(
severity=ValidationSeverity.ERROR,
component_ref=component_ref,
message=f"Component {component_ref} at ({x:.2f}, {y:.2f}) is outside A4 bounds",
position=(x, y),
suggested_position=(corrected_x, corrected_y),
component_type=component_type,
)
# Check if position is within usable area (with margins)
if not validate_position(x, y, use_margins=False):
# Position is within absolute bounds but outside usable area
return ValidationIssue(
severity=ValidationSeverity.WARNING,
component_ref=component_ref,
message=f"Component {component_ref} at ({x:.2f}, {y:.2f}) is outside usable area (margins)",
position=(x, y),
component_type=component_type,
)
# Position is valid
return ValidationIssue(
severity=ValidationSeverity.INFO,
component_ref=component_ref,
message=f"Component {component_ref} position is valid",
position=(x, y),
component_type=component_type,
)
def validate_circuit_components(self, components: list[dict[str, Any]]) -> ValidationReport:
"""
Validate positioning for all components in a circuit.
Args:
components: List of component dictionaries with position information
Returns:
ValidationReport with comprehensive validation results
"""
issues = []
corrected_positions = {}
out_of_bounds_count = 0
# Reset layout manager for this validation
self.layout_manager.clear_layout()
for component in components:
component_ref = component.get("reference", "Unknown")
component_type = component.get("component_type", "default")
# Extract position - handle different formats
position = component.get("position")
if position is None:
# No position specified - this is an info issue
issues.append(
ValidationIssue(
severity=ValidationSeverity.INFO,
component_ref=component_ref,
message=f"Component {component_ref} has no position specified",
position=(0, 0),
component_type=component_type,
)
)
continue
# Handle position as tuple or list
if isinstance(position, list | tuple) and len(position) >= 2:
x, y = float(position[0]), float(position[1])
else:
issues.append(
ValidationIssue(
severity=ValidationSeverity.ERROR,
component_ref=component_ref,
message=f"Component {component_ref} has invalid position format: {position}",
position=(0, 0),
component_type=component_type,
)
)
continue
# Validate the position
validation_issue = self.validate_component_position(component_ref, x, y, component_type)
issues.append(validation_issue)
# Track out of bounds components
if validation_issue.severity == ValidationSeverity.ERROR:
out_of_bounds_count += 1
if validation_issue.suggested_position:
corrected_positions[component_ref] = validation_issue.suggested_position
# Generate report
report = ValidationReport(
success=out_of_bounds_count == 0,
issues=issues,
total_components=len(components),
validated_components=len([c for c in components if c.get("position") is not None]),
out_of_bounds_count=out_of_bounds_count,
corrected_positions=corrected_positions,
)
return report
def validate_wire_connection(
self, start_x: float, start_y: float, end_x: float, end_y: float
) -> list[ValidationIssue]:
"""
Validate wire connection endpoints.
Args:
start_x: Starting X coordinate in mm
start_y: Starting Y coordinate in mm
end_x: Ending X coordinate in mm
end_y: Ending Y coordinate in mm
Returns:
List of validation issues for wire endpoints
"""
issues = []
# Validate start point
if not validate_position(start_x, start_y, use_margins=True):
issues.append(
ValidationIssue(
severity=ValidationSeverity.ERROR,
component_ref="WIRE_START",
message=f"Wire start point ({start_x:.2f}, {start_y:.2f}) is outside bounds",
position=(start_x, start_y),
)
)
# Validate end point
if not validate_position(end_x, end_y, use_margins=True):
issues.append(
ValidationIssue(
severity=ValidationSeverity.ERROR,
component_ref="WIRE_END",
message=f"Wire end point ({end_x:.2f}, {end_y:.2f}) is outside bounds",
position=(end_x, end_y),
)
)
return issues
def auto_correct_positions(
self, components: list[dict[str, Any]]
) -> tuple[list[dict[str, Any]], ValidationReport]:
"""
Automatically correct out-of-bounds component positions.
Args:
components: List of component dictionaries
Returns:
Tuple of (corrected_components, validation_report)
"""
# First validate to get correction suggestions
validation_report = self.validate_circuit_components(components)
# Apply corrections
corrected_components = []
for component in components:
component_ref = component.get("reference", "Unknown")
if component_ref in validation_report.corrected_positions:
# Apply correction
corrected_component = component.copy()
corrected_component["position"] = validation_report.corrected_positions[
component_ref
]
corrected_components.append(corrected_component)
else:
corrected_components.append(component)
return corrected_components, validation_report
def generate_validation_report_text(self, report: ValidationReport) -> str:
"""
Generate a human-readable validation report.
Args:
report: ValidationReport to format
Returns:
Formatted text report
"""
lines = []
lines.append("=" * 60)
lines.append("BOUNDARY VALIDATION REPORT")
lines.append("=" * 60)
# Summary
lines.append(f"Status: {'PASS' if report.success else 'FAIL'}")
lines.append(f"Total Components: {report.total_components}")
lines.append(f"Validated Components: {report.validated_components}")
lines.append(f"Out of Bounds: {report.out_of_bounds_count}")
lines.append(f"Corrected Positions: {len(report.corrected_positions)}")
lines.append("")
# Issues by severity
errors = report.get_issues_by_severity(ValidationSeverity.ERROR)
warnings = report.get_issues_by_severity(ValidationSeverity.WARNING)
info = report.get_issues_by_severity(ValidationSeverity.INFO)
if errors:
lines.append("ERRORS:")
for issue in errors:
lines.append(f"{issue.message}")
if issue.suggested_position:
lines.append(f" → Suggested: {issue.suggested_position}")
lines.append("")
if warnings:
lines.append("WARNINGS:")
for issue in warnings:
lines.append(f" ⚠️ {issue.message}")
lines.append("")
if info:
lines.append("INFO:")
for issue in info:
lines.append(f" {issue.message}")
lines.append("")
# Corrected positions
if report.corrected_positions:
lines.append("CORRECTED POSITIONS:")
for component_ref, (x, y) in report.corrected_positions.items():
lines.append(f" {component_ref}: ({x:.2f}, {y:.2f})")
return "\n".join(lines)
def export_validation_report(self, report: ValidationReport, filepath: str) -> None:
"""
Export validation report to JSON file.
Args:
report: ValidationReport to export
filepath: Path to output file
"""
# Convert report to serializable format
export_data = {
"success": report.success,
"total_components": report.total_components,
"validated_components": report.validated_components,
"out_of_bounds_count": report.out_of_bounds_count,
"corrected_positions": report.corrected_positions,
"issues": [
{
"severity": issue.severity.value,
"component_ref": issue.component_ref,
"message": issue.message,
"position": issue.position,
"suggested_position": issue.suggested_position,
"component_type": issue.component_type,
}
for issue in report.issues
],
}
with open(filepath, "w") as f:
json.dump(export_data, f, indent=2)

View File

@ -1,35 +0,0 @@
"""
Component layout management for KiCad schematics.
Stub implementation to fix import issues.
"""
from dataclasses import dataclass
@dataclass
class SchematicBounds:
"""Represents the bounds of a schematic area."""
x_min: float
x_max: float
y_min: float
y_max: float
def contains_point(self, x: float, y: float) -> bool:
"""Check if a point is within the bounds."""
return self.x_min <= x <= self.x_max and self.y_min <= y <= self.y_max
class ComponentLayoutManager:
"""Manages component layout in schematic."""
def __init__(self):
self.bounds = SchematicBounds(-1000, 1000, -1000, 1000)
def get_bounds(self) -> SchematicBounds:
"""Get the schematic bounds."""
return self.bounds
def validate_placement(self, x: float, y: float) -> bool:
"""Validate if a component can be placed at the given coordinates."""
return self.bounds.contains_point(x, y)

View File

@ -1,582 +0,0 @@
"""
Utility functions for working with KiCad component values and properties.
"""
from enum import Enum
import re
from typing import Any
class ComponentType(Enum):
"""Enumeration of electronic component types."""
RESISTOR = "resistor"
CAPACITOR = "capacitor"
INDUCTOR = "inductor"
DIODE = "diode"
TRANSISTOR = "transistor"
IC = "integrated_circuit"
CONNECTOR = "connector"
CRYSTAL = "crystal"
VOLTAGE_REGULATOR = "voltage_regulator"
FUSE = "fuse"
SWITCH = "switch"
RELAY = "relay"
TRANSFORMER = "transformer"
LED = "led"
UNKNOWN = "unknown"
def extract_voltage_from_regulator(value: str) -> str:
"""Extract output voltage from a voltage regulator part number or description.
Args:
value: Regulator part number or description
Returns:
Extracted voltage as a string or "unknown" if not found
"""
# Common patterns:
# 78xx/79xx series: 7805 = 5V, 7812 = 12V
# LDOs often have voltage in the part number, like LM1117-3.3
# 78xx/79xx series
match = re.search(r"78(\d\d)|79(\d\d)", value, re.IGNORECASE)
if match:
group = match.group(1) or match.group(2)
# Convert code to voltage (e.g., 05 -> 5V, 12 -> 12V)
try:
voltage = int(group)
# For 78xx series, voltage code is directly in volts
if voltage < 50: # Sanity check to prevent weird values
return f"{voltage}V"
except ValueError:
pass
# Look for common voltage indicators in the string
voltage_patterns = [
r"(\d+\.?\d*)V", # 3.3V, 5V, etc.
r"-(\d+\.?\d*)V", # -5V, -12V, etc. (for negative regulators)
r"(\d+\.?\d*)[_-]?V", # 3.3_V, 5-V, etc.
r"[_-](\d+\.?\d*)", # LM1117-3.3, LD1117-3.3, etc.
]
for pattern in voltage_patterns:
match = re.search(pattern, value, re.IGNORECASE)
if match:
try:
voltage = float(match.group(1))
if 0 < voltage < 50: # Sanity check
# Format as integer if it's a whole number
if voltage.is_integer():
return f"{int(voltage)}V"
else:
return f"{voltage}V"
except ValueError:
pass
# Check for common fixed voltage regulators
regulators = {
"LM7805": "5V",
"LM7809": "9V",
"LM7812": "12V",
"LM7905": "-5V",
"LM7912": "-12V",
"LM1117-3.3": "3.3V",
"LM1117-5": "5V",
"LM317": "Adjustable",
"LM337": "Adjustable (Negative)",
"AP1117-3.3": "3.3V",
"AMS1117-3.3": "3.3V",
"L7805": "5V",
"L7812": "12V",
"MCP1700-3.3": "3.3V",
"MCP1700-5.0": "5V",
}
for reg, volt in regulators.items():
if re.search(re.escape(reg), value, re.IGNORECASE):
return volt
return "unknown"
def extract_frequency_from_value(value: str) -> str:
"""Extract frequency information from a component value or description.
Args:
value: Component value or description (e.g., "16MHz", "Crystal 8MHz")
Returns:
Frequency as a string or "unknown" if not found
"""
# Common frequency patterns with various units
frequency_patterns = [
r"(\d+\.?\d*)[\s-]*([kKmMgG]?)[hH][zZ]", # 16MHz, 32.768 kHz, etc.
r"(\d+\.?\d*)[\s-]*([kKmMgG])", # 16M, 32.768k, etc.
]
for pattern in frequency_patterns:
match = re.search(pattern, value, re.IGNORECASE)
if match:
try:
freq = float(match.group(1))
unit = match.group(2).upper() if match.group(2) else ""
# Make sure the frequency is in a reasonable range
if freq > 0:
# Format the output
if unit == "K":
if freq >= 1000:
return f"{freq / 1000:.3f}MHz"
else:
return f"{freq:.3f}kHz"
elif unit == "M":
if freq >= 1000:
return f"{freq / 1000:.3f}GHz"
else:
return f"{freq:.3f}MHz"
elif unit == "G":
return f"{freq:.3f}GHz"
else: # No unit, need to determine based on value
if freq < 1000:
return f"{freq:.3f}Hz"
elif freq < 1000000:
return f"{freq / 1000:.3f}kHz"
elif freq < 1000000000:
return f"{freq / 1000000:.3f}MHz"
else:
return f"{freq / 1000000000:.3f}GHz"
except ValueError:
pass
# Check for common crystal frequencies
if "32.768" in value or "32768" in value:
return "32.768kHz" # Common RTC crystal
elif "16M" in value or "16MHZ" in value.upper():
return "16MHz" # Common MCU crystal
elif "8M" in value or "8MHZ" in value.upper():
return "8MHz"
elif "20M" in value or "20MHZ" in value.upper():
return "20MHz"
elif "27M" in value or "27MHZ" in value.upper():
return "27MHz"
elif "25M" in value or "25MHZ" in value.upper():
return "25MHz"
return "unknown"
def extract_resistance_value(value: str) -> tuple[float | None, str | None]:
"""Extract resistance value and unit from component value.
Args:
value: Resistance value (e.g., "10k", "4.7k", "100")
Returns:
Tuple of (numeric value, unit) or (None, None) if parsing fails
"""
# Common resistance patterns
# 10k, 4.7k, 100R, 1M, 10, etc.
match = re.search(r"(\d+\.?\d*)([kKmMrRΩ]?)", value)
if match:
try:
resistance = float(match.group(1))
unit = match.group(2).upper() if match.group(2) else "Ω"
# Normalize unit
if unit == "R" or unit == "":
unit = "Ω"
return resistance, unit
except ValueError:
pass
# Handle special case like "4k7" (means 4.7k)
match = re.search(r"(\d+)[kKmM](\d+)", value)
if match:
try:
value1 = int(match.group(1))
value2 = int(match.group(2))
resistance = float(f"{value1}.{value2}")
unit = "k" if "k" in value.lower() else "M" if "m" in value.lower() else "Ω"
return resistance, unit
except ValueError:
pass
return None, None
def extract_capacitance_value(value: str) -> tuple[float | None, str | None]:
"""Extract capacitance value and unit from component value.
Args:
value: Capacitance value (e.g., "10uF", "4.7nF", "100pF")
Returns:
Tuple of (numeric value, unit) or (None, None) if parsing fails
"""
# Common capacitance patterns
# 10uF, 4.7nF, 100pF, etc.
match = re.search(r"(\d+\.?\d*)([pPnNuUμF]+)", value)
if match:
try:
capacitance = float(match.group(1))
unit = match.group(2).lower()
# Normalize unit
if "p" in unit or "pf" in unit:
unit = "pF"
elif "n" in unit or "nf" in unit:
unit = "nF"
elif "u" in unit or "μ" in unit or "uf" in unit or "μf" in unit:
unit = "μF"
else:
unit = "F"
return capacitance, unit
except ValueError:
pass
# Handle special case like "4n7" (means 4.7nF)
match = re.search(r"(\d+)[pPnNuUμ](\d+)", value)
if match:
try:
value1 = int(match.group(1))
value2 = int(match.group(2))
capacitance = float(f"{value1}.{value2}")
if "p" in value.lower():
unit = "pF"
elif "n" in value.lower():
unit = "nF"
elif "u" in value.lower() or "μ" in value:
unit = "μF"
else:
unit = "F"
return capacitance, unit
except ValueError:
pass
return None, None
def extract_inductance_value(value: str) -> tuple[float | None, str | None]:
"""Extract inductance value and unit from component value.
Args:
value: Inductance value (e.g., "10uH", "4.7nH", "100mH")
Returns:
Tuple of (numeric value, unit) or (None, None) if parsing fails
"""
# Common inductance patterns
# 10uH, 4.7nH, 100mH, etc.
match = re.search(r"(\d+\.?\d*)([pPnNuUμmM][hH])", value)
if match:
try:
inductance = float(match.group(1))
unit = match.group(2).lower()
# Normalize unit
if "p" in unit:
unit = "pH"
elif "n" in unit:
unit = "nH"
elif "u" in unit or "μ" in unit:
unit = "μH"
elif "m" in unit:
unit = "mH"
else:
unit = "H"
return inductance, unit
except ValueError:
pass
# Handle special case like "4u7" (means 4.7uH)
match = re.search(r"(\d+)[pPnNuUμmM](\d+)[hH]", value)
if match:
try:
value1 = int(match.group(1))
value2 = int(match.group(2))
inductance = float(f"{value1}.{value2}")
if "p" in value.lower():
unit = "pH"
elif "n" in value.lower():
unit = "nH"
elif "u" in value.lower() or "μ" in value:
unit = "μH"
elif "m" in value.lower():
unit = "mH"
else:
unit = "H"
return inductance, unit
except ValueError:
pass
return None, None
def format_resistance(resistance: float, unit: str) -> str:
"""Format resistance value with appropriate unit.
Args:
resistance: Resistance value
unit: Unit string (Ω, k, M)
Returns:
Formatted resistance string
"""
if unit == "Ω":
return f"{resistance:.0f}Ω" if resistance.is_integer() else f"{resistance}Ω"
elif unit == "k":
return f"{resistance:.0f}" if resistance.is_integer() else f"{resistance}"
elif unit == "M":
return f"{resistance:.0f}" if resistance.is_integer() else f"{resistance}"
else:
return f"{resistance}{unit}"
def format_capacitance(capacitance: float, unit: str) -> str:
"""Format capacitance value with appropriate unit.
Args:
capacitance: Capacitance value
unit: Unit string (pF, nF, μF, F)
Returns:
Formatted capacitance string
"""
if capacitance.is_integer():
return f"{int(capacitance)}{unit}"
else:
return f"{capacitance}{unit}"
def format_inductance(inductance: float, unit: str) -> str:
"""Format inductance value with appropriate unit.
Args:
inductance: Inductance value
unit: Unit string (pH, nH, μH, mH, H)
Returns:
Formatted inductance string
"""
if inductance.is_integer():
return f"{int(inductance)}{unit}"
else:
return f"{inductance}{unit}"
def normalize_component_value(value: str, component_type: str) -> str:
"""Normalize a component value string based on component type.
Args:
value: Raw component value string
component_type: Type of component (R, C, L, etc.)
Returns:
Normalized value string
"""
if component_type == "R":
resistance, unit = extract_resistance_value(value)
if resistance is not None and unit is not None:
return format_resistance(resistance, unit)
elif component_type == "C":
capacitance, unit = extract_capacitance_value(value)
if capacitance is not None and unit is not None:
return format_capacitance(capacitance, unit)
elif component_type == "L":
inductance, unit = extract_inductance_value(value)
if inductance is not None and unit is not None:
return format_inductance(inductance, unit)
# For other component types or if parsing fails, return the original value
return value
def get_component_type_from_reference(reference: str) -> str:
"""Determine component type from reference designator.
Args:
reference: Component reference (e.g., R1, C2, U3)
Returns:
Component type letter (R, C, L, Q, etc.)
"""
# Extract the alphabetic prefix (component type)
match = re.match(r"^([A-Za-z_]+)", reference)
if match:
return match.group(1)
return ""
def is_power_component(component: dict[str, Any]) -> bool:
"""Check if a component is likely a power-related component.
Args:
component: Component information dictionary
Returns:
True if the component is power-related, False otherwise
"""
ref = component.get("reference", "")
value = component.get("value", "").upper()
lib_id = component.get("lib_id", "").upper()
# Check reference designator
if ref.startswith(("VR", "PS", "REG")):
return True
# Check for power-related terms in value or library ID
power_terms = ["VCC", "VDD", "GND", "POWER", "PWR", "SUPPLY", "REGULATOR", "LDO"]
if any(term in value or term in lib_id for term in power_terms):
return True
# Check for regulator part numbers
regulator_patterns = [
r"78\d\d", # 7805, 7812, etc.
r"79\d\d", # 7905, 7912, etc.
r"LM\d{3}", # LM317, LM337, etc.
r"LM\d{4}", # LM1117, etc.
r"AMS\d{4}", # AMS1117, etc.
r"MCP\d{4}", # MCP1700, etc.
]
if any(re.search(pattern, value, re.IGNORECASE) for pattern in regulator_patterns):
return True
# Not identified as a power component
return False
def get_component_type(value: str) -> ComponentType:
"""Determine component type from value string.
Args:
value: Component value or part number
Returns:
ComponentType enum value
"""
value_lower = value.lower()
# Check for resistor patterns
if (re.search(r'\d+[kmgr]?ω|ω', value_lower) or
re.search(r'\d+[kmgr]?ohm', value_lower) or
re.search(r'resistor', value_lower)):
return ComponentType.RESISTOR
# Check for capacitor patterns
if (re.search(r'\d+[pnumkμ]?f', value_lower) or
re.search(r'capacitor|cap', value_lower)):
return ComponentType.CAPACITOR
# Check for inductor patterns
if (re.search(r'\d+[pnumkμ]?h', value_lower) or
re.search(r'inductor|coil', value_lower)):
return ComponentType.INDUCTOR
# Check for diode patterns
if ('diode' in value_lower or 'led' in value_lower or
value_lower.startswith(('1n', 'bar', 'ss'))):
if 'led' in value_lower:
return ComponentType.LED
return ComponentType.DIODE
# Check for transistor patterns
if (re.search(r'transistor|mosfet|bjt|fet', value_lower) or
value_lower.startswith(('2n', 'bc', 'tip', 'irf', 'fqp'))):
return ComponentType.TRANSISTOR
# Check for IC patterns
if (re.search(r'ic|chip|processor|mcu|cpu', value_lower) or
value_lower.startswith(('lm', 'tlv', 'op', 'ad', 'max', 'lt'))):
return ComponentType.IC
# Check for voltage regulator patterns
if (re.search(r'regulator|ldo', value_lower) or
re.search(r'78\d\d|79\d\d|lm317|ams1117', value_lower)):
return ComponentType.VOLTAGE_REGULATOR
# Check for connector patterns
if re.search(r'connector|conn|jack|plug|header', value_lower):
return ComponentType.CONNECTOR
# Check for crystal patterns
if re.search(r'crystal|xtal|oscillator|mhz|khz', value_lower):
return ComponentType.CRYSTAL
# Check for fuse patterns
if re.search(r'fuse|ptc', value_lower):
return ComponentType.FUSE
# Check for switch patterns
if re.search(r'switch|button|sw', value_lower):
return ComponentType.SWITCH
# Check for relay patterns
if re.search(r'relay', value_lower):
return ComponentType.RELAY
# Check for transformer patterns
if re.search(r'transformer|trans', value_lower):
return ComponentType.TRANSFORMER
return ComponentType.UNKNOWN
def get_standard_values(component_type: ComponentType) -> list[str]:
"""Get standard component values for a given component type.
Args:
component_type: Type of component
Returns:
List of standard values as strings
"""
if component_type == ComponentType.RESISTOR:
return [
"", "1.2Ω", "1.5Ω", "1.8Ω", "2.2Ω", "2.7Ω", "3.3Ω", "3.9Ω", "4.7Ω", "5.6Ω", "6.8Ω", "8.2Ω",
"10Ω", "12Ω", "15Ω", "18Ω", "22Ω", "27Ω", "33Ω", "39Ω", "47Ω", "56Ω", "68Ω", "82Ω",
"100Ω", "120Ω", "150Ω", "180Ω", "220Ω", "270Ω", "330Ω", "390Ω", "470Ω", "560Ω", "680Ω", "820Ω",
"1kΩ", "1.2kΩ", "1.5kΩ", "1.8kΩ", "2.2kΩ", "2.7kΩ", "3.3kΩ", "3.9kΩ", "4.7kΩ", "5.6kΩ", "6.8kΩ", "8.2kΩ",
"10kΩ", "12kΩ", "15kΩ", "18kΩ", "22kΩ", "27kΩ", "33kΩ", "39kΩ", "47kΩ", "56kΩ", "68kΩ", "82kΩ",
"100kΩ", "120kΩ", "150kΩ", "180kΩ", "220kΩ", "270kΩ", "330kΩ", "390kΩ", "470kΩ", "560kΩ", "680kΩ", "820kΩ",
"1MΩ", "1.2MΩ", "1.5MΩ", "1.8MΩ", "2.2MΩ", "2.7MΩ", "3.3MΩ", "3.9MΩ", "4.7MΩ", "5.6MΩ", "6.8MΩ", "8.2MΩ",
"10MΩ"
]
elif component_type == ComponentType.CAPACITOR:
return [
"1pF", "1.5pF", "2.2pF", "3.3pF", "4.7pF", "6.8pF", "10pF", "15pF", "22pF", "33pF", "47pF", "68pF",
"100pF", "150pF", "220pF", "330pF", "470pF", "680pF",
"1nF", "1.5nF", "2.2nF", "3.3nF", "4.7nF", "6.8nF", "10nF", "15nF", "22nF", "33nF", "47nF", "68nF",
"100nF", "150nF", "220nF", "330nF", "470nF", "680nF",
"1μF", "1.5μF", "2.2μF", "3.3μF", "4.7μF", "6.8μF", "10μF", "15μF", "22μF", "33μF", "47μF", "68μF",
"100μF", "150μF", "220μF", "330μF", "470μF", "680μF",
"1000μF", "1500μF", "2200μF", "3300μF", "4700μF", "6800μF", "10000μF"
]
elif component_type == ComponentType.INDUCTOR:
return [
"1nH", "1.5nH", "2.2nH", "3.3nH", "4.7nH", "6.8nH", "10nH", "15nH", "22nH", "33nH", "47nH", "68nH",
"100nH", "150nH", "220nH", "330nH", "470nH", "680nH",
"1μH", "1.5μH", "2.2μH", "3.3μH", "4.7μH", "6.8μH", "10μH", "15μH", "22μH", "33μH", "47μH", "68μH",
"100μH", "150μH", "220μH", "330μH", "470μH", "680μH",
"1mH", "1.5mH", "2.2mH", "3.3mH", "4.7mH", "6.8mH", "10mH", "15mH", "22mH", "33mH", "47mH", "68mH",
"100mH", "150mH", "220mH", "330mH", "470mH", "680mH"
]
elif component_type == ComponentType.CRYSTAL:
return [
"32.768kHz", "1MHz", "2MHz", "4MHz", "8MHz", "10MHz", "12MHz", "16MHz", "20MHz", "24MHz", "25MHz", "27MHz"
]
else:
return []

View File

@ -1,28 +0,0 @@
"""
Coordinate conversion utilities for KiCad.
Stub implementation to fix import issues.
"""
class CoordinateConverter:
"""Converts between different coordinate systems in KiCad."""
def __init__(self):
self.scale_factor = 1.0
def to_kicad_units(self, mm: float) -> float:
"""Convert millimeters to KiCad internal units."""
return mm * 1e6 # KiCad uses nanometers internally
def from_kicad_units(self, units: float) -> float:
"""Convert KiCad internal units to millimeters."""
return units / 1e6
def validate_position(x: float | int, y: float | int) -> bool:
"""Validate if a position is within reasonable bounds."""
# Basic validation - positions should be reasonable
max_coord = 1000 # mm
return abs(x) <= max_coord and abs(y) <= max_coord

View File

@ -1,183 +0,0 @@
"""
Utilities for tracking DRC history for KiCad projects.
This will allow users to compare DRC results over time.
"""
from datetime import datetime
import json
import os
import platform
import time
from typing import Any
# Directory for storing DRC history
if platform.system() == "Windows":
# Windows: Use APPDATA or LocalAppData
DRC_HISTORY_DIR = os.path.join(
os.environ.get("APPDATA", os.path.expanduser("~")), "mckicad", "drc_history"
)
else:
# macOS/Linux: Use ~/.mckicad/drc_history
DRC_HISTORY_DIR = os.path.expanduser("~/.mckicad/drc_history")
def ensure_history_dir() -> None:
"""Ensure the DRC history directory exists."""
os.makedirs(DRC_HISTORY_DIR, exist_ok=True)
def get_project_history_path(project_path: str) -> str:
"""Get the path to the DRC history file for a project.
Args:
project_path: Path to the KiCad project file
Returns:
Path to the project's DRC history file
"""
# Create a safe filename from the project path
project_hash = hash(project_path) & 0xFFFFFFFF # Ensure positive hash
basename = os.path.basename(project_path)
history_filename = f"{basename}_{project_hash}_drc_history.json"
return os.path.join(DRC_HISTORY_DIR, history_filename)
def save_drc_result(project_path: str, drc_result: dict[str, Any]) -> None:
"""Save a DRC result to the project's history.
Args:
project_path: Path to the KiCad project file
drc_result: DRC result dictionary
"""
ensure_history_dir()
history_path = get_project_history_path(project_path)
# Create a history entry
timestamp = time.time()
formatted_time = datetime.fromtimestamp(timestamp).strftime("%Y-%m-%d %H:%M:%S")
history_entry = {
"timestamp": timestamp,
"datetime": formatted_time,
"total_violations": drc_result.get("total_violations", 0),
"violation_categories": drc_result.get("violation_categories", {}),
}
# Load existing history or create new
if os.path.exists(history_path):
try:
with open(history_path) as f:
history = json.load(f)
except (OSError, json.JSONDecodeError) as e:
print(f"Error loading DRC history: {str(e)}")
history = {"project_path": project_path, "entries": []}
else:
history = {"project_path": project_path, "entries": []}
# Add new entry and save
history["entries"].append(history_entry)
# Keep only the last 10 entries to avoid excessive storage
if len(history["entries"]) > 10:
history["entries"] = sorted(history["entries"], key=lambda x: x["timestamp"], reverse=True)[
:10
]
try:
with open(history_path, "w") as f:
json.dump(history, f, indent=2)
print(f"Saved DRC history entry to {history_path}")
except OSError as e:
print(f"Error saving DRC history: {str(e)}")
def get_drc_history(project_path: str) -> list[dict[str, Any]]:
"""Get the DRC history for a project.
Args:
project_path: Path to the KiCad project file
Returns:
List of DRC history entries, sorted by timestamp (newest first)
"""
history_path = get_project_history_path(project_path)
if not os.path.exists(history_path):
print(f"No DRC history found for {project_path}")
return []
try:
with open(history_path) as f:
history = json.load(f)
# Sort entries by timestamp (newest first)
entries = sorted(
history.get("entries", []), key=lambda x: x.get("timestamp", 0), reverse=True
)
return entries
except (OSError, json.JSONDecodeError) as e:
print(f"Error reading DRC history: {str(e)}")
return []
def compare_with_previous(
project_path: str, current_result: dict[str, Any]
) -> dict[str, Any] | None:
"""Compare current DRC result with the previous one.
Args:
project_path: Path to the KiCad project file
current_result: Current DRC result dictionary
Returns:
Comparison dictionary or None if no history exists
"""
history = get_drc_history(project_path)
if not history or len(history) < 2: # Need at least one previous entry
return None
previous = history[0] # Most recent entry
current_violations = current_result.get("total_violations", 0)
previous_violations = previous.get("total_violations", 0)
# Compare violation categories
current_categories = current_result.get("violation_categories", {})
previous_categories = previous.get("violation_categories", {})
# Find new categories
new_categories = {}
for category, count in current_categories.items():
if category not in previous_categories:
new_categories[category] = count
# Find resolved categories
resolved_categories = {}
for category, count in previous_categories.items():
if category not in current_categories:
resolved_categories[category] = count
# Find changed categories
changed_categories = {}
for category, count in current_categories.items():
if category in previous_categories and count != previous_categories[category]:
changed_categories[category] = {
"current": count,
"previous": previous_categories[category],
"change": count - previous_categories[category],
}
comparison = {
"current_violations": current_violations,
"previous_violations": previous_violations,
"change": current_violations - previous_violations,
"previous_datetime": previous.get("datetime", "unknown"),
"new_categories": new_categories,
"resolved_categories": resolved_categories,
"changed_categories": changed_categories,
}
return comparison

View File

@ -1,124 +0,0 @@
"""
Environment variable handling for KiCad MCP Server.
"""
import logging
import os
def load_dotenv(env_file: str = ".env") -> dict[str, str]:
"""Load environment variables from .env file.
Args:
env_file: Path to the .env file
Returns:
Dictionary of loaded environment variables
"""
env_vars = {}
logging.info(f"load_dotenv called for file: {env_file}")
# Try to find .env file in the current directory or parent directories
env_path = find_env_file(env_file)
if not env_path:
logging.warning(f"No .env file found matching: {env_file}")
return env_vars
logging.info(f"Found .env file at: {env_path}")
try:
with open(env_path) as f:
logging.info(f"Successfully opened {env_path} for reading.")
line_num = 0
for line in f:
line_num += 1
line = line.strip()
# Skip empty lines and comments
if not line or line.startswith("#"):
logging.debug(f"Skipping line {line_num} (comment/empty): {line}")
continue
# Parse key-value pairs
if "=" in line:
key, value = line.split("=", 1)
key = key.strip()
value = value.strip()
logging.debug(f"Parsed line {line_num}: Key='{key}', RawValue='{value}'")
# Remove quotes if present
if value.startswith('"') and value.endswith('"') or value.startswith("'") and value.endswith("'"):
value = value[1:-1]
# Expand ~ to user's home directory
original_value = value
if "~" in value:
value = os.path.expanduser(value)
if value != original_value:
logging.debug(
f"Expanded ~ in value for key '{key}': '{original_value}' -> '{value}'"
)
# Set environment variable
logging.info(f"Setting os.environ['{key}'] = '{value}'")
os.environ[key] = value
env_vars[key] = value
else:
logging.warning(f"Skipping line {line_num} (no '=' found): {line}")
logging.info(f"Finished processing {env_path}")
except Exception:
# Use logging.exception to include traceback
logging.exception(f"Error loading .env file '{env_path}'")
logging.info(f"load_dotenv returning: {env_vars}")
return env_vars
def find_env_file(filename: str = ".env") -> str | None:
"""Find a .env file in the current directory or parent directories.
Args:
filename: Name of the env file to find
Returns:
Path to the env file if found, None otherwise
"""
current_dir = os.getcwd()
logging.info(f"find_env_file starting search from: {current_dir}")
max_levels = 3 # Limit how far up to search
for _ in range(max_levels):
env_path = os.path.join(current_dir, filename)
if os.path.exists(env_path):
return env_path
# Move up one directory
parent_dir = os.path.dirname(current_dir)
if parent_dir == current_dir: # We've reached the root
break
current_dir = parent_dir
return None
def get_env_list(env_var: str, default: str = "") -> list:
"""Get a list from a comma-separated environment variable.
Args:
env_var: Name of the environment variable
default: Default value if environment variable is not set
Returns:
List of values
"""
value = os.environ.get(env_var, default)
if not value:
return []
# Split by comma and strip whitespace
items = [item.strip() for item in value.split(",")]
# Filter out empty items
return [item for item in items if item]

View File

@ -1,71 +0,0 @@
"""
Utility functions for detecting and selecting available KiCad API approaches.
"""
import os
import shutil
import subprocess
from mckicad.config import system
def check_for_cli_api() -> bool:
"""Check if KiCad CLI API is available.
Returns:
True if KiCad CLI is available, False otherwise
"""
try:
# Check if kicad-cli is in PATH
if system == "Windows":
# On Windows, check for kicad-cli.exe
kicad_cli = shutil.which("kicad-cli.exe")
else:
# On Unix-like systems
kicad_cli = shutil.which("kicad-cli")
if kicad_cli:
# Verify it's a working kicad-cli
if system == "Windows":
cmd = [kicad_cli, "--version"]
else:
cmd = [kicad_cli, "--version"]
result = subprocess.run(cmd, capture_output=True, text=True)
if result.returncode == 0:
print(f"Found working kicad-cli: {kicad_cli}")
return True
# Check common installation locations if not found in PATH
if system == "Windows":
# Common Windows installation paths
potential_paths = [
r"C:\Program Files\KiCad\bin\kicad-cli.exe",
r"C:\Program Files (x86)\KiCad\bin\kicad-cli.exe",
]
elif system == "Darwin": # macOS
# Common macOS installation paths
potential_paths = [
"/Applications/KiCad/KiCad.app/Contents/MacOS/kicad-cli",
"/Applications/KiCad/kicad-cli",
]
else: # Linux
# Common Linux installation paths
potential_paths = [
"/usr/bin/kicad-cli",
"/usr/local/bin/kicad-cli",
"/opt/kicad/bin/kicad-cli",
]
# Check each potential path
for path in potential_paths:
if os.path.exists(path) and os.access(path, os.X_OK):
print(f"Found kicad-cli at common location: {path}")
return True
print("KiCad CLI API is not available")
return False
except Exception as e:
print(f"Error checking for KiCad CLI API: {str(e)}")
return False

View File

@ -1,558 +0,0 @@
"""
PCB Layer Stack-up Analysis utilities for KiCad.
Provides functionality to analyze PCB layer configurations, impedance calculations,
manufacturing constraints, and design rule validation for multi-layer boards.
"""
from dataclasses import dataclass
import logging
import math
import re
from typing import Any
logger = logging.getLogger(__name__)
@dataclass
class LayerDefinition:
"""Represents a single layer in the PCB stack-up."""
name: str
layer_type: str # "signal", "power", "ground", "dielectric", "soldermask", "silkscreen"
thickness: float # in mm
material: str
dielectric_constant: float | None = None
loss_tangent: float | None = None
copper_weight: float | None = None # in oz (for copper layers)
layer_number: int | None = None
kicad_layer_id: str | None = None
@dataclass
class ImpedanceCalculation:
"""Impedance calculation results for a trace configuration."""
trace_width: float
trace_spacing: float | None # For differential pairs
impedance_single: float | None
impedance_differential: float | None
layer_name: str
reference_layers: list[str]
calculation_method: str
@dataclass
class StackupConstraints:
"""Manufacturing and design constraints for the stack-up."""
min_trace_width: float
min_via_drill: float
min_annular_ring: float
aspect_ratio_limit: float
dielectric_thickness_limits: tuple[float, float]
copper_weight_options: list[float]
layer_count_limit: int
@dataclass
class LayerStackup:
"""Complete PCB layer stack-up definition."""
name: str
layers: list[LayerDefinition]
total_thickness: float
layer_count: int
impedance_calculations: list[ImpedanceCalculation]
constraints: StackupConstraints
manufacturing_notes: list[str]
class LayerStackupAnalyzer:
"""Analyzer for PCB layer stack-up configurations."""
def __init__(self):
"""Initialize the layer stack-up analyzer."""
self.standard_materials = self._load_standard_materials()
self.impedance_calculator = ImpedanceCalculator()
def _load_standard_materials(self) -> dict[str, dict[str, Any]]:
"""Load standard PCB materials database."""
return {
"FR4_Standard": {
"dielectric_constant": 4.35,
"loss_tangent": 0.02,
"description": "Standard FR4 epoxy fiberglass"
},
"FR4_High_Tg": {
"dielectric_constant": 4.2,
"loss_tangent": 0.015,
"description": "High Tg FR4 for lead-free soldering"
},
"Rogers_4003C": {
"dielectric_constant": 3.38,
"loss_tangent": 0.0027,
"description": "Rogers low-loss hydrocarbon ceramic"
},
"Rogers_4350B": {
"dielectric_constant": 3.48,
"loss_tangent": 0.0037,
"description": "Rogers woven glass reinforced hydrocarbon"
},
"Polyimide": {
"dielectric_constant": 3.5,
"loss_tangent": 0.002,
"description": "Flexible polyimide substrate"
},
"Prepreg_106": {
"dielectric_constant": 4.2,
"loss_tangent": 0.02,
"description": "Standard prepreg 106 glass style"
},
"Prepreg_1080": {
"dielectric_constant": 4.4,
"loss_tangent": 0.02,
"description": "Thick prepreg 1080 glass style"
}
}
def analyze_pcb_stackup(self, pcb_file_path: str) -> LayerStackup:
"""Analyze PCB file and extract layer stack-up information."""
try:
with open(pcb_file_path, encoding='utf-8') as f:
content = f.read()
# Extract layer definitions
layers = self._parse_layers(content)
# Calculate total thickness
total_thickness = sum(layer.thickness for layer in layers if layer.thickness)
# Extract manufacturing constraints
constraints = self._extract_constraints(content)
# Perform impedance calculations
impedance_calcs = self._calculate_impedances(layers, content)
# Generate manufacturing notes
notes = self._generate_manufacturing_notes(layers, total_thickness)
stackup = LayerStackup(
name=f"PCB_Stackup_{len(layers)}_layers",
layers=layers,
total_thickness=total_thickness,
layer_count=len([l for l in layers if l.layer_type in ["signal", "power", "ground"]]),
impedance_calculations=impedance_calcs,
constraints=constraints,
manufacturing_notes=notes
)
logger.info(f"Analyzed {len(layers)}-layer stack-up with {total_thickness:.3f}mm total thickness")
return stackup
except Exception as e:
logger.error(f"Failed to analyze PCB stack-up from {pcb_file_path}: {e}")
raise
def _parse_layers(self, content: str) -> list[LayerDefinition]:
"""Parse layer definitions from PCB content."""
layers = []
# Extract layer setup section
setup_match = re.search(r'\(setup[^)]*\(stackup[^)]*\)', content, re.DOTALL)
if not setup_match:
# Fallback to basic layer extraction
return self._parse_basic_layers(content)
stackup_content = setup_match.group(0)
# Parse individual layers
layer_pattern = r'\(layer\s+"([^"]+)"\s+\(type\s+(\w+)\)\s*(?:\(thickness\s+([\d.]+)\))?\s*(?:\(material\s+"([^"]+)"\))?'
for match in re.finditer(layer_pattern, stackup_content):
layer_name = match.group(1)
layer_type = match.group(2)
thickness = float(match.group(3)) if match.group(3) else None
material = match.group(4) or "Unknown"
# Get material properties
material_props = self.standard_materials.get(material, {})
layer = LayerDefinition(
name=layer_name,
layer_type=layer_type,
thickness=thickness or 0.0,
material=material,
dielectric_constant=material_props.get("dielectric_constant"),
loss_tangent=material_props.get("loss_tangent"),
copper_weight=1.0 if layer_type in ["signal", "power", "ground"] else None
)
layers.append(layer)
# If no stack-up found, create standard layers
if not layers:
layers = self._create_standard_stackup(content)
return layers
def _parse_basic_layers(self, content: str) -> list[LayerDefinition]:
"""Parse basic layer information when detailed stack-up is not available."""
layers = []
# Find layer definitions in PCB
layer_pattern = r'\((\d+)\s+"([^"]+)"\s+(signal|power|user)\)'
found_layers = []
for match in re.finditer(layer_pattern, content):
layer_num = int(match.group(1))
layer_name = match.group(2)
layer_type = match.group(3)
found_layers.append((layer_num, layer_name, layer_type))
found_layers.sort(key=lambda x: x[0]) # Sort by layer number
# Create layer definitions with estimated properties
for i, (layer_num, layer_name, layer_type) in enumerate(found_layers):
# Estimate thickness based on layer type and position
if i == 0 or i == len(found_layers) - 1: # Top/bottom layers
thickness = 0.035 # 35μm copper
else:
thickness = 0.017 # 17μm inner layers
layer = LayerDefinition(
name=layer_name,
layer_type="signal" if layer_type == "signal" else layer_type,
thickness=thickness,
material="Copper",
copper_weight=1.0,
layer_number=layer_num,
kicad_layer_id=str(layer_num)
)
layers.append(layer)
# Add dielectric layer between copper layers (except after last layer)
if i < len(found_layers) - 1:
dielectric_thickness = 0.2 if len(found_layers) <= 4 else 0.1
dielectric = LayerDefinition(
name=f"Dielectric_{i+1}",
layer_type="dielectric",
thickness=dielectric_thickness,
material="FR4_Standard",
dielectric_constant=4.35,
loss_tangent=0.02
)
layers.append(dielectric)
return layers
def _create_standard_stackup(self, content: str) -> list[LayerDefinition]:
"""Create a standard 4-layer stack-up when no stack-up is defined."""
return [
LayerDefinition("Top", "signal", 0.035, "Copper", copper_weight=1.0),
LayerDefinition("Prepreg_1", "dielectric", 0.2, "Prepreg_106",
dielectric_constant=4.2, loss_tangent=0.02),
LayerDefinition("Inner1", "power", 0.017, "Copper", copper_weight=0.5),
LayerDefinition("Core", "dielectric", 1.2, "FR4_Standard",
dielectric_constant=4.35, loss_tangent=0.02),
LayerDefinition("Inner2", "ground", 0.017, "Copper", copper_weight=0.5),
LayerDefinition("Prepreg_2", "dielectric", 0.2, "Prepreg_106",
dielectric_constant=4.2, loss_tangent=0.02),
LayerDefinition("Bottom", "signal", 0.035, "Copper", copper_weight=1.0)
]
def _extract_constraints(self, content: str) -> StackupConstraints:
"""Extract manufacturing constraints from PCB."""
# Default constraints - could be extracted from design rules
return StackupConstraints(
min_trace_width=0.1, # 100μm
min_via_drill=0.2, # 200μm
min_annular_ring=0.05, # 50μm
aspect_ratio_limit=8.0, # 8:1 drill depth to diameter
dielectric_thickness_limits=(0.05, 3.0), # 50μm to 3mm
copper_weight_options=[0.5, 1.0, 2.0], # oz
layer_count_limit=16
)
def _calculate_impedances(self, layers: list[LayerDefinition],
content: str) -> list[ImpedanceCalculation]:
"""Calculate characteristic impedances for signal layers."""
impedance_calcs = []
signal_layers = [l for l in layers if l.layer_type == "signal"]
for signal_layer in signal_layers:
# Find reference layers (adjacent power/ground planes)
ref_layers = self._find_reference_layers(signal_layer, layers)
# Calculate for standard trace widths
for trace_width in [0.1, 0.15, 0.2, 0.25]: # mm
single_ended = self.impedance_calculator.calculate_microstrip_impedance(
trace_width, signal_layer, layers
)
differential = self.impedance_calculator.calculate_differential_impedance(
trace_width, 0.15, signal_layer, layers # 0.15mm spacing
)
impedance_calcs.append(ImpedanceCalculation(
trace_width=trace_width,
trace_spacing=0.15,
impedance_single=single_ended,
impedance_differential=differential,
layer_name=signal_layer.name,
reference_layers=ref_layers,
calculation_method="microstrip"
))
return impedance_calcs
def _find_reference_layers(self, signal_layer: LayerDefinition,
layers: list[LayerDefinition]) -> list[str]:
"""Find reference planes for a signal layer."""
ref_layers = []
signal_idx = layers.index(signal_layer)
# Look for adjacent power/ground layers
for i in range(max(0, signal_idx - 2), min(len(layers), signal_idx + 3)):
if i != signal_idx and layers[i].layer_type in ["power", "ground"]:
ref_layers.append(layers[i].name)
return ref_layers
def _generate_manufacturing_notes(self, layers: list[LayerDefinition],
total_thickness: float) -> list[str]:
"""Generate manufacturing and assembly notes."""
notes = []
copper_layers = len([l for l in layers if l.layer_type in ["signal", "power", "ground"]])
if copper_layers > 8:
notes.append("High layer count may require specialized manufacturing")
if total_thickness > 3.0:
notes.append("Thick board may require extended drill programs")
elif total_thickness < 0.8:
notes.append("Thin board requires careful handling during assembly")
# Check for impedance control requirements
signal_layers = len([l for l in layers if l.layer_type == "signal"])
if signal_layers > 2:
notes.append("Multi-layer design - impedance control recommended")
# Material considerations
materials = set(l.material for l in layers if l.layer_type == "dielectric")
if len(materials) > 1:
notes.append("Mixed dielectric materials - verify thermal expansion compatibility")
return notes
def validate_stackup(self, stackup: LayerStackup) -> list[str]:
"""Validate stack-up for manufacturability and design rules."""
issues = []
# Check layer count
if stackup.layer_count > stackup.constraints.layer_count_limit:
issues.append(f"Layer count {stackup.layer_count} exceeds limit of {stackup.constraints.layer_count_limit}")
# Check total thickness
if stackup.total_thickness > 6.0:
issues.append(f"Total thickness {stackup.total_thickness:.2f}mm may be difficult to manufacture")
# Check for proper reference planes
signal_layers = [l for l in stackup.layers if l.layer_type == "signal"]
power_ground_layers = [l for l in stackup.layers if l.layer_type in ["power", "ground"]]
if len(signal_layers) > 2 and len(power_ground_layers) < 2:
issues.append("Multi-layer design should have dedicated power and ground planes")
# Check dielectric thickness
for layer in stackup.layers:
if layer.layer_type == "dielectric":
if layer.thickness < stackup.constraints.dielectric_thickness_limits[0]:
issues.append(f"Dielectric layer '{layer.name}' thickness {layer.thickness:.3f}mm is too thin")
elif layer.thickness > stackup.constraints.dielectric_thickness_limits[1]:
issues.append(f"Dielectric layer '{layer.name}' thickness {layer.thickness:.3f}mm is too thick")
# Check copper balance
top_copper = sum(l.thickness for l in stackup.layers[:len(stackup.layers)//2] if l.copper_weight)
bottom_copper = sum(l.thickness for l in stackup.layers[len(stackup.layers)//2:] if l.copper_weight)
if abs(top_copper - bottom_copper) / max(top_copper, bottom_copper) > 0.3:
issues.append("Copper distribution is unbalanced - may cause warpage")
return issues
def generate_stackup_report(self, stackup: LayerStackup) -> dict[str, Any]:
"""Generate comprehensive stack-up analysis report."""
validation_issues = self.validate_stackup(stackup)
# Calculate electrical properties
electrical_props = self._calculate_electrical_properties(stackup)
# Generate recommendations
recommendations = self._generate_stackup_recommendations(stackup, validation_issues)
return {
"stackup_info": {
"name": stackup.name,
"layer_count": stackup.layer_count,
"total_thickness_mm": stackup.total_thickness,
"copper_layers": len([l for l in stackup.layers if l.copper_weight]),
"dielectric_layers": len([l for l in stackup.layers if l.layer_type == "dielectric"])
},
"layer_details": [
{
"name": layer.name,
"type": layer.layer_type,
"thickness_mm": layer.thickness,
"material": layer.material,
"dielectric_constant": layer.dielectric_constant,
"loss_tangent": layer.loss_tangent,
"copper_weight_oz": layer.copper_weight
}
for layer in stackup.layers
],
"impedance_analysis": [
{
"layer": imp.layer_name,
"trace_width_mm": imp.trace_width,
"single_ended_ohm": imp.impedance_single,
"differential_ohm": imp.impedance_differential,
"reference_layers": imp.reference_layers
}
for imp in stackup.impedance_calculations
],
"electrical_properties": electrical_props,
"manufacturing": {
"constraints": {
"min_trace_width_mm": stackup.constraints.min_trace_width,
"min_via_drill_mm": stackup.constraints.min_via_drill,
"aspect_ratio_limit": stackup.constraints.aspect_ratio_limit
},
"notes": stackup.manufacturing_notes
},
"validation": {
"issues": validation_issues,
"passed": len(validation_issues) == 0
},
"recommendations": recommendations
}
def _calculate_electrical_properties(self, stackup: LayerStackup) -> dict[str, Any]:
"""Calculate overall electrical properties of the stack-up."""
# Calculate effective dielectric constant
dielectric_layers = [l for l in stackup.layers if l.layer_type == "dielectric" and l.dielectric_constant]
if dielectric_layers:
weighted_dk = sum(l.dielectric_constant * l.thickness for l in dielectric_layers) / sum(l.thickness for l in dielectric_layers)
avg_loss_tangent = sum(l.loss_tangent or 0 for l in dielectric_layers) / len(dielectric_layers)
else:
weighted_dk = 4.35 # Default FR4
avg_loss_tangent = 0.02
return {
"effective_dielectric_constant": weighted_dk,
"average_loss_tangent": avg_loss_tangent,
"total_copper_thickness_mm": sum(l.thickness for l in stackup.layers if l.copper_weight),
"total_dielectric_thickness_mm": sum(l.thickness for l in stackup.layers if l.layer_type == "dielectric")
}
def _generate_stackup_recommendations(self, stackup: LayerStackup,
issues: list[str]) -> list[str]:
"""Generate recommendations for stack-up optimization."""
recommendations = []
if issues:
recommendations.append("Address validation issues before manufacturing")
# Impedance recommendations
impedance_50ohm = [imp for imp in stackup.impedance_calculations if imp.impedance_single and abs(imp.impedance_single - 50) < 5]
if not impedance_50ohm and stackup.impedance_calculations:
recommendations.append("Consider adjusting trace widths to achieve 50Ω characteristic impedance")
# Layer count recommendations
if stackup.layer_count == 2:
recommendations.append("Consider 4-layer stack-up for better signal integrity and power distribution")
elif stackup.layer_count > 8:
recommendations.append("High layer count - ensure proper via management and signal routing")
# Material recommendations
materials = set(l.material for l in stackup.layers if l.layer_type == "dielectric")
if "Rogers" in str(materials) and "FR4" in str(materials):
recommendations.append("Mixed materials detected - verify thermal expansion compatibility")
return recommendations
class ImpedanceCalculator:
"""Calculator for transmission line impedance."""
def calculate_microstrip_impedance(self, trace_width: float, signal_layer: LayerDefinition,
layers: list[LayerDefinition]) -> float | None:
"""Calculate microstrip impedance for a trace."""
try:
# Find the dielectric layer below the signal layer
signal_idx = layers.index(signal_layer)
dielectric = None
for i in range(signal_idx + 1, len(layers)):
if layers[i].layer_type == "dielectric":
dielectric = layers[i]
break
if not dielectric or not dielectric.dielectric_constant:
return None
# Microstrip impedance calculation (simplified)
h = dielectric.thickness # dielectric height
w = trace_width # trace width
er = dielectric.dielectric_constant
# Wheeler's formula for microstrip impedance
if w/h > 1:
z0 = (120 * math.pi) / (math.sqrt(er) * (w/h + 1.393 + 0.667 * math.log(w/h + 1.444)))
else:
z0 = (60 * math.log(8*h/w + w/(4*h))) / math.sqrt(er)
return round(z0, 1)
except (ValueError, ZeroDivisionError, IndexError):
return None
def calculate_differential_impedance(self, trace_width: float, trace_spacing: float,
signal_layer: LayerDefinition,
layers: list[LayerDefinition]) -> float | None:
"""Calculate differential impedance for a trace pair."""
try:
single_ended = self.calculate_microstrip_impedance(trace_width, signal_layer, layers)
if not single_ended:
return None
# Find the dielectric layer below the signal layer
signal_idx = layers.index(signal_layer)
dielectric = None
for i in range(signal_idx + 1, len(layers)):
if layers[i].layer_type == "dielectric":
dielectric = layers[i]
break
if not dielectric:
return None
# Approximate differential impedance calculation
h = dielectric.thickness
w = trace_width
s = trace_spacing
# Coupling factor (simplified)
k = s / (s + 2*w)
# Differential impedance approximation
z_diff = 2 * single_ended * (1 - k)
return round(z_diff, 1)
except (ValueError, ZeroDivisionError):
return None
def create_stackup_analyzer() -> LayerStackupAnalyzer:
"""Create and initialize a layer stack-up analyzer."""
return LayerStackupAnalyzer()

View File

@ -1,402 +0,0 @@
"""
3D Model Analysis utilities for KiCad PCB files.
Provides functionality to analyze 3D models, visualizations, and mechanical constraints
from KiCad PCB files including component placement, clearances, and board dimensions.
"""
from dataclasses import dataclass
import logging
import re
from typing import Any
logger = logging.getLogger(__name__)
@dataclass
class Component3D:
"""Represents a 3D component with position and model information."""
reference: str
position: tuple[float, float, float] # X, Y, Z coordinates in mm
rotation: tuple[float, float, float] # Rotation around X, Y, Z axes
model_path: str | None
model_scale: tuple[float, float, float] = (1.0, 1.0, 1.0)
model_offset: tuple[float, float, float] = (0.0, 0.0, 0.0)
footprint: str | None = None
value: str | None = None
@dataclass
class BoardDimensions:
"""PCB board physical dimensions and constraints."""
width: float # mm
height: float # mm
thickness: float # mm
outline_points: list[tuple[float, float]] # Board outline coordinates
holes: list[tuple[float, float, float]] # Hole positions and diameters
keepout_areas: list[dict[str, Any]] # Keepout zones
@dataclass
class MechanicalAnalysis:
"""Results of mechanical/3D analysis."""
board_dimensions: BoardDimensions
components: list[Component3D]
clearance_violations: list[dict[str, Any]]
height_analysis: dict[str, float] # min, max, average heights
mechanical_constraints: list[str] # Constraint violations or warnings
class Model3DAnalyzer:
"""Analyzer for 3D models and mechanical aspects of KiCad PCBs."""
def __init__(self, pcb_file_path: str):
"""Initialize with PCB file path."""
self.pcb_file_path = pcb_file_path
self.pcb_data = None
self._load_pcb_data()
def _load_pcb_data(self) -> None:
"""Load and parse PCB file data."""
try:
with open(self.pcb_file_path, encoding='utf-8') as f:
content = f.read()
# Parse S-expression format (simplified)
self.pcb_data = content
except Exception as e:
logger.error(f"Failed to load PCB file {self.pcb_file_path}: {e}")
self.pcb_data = None
def extract_3d_components(self) -> list[Component3D]:
"""Extract 3D component information from PCB data."""
components = []
if not self.pcb_data:
return components
# Parse footprint modules with 3D models
footprint_pattern = r'\(footprint\s+"([^"]+)"[^)]*\(at\s+([\d.-]+)\s+([\d.-]+)(?:\s+([\d.-]+))?\)'
model_pattern = r'\(model\s+"([^"]+)"[^)]*\(at\s+\(xyz\s+([\d.-]+)\s+([\d.-]+)\s+([\d.-]+)\)\)[^)]*\(scale\s+\(xyz\s+([\d.-]+)\s+([\d.-]+)\s+([\d.-]+)\)\)'
reference_pattern = r'\(fp_text\s+reference\s+"([^"]+)"'
value_pattern = r'\(fp_text\s+value\s+"([^"]+)"'
# Find all footprints
for footprint_match in re.finditer(footprint_pattern, self.pcb_data, re.MULTILINE):
footprint_name = footprint_match.group(1)
x_pos = float(footprint_match.group(2))
y_pos = float(footprint_match.group(3))
rotation = float(footprint_match.group(4)) if footprint_match.group(4) else 0.0
# Extract the footprint section
start_pos = footprint_match.start()
footprint_section = self._extract_footprint_section(start_pos)
# Find reference and value within this footprint
ref_match = re.search(reference_pattern, footprint_section)
val_match = re.search(value_pattern, footprint_section)
reference = ref_match.group(1) if ref_match else "Unknown"
value = val_match.group(1) if val_match else ""
# Find 3D model within this footprint
model_match = re.search(model_pattern, footprint_section)
if model_match:
model_path = model_match.group(1)
model_x = float(model_match.group(2))
model_y = float(model_match.group(3))
model_z = float(model_match.group(4))
scale_x = float(model_match.group(5))
scale_y = float(model_match.group(6))
scale_z = float(model_match.group(7))
component = Component3D(
reference=reference,
position=(x_pos, y_pos, 0.0), # Z will be calculated from model
rotation=(0.0, 0.0, rotation),
model_path=model_path,
model_scale=(scale_x, scale_y, scale_z),
model_offset=(model_x, model_y, model_z),
footprint=footprint_name,
value=value
)
components.append(component)
logger.info(f"Extracted {len(components)} 3D components from PCB")
return components
def _extract_footprint_section(self, start_pos: int) -> str:
"""Extract a complete footprint section from PCB data."""
if not self.pcb_data:
return ""
# Find the matching closing parenthesis
level = 0
i = start_pos
while i < len(self.pcb_data):
if self.pcb_data[i] == '(':
level += 1
elif self.pcb_data[i] == ')':
level -= 1
if level == 0:
return self.pcb_data[start_pos:i+1]
i += 1
return self.pcb_data[start_pos:start_pos + 10000] # Fallback
def analyze_board_dimensions(self) -> BoardDimensions:
"""Analyze board physical dimensions and constraints."""
if not self.pcb_data:
return BoardDimensions(0, 0, 1.6, [], [], [])
# Extract board outline (Edge.Cuts layer)
edge_pattern = r'\(gr_line\s+\(start\s+([\d.-]+)\s+([\d.-]+)\)\s+\(end\s+([\d.-]+)\s+([\d.-]+)\)\s+\(stroke[^)]*\)\s+\(layer\s+"Edge\.Cuts"\)'
outline_points = []
for match in re.finditer(edge_pattern, self.pcb_data):
start_x, start_y = float(match.group(1)), float(match.group(2))
end_x, end_y = float(match.group(3)), float(match.group(4))
outline_points.extend([(start_x, start_y), (end_x, end_y)])
# Calculate board dimensions
if outline_points:
x_coords = [p[0] for p in outline_points]
y_coords = [p[1] for p in outline_points]
width = max(x_coords) - min(x_coords)
height = max(y_coords) - min(y_coords)
else:
width = height = 0
# Extract board thickness from stackup (if available) or default to 1.6mm
thickness = 1.6
thickness_pattern = r'\(thickness\s+([\d.]+)\)'
thickness_match = re.search(thickness_pattern, self.pcb_data)
if thickness_match:
thickness = float(thickness_match.group(1))
# Find holes
holes = []
hole_pattern = r'\(pad[^)]*\(type\s+thru_hole\)[^)]*\(at\s+([\d.-]+)\s+([\d.-]+)\)[^)]*\(size\s+([\d.-]+)'
for match in re.finditer(hole_pattern, self.pcb_data):
x, y, diameter = float(match.group(1)), float(match.group(2)), float(match.group(3))
holes.append((x, y, diameter))
return BoardDimensions(
width=width,
height=height,
thickness=thickness,
outline_points=list(set(outline_points)), # Remove duplicates
holes=holes,
keepout_areas=[] # TODO: Extract keepout zones
)
def analyze_component_heights(self, components: list[Component3D]) -> dict[str, float]:
"""Analyze component height distribution."""
heights = []
for component in components:
if component.model_path:
# Estimate height from model scale and type
estimated_height = self._estimate_component_height(component)
heights.append(estimated_height)
if not heights:
return {"min": 0, "max": 0, "average": 0, "count": 0}
return {
"min": min(heights),
"max": max(heights),
"average": sum(heights) / len(heights),
"count": len(heights)
}
def _estimate_component_height(self, component: Component3D) -> float:
"""Estimate component height based on footprint and model."""
# Component height estimation based on common footprint patterns
footprint_heights = {
# SMD packages
"0402": 0.6,
"0603": 0.95,
"0805": 1.35,
"1206": 1.7,
# IC packages
"SOIC": 2.65,
"QFP": 1.75,
"BGA": 1.5,
"TQFP": 1.4,
# Through-hole
"DIP": 4.0,
"TO-220": 4.5,
"TO-92": 4.5,
}
# Check footprint name for height hints
footprint = component.footprint or ""
for pattern, height in footprint_heights.items():
if pattern in footprint.upper():
return height * component.model_scale[2] # Apply Z scaling
# Default height based on model scale
return 2.0 * component.model_scale[2]
def check_clearance_violations(self, components: list[Component3D],
board_dims: BoardDimensions) -> list[dict[str, Any]]:
"""Check for 3D clearance violations between components."""
violations = []
# Component-to-component clearance
for i, comp1 in enumerate(components):
for j, comp2 in enumerate(components[i+1:], i+1):
distance = self._calculate_3d_distance(comp1, comp2)
min_clearance = self._get_minimum_clearance(comp1, comp2)
if distance < min_clearance:
violations.append({
"type": "component_clearance",
"component1": comp1.reference,
"component2": comp2.reference,
"distance": distance,
"required_clearance": min_clearance,
"severity": "warning" if distance > min_clearance * 0.8 else "error"
})
# Board edge clearance
for component in components:
edge_distance = self._distance_to_board_edge(component, board_dims)
min_edge_clearance = 0.5 # 0.5mm minimum edge clearance
if edge_distance < min_edge_clearance:
violations.append({
"type": "board_edge_clearance",
"component": component.reference,
"distance": edge_distance,
"required_clearance": min_edge_clearance,
"severity": "warning"
})
return violations
def _calculate_3d_distance(self, comp1: Component3D, comp2: Component3D) -> float:
"""Calculate 3D distance between two components."""
dx = comp1.position[0] - comp2.position[0]
dy = comp1.position[1] - comp2.position[1]
dz = comp1.position[2] - comp2.position[2]
return (dx*dx + dy*dy + dz*dz) ** 0.5
def _get_minimum_clearance(self, comp1: Component3D, comp2: Component3D) -> float:
"""Get minimum required clearance between components."""
# Base clearance rules (can be made more sophisticated)
base_clearance = 0.2 # 0.2mm base clearance
# Larger clearance for high-power components
if any(keyword in (comp1.value or "") + (comp2.value or "")
for keyword in ["POWER", "REGULATOR", "MOSFET"]):
return base_clearance + 1.0
return base_clearance
def _distance_to_board_edge(self, component: Component3D,
board_dims: BoardDimensions) -> float:
"""Calculate minimum distance from component to board edge."""
if not board_dims.outline_points:
return float('inf')
# Simplified calculation - distance to bounding rectangle
x_coords = [p[0] for p in board_dims.outline_points]
y_coords = [p[1] for p in board_dims.outline_points]
min_x, max_x = min(x_coords), max(x_coords)
min_y, max_y = min(y_coords), max(y_coords)
comp_x, comp_y = component.position[0], component.position[1]
# Distance to each edge
distances = [
comp_x - min_x, # Left edge
max_x - comp_x, # Right edge
comp_y - min_y, # Bottom edge
max_y - comp_y # Top edge
]
return min(distances)
def generate_3d_visualization_data(self) -> dict[str, Any]:
"""Generate data structure for 3D visualization."""
components = self.extract_3d_components()
board_dims = self.analyze_board_dimensions()
height_analysis = self.analyze_component_heights(components)
clearance_violations = self.check_clearance_violations(components, board_dims)
return {
"board_dimensions": {
"width": board_dims.width,
"height": board_dims.height,
"thickness": board_dims.thickness,
"outline": board_dims.outline_points,
"holes": board_dims.holes
},
"components": [
{
"reference": comp.reference,
"position": comp.position,
"rotation": comp.rotation,
"model_path": comp.model_path,
"footprint": comp.footprint,
"value": comp.value,
"estimated_height": self._estimate_component_height(comp)
}
for comp in components
],
"height_analysis": height_analysis,
"clearance_violations": clearance_violations,
"stats": {
"total_components": len(components),
"components_with_3d_models": len([c for c in components if c.model_path]),
"violation_count": len(clearance_violations)
}
}
def perform_mechanical_analysis(self) -> MechanicalAnalysis:
"""Perform comprehensive mechanical analysis."""
components = self.extract_3d_components()
board_dims = self.analyze_board_dimensions()
height_analysis = self.analyze_component_heights(components)
clearance_violations = self.check_clearance_violations(components, board_dims)
# Generate mechanical constraints and warnings
constraints = []
if height_analysis["max"] > 10.0: # 10mm height limit example
constraints.append(f"Board height {height_analysis['max']:.1f}mm exceeds 10mm limit")
if board_dims.width > 100 or board_dims.height > 100:
constraints.append(f"Board dimensions {board_dims.width:.1f}x{board_dims.height:.1f}mm are large")
if len(clearance_violations) > 0:
constraints.append(f"{len(clearance_violations)} clearance violations found")
return MechanicalAnalysis(
board_dimensions=board_dims,
components=components,
clearance_violations=clearance_violations,
height_analysis=height_analysis,
mechanical_constraints=constraints
)
def analyze_pcb_3d_models(pcb_file_path: str) -> dict[str, Any]:
"""Convenience function to analyze 3D models in a PCB file."""
try:
analyzer = Model3DAnalyzer(pcb_file_path)
return analyzer.generate_3d_visualization_data()
except Exception as e:
logger.error(f"Failed to analyze 3D models in {pcb_file_path}: {e}")
return {"error": str(e)}
def get_mechanical_constraints(pcb_file_path: str) -> MechanicalAnalysis:
"""Get mechanical analysis and constraints for a PCB."""
analyzer = Model3DAnalyzer(pcb_file_path)
return analyzer.perform_mechanical_analysis()

View File

@ -1,521 +0,0 @@
"""
KiCad schematic netlist extraction utilities.
"""
from collections import defaultdict
import os
import re
from typing import Any
class SchematicParser:
"""Parser for KiCad schematic files to extract netlist information."""
def __init__(self, schematic_path: str):
"""Initialize the schematic parser.
Args:
schematic_path: Path to the KiCad schematic file (.kicad_sch)
"""
self.schematic_path = schematic_path
self.content = ""
self.components = []
self.labels = []
self.wires = []
self.junctions = []
self.no_connects = []
self.power_symbols = []
self.hierarchical_labels = []
self.global_labels = []
# Netlist information
self.nets = defaultdict(list) # Net name -> connected pins
self.component_pins = {} # (component_ref, pin_num) -> net_name
# Component information
self.component_info = {} # component_ref -> component details
# Load the file
self._load_schematic()
def _load_schematic(self) -> None:
"""Load the schematic file content."""
if not os.path.exists(self.schematic_path):
print(f"Schematic file not found: {self.schematic_path}")
raise FileNotFoundError(f"Schematic file not found: {self.schematic_path}")
try:
with open(self.schematic_path) as f:
self.content = f.read()
print(f"Successfully loaded schematic: {self.schematic_path}")
except Exception as e:
print(f"Error reading schematic file: {str(e)}")
raise
def parse(self) -> dict[str, Any]:
"""Parse the schematic to extract netlist information.
Returns:
Dictionary with parsed netlist information
"""
print("Starting schematic parsing")
# Extract symbols (components)
self._extract_components()
# Extract wires
self._extract_wires()
# Extract junctions
self._extract_junctions()
# Extract labels
self._extract_labels()
# Extract power symbols
self._extract_power_symbols()
# Extract no-connects
self._extract_no_connects()
# Build netlist
self._build_netlist()
# Create result
result = {
"components": self.component_info,
"nets": dict(self.nets),
"labels": self.labels,
"wires": self.wires,
"junctions": self.junctions,
"power_symbols": self.power_symbols,
"component_count": len(self.component_info),
"net_count": len(self.nets),
}
print(
f"Schematic parsing complete: found {len(self.component_info)} components and {len(self.nets)} nets"
)
return result
def _extract_s_expressions(self, pattern: str) -> list[str]:
"""Extract all matching S-expressions from the schematic content.
Args:
pattern: Regex pattern to match the start of S-expressions
Returns:
List of matching S-expressions
"""
matches = []
positions = []
# Find all starting positions of matches
for match in re.finditer(pattern, self.content):
positions.append(match.start())
# Extract full S-expressions for each match
for pos in positions:
# Start from the matching position
current_pos = pos
depth = 0
s_exp = ""
# Extract the full S-expression by tracking parentheses
while current_pos < len(self.content):
char = self.content[current_pos]
s_exp += char
if char == "(":
depth += 1
elif char == ")":
depth -= 1
if depth == 0:
# Found the end of the S-expression
break
current_pos += 1
matches.append(s_exp)
return matches
def _extract_components(self) -> None:
"""Extract component information from schematic."""
print("Extracting components")
# Extract all symbol expressions (components)
symbols = self._extract_s_expressions(r"\(symbol\s+")
for symbol in symbols:
component = self._parse_component(symbol)
if component:
self.components.append(component)
# Add to component info dictionary
ref = component.get("reference", "Unknown")
self.component_info[ref] = component
print(f"Extracted {len(self.components)} components")
def _parse_component(self, symbol_expr: str) -> dict[str, Any]:
"""Parse a component from a symbol S-expression.
Args:
symbol_expr: Symbol S-expression
Returns:
Component information dictionary
"""
component = {}
# Extract library component ID
lib_id_match = re.search(r'\(lib_id\s+"([^"]+)"\)', symbol_expr)
if lib_id_match:
component["lib_id"] = lib_id_match.group(1)
# Extract reference (e.g., R1, C2)
property_matches = re.finditer(r'\(property\s+"([^"]+)"\s+"([^"]+)"', symbol_expr)
for match in property_matches:
prop_name = match.group(1)
prop_value = match.group(2)
if prop_name == "Reference":
component["reference"] = prop_value
elif prop_name == "Value":
component["value"] = prop_value
elif prop_name == "Footprint":
component["footprint"] = prop_value
else:
# Store other properties
if "properties" not in component:
component["properties"] = {}
component["properties"][prop_name] = prop_value
# Extract position
pos_match = re.search(r"\(at\s+([\d\.-]+)\s+([\d\.-]+)(\s+[\d\.-]+)?\)", symbol_expr)
if pos_match:
component["position"] = {
"x": float(pos_match.group(1)),
"y": float(pos_match.group(2)),
"angle": float(pos_match.group(3).strip() if pos_match.group(3) else 0),
}
# Extract pins
pins = []
pin_matches = re.finditer(
r'\(pin\s+\(num\s+"([^"]+)"\)\s+\(name\s+"([^"]+)"\)', symbol_expr
)
for match in pin_matches:
pin_num = match.group(1)
pin_name = match.group(2)
pins.append({"num": pin_num, "name": pin_name})
if pins:
component["pins"] = pins
return component
def _extract_wires(self) -> None:
"""Extract wire information from schematic."""
print("Extracting wires")
# Extract all wire expressions
wires = self._extract_s_expressions(r"\(wire\s+")
for wire in wires:
# Extract the wire coordinates
pts_match = re.search(
r"\(pts\s+\(xy\s+([\d\.-]+)\s+([\d\.-]+)\)\s+\(xy\s+([\d\.-]+)\s+([\d\.-]+)\)\)",
wire,
)
if pts_match:
self.wires.append(
{
"start": {"x": float(pts_match.group(1)), "y": float(pts_match.group(2))},
"end": {"x": float(pts_match.group(3)), "y": float(pts_match.group(4))},
}
)
print(f"Extracted {len(self.wires)} wires")
def _extract_junctions(self) -> None:
"""Extract junction information from schematic."""
print("Extracting junctions")
# Extract all junction expressions
junctions = self._extract_s_expressions(r"\(junction\s+")
for junction in junctions:
# Extract the junction coordinates
xy_match = re.search(r"\(junction\s+\(xy\s+([\d\.-]+)\s+([\d\.-]+)\)\)", junction)
if xy_match:
self.junctions.append(
{"x": float(xy_match.group(1)), "y": float(xy_match.group(2))}
)
print(f"Extracted {len(self.junctions)} junctions")
def _extract_labels(self) -> None:
"""Extract label information from schematic."""
print("Extracting labels")
# Extract local labels
local_labels = self._extract_s_expressions(r"\(label\s+")
for label in local_labels:
# Extract label text and position
label_match = re.search(
r'\(label\s+"([^"]+)"\s+\(at\s+([\d\.-]+)\s+([\d\.-]+)(\s+[\d\.-]+)?\)', label
)
if label_match:
self.labels.append(
{
"type": "local",
"text": label_match.group(1),
"position": {
"x": float(label_match.group(2)),
"y": float(label_match.group(3)),
"angle": float(
label_match.group(4).strip() if label_match.group(4) else 0
),
},
}
)
# Extract global labels
global_labels = self._extract_s_expressions(r"\(global_label\s+")
for label in global_labels:
# Extract global label text and position
label_match = re.search(
r'\(global_label\s+"([^"]+)"\s+\(shape\s+([^\s\)]+)\)\s+\(at\s+([\d\.-]+)\s+([\d\.-]+)(\s+[\d\.-]+)?\)',
label,
)
if label_match:
self.global_labels.append(
{
"type": "global",
"text": label_match.group(1),
"shape": label_match.group(2),
"position": {
"x": float(label_match.group(3)),
"y": float(label_match.group(4)),
"angle": float(
label_match.group(5).strip() if label_match.group(5) else 0
),
},
}
)
# Extract hierarchical labels
hierarchical_labels = self._extract_s_expressions(r"\(hierarchical_label\s+")
for label in hierarchical_labels:
# Extract hierarchical label text and position
label_match = re.search(
r'\(hierarchical_label\s+"([^"]+)"\s+\(shape\s+([^\s\)]+)\)\s+\(at\s+([\d\.-]+)\s+([\d\.-]+)(\s+[\d\.-]+)?\)',
label,
)
if label_match:
self.hierarchical_labels.append(
{
"type": "hierarchical",
"text": label_match.group(1),
"shape": label_match.group(2),
"position": {
"x": float(label_match.group(3)),
"y": float(label_match.group(4)),
"angle": float(
label_match.group(5).strip() if label_match.group(5) else 0
),
},
}
)
print(
f"Extracted {len(self.labels)} local labels, {len(self.global_labels)} global labels, and {len(self.hierarchical_labels)} hierarchical labels"
)
def _extract_power_symbols(self) -> None:
"""Extract power symbol information from schematic."""
print("Extracting power symbols")
# Extract all power symbol expressions
power_symbols = self._extract_s_expressions(r'\(symbol\s+\(lib_id\s+"power:')
for symbol in power_symbols:
# Extract power symbol type and position
type_match = re.search(r'\(lib_id\s+"power:([^"]+)"\)', symbol)
pos_match = re.search(r"\(at\s+([\d\.-]+)\s+([\d\.-]+)(\s+[\d\.-]+)?\)", symbol)
if type_match and pos_match:
self.power_symbols.append(
{
"type": type_match.group(1),
"position": {
"x": float(pos_match.group(1)),
"y": float(pos_match.group(2)),
"angle": float(pos_match.group(3).strip() if pos_match.group(3) else 0),
},
}
)
print(f"Extracted {len(self.power_symbols)} power symbols")
def _extract_no_connects(self) -> None:
"""Extract no-connect information from schematic."""
print("Extracting no-connects")
# Extract all no-connect expressions
no_connects = self._extract_s_expressions(r"\(no_connect\s+")
for no_connect in no_connects:
# Extract the no-connect coordinates
xy_match = re.search(r"\(no_connect\s+\(at\s+([\d\.-]+)\s+([\d\.-]+)\)", no_connect)
if xy_match:
self.no_connects.append(
{"x": float(xy_match.group(1)), "y": float(xy_match.group(2))}
)
print(f"Extracted {len(self.no_connects)} no-connects")
def _build_netlist(self) -> None:
"""Build the netlist from extracted components and connections."""
print("Building netlist from schematic data")
# TODO: Implement netlist building algorithm
# This is a complex task that involves:
# 1. Tracking connections between components via wires
# 2. Handling labels (local, global, hierarchical)
# 3. Processing power symbols
# 4. Resolving junctions
# For now, we'll implement a basic version that creates a list of nets
# based on component references and pin numbers
# Process global labels as nets
for label in self.global_labels:
net_name = label["text"]
self.nets[net_name] = [] # Initialize empty list for this net
# Process power symbols as nets
for power in self.power_symbols:
net_name = power["type"]
if net_name not in self.nets:
self.nets[net_name] = []
# In a full implementation, we would now trace connections between
# components, but that requires a more complex algorithm to follow wires
# and detect connected pins
# For demonstration, we'll add a placeholder note
print("Note: Full netlist building requires complex connectivity tracing")
print(f"Found {len(self.nets)} potential nets from labels and power symbols")
def extract_netlist(schematic_path: str) -> dict[str, Any]:
"""Extract netlist information from a KiCad schematic file.
Args:
schematic_path: Path to the KiCad schematic file (.kicad_sch)
Returns:
Dictionary with netlist information
"""
try:
parser = SchematicParser(schematic_path)
return parser.parse()
except Exception as e:
print(f"Error extracting netlist: {str(e)}")
return {"error": str(e), "components": {}, "nets": {}, "component_count": 0, "net_count": 0}
def parse_netlist_file(schematic_path: str) -> dict[str, Any]:
"""Parse a KiCad schematic file and extract netlist data.
This is the main interface function used by AI tools for circuit analysis.
Args:
schematic_path: Path to the KiCad schematic file (.kicad_sch)
Returns:
Dictionary containing:
- components: List of component dictionaries with reference, value, etc.
- nets: Dictionary of net names and connected components
- component_count: Total number of components
- net_count: Total number of nets
"""
try:
# Extract raw netlist data
netlist_data = extract_netlist(schematic_path)
# Convert components dict to list format expected by AI tools
components = []
for ref, component_info in netlist_data.get("components", {}).items():
component = {
"reference": ref,
"value": component_info.get("value", ""),
"footprint": component_info.get("footprint", ""),
"lib_id": component_info.get("lib_id", ""),
}
# Add any additional properties
if "properties" in component_info:
component.update(component_info["properties"])
components.append(component)
return {
"components": components,
"nets": netlist_data.get("nets", {}),
"component_count": len(components),
"net_count": len(netlist_data.get("nets", {})),
"labels": netlist_data.get("labels", []),
"power_symbols": netlist_data.get("power_symbols", [])
}
except Exception as e:
print(f"Error parsing netlist file: {str(e)}")
return {
"components": [],
"nets": {},
"component_count": 0,
"net_count": 0,
"error": str(e)
}
def analyze_netlist(netlist_data: dict[str, Any]) -> dict[str, Any]:
"""Analyze netlist data to provide insights.
Args:
netlist_data: Dictionary with netlist information
Returns:
Dictionary with analysis results
"""
results = {
"component_count": netlist_data.get("component_count", 0),
"net_count": netlist_data.get("net_count", 0),
"component_types": defaultdict(int),
"power_nets": [],
}
# Analyze component types
for ref, component in netlist_data.get("components", {}).items():
# Extract component type from reference (e.g., R1 -> R)
comp_type = re.match(r"^([A-Za-z_]+)", ref)
if comp_type:
results["component_types"][comp_type.group(1)] += 1
# Identify power nets
for net_name in netlist_data.get("nets", {}):
if any(
net_name.startswith(prefix) for prefix in ["VCC", "VDD", "GND", "+5V", "+3V3", "+12V"]
):
results["power_nets"].append(net_name)
# Count pin connections
total_pins = sum(len(pins) for pins in netlist_data.get("nets", {}).values())
results["total_pin_connections"] = total_pins
return results

File diff suppressed because it is too large Load Diff

View File

@ -1,544 +0,0 @@
"""
Symbol Library Management utilities for KiCad.
Provides functionality to analyze, manage, and manipulate KiCad symbol libraries
including library validation, symbol extraction, and library organization.
"""
from dataclasses import dataclass
import logging
import os
import re
from typing import Any
logger = logging.getLogger(__name__)
@dataclass
class SymbolPin:
"""Represents a symbol pin with electrical and geometric properties."""
number: str
name: str
position: tuple[float, float]
orientation: str # "L", "R", "U", "D"
electrical_type: str # "input", "output", "bidirectional", "power_in", etc.
graphic_style: str # "line", "inverted", "clock", etc.
length: float = 2.54 # Default pin length in mm
@dataclass
class SymbolProperty:
"""Symbol property like reference, value, footprint, etc."""
name: str
value: str
position: tuple[float, float]
rotation: float = 0.0
visible: bool = True
justify: str = "left"
@dataclass
class SymbolGraphics:
"""Graphical elements of a symbol."""
rectangles: list[dict[str, Any]]
circles: list[dict[str, Any]]
arcs: list[dict[str, Any]]
polylines: list[dict[str, Any]]
text: list[dict[str, Any]]
@dataclass
class Symbol:
"""Represents a KiCad symbol with all its properties."""
name: str
library_id: str
description: str
keywords: list[str]
pins: list[SymbolPin]
properties: list[SymbolProperty]
graphics: SymbolGraphics
footprint_filters: list[str]
aliases: list[str] = None
power_symbol: bool = False
extends: str | None = None # For derived symbols
@dataclass
class SymbolLibrary:
"""Represents a KiCad symbol library (.kicad_sym file)."""
name: str
file_path: str
version: str
symbols: list[Symbol]
metadata: dict[str, Any]
class SymbolLibraryAnalyzer:
"""Analyzer for KiCad symbol libraries."""
def __init__(self):
"""Initialize the symbol library analyzer."""
self.libraries = {}
self.symbol_cache = {}
def load_library(self, library_path: str) -> SymbolLibrary:
"""Load a KiCad symbol library file."""
try:
with open(library_path, encoding='utf-8') as f:
content = f.read()
# Parse library header
library_name = os.path.basename(library_path).replace('.kicad_sym', '')
version = self._extract_version(content)
# Parse symbols
symbols = self._parse_symbols(content)
library = SymbolLibrary(
name=library_name,
file_path=library_path,
version=version,
symbols=symbols,
metadata=self._extract_metadata(content)
)
self.libraries[library_name] = library
logger.info(f"Loaded library '{library_name}' with {len(symbols)} symbols")
return library
except Exception as e:
logger.error(f"Failed to load library {library_path}: {e}")
raise
def _extract_version(self, content: str) -> str:
"""Extract version from library content."""
version_match = re.search(r'\(version\s+(\d+)\)', content)
return version_match.group(1) if version_match else "unknown"
def _extract_metadata(self, content: str) -> dict[str, Any]:
"""Extract library metadata."""
metadata = {}
# Extract generator info
generator_match = re.search(r'\(generator\s+"([^"]+)"\)', content)
if generator_match:
metadata["generator"] = generator_match.group(1)
return metadata
def _parse_symbols(self, content: str) -> list[Symbol]:
"""Parse symbols from library content."""
symbols = []
# Find all symbol definitions
symbol_pattern = r'\(symbol\s+"([^"]+)"[^)]*\)'
symbol_matches = []
# Use a more sophisticated parser to handle nested parentheses
level = 0
current_symbol = None
symbol_start = 0
for i, char in enumerate(content):
if char == '(':
if level == 0 and content[i:i+8] == '(symbol ':
symbol_start = i
level += 1
elif char == ')':
level -= 1
if level == 0 and current_symbol is not None:
symbol_content = content[symbol_start:i+1]
symbol = self._parse_single_symbol(symbol_content)
if symbol:
symbols.append(symbol)
current_symbol = None
# Check if we're starting a symbol
if level == 1 and content[i:i+8] == '(symbol ' and current_symbol is None:
# Extract symbol name
name_match = re.search(r'\(symbol\s+"([^"]+)"', content[i:i+100])
if name_match:
current_symbol = name_match.group(1)
logger.info(f"Parsed {len(symbols)} symbols from library")
return symbols
def _parse_single_symbol(self, symbol_content: str) -> Symbol | None:
"""Parse a single symbol definition."""
try:
# Extract symbol name
name_match = re.search(r'\(symbol\s+"([^"]+)"', symbol_content)
if not name_match:
return None
name = name_match.group(1)
# Parse basic properties
description = self._extract_property(symbol_content, "description") or ""
keywords = self._extract_keywords(symbol_content)
# Parse pins
pins = self._parse_pins(symbol_content)
# Parse properties
properties = self._parse_properties(symbol_content)
# Parse graphics
graphics = self._parse_graphics(symbol_content)
# Parse footprint filters
footprint_filters = self._parse_footprint_filters(symbol_content)
# Check if it's a power symbol
power_symbol = "(power)" in symbol_content
# Check for extends (derived symbols)
extends_match = re.search(r'\(extends\s+"([^"]+)"\)', symbol_content)
extends = extends_match.group(1) if extends_match else None
return Symbol(
name=name,
library_id=name, # Will be updated with library prefix
description=description,
keywords=keywords,
pins=pins,
properties=properties,
graphics=graphics,
footprint_filters=footprint_filters,
aliases=[],
power_symbol=power_symbol,
extends=extends
)
except Exception as e:
logger.error(f"Failed to parse symbol: {e}")
return None
def _extract_property(self, content: str, prop_name: str) -> str | None:
"""Extract a property value from symbol content."""
pattern = f'\\(property\\s+"{prop_name}"\\s+"([^"]*)"'
match = re.search(pattern, content)
return match.group(1) if match else None
def _extract_keywords(self, content: str) -> list[str]:
"""Extract keywords from symbol content."""
keywords_match = re.search(r'\(keywords\s+"([^"]*)"\)', content)
if keywords_match:
return [k.strip() for k in keywords_match.group(1).split() if k.strip()]
return []
def _parse_pins(self, content: str) -> list[SymbolPin]:
"""Parse pins from symbol content."""
pins = []
# Pin pattern - matches KiCad 6+ format
pin_pattern = r'\(pin\s+(\w+)\s+(\w+)\s+\(at\s+([-\d.]+)\s+([-\d.]+)\s+(\d+)\)\s+\(length\s+([-\d.]+)\)[^)]*\(name\s+"([^"]*)"\s+[^)]*\)\s+\(number\s+"([^"]*)"\s+[^)]*\)'
for match in re.finditer(pin_pattern, content):
electrical_type = match.group(1)
graphic_style = match.group(2)
x = float(match.group(3))
y = float(match.group(4))
orientation_angle = int(match.group(5))
length = float(match.group(6))
pin_name = match.group(7)
pin_number = match.group(8)
# Convert angle to orientation
orientation_map = {0: "R", 90: "U", 180: "L", 270: "D"}
orientation = orientation_map.get(orientation_angle, "R")
pin = SymbolPin(
number=pin_number,
name=pin_name,
position=(x, y),
orientation=orientation,
electrical_type=electrical_type,
graphic_style=graphic_style,
length=length
)
pins.append(pin)
return pins
def _parse_properties(self, content: str) -> list[SymbolProperty]:
"""Parse symbol properties."""
properties = []
# Property pattern
prop_pattern = r'\(property\s+"([^"]+)"\s+"([^"]*)"\s+\(at\s+([-\d.]+)\s+([-\d.]+)\s+([-\d.]+)\)'
for match in re.finditer(prop_pattern, content):
name = match.group(1)
value = match.group(2)
x = float(match.group(3))
y = float(match.group(4))
rotation = float(match.group(5))
prop = SymbolProperty(
name=name,
value=value,
position=(x, y),
rotation=rotation
)
properties.append(prop)
return properties
def _parse_graphics(self, content: str) -> SymbolGraphics:
"""Parse graphical elements from symbol."""
rectangles = []
circles = []
arcs = []
polylines = []
text = []
# Parse rectangles
rect_pattern = r'\(rectangle\s+\(start\s+([-\d.]+)\s+([-\d.]+)\)\s+\(end\s+([-\d.]+)\s+([-\d.]+)\)'
for match in re.finditer(rect_pattern, content):
rectangles.append({
"start": (float(match.group(1)), float(match.group(2))),
"end": (float(match.group(3)), float(match.group(4)))
})
# Parse circles
circle_pattern = r'\(circle\s+\(center\s+([-\d.]+)\s+([-\d.]+)\)\s+\(radius\s+([-\d.]+)\)'
for match in re.finditer(circle_pattern, content):
circles.append({
"center": (float(match.group(1)), float(match.group(2))),
"radius": float(match.group(3))
})
# Parse polylines (simplified)
poly_pattern = r'\(polyline[^)]*\(pts[^)]+\)'
polylines = [{"data": match.group(0)} for match in re.finditer(poly_pattern, content)]
return SymbolGraphics(
rectangles=rectangles,
circles=circles,
arcs=arcs,
polylines=polylines,
text=text
)
def _parse_footprint_filters(self, content: str) -> list[str]:
"""Parse footprint filters from symbol."""
filters = []
# Look for footprint filter section
fp_filter_match = re.search(r'\(fp_filters[^)]*\)', content, re.DOTALL)
if fp_filter_match:
filter_content = fp_filter_match.group(0)
filter_pattern = r'"([^"]+)"'
filters = [match.group(1) for match in re.finditer(filter_pattern, filter_content)]
return filters
def analyze_library_coverage(self, library: SymbolLibrary) -> dict[str, Any]:
"""Analyze symbol library coverage and statistics."""
analysis = {
"total_symbols": len(library.symbols),
"categories": {},
"electrical_types": {},
"pin_counts": {},
"missing_properties": [],
"duplicate_symbols": [],
"unused_symbols": [],
"statistics": {}
}
# Analyze by categories (based on keywords/names)
categories = {}
electrical_types = {}
pin_counts = {}
for symbol in library.symbols:
# Categorize by keywords
for keyword in symbol.keywords:
categories[keyword] = categories.get(keyword, 0) + 1
# Count pin types
for pin in symbol.pins:
electrical_types[pin.electrical_type] = electrical_types.get(pin.electrical_type, 0) + 1
# Pin count distribution
pin_count = len(symbol.pins)
pin_counts[pin_count] = pin_counts.get(pin_count, 0) + 1
# Check for missing essential properties
essential_props = ["Reference", "Value", "Footprint"]
symbol_props = [p.name for p in symbol.properties]
for prop in essential_props:
if prop not in symbol_props:
analysis["missing_properties"].append({
"symbol": symbol.name,
"missing_property": prop
})
analysis.update({
"categories": categories,
"electrical_types": electrical_types,
"pin_counts": pin_counts,
"statistics": {
"avg_pins_per_symbol": sum(pin_counts.keys()) / len(library.symbols) if library.symbols else 0,
"most_common_category": max(categories.items(), key=lambda x: x[1])[0] if categories else None,
"symbols_with_footprint_filters": len([s for s in library.symbols if s.footprint_filters]),
"power_symbols": len([s for s in library.symbols if s.power_symbol])
}
})
return analysis
def find_similar_symbols(self, symbol: Symbol, library: SymbolLibrary,
threshold: float = 0.7) -> list[tuple[Symbol, float]]:
"""Find symbols similar to the given symbol."""
similar = []
for candidate in library.symbols:
if candidate.name == symbol.name:
continue
similarity = self._calculate_symbol_similarity(symbol, candidate)
if similarity >= threshold:
similar.append((candidate, similarity))
return sorted(similar, key=lambda x: x[1], reverse=True)
def _calculate_symbol_similarity(self, symbol1: Symbol, symbol2: Symbol) -> float:
"""Calculate similarity score between two symbols."""
score = 0.0
factors = 0
# Pin count similarity
if symbol1.pins and symbol2.pins:
pin_diff = abs(len(symbol1.pins) - len(symbol2.pins))
max_pins = max(len(symbol1.pins), len(symbol2.pins))
pin_similarity = 1.0 - (pin_diff / max_pins) if max_pins > 0 else 1.0
score += pin_similarity * 0.4
factors += 0.4
# Keyword similarity
keywords1 = set(symbol1.keywords)
keywords2 = set(symbol2.keywords)
if keywords1 or keywords2:
keyword_intersection = len(keywords1.intersection(keywords2))
keyword_union = len(keywords1.union(keywords2))
keyword_similarity = keyword_intersection / keyword_union if keyword_union > 0 else 0.0
score += keyword_similarity * 0.3
factors += 0.3
# Name similarity (simple string comparison)
name_similarity = self._string_similarity(symbol1.name, symbol2.name)
score += name_similarity * 0.3
factors += 0.3
return score / factors if factors > 0 else 0.0
def _string_similarity(self, str1: str, str2: str) -> float:
"""Calculate string similarity using simple character overlap."""
if not str1 or not str2:
return 0.0
str1_lower = str1.lower()
str2_lower = str2.lower()
# Simple character-based similarity
intersection = len(set(str1_lower).intersection(set(str2_lower)))
union = len(set(str1_lower).union(set(str2_lower)))
return intersection / union if union > 0 else 0.0
def validate_symbol(self, symbol: Symbol) -> list[str]:
"""Validate a symbol and return list of issues."""
issues = []
# Check for essential properties
prop_names = [p.name for p in symbol.properties]
essential_props = ["Reference", "Value"]
for prop in essential_props:
if prop not in prop_names:
issues.append(f"Missing essential property: {prop}")
# Check pin consistency
pin_numbers = [p.number for p in symbol.pins]
if len(pin_numbers) != len(set(pin_numbers)):
issues.append("Duplicate pin numbers found")
# Check for pins without names
unnamed_pins = [p.number for p in symbol.pins if not p.name]
if unnamed_pins:
issues.append(f"Pins without names: {', '.join(unnamed_pins)}")
# Validate electrical types
valid_types = ["input", "output", "bidirectional", "tri_state", "passive",
"free", "unspecified", "power_in", "power_out", "open_collector",
"open_emitter", "no_connect"]
for pin in symbol.pins:
if pin.electrical_type not in valid_types:
issues.append(f"Invalid electrical type '{pin.electrical_type}' for pin {pin.number}")
return issues
def export_symbol_report(self, library: SymbolLibrary) -> dict[str, Any]:
"""Export a comprehensive symbol library report."""
analysis = self.analyze_library_coverage(library)
# Add validation results
validation_results = []
for symbol in library.symbols:
issues = self.validate_symbol(symbol)
if issues:
validation_results.append({
"symbol": symbol.name,
"issues": issues
})
return {
"library_info": {
"name": library.name,
"file_path": library.file_path,
"version": library.version,
"total_symbols": len(library.symbols)
},
"analysis": analysis,
"validation": {
"total_issues": len(validation_results),
"symbols_with_issues": len(validation_results),
"issues_by_symbol": validation_results
},
"recommendations": self._generate_recommendations(library, analysis, validation_results)
}
def _generate_recommendations(self, library: SymbolLibrary,
analysis: dict[str, Any],
validation_results: list[dict[str, Any]]) -> list[str]:
"""Generate recommendations for library improvement."""
recommendations = []
# Check for missing footprint filters
no_filters = [s for s in library.symbols if not s.footprint_filters]
if len(no_filters) > len(library.symbols) * 0.5:
recommendations.append("Consider adding footprint filters to more symbols for better component matching")
# Check for validation issues
if validation_results:
recommendations.append(f"Address {len(validation_results)} symbols with validation issues")
# Check pin distribution
if analysis["statistics"]["avg_pins_per_symbol"] > 50:
recommendations.append("Library contains many high-pin-count symbols - consider splitting complex symbols")
# Check category distribution
if len(analysis["categories"]) < 5:
recommendations.append("Consider adding more keyword categories for better symbol organization")
return recommendations
def create_symbol_analyzer() -> SymbolLibraryAnalyzer:
"""Create and initialize a symbol library analyzer."""
return SymbolLibraryAnalyzer()

View File

@ -1,26 +0,0 @@
"""
Utility for managing temporary directories.
"""
# List of temporary directories to clean up
_temp_dirs: list[str] = []
def register_temp_dir(temp_dir: str) -> None:
"""Register a temporary directory for cleanup.
Args:
temp_dir: Path to the temporary directory
"""
if temp_dir not in _temp_dirs:
_temp_dirs.append(temp_dir)
def get_temp_dirs() -> list[str]:
"""Get all registered temporary directories.
Returns:
List of temporary directory paths
"""
return _temp_dirs.copy()

View File

@ -4,124 +4,62 @@ build-backend = "hatchling.build"
[project]
name = "mckicad"
version = "0.1.0"
description = "Model Context Protocol (MCP) server for KiCad electronic design automation (EDA) files"
version = "2026.03.03"
description = "MCP server for KiCad electronic design automation"
readme = "README.md"
license = { text = "MIT" }
authors = [
{ name = "KiCad MCP Contributors" }
]
maintainers = [
{ name = "KiCad MCP Contributors" }
]
keywords = [
"kicad",
"eda",
"electronics",
"schematic",
"pcb",
"mcp",
"model-context-protocol",
"ai",
"assistant"
]
authors = [{ name = "Ryan Malloy", email = "ryan@supported.systems" }]
keywords = ["kicad", "eda", "electronics", "pcb", "mcp", "model-context-protocol"]
classifiers = [
"Development Status :: 4 - Beta",
"Intended Audience :: Developers",
"Intended Audience :: Manufacturing",
"License :: OSI Approved :: MIT License",
"Operating System :: OS Independent",
"Programming Language :: Python :: 3",
"Programming Language :: Python :: 3.12",
"Programming Language :: Python :: 3.13",
"Topic :: Scientific/Engineering :: Electronic Design Automation (EDA)",
"Topic :: Software Development :: Libraries :: Python Modules",
"Typing :: Typed"
"Typing :: Typed",
]
requires-python = ">=3.12"
dependencies = [
"mcp[cli]>=1.26.0",
"fastmcp>=2.14.5",
"pandas>=2.3.3",
"fastmcp>=3.1.0",
"pyyaml>=6.0.3",
"defusedxml>=0.7.1", # Secure XML parsing
"kicad-python>=0.5.0", # KiCad IPC API bindings
"requests>=2.32.5", # HTTP client for FreeRouting integration
"defusedxml>=0.7.1",
"kicad-python>=0.5.0",
"kicad-sch-api>=0.5.6",
"requests>=2.32.5",
]
[project.urls]
"Homepage" = "https://github.com/lamaalrajih/mckicad"
"Bug Tracker" = "https://github.com/lamaalrajih/mckicad/issues"
"Documentation" = "https://github.com/lamaalrajih/mckicad#readme"
Homepage = "https://git.supported.systems/warehack.ing/mckicad"
[project.scripts]
mckicad = "mckicad.server:main"
[tool.hatch.build.targets.wheel]
packages = ["src/mckicad"]
[dependency-groups]
dev = [
"pytest>=8.4.2",
"pytest-asyncio>=1.3.0",
"pytest-mock>=3.15.1",
"pytest-cov>=7.0.0",
"pytest-xdist>=3.8.0",
"ruff>=0.15.1",
"mypy>=1.19.1",
"pre-commit>=4.5.1",
"bandit>=1.9.3", # Security linting for pre-commit hooks
]
docs = [
"sphinx>=9.1.0",
"sphinx-rtd-theme>=3.1.0",
"myst-parser>=5.0.0",
]
security = [
"bandit>=1.9.3",
"safety>=3.7.0",
]
performance = [
"memory-profiler>=0.61.0",
"py-spy>=0.3.0",
]
visualization = [
"cairosvg>=2.8.2", # SVG to PNG conversion
"Pillow>=12.1.1", # Image processing
]
[tool.ruff]
target-version = "py312"
line-length = 100
src = ["src", "tests"]
[tool.ruff.lint]
select = [
"E", # pycodestyle errors
"W", # pycodestyle warnings
"F", # pyflakes
"I", # isort
"B", # flake8-bugbear
"C4", # flake8-comprehensions
"UP", # pyupgrade
"SIM", # flake8-simplify
"UP", # pyupgrade
]
ignore = [
"E501", # line too long, handled by ruff format
"B008", # do not perform function calls in argument defaults
"C901", # too complex (handled by other tools)
"B905", # zip() without an explicit strict= parameter
]
unfixable = [
"B", # Avoid trying to fix flake8-bugbear violations
]
select = ["E", "W", "F", "I", "B", "C4", "UP", "SIM"]
ignore = ["E501", "B008", "C901", "B905"]
unfixable = ["B"]
[tool.ruff.lint.per-file-ignores]
"tests/**/*.py" = [
"S101", # Use of assert detected
"D103", # Missing docstring in public function
"SLF001", # Private member accessed
]
"mckicad/config.py" = [
"E501", # Long lines in config are ok
]
"tests/**/*.py" = ["S101", "D103", "SLF001"]
[tool.ruff.lint.isort]
known-first-party = ["mckicad"]
@ -130,105 +68,49 @@ force-sort-within-sections = true
[tool.ruff.format]
quote-style = "double"
indent-style = "space"
skip-magic-trailing-comma = false
line-ending = "auto"
[tool.mypy]
python_version = "3.12"
warn_return_any = true
warn_unused_configs = true
disallow_untyped_defs = false
disallow_incomplete_defs = false
check_untyped_defs = true
disallow_untyped_decorators = false
no_implicit_optional = true
warn_redundant_casts = true
warn_unused_ignores = true
warn_no_return = true
warn_unreachable = true
strict_equality = true
show_error_codes = true
[[tool.mypy.overrides]]
module = [
"pandas.*",
"mcp.*",
"kipy.*", # KiCad Python bindings
"requests.*",
]
module = ["kipy.*", "kicad_sch_api.*", "requests.*"]
ignore_missing_imports = true
[tool.pytest.ini_options]
minversion = "7.0"
addopts = [
"--strict-markers",
"--strict-config",
"--cov=mckicad",
"--cov-report=term-missing",
"--cov-report=html:htmlcov",
"--cov-report=xml",
"--cov-fail-under=80",
"-ra",
"--tb=short",
]
testpaths = ["tests"]
python_files = ["test_*.py"]
python_classes = ["Test*"]
python_functions = ["test_*"]
markers = [
"unit: Unit tests",
"integration: Integration tests",
"slow: Tests that take more than a few seconds",
"requires_kicad: Tests that require KiCad CLI to be installed",
"performance: Performance benchmarking tests",
]
asyncio_mode = "auto"
filterwarnings = [
"ignore::DeprecationWarning",
"ignore::PendingDeprecationWarning",
"ignore::RuntimeWarning:asyncio",
]
[tool.coverage.run]
source = ["mckicad"]
branch = true
omit = [
"tests/*",
"mckicad/__init__.py",
"*/migrations/*",
"*/venv/*",
"*/.venv/*",
]
omit = ["tests/*"]
[tool.coverage.report]
precision = 2
show_missing = true
skip_covered = false
exclude_lines = [
"pragma: no cover",
"def __repr__",
"if self.debug:",
"if settings.DEBUG",
"raise AssertionError",
"raise NotImplementedError",
"if 0:",
"if __name__ == .__main__.:",
"class .*\\bProtocol\\):",
"@(abc\\.)?abstractmethod",
]
[tool.bandit]
exclude_dirs = ["tests", "build", "dist"]
skips = ["B101", "B601", "B404", "B603", "B110", "B112"] # Skip low-severity subprocess and exception handling warnings
[tool.bandit.assert_used]
skips = ["*_test.py", "*/test_*.py"]
[tool.setuptools.packages.find]
where = ["."]
include = ["mckicad*"]
exclude = ["tests*", "docs*"]
[tool.setuptools.package-data]
"mckicad" = ["prompts/*.txt", "resources/**/*.json"]

View File

@ -1,61 +0,0 @@
#!/usr/bin/env python3
"""
Test runner for KiCad MCP project.
"""
import subprocess
import sys
from pathlib import Path
def run_command(cmd: list[str], description: str) -> int:
"""Run a command and return the exit code."""
print(f"\n🔍 {description}")
print(f"Running: {' '.join(cmd)}")
try:
result = subprocess.run(cmd, check=False)
if result.returncode == 0:
print(f"{description} passed")
else:
print(f"{description} failed with exit code {result.returncode}")
return result.returncode
except FileNotFoundError:
print(f"❌ Command not found: {cmd[0]}")
return 1
def main():
"""Run all tests and checks."""
project_root = Path(__file__).parent
# Change to project directory
import os
os.chdir(project_root)
exit_code = 0
# Run linting
exit_code |= run_command(["uv", "run", "ruff", "check", "mckicad/", "tests/"], "Lint check")
# Run formatting check
exit_code |= run_command(
["uv", "run", "ruff", "format", "--check", "mckicad/", "tests/"], "Format check"
)
# Run type checking
exit_code |= run_command(["uv", "run", "mypy", "mckicad/"], "Type check")
# Run tests
exit_code |= run_command(["uv", "run", "python", "-m", "pytest", "tests/", "-v"], "Unit tests")
if exit_code == 0:
print("\n🎉 All checks passed!")
else:
print(f"\n💥 Some checks failed (exit code: {exit_code})")
return exit_code
if __name__ == "__main__":
sys.exit(main())

3
src/mckicad/__init__.py Normal file
View File

@ -0,0 +1,3 @@
"""mckicad - MCP server for KiCad electronic design automation."""
__version__ = "2026.03.03"

128
src/mckicad/config.py Normal file
View File

@ -0,0 +1,128 @@
"""
Configuration for the mckicad MCP server.
All config is accessed via functions to avoid module-level os.environ.get()
race conditions with .env loading.
"""
import os
import platform
def get_system() -> str:
"""Get the current operating system name."""
return platform.system()
def get_kicad_user_dir() -> str:
"""Get KiCad user documents directory, respecting env override."""
env_val = os.environ.get("KICAD_USER_DIR")
if env_val:
return os.path.expanduser(env_val)
system = get_system()
if system == "Darwin" or system == "Windows":
return os.path.expanduser("~/Documents/KiCad")
elif system == "Linux":
return os.path.expanduser("~/KiCad")
return os.path.expanduser("~/Documents/KiCad")
def get_kicad_app_path() -> str:
"""Get KiCad application installation path, respecting env override."""
env_val = os.environ.get("KICAD_APP_PATH")
if env_val:
return env_val
_app_paths = {
"Darwin": "/Applications/KiCad/KiCad.app",
"Windows": r"C:\Program Files\KiCad",
"Linux": "/usr/share/kicad",
}
return _app_paths.get(get_system(), "/Applications/KiCad/KiCad.app")
def get_search_paths() -> list[str]:
"""Read KICAD_SEARCH_PATHS from env, expand ~, filter to existing dirs."""
paths: list[str] = []
env_val = os.environ.get("KICAD_SEARCH_PATHS", "")
if env_val:
for p in env_val.split(","):
expanded = os.path.expanduser(p.strip())
if os.path.isdir(expanded) and expanded not in paths:
paths.append(expanded)
# Auto-detect common project locations
default_locations = [
"~/Documents/PCB",
"~/PCB",
"~/Electronics",
"~/Projects/Electronics",
"~/Projects/PCB",
"~/Projects/KiCad",
]
for loc in default_locations:
expanded = os.path.expanduser(loc)
if os.path.isdir(expanded) and expanded not in paths:
paths.append(expanded)
return paths
# --- Static configuration (no env dependency) ---
KICAD_EXTENSIONS = {
"project": ".kicad_pro",
"pcb": ".kicad_pcb",
"schematic": ".kicad_sch",
"design_rules": ".kicad_dru",
"worksheet": ".kicad_wks",
"footprint": ".kicad_mod",
"netlist": "_netlist.net",
"kibot_config": ".kibot.yaml",
}
DATA_EXTENSIONS = [".csv", ".pos", ".net", ".zip", ".drl"]
TIMEOUT_CONSTANTS = {
"kicad_cli_version_check": 10.0,
"kicad_cli_export": 30.0,
"application_open": 10.0,
"subprocess_default": 30.0,
}
COMMON_LIBRARIES = {
"basic": {
"resistor": {"library": "Device", "symbol": "R"},
"capacitor": {"library": "Device", "symbol": "C"},
"inductor": {"library": "Device", "symbol": "L"},
"led": {"library": "Device", "symbol": "LED"},
"diode": {"library": "Device", "symbol": "D"},
},
"power": {
"vcc": {"library": "power", "symbol": "VCC"},
"gnd": {"library": "power", "symbol": "GND"},
"+5v": {"library": "power", "symbol": "+5V"},
"+3v3": {"library": "power", "symbol": "+3V3"},
},
"connectors": {
"conn_2pin": {"library": "Connector", "symbol": "Conn_01x02_Male"},
"conn_4pin": {"library": "Connector_Generic", "symbol": "Conn_01x04"},
},
}
DEFAULT_FOOTPRINTS = {
"R": [
"Resistor_SMD:R_0805_2012Metric",
"Resistor_SMD:R_0603_1608Metric",
"Resistor_THT:R_Axial_DIN0207_L6.3mm_D2.5mm_P10.16mm_Horizontal",
],
"C": [
"Capacitor_SMD:C_0805_2012Metric",
"Capacitor_SMD:C_0603_1608Metric",
"Capacitor_THT:C_Disc_D5.0mm_W2.5mm_P5.00mm",
],
"LED": ["LED_SMD:LED_0805_2012Metric", "LED_THT:LED_D5.0mm"],
"D": ["Diode_SMD:D_SOD-123", "Diode_THT:D_DO-35_SOD27_P7.62mm_Horizontal"],
}

View File

@ -0,0 +1,37 @@
"""
Consolidated MCP prompt templates for KiCad workflows.
"""
from mckicad.server import mcp
@mcp.prompt()
def debug_pcb(project_path: str) -> str:
"""Help debug PCB design issues."""
return f"""Analyze the KiCad PCB project at {project_path} for design issues.
Check for: DRC violations, unrouted nets, component placement problems,
signal integrity concerns, and manufacturing constraints."""
@mcp.prompt()
def analyze_bom(project_path: str) -> str:
"""Analyze a project's Bill of Materials."""
return f"""Analyze the BOM for the KiCad project at {project_path}.
Identify: component counts, categories, cost estimates if available,
and any missing or duplicate components."""
@mcp.prompt()
def design_circuit(description: str) -> str:
"""Guided circuit design workflow."""
return f"""Design a circuit based on: {description}
Steps: 1) Select components, 2) Create schematic, 3) Add connections,
4) Validate design, 5) Generate output files."""
@mcp.prompt()
def debug_schematic(schematic_path: str) -> str:
"""Help debug schematic connectivity issues."""
return f"""Analyze the schematic at {schematic_path} for issues.
Check for: unconnected pins, missing power connections, incorrect
component values, and ERC violations."""

View File

@ -0,0 +1,16 @@
"""
MCP resource for KiCad project file content.
"""
import json
from mckicad.server import mcp
from mckicad.utils.file_utils import get_project_files, load_project_json
@mcp.resource("kicad://project/{project_path}")
def get_project_resource(project_path: str) -> str:
"""Get details for a specific KiCad project."""
files = get_project_files(project_path)
metadata = load_project_json(project_path)
return json.dumps({"files": files, "metadata": metadata}, indent=2)

View File

@ -0,0 +1,15 @@
"""
MCP resource for KiCad project listing.
"""
import json
from mckicad.server import mcp
from mckicad.utils.kicad_utils import find_kicad_projects
@mcp.resource("kicad://projects")
def list_projects_resource() -> str:
"""List all KiCad projects found on this system."""
projects = find_kicad_projects()
return json.dumps(projects, indent=2)

61
src/mckicad/server.py Normal file
View File

@ -0,0 +1,61 @@
"""
mckicad MCP server FastMCP 3 architecture.
Tools are registered via module-level @mcp.tool decorators in their
respective modules. Importing the module is all that's needed.
"""
from contextlib import asynccontextmanager
import logging
from typing import Any
from fastmcp import FastMCP
from mckicad.config import get_kicad_user_dir, get_search_paths
logger = logging.getLogger(__name__)
@asynccontextmanager
async def lifespan(server: FastMCP):
"""Manage server lifecycle — initialize shared state, yield, clean up."""
logger.info("mckicad server starting")
kicad_user_dir = get_kicad_user_dir()
search_paths = get_search_paths()
logger.info(f"KiCad user dir: {kicad_user_dir}")
logger.info(f"Search paths: {search_paths}")
state: dict[str, Any] = {
"cache": {},
"kicad_user_dir": kicad_user_dir,
"search_paths": search_paths,
}
try:
yield state
finally:
state["cache"].clear()
logger.info("mckicad server stopped")
mcp = FastMCP("mckicad", lifespan=lifespan)
# Import tool/resource/prompt modules so their decorators register with `mcp`.
# Order doesn't matter — each module does @mcp.tool() at module level.
from mckicad.prompts import templates # noqa: E402, F401
from mckicad.resources import files, projects # noqa: E402, F401
from mckicad.tools import ( # noqa: E402, F401
analysis,
bom,
drc,
export,
pcb,
project,
routing,
schematic,
)
def main():
mcp.run(transport="stdio")

View File

@ -0,0 +1,445 @@
"""
Project analysis and validation tools.
Combines project validation, real-time board analysis, and component
detail retrieval into a single module. Uses KiCad IPC when available
for live data, falling back to file-based checks otherwise.
"""
import json
import logging
import os
from typing import Any
from mckicad.server import mcp
from mckicad.utils.file_utils import get_project_files
from mckicad.utils.ipc_client import check_kicad_availability, kicad_ipc_session
logger = logging.getLogger(__name__)
@mcp.tool()
def validate_project(project_path: str) -> dict[str, Any]:
"""Validate a KiCad project's structure and essential files.
Accepts either a path to a .kicad_pro file or a directory containing
exactly one .kicad_pro file. Checks that the project JSON parses
correctly, that schematic and PCB files exist, and -- when KiCad is
running -- performs a live IPC check for component count, routing
completion, and unrouted nets.
Args:
project_path: Path to a .kicad_pro file or a directory that
contains one.
Returns:
Dictionary with validation result, list of issues found, files
discovered, and optional real-time IPC analysis.
"""
# Resolve directory to .kicad_pro file
if os.path.isdir(project_path):
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}",
}
if len(kicad_pro_files) > 1:
return {
"valid": False,
"error": (
f"Multiple .kicad_pro files in directory: {project_path}. "
"Specify the exact file."
),
}
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"Expected .kicad_pro file, got: {project_path}",
}
issues: list[str] = []
# Discover associated files
try:
files = get_project_files(project_path)
except Exception as e:
return {
"valid": False,
"error": f"Error analysing project files: {e}",
}
if "pcb" not in files:
issues.append("Missing PCB layout file (.kicad_pcb)")
if "schematic" not in files:
issues.append("Missing schematic file (.kicad_sch)")
# Validate JSON integrity of the project file
try:
with open(project_path) as f:
json.load(f)
except json.JSONDecodeError as e:
issues.append(f"Invalid project file JSON: {e}")
except Exception as e:
issues.append(f"Error reading project file: {e}")
# Optional live analysis via KiCad IPC
ipc_analysis: dict[str, Any] = {}
ipc_status = check_kicad_availability()
if ipc_status["available"] and "pcb" in files:
try:
with kicad_ipc_session(board_path=files["pcb"]) as client:
board_stats = client.get_board_statistics()
connectivity = client.check_connectivity()
ipc_analysis = {
"real_time_analysis": True,
"board_statistics": board_stats,
"connectivity_status": connectivity,
"routing_completion": connectivity.get("routing_completion", 0),
"component_count": board_stats.get("footprint_count", 0),
"net_count": board_stats.get("net_count", 0),
}
if connectivity.get("unrouted_nets", 0) > 0:
issues.append(
f"{connectivity['unrouted_nets']} net(s) are not routed"
)
if board_stats.get("footprint_count", 0) == 0:
issues.append("No components found on PCB")
except Exception as e:
logger.debug(f"IPC analysis unavailable: {e}")
ipc_analysis = {
"real_time_analysis": False,
"ipc_error": str(e),
}
else:
ipc_analysis = {
"real_time_analysis": False,
"reason": ipc_status.get(
"message", "KiCad IPC not available or PCB file missing"
),
}
return {
"valid": len(issues) == 0,
"path": project_path,
"issues": issues if issues else None,
"files_found": list(files.keys()),
"ipc_analysis": ipc_analysis,
"validation_mode": (
"enhanced_with_ipc"
if ipc_analysis.get("real_time_analysis")
else "file_based"
),
}
@mcp.tool()
def analyze_board_real_time(project_path: str) -> dict[str, Any]:
"""Live board analysis via KiCad IPC.
Connects to a running KiCad instance and pulls footprint, net,
track, and connectivity data to build a comprehensive snapshot of
the board state. Covers placement density, routing completion,
design quality scoring, and manufacturability assessment.
Requires KiCad to be running with the target project open.
Args:
project_path: Path to the KiCad project (.kicad_pro) or its
parent directory.
Returns:
Dictionary with placement analysis, routing analysis, quality
scores, and recommendations.
"""
try:
files = get_project_files(project_path)
if "pcb" not in files:
return {
"success": False,
"error": "PCB file not found in project",
}
ipc_status = check_kicad_availability()
if not ipc_status["available"]:
return {
"success": False,
"error": f"KiCad IPC not available: {ipc_status['message']}",
}
board_path = files["pcb"]
with kicad_ipc_session(board_path=board_path) as client:
footprints = client.get_footprints()
nets = client.get_nets()
tracks = client.get_tracks()
board_stats = client.get_board_statistics()
connectivity = client.check_connectivity()
# --- placement ---
placement_analysis = {
"total_components": len(footprints),
"component_types": board_stats.get("component_types", {}),
"placement_density": _placement_density(footprints),
"component_distribution": _component_distribution(footprints),
}
# --- routing ---
trace_items = [t for t in tracks if not hasattr(t, "drill")]
via_items = [t for t in tracks if hasattr(t, "drill")]
routing_analysis = {
"total_nets": len(nets),
"routed_nets": connectivity.get("routed_nets", 0),
"unrouted_nets": connectivity.get("unrouted_nets", 0),
"routing_completion": connectivity.get("routing_completion", 0),
"track_count": len(trace_items),
"via_count": len(via_items),
"routing_efficiency": _routing_efficiency(tracks, nets),
}
# --- quality ---
design_score = _design_score(placement_analysis, routing_analysis)
critical_issues = _critical_issues(footprints, tracks, nets)
optimization_opps = _optimization_opportunities(
placement_analysis, routing_analysis
)
mfg_score = _manufacturability_score(tracks, footprints)
quality_analysis = {
"design_score": design_score,
"critical_issues": critical_issues,
"optimization_opportunities": optimization_opps,
"manufacturability_score": mfg_score,
}
recommendations = _board_recommendations(
placement_analysis, routing_analysis, quality_analysis
)
return {
"success": True,
"project_path": project_path,
"board_path": board_path,
"analysis_timestamp": os.path.getmtime(board_path),
"placement_analysis": placement_analysis,
"routing_analysis": routing_analysis,
"quality_analysis": quality_analysis,
"recommendations": recommendations,
"board_statistics": board_stats,
"analysis_mode": "real_time_ipc",
}
except Exception as e:
logger.error(f"Error in real-time board analysis: {e}")
return {
"success": False,
"error": str(e),
"project_path": project_path,
}
@mcp.tool()
def get_component_details(
project_path: str,
component_reference: str | None = None,
) -> dict[str, Any]:
"""Retrieve component details from a live KiCad board via IPC.
When *component_reference* is given (e.g. ``"R1"``, ``"U3"``),
returns position, rotation, layer, value, and footprint name for
that single component. When omitted, returns the same information
for every component on the board.
Requires KiCad to be running with the target project open.
Args:
project_path: Path to the KiCad project (.kicad_pro) or its
parent directory.
component_reference: Reference designator of a specific
component, or None to list all.
Returns:
Dictionary with component detail(s) or an error message.
"""
try:
files = get_project_files(project_path)
if "pcb" not in files:
return {
"success": False,
"error": "PCB file not found in project",
}
ipc_status = check_kicad_availability()
if not ipc_status["available"]:
return {
"success": False,
"error": f"KiCad IPC not available: {ipc_status['message']}",
}
board_path = files["pcb"]
with kicad_ipc_session(board_path=board_path) as client:
if component_reference:
fp = client.get_footprint_by_reference(component_reference)
if not fp:
return {
"success": False,
"error": f"Component '{component_reference}' not found",
}
return {
"success": True,
"project_path": project_path,
"component_reference": component_reference,
"component_details": _extract_component_details(fp),
}
# All components
footprints = client.get_footprints()
all_components = {}
for fp in footprints:
ref = getattr(fp, "reference", None)
if ref:
all_components[ref] = _extract_component_details(fp)
return {
"success": True,
"project_path": project_path,
"total_components": len(all_components),
"components": all_components,
}
except Exception as e:
logger.error(f"Error getting component details: {e}")
return {
"success": False,
"error": str(e),
"project_path": project_path,
}
# ---------------------------------------------------------------------------
# Private helpers
# ---------------------------------------------------------------------------
def _extract_component_details(footprint: Any) -> dict[str, Any]:
"""Pull key attributes from a FootprintInstance into a plain dict."""
pos = getattr(footprint, "position", None)
return {
"reference": getattr(footprint, "reference", "Unknown"),
"value": getattr(footprint, "value", "Unknown"),
"position": {
"x": getattr(pos, "x", 0) if pos else 0,
"y": getattr(pos, "y", 0) if pos else 0,
},
"rotation": getattr(footprint, "rotation", 0),
"layer": getattr(footprint, "layer", "F.Cu"),
"footprint_name": getattr(footprint, "footprint", "Unknown"),
}
def _placement_density(footprints: list) -> float:
"""Estimate placement density (simplified, 0.0 -- 1.0)."""
if not footprints:
return 0.0
return min(len(footprints) / 100.0, 1.0)
def _component_distribution(footprints: list) -> dict[str, str]:
"""Simplified distribution characterisation."""
if not footprints:
return {"distribution": "empty"}
return {
"distribution": "distributed",
"clustering": "moderate",
"edge_utilization": "good",
}
def _routing_efficiency(tracks: list, nets: list) -> float:
"""Track-to-net ratio as a percentage (0 -- 100)."""
net_count = len(nets)
if net_count == 0:
return 0.0
return round(min(len(tracks) / (net_count * 2), 1.0) * 100, 1)
def _design_score(
placement: dict[str, Any], routing: dict[str, Any]
) -> int:
"""Composite design quality score (0 -- 100)."""
base = 70
density_bonus = placement.get("placement_density", 0) * 15
completion_bonus = routing.get("routing_completion", 0) * 0.15
return min(int(base + density_bonus + completion_bonus), 100)
def _critical_issues(
footprints: list, tracks: list, nets: list
) -> list[str]:
"""Return a list of blocking design issues."""
issues: list[str] = []
if not footprints:
issues.append("No components placed on board")
if not tracks and nets:
issues.append("No routing present despite having nets defined")
return issues
def _optimization_opportunities(
placement: dict[str, Any], routing: dict[str, Any]
) -> list[str]:
"""Suggest areas where the design could be improved."""
opps: list[str] = []
if placement.get("placement_density", 0) < 0.3:
opps.append("Board area could be reduced for better cost efficiency")
if routing.get("routing_completion", 0) < 100:
opps.append("Complete remaining routing for full functionality")
return opps
def _manufacturability_score(tracks: list, footprints: list) -> int:
"""Heuristic manufacturability score (0 -- 100)."""
score = 85
if len(tracks) > 1000:
score -= 10
if len(footprints) > 100:
score -= 5
return max(score, 0)
def _board_recommendations(
placement: dict[str, Any],
routing: dict[str, Any],
quality: dict[str, Any],
) -> list[str]:
"""Compile a prioritised list of recommendations."""
recs: list[str] = []
if quality.get("design_score", 0) < 80:
recs.append("Design score is below 80 -- consider optimisation")
unrouted = routing.get("unrouted_nets", 0)
if unrouted > 0:
recs.append(f"Complete routing for {unrouted} unrouted net(s)")
if placement.get("total_components", 0) > 0:
recs.append("Review thermal management for power components")
recs.append("Run DRC check to validate design rules")
return recs

404
src/mckicad/tools/bom.py Normal file
View File

@ -0,0 +1,404 @@
"""
Bill of Materials (BOM) tools for KiCad MCP server.
Provides BOM analysis (CSV parsing with stdlib only -- no pandas) and
BOM export via kicad-cli.
"""
from collections import Counter
import csv
import logging
import os
import re
from typing import Any
from mckicad.server import mcp
from mckicad.utils.file_utils import get_project_files
from mckicad.utils.secure_subprocess import run_kicad_command
logger = logging.getLogger(__name__)
# ---------------------------------------------------------------------------
# Reference-prefix to human-readable category mapping
# ---------------------------------------------------------------------------
_CATEGORY_MAP: dict[str, str] = {
"R": "Resistors",
"C": "Capacitors",
"L": "Inductors",
"D": "Diodes",
"Q": "Transistors",
"U": "ICs",
"SW": "Switches",
"J": "Connectors",
"K": "Relays",
"Y": "Crystals/Oscillators",
"F": "Fuses",
"T": "Transformers",
"LED": "LEDs",
"TP": "Test Points",
"BT": "Batteries",
"M": "Motors",
"RN": "Resistor Networks",
"FB": "Ferrite Beads",
}
# ---------------------------------------------------------------------------
# Internal helpers
# ---------------------------------------------------------------------------
def _find_column(headers: list[str], candidates: list[str]) -> str | None:
"""Return the first header from *candidates* that appears in *headers* (case-insensitive)."""
lower_headers = {h.lower(): h for h in headers}
for candidate in candidates:
if candidate.lower() in lower_headers:
return lower_headers[candidate.lower()]
return None
def _extract_ref_prefix(reference: str) -> str:
"""Extract the alphabetic prefix from a reference designator (e.g. 'R12' -> 'R')."""
match = re.match(r"^([A-Za-z]+)", reference.strip())
return match.group(1) if match else "Other"
def _detect_delimiter(sample: str) -> str:
"""Guess the CSV delimiter from a text sample."""
for delim in [",", ";", "\t"]:
if delim in sample:
return delim
return ","
def _parse_bom_csv(file_path: str) -> tuple[list[dict[str, str]], dict[str, Any]]:
"""Parse a CSV BOM file using stdlib csv.DictReader.
Returns:
Tuple of (rows as list of dicts, format_info dict).
"""
format_info: dict[str, Any] = {
"file_type": ".csv",
"detected_format": "unknown",
"header_fields": [],
}
components: list[dict[str, str]] = []
try:
with open(file_path, encoding="utf-8-sig") as f:
sample = "".join(f.readline() for _ in range(5))
f.seek(0)
delimiter = _detect_delimiter(sample)
format_info["delimiter"] = delimiter
reader = csv.DictReader(f, delimiter=delimiter)
format_info["header_fields"] = list(reader.fieldnames or [])
header_lower = ",".join(format_info["header_fields"]).lower()
if "reference" in header_lower and "value" in header_lower:
format_info["detected_format"] = "kicad"
elif "designator" in header_lower:
format_info["detected_format"] = "altium"
elif "part number" in header_lower or "manufacturer part" in header_lower:
format_info["detected_format"] = "generic"
for row in reader:
components.append(dict(row))
except Exception as exc:
logger.error("Error parsing BOM CSV %s: %s", file_path, exc)
format_info["error"] = str(exc)
return components, format_info
def _analyze_components(
components: list[dict[str, str]],
format_info: dict[str, Any],
) -> dict[str, Any]:
"""Analyse a list of component rows without pandas."""
analysis: dict[str, Any] = {
"unique_component_count": 0,
"total_component_count": 0,
"categories": {},
"has_cost_data": False,
}
if not components:
return analysis
headers = list(components[0].keys())
ref_col = _find_column(
headers, ["Reference", "Designator", "References", "Designators", "RefDes", "Ref"]
)
value_col = _find_column(
headers, ["Value", "Component", "Comp", "Part", "Component Value"]
)
quantity_col = _find_column(
headers, ["Quantity", "Qty", "Count", "Amount"]
)
cost_col = _find_column(
headers, ["Cost", "Price", "Unit Price", "Unit Cost", "Cost Each"]
)
analysis["unique_component_count"] = len(components)
# Compute total component count
total = 0
if quantity_col:
for row in components:
try:
total += int(float(row.get(quantity_col, "1") or "1"))
except (ValueError, TypeError):
total += 1
else:
total = len(components)
analysis["total_component_count"] = total
# Build category counts from reference prefixes
prefix_counter: Counter[str] = Counter()
for row in components:
if ref_col:
raw_ref = row.get(ref_col, "")
# Handle comma-separated multi-reference cells
refs = [r.strip() for r in raw_ref.split(",") if r.strip()]
if not refs:
refs = ["Other"]
for ref in refs:
prefix_counter[_extract_ref_prefix(ref)] += 1
else:
prefix_counter["Unknown"] += 1
# Map prefixes to readable names
mapped: dict[str, int] = {}
for prefix, count in prefix_counter.items():
friendly = _CATEGORY_MAP.get(prefix, prefix)
mapped[friendly] = mapped.get(friendly, 0) + count
analysis["categories"] = mapped
# Cost aggregation (best-effort)
if cost_col:
total_cost = 0.0
currency = "USD"
cost_found = False
for row in components:
raw_cost = row.get(cost_col, "")
if not raw_cost:
continue
# Detect currency from first non-empty value
if not cost_found:
if "$" in raw_cost:
currency = "USD"
elif "\u20ac" in raw_cost:
currency = "EUR"
elif "\u00a3" in raw_cost:
currency = "GBP"
cleaned = re.sub(r"[^0-9.]", "", raw_cost)
try:
unit_cost = float(cleaned)
except ValueError:
continue
cost_found = True
qty = 1
if quantity_col:
try:
qty = int(float(row.get(quantity_col, "1") or "1"))
except (ValueError, TypeError):
qty = 1
total_cost += unit_cost * qty
if cost_found:
analysis["has_cost_data"] = True
analysis["total_cost"] = round(total_cost, 2)
analysis["currency"] = currency
# Most common values
if value_col:
value_counter: Counter[str] = Counter()
for row in components:
val = row.get(value_col, "").strip()
if val:
value_counter[val] += 1
if value_counter:
analysis["most_common_values"] = dict(value_counter.most_common(5))
return analysis
# ---------------------------------------------------------------------------
# MCP Tool definitions
# ---------------------------------------------------------------------------
@mcp.tool()
def analyze_bom(project_path: str) -> dict[str, Any]:
"""Analyse the Bill of Materials for a KiCad project.
Scans for BOM CSV files associated with the project, parses them
using stdlib csv (no pandas), and returns component counts broken
down by category (derived from reference designator prefixes),
along with cost data when available.
Args:
project_path: Absolute path to the .kicad_pro file.
Returns:
Dictionary with per-file analysis, overall component summary,
and cost totals.
"""
logger.info("Analysing BOM for project: %s", project_path)
if not os.path.exists(project_path):
logger.warning("Project not found: %s", project_path)
return {"success": False, "data": None, "error": f"Project not found: {project_path}"}
files = get_project_files(project_path)
# Collect any file that looks like a BOM
bom_files: dict[str, str] = {}
for file_type, file_path in files.items():
if "bom" in file_type.lower() or file_path.lower().endswith(".csv"):
bom_files[file_type] = file_path
logger.debug("Found potential BOM file: %s", file_path)
if not bom_files:
logger.warning("No BOM files found for project: %s", project_path)
return {
"success": False,
"data": None,
"error": "No BOM files found. Export a BOM from KiCad first.",
}
bom_results: dict[str, Any] = {}
total_unique = 0
total_components = 0
all_categories: Counter[str] = Counter()
aggregate_cost = 0.0
cost_available = False
for file_type, file_path in bom_files.items():
try:
components, format_info = _parse_bom_csv(file_path)
if not components:
logger.warning("No components parsed from: %s", file_path)
bom_results[file_type] = {"path": file_path, "error": "No components found"}
continue
analysis = _analyze_components(components, format_info)
bom_results[file_type] = {
"path": file_path,
"format": format_info,
"analysis": analysis,
}
total_unique += analysis["unique_component_count"]
total_components += analysis["total_component_count"]
all_categories.update(analysis["categories"])
if analysis.get("has_cost_data"):
aggregate_cost += analysis.get("total_cost", 0.0)
cost_available = True
logger.info("Analysed BOM file: %s (%d components)", file_path, analysis["total_component_count"])
except Exception as exc:
logger.error("Error analysing BOM file %s: %s", file_path, exc)
bom_results[file_type] = {"path": file_path, "error": str(exc)}
summary: dict[str, Any] = {
"total_unique_components": total_unique,
"total_components": total_components,
"categories": dict(all_categories),
}
if cost_available:
summary["total_cost"] = round(aggregate_cost, 2)
return {
"success": True,
"data": {
"project_path": project_path,
"bom_files": bom_results,
"component_summary": summary,
},
"error": None,
}
@mcp.tool()
def export_bom(project_path: str) -> dict[str, Any]:
"""Export a BOM CSV from a KiCad schematic using kicad-cli.
Runs ``kicad-cli sch export bom`` on the schematic file associated
with the project and writes the output CSV alongside the project.
Args:
project_path: Absolute path to the .kicad_pro file.
Returns:
Dictionary with the exported file path and size on success.
"""
logger.info("Exporting BOM for project: %s", project_path)
if not os.path.exists(project_path):
logger.warning("Project not found: %s", project_path)
return {"success": False, "data": None, "error": f"Project not found: {project_path}"}
files = get_project_files(project_path)
if "schematic" not in files:
logger.warning("Schematic not found for project: %s", project_path)
return {"success": False, "data": None, "error": "Schematic file not found in project"}
schematic_file = files["schematic"]
project_dir = os.path.dirname(project_path)
basename = os.path.basename(project_path)
project_name = basename.rsplit(".kicad_pro", 1)[0] if basename.endswith(".kicad_pro") else basename
output_file = os.path.join(project_dir, f"{project_name}_bom.csv")
try:
result = run_kicad_command(
command_args=[
"sch", "export", "bom",
"--output", output_file,
schematic_file,
],
input_files=[schematic_file],
output_files=[output_file],
)
if result.returncode != 0:
error_msg = result.stderr.strip() if result.stderr else "BOM export command failed"
logger.error("BOM export failed (rc=%d): %s", result.returncode, error_msg)
return {"success": False, "data": None, "error": error_msg}
if not os.path.exists(output_file):
logger.error("BOM output file not created: %s", output_file)
return {"success": False, "data": None, "error": "BOM output file was not created"}
file_size = os.path.getsize(output_file)
if file_size == 0:
logger.warning("Generated BOM file is empty: %s", output_file)
return {"success": False, "data": None, "error": "Generated BOM file is empty"}
logger.info("BOM exported to %s (%d bytes)", output_file, file_size)
return {
"success": True,
"data": {
"output_file": output_file,
"schematic_file": schematic_file,
"file_size": file_size,
},
"error": None,
}
except Exception as e:
logger.error("BOM export failed: %s", e, exc_info=True)
return {"success": False, "data": None, "error": str(e)}

464
src/mckicad/tools/drc.py Normal file
View File

@ -0,0 +1,464 @@
"""
Design Rule Check (DRC) tools for KiCad MCP server.
Combines basic DRC checking via kicad-cli with advanced rule set
management for different PCB technologies (standard, HDI, RF, automotive).
"""
import json
import logging
import os
import tempfile
from typing import Any
from mckicad.server import mcp
from mckicad.utils.file_utils import get_project_files
from mckicad.utils.secure_subprocess import run_kicad_command
logger = logging.getLogger(__name__)
# ---------------------------------------------------------------------------
# Technology-specific manufacturing constraints and rule definitions
# ---------------------------------------------------------------------------
_MANUFACTURING_CONSTRAINTS: dict[str, dict[str, Any]] = {
"standard": {
"min_track_width_mm": 0.15,
"min_clearance_mm": 0.15,
"min_via_drill_mm": 0.3,
"min_via_diameter_mm": 0.6,
"min_annular_ring_mm": 0.15,
"min_drill_size_mm": 0.2,
"max_layer_count": 6,
"min_board_thickness_mm": 0.8,
"max_board_thickness_mm": 3.2,
"copper_weights_oz": [0.5, 1.0, 2.0],
"min_silk_width_mm": 0.15,
"min_silk_clearance_mm": 0.15,
"min_courtyard_clearance_mm": 0.25,
},
"hdi": {
"min_track_width_mm": 0.075,
"min_clearance_mm": 0.075,
"min_via_drill_mm": 0.1,
"min_via_diameter_mm": 0.25,
"min_annular_ring_mm": 0.075,
"min_drill_size_mm": 0.1,
"max_layer_count": 20,
"min_board_thickness_mm": 0.4,
"max_board_thickness_mm": 3.2,
"copper_weights_oz": [0.33, 0.5, 1.0],
"min_silk_width_mm": 0.1,
"min_silk_clearance_mm": 0.1,
"min_courtyard_clearance_mm": 0.15,
"microvia_supported": True,
"sequential_buildup": True,
},
"rf": {
"min_track_width_mm": 0.127,
"min_clearance_mm": 0.2,
"min_via_drill_mm": 0.25,
"min_via_diameter_mm": 0.5,
"min_annular_ring_mm": 0.125,
"min_drill_size_mm": 0.2,
"max_layer_count": 8,
"min_board_thickness_mm": 0.8,
"max_board_thickness_mm": 3.2,
"copper_weights_oz": [0.5, 1.0],
"min_silk_width_mm": 0.15,
"min_silk_clearance_mm": 0.15,
"min_courtyard_clearance_mm": 0.25,
"controlled_impedance": True,
"via_stitching_pitch_mm": 2.0,
},
"automotive": {
"min_track_width_mm": 0.2,
"min_clearance_mm": 0.25,
"min_via_drill_mm": 0.35,
"min_via_diameter_mm": 0.7,
"min_annular_ring_mm": 0.175,
"min_drill_size_mm": 0.3,
"max_layer_count": 8,
"min_board_thickness_mm": 1.0,
"max_board_thickness_mm": 3.2,
"copper_weights_oz": [1.0, 2.0, 3.0],
"min_silk_width_mm": 0.2,
"min_silk_clearance_mm": 0.2,
"min_courtyard_clearance_mm": 0.5,
"temp_range_c": [-40, 125],
"vibration_rated": True,
},
}
_TECHNOLOGY_RECOMMENDATIONS: dict[str, list[str]] = {
"standard": [
"Maintain 0.15mm minimum track width for cost-effective manufacturing",
"Use 0.15mm clearance for reliable production yields",
"Consider 6-layer maximum for standard processes",
],
"hdi": [
"Use microvias for high-density routing",
"Maintain controlled impedance for signal integrity",
"Consider sequential build-up for complex designs",
],
"rf": [
"Maintain consistent dielectric properties",
"Use ground via stitching for EMI control",
"Control trace geometry for impedance matching",
],
"automotive": [
"Design for extended temperature range operation (-40 to +125 C)",
"Increase clearances for vibration resistance",
"Use thermal management for high-power components",
],
}
_APPLICABLE_STANDARDS: dict[str, list[str]] = {
"standard": ["IPC-2221", "IPC-2222"],
"hdi": ["IPC-2226", "IPC-6016"],
"rf": ["IPC-2221", "IPC-2141"],
"automotive": ["ISO 26262", "AEC-Q100"],
}
def _build_rule_set(name: str, technology: str, description: str) -> dict[str, Any]:
"""Build a rule set from manufacturing constraints for a given technology."""
tech = technology.lower()
if tech not in _MANUFACTURING_CONSTRAINTS:
tech = "standard"
constraints = _MANUFACTURING_CONSTRAINTS[tech]
rules = []
rules.append({
"name": f"{name}_clearance",
"type": "clearance",
"severity": "error",
"constraint": {"min_mm": constraints["min_clearance_mm"]},
"enabled": True,
})
rules.append({
"name": f"{name}_track_width",
"type": "track_width",
"severity": "error",
"constraint": {"min_mm": constraints["min_track_width_mm"]},
"enabled": True,
})
rules.append({
"name": f"{name}_via_size",
"type": "via_size",
"severity": "error",
"constraint": {
"min_drill_mm": constraints["min_via_drill_mm"],
"min_diameter_mm": constraints["min_via_diameter_mm"],
},
"enabled": True,
})
rules.append({
"name": f"{name}_annular_ring",
"type": "annular_ring",
"severity": "error",
"constraint": {"min_mm": constraints["min_annular_ring_mm"]},
"enabled": True,
})
rules.append({
"name": f"{name}_drill_size",
"type": "drill_size",
"severity": "warning",
"constraint": {"min_mm": constraints["min_drill_size_mm"]},
"enabled": True,
})
rules.append({
"name": f"{name}_silk_clearance",
"type": "silk_clearance",
"severity": "warning",
"constraint": {
"min_width_mm": constraints["min_silk_width_mm"],
"min_clearance_mm": constraints["min_silk_clearance_mm"],
},
"enabled": True,
})
rules.append({
"name": f"{name}_courtyard",
"type": "courtyard_clearance",
"severity": "warning",
"constraint": {"min_mm": constraints["min_courtyard_clearance_mm"]},
"enabled": True,
})
return {
"name": name,
"technology": tech,
"description": description or f"{tech.upper()} PCB rules for {name}",
"rules": rules,
"rule_count": len(rules),
}
def _rules_to_kicad_format(rule_set: dict[str, Any]) -> str:
"""Convert a rule set to KiCad DRC rule text format."""
lines = [
f"# KiCad DRC Rules: {rule_set['name']}",
f"# Technology: {rule_set['technology']}",
f"# {rule_set['description']}",
"",
]
for rule in rule_set["rules"]:
if not rule.get("enabled", True):
continue
constraint = rule["constraint"]
rule_type = rule["type"]
lines.append(f"(rule \"{rule['name']}\"")
lines.append(f" (severity {rule['severity']})")
if rule_type == "clearance":
lines.append(f" (constraint clearance (min {constraint['min_mm']}mm))")
elif rule_type == "track_width":
lines.append(f" (constraint track_width (min {constraint['min_mm']}mm))")
elif rule_type == "via_size":
lines.append(f" (constraint via_diameter (min {constraint['min_diameter_mm']}mm))")
lines.append(f" (constraint hole_size (min {constraint['min_drill_mm']}mm))")
elif rule_type == "annular_ring":
lines.append(f" (constraint annular_width (min {constraint['min_mm']}mm))")
elif rule_type == "drill_size":
lines.append(f" (constraint hole_size (min {constraint['min_mm']}mm))")
elif rule_type == "silk_clearance":
lines.append(f" (constraint silk_clearance (min {constraint['min_clearance_mm']}mm))")
elif rule_type == "courtyard_clearance":
lines.append(f" (constraint courtyard_clearance (min {constraint['min_mm']}mm))")
lines.append(")")
lines.append("")
return "\n".join(lines)
# ---------------------------------------------------------------------------
# MCP Tool definitions
# ---------------------------------------------------------------------------
@mcp.tool()
def run_drc_check(project_path: str) -> dict[str, Any]:
"""Run a Design Rule Check on a KiCad PCB using kicad-cli.
Locates the .kicad_pcb file for the given project, runs DRC via
``kicad-cli pcb drc``, and parses the JSON report to extract
violation counts and categories.
Args:
project_path: Absolute path to the .kicad_pro file.
Returns:
Dictionary with violation count, categorised violations, and
the raw violation list from KiCad.
"""
logger.info("Running DRC check for project: %s", project_path)
if not os.path.exists(project_path):
logger.warning("Project not found: %s", project_path)
return {"success": False, "data": None, "error": f"Project not found: {project_path}"}
# Locate the PCB file
files = get_project_files(project_path)
if "pcb" not in files:
logger.warning("PCB file not found in project: %s", project_path)
return {"success": False, "data": None, "error": "PCB file not found in project"}
pcb_file = files["pcb"]
logger.info("Found PCB file: %s", pcb_file)
try:
with tempfile.TemporaryDirectory(prefix="mckicad_drc_") as temp_dir:
output_file = os.path.join(temp_dir, "drc_report.json")
result = run_kicad_command(
command_args=[
"pcb", "drc",
"--format", "json",
"--output", output_file,
pcb_file,
],
input_files=[pcb_file],
output_files=[output_file],
)
# kicad-cli may return non-zero when violations exist, so we
# check for the output file rather than just the return code.
if not os.path.exists(output_file):
error_msg = result.stderr.strip() if result.stderr else "DRC report file not created"
logger.error("DRC report not created: %s", error_msg)
return {"success": False, "data": None, "error": error_msg}
with open(output_file) as f:
try:
drc_report = json.load(f)
except json.JSONDecodeError as exc:
logger.error("Failed to parse DRC JSON report: %s", exc)
return {"success": False, "data": None, "error": "Failed to parse DRC report JSON"}
violations = drc_report.get("violations", [])
violation_count = len(violations)
# Categorise violations by message
violation_categories: dict[str, int] = {}
for v in violations:
msg = v.get("message", "Unknown")
violation_categories[msg] = violation_categories.get(msg, 0) + 1
logger.info("DRC completed: %d violation(s)", violation_count)
return {
"success": True,
"data": {
"pcb_file": pcb_file,
"total_violations": violation_count,
"violation_categories": violation_categories,
"violations": violations,
},
"error": None,
}
except Exception as e:
logger.error("DRC check failed: %s", e, exc_info=True)
return {"success": False, "data": None, "error": str(e)}
@mcp.tool()
def create_drc_rule_set(
name: str,
technology: str = "standard",
description: str = "",
) -> dict[str, Any]:
"""Create a DRC rule set optimised for a specific PCB technology.
Generates a collection of manufacturing rules (clearance, track width,
via size, annular ring, drill size, silk, courtyard) tuned for the
requested technology tier.
Args:
name: Human-readable name for the rule set (e.g. "MyBoard_Rules").
technology: One of "standard", "hdi", "rf", or "automotive".
description: Optional free-text description.
Returns:
Dictionary containing the generated rules, their parameters, and
the technology profile used.
"""
logger.info("Creating DRC rule set '%s' for technology '%s'", name, technology)
tech = technology.lower()
if tech not in _MANUFACTURING_CONSTRAINTS:
return {
"success": False,
"data": None,
"error": (
f"Unknown technology: {technology}. "
f"Valid options: {list(_MANUFACTURING_CONSTRAINTS.keys())}"
),
}
try:
rule_set = _build_rule_set(name, tech, description)
logger.info("Created rule set '%s' with %d rules", name, rule_set["rule_count"])
return {"success": True, "data": rule_set, "error": None}
except Exception as e:
logger.error("Failed to create rule set '%s': %s", name, e)
return {"success": False, "data": None, "error": str(e)}
@mcp.tool()
def export_kicad_drc_rules(
rule_set_name: str = "Standard",
technology: str = "standard",
) -> dict[str, Any]:
"""Export DRC rules in KiCad-compatible text format.
Builds a rule set for the given technology and serialises it to the
KiCad custom DRC rule syntax that can be pasted into a project's
design rules file.
Args:
rule_set_name: Name to assign to the exported rule set.
technology: Technology profile ("standard", "hdi", "rf", "automotive").
Returns:
Dictionary containing the KiCad-format rule text and metadata.
"""
logger.info("Exporting KiCad DRC rules for '%s' (%s)", rule_set_name, technology)
tech = technology.lower()
if tech not in _MANUFACTURING_CONSTRAINTS:
return {
"success": False,
"data": None,
"error": (
f"Unknown technology: {technology}. "
f"Valid options: {list(_MANUFACTURING_CONSTRAINTS.keys())}"
),
}
try:
rule_set = _build_rule_set(rule_set_name, tech, "")
kicad_text = _rules_to_kicad_format(rule_set)
active_count = sum(1 for r in rule_set["rules"] if r.get("enabled", True))
return {
"success": True,
"data": {
"rule_set_name": rule_set_name,
"technology": tech,
"kicad_rules": kicad_text,
"rule_count": rule_set["rule_count"],
"active_rules": active_count,
"usage": "Copy the kicad_rules text into your project's custom DRC rules file",
},
"error": None,
}
except Exception as e:
logger.error("Failed to export DRC rules: %s", e)
return {"success": False, "data": None, "error": str(e)}
@mcp.tool()
def get_manufacturing_constraints(technology: str = "standard") -> dict[str, Any]:
"""Get manufacturing constraints and design guidelines for a PCB technology.
Returns the numeric manufacturing limits (minimum track width,
clearance, via size, etc.) along with design recommendations and
applicable industry standards for the chosen technology tier.
Args:
technology: Technology profile ("standard", "hdi", "rf", "automotive").
Returns:
Dictionary with constraints, recommendations, and applicable standards.
"""
logger.info("Getting manufacturing constraints for technology: %s", technology)
tech = technology.lower()
if tech not in _MANUFACTURING_CONSTRAINTS:
return {
"success": False,
"data": None,
"error": (
f"Unknown technology: {technology}. "
f"Valid options: {list(_MANUFACTURING_CONSTRAINTS.keys())}"
),
}
return {
"success": True,
"data": {
"technology": tech,
"constraints": _MANUFACTURING_CONSTRAINTS[tech],
"recommendations": _TECHNOLOGY_RECOMMENDATIONS.get(tech, []),
"applicable_standards": _APPLICABLE_STANDARDS.get(tech, []),
},
"error": None,
}

345
src/mckicad/tools/export.py Normal file
View File

@ -0,0 +1,345 @@
"""
File export tools for KiCad MCP server.
Provides tools for generating SVG renders, Gerber files, drill files,
and PDFs from KiCad PCB and schematic files using kicad-cli.
"""
import contextlib
import logging
import os
from typing import Any
from mckicad.server import mcp
from mckicad.utils.file_utils import get_project_files
from mckicad.utils.secure_subprocess import run_kicad_command
logger = logging.getLogger(__name__)
# ---------------------------------------------------------------------------
# Internal helpers
# ---------------------------------------------------------------------------
def _resolve_pcb(project_path: str) -> tuple[str | None, str | None]:
"""Return (pcb_file, error_message). One will always be None."""
if not os.path.exists(project_path):
return None, f"Project not found: {project_path}"
files = get_project_files(project_path)
pcb = files.get("pcb")
if not pcb:
return None, "PCB file not found in project"
return pcb, None
def _resolve_file(project_path: str, file_type: str) -> tuple[str | None, str | None]:
"""Return (resolved_file, error_message) for pcb or schematic."""
if not os.path.exists(project_path):
return None, f"Project not found: {project_path}"
files = get_project_files(project_path)
target = files.get(file_type)
if not target:
return None, f"{file_type.capitalize()} file not found in project"
return target, None
# ---------------------------------------------------------------------------
# MCP Tool definitions
# ---------------------------------------------------------------------------
@mcp.tool()
def generate_pcb_svg(project_path: str) -> dict[str, Any]:
"""Generate an SVG render of a KiCad PCB layout.
Uses ``kicad-cli pcb export svg`` to produce a multi-layer SVG of
the board. The SVG content is returned as a string so the caller
can display or save it.
Args:
project_path: Absolute path to the .kicad_pro file.
Returns:
Dictionary with the SVG content, output path, and file size.
"""
logger.info("Generating PCB SVG for project: %s", project_path)
pcb_file, err = _resolve_pcb(project_path)
if err or not pcb_file:
logger.warning(err)
return {"success": False, "data": None, "error": err}
project_dir = os.path.dirname(project_path)
basename = os.path.basename(pcb_file)
stem = os.path.splitext(basename)[0]
output_file = os.path.join(project_dir, f"{stem}.svg")
try:
result = run_kicad_command(
command_args=[
"pcb", "export", "svg",
"--output", output_file,
"--layers",
"F.Cu,B.Cu,F.SilkS,B.SilkS,F.Mask,B.Mask,Edge.Cuts",
pcb_file,
],
input_files=[pcb_file],
output_files=[output_file],
)
if result.returncode != 0:
error_msg = result.stderr.strip() if result.stderr else "SVG export failed"
logger.error("SVG export failed (rc=%d): %s", result.returncode, error_msg)
return {"success": False, "data": None, "error": error_msg}
if not os.path.exists(output_file):
logger.error("SVG output file not created: %s", output_file)
return {"success": False, "data": None, "error": "SVG output file was not created"}
with open(output_file, encoding="utf-8") as f:
svg_content = f.read()
file_size = os.path.getsize(output_file)
logger.info("SVG generated: %s (%d bytes)", output_file, file_size)
return {
"success": True,
"data": {
"output_file": output_file,
"file_size": file_size,
"svg_content": svg_content,
},
"error": None,
}
except Exception as e:
logger.error("SVG generation failed: %s", e, exc_info=True)
return {"success": False, "data": None, "error": str(e)}
@mcp.tool()
def export_gerbers(project_path: str) -> dict[str, Any]:
"""Export Gerber manufacturing files from a KiCad PCB.
Runs ``kicad-cli pcb export gerbers`` and writes the output into a
``gerbers/`` subdirectory alongside the project. Returns the list
of generated files.
Args:
project_path: Absolute path to the .kicad_pro file.
Returns:
Dictionary with output directory path and list of generated files.
"""
logger.info("Exporting Gerbers for project: %s", project_path)
pcb_file, err = _resolve_pcb(project_path)
if err or not pcb_file:
logger.warning(err)
return {"success": False, "data": None, "error": err}
project_dir = os.path.dirname(project_path)
output_dir = os.path.join(project_dir, "gerbers")
os.makedirs(output_dir, exist_ok=True)
try:
result = run_kicad_command(
command_args=[
"pcb", "export", "gerbers",
"--output", output_dir + os.sep,
pcb_file,
],
input_files=[pcb_file],
)
if result.returncode != 0:
error_msg = result.stderr.strip() if result.stderr else "Gerber export failed"
logger.error("Gerber export failed (rc=%d): %s", result.returncode, error_msg)
return {"success": False, "data": None, "error": error_msg}
generated_files = []
try:
for entry in os.listdir(output_dir):
full = os.path.join(output_dir, entry)
if os.path.isfile(full):
generated_files.append(entry)
except OSError as exc:
logger.warning("Could not list Gerber output directory: %s", exc)
if not generated_files:
logger.warning("No Gerber files were generated")
return {"success": False, "data": None, "error": "No Gerber files were generated"}
logger.info("Exported %d Gerber file(s) to %s", len(generated_files), output_dir)
return {
"success": True,
"data": {
"output_dir": output_dir,
"files": sorted(generated_files),
"file_count": len(generated_files),
},
"error": None,
}
except Exception as e:
logger.error("Gerber export failed: %s", e, exc_info=True)
return {"success": False, "data": None, "error": str(e)}
@mcp.tool()
def export_drill(project_path: str) -> dict[str, Any]:
"""Export drill files from a KiCad PCB.
Runs ``kicad-cli pcb export drill`` and writes output to a
``gerbers/`` subdirectory (common convention to co-locate with
Gerber files).
Args:
project_path: Absolute path to the .kicad_pro file.
Returns:
Dictionary with output directory path and list of generated files.
"""
logger.info("Exporting drill files for project: %s", project_path)
pcb_file, err = _resolve_pcb(project_path)
if err or not pcb_file:
logger.warning(err)
return {"success": False, "data": None, "error": err}
project_dir = os.path.dirname(project_path)
output_dir = os.path.join(project_dir, "gerbers")
os.makedirs(output_dir, exist_ok=True)
try:
result = run_kicad_command(
command_args=[
"pcb", "export", "drill",
"--output", output_dir + os.sep,
pcb_file,
],
input_files=[pcb_file],
)
if result.returncode != 0:
error_msg = result.stderr.strip() if result.stderr else "Drill export failed"
logger.error("Drill export failed (rc=%d): %s", result.returncode, error_msg)
return {"success": False, "data": None, "error": error_msg}
# Collect drill-related files (.drl, .exc, .xln)
drill_extensions = {".drl", ".exc", ".xln"}
generated_files = []
try:
for entry in os.listdir(output_dir):
full = os.path.join(output_dir, entry)
_, ext = os.path.splitext(entry)
if os.path.isfile(full) and ext.lower() in drill_extensions:
generated_files.append(entry)
except OSError as exc:
logger.warning("Could not list drill output directory: %s", exc)
if not generated_files:
# Maybe kicad-cli used a different extension -- list everything new
with contextlib.suppress(OSError):
generated_files = [
e for e in os.listdir(output_dir) if os.path.isfile(os.path.join(output_dir, e))
]
logger.info("Exported %d drill file(s) to %s", len(generated_files), output_dir)
return {
"success": True,
"data": {
"output_dir": output_dir,
"files": sorted(generated_files),
"file_count": len(generated_files),
},
"error": None,
}
except Exception as e:
logger.error("Drill export failed: %s", e, exc_info=True)
return {"success": False, "data": None, "error": str(e)}
@mcp.tool()
def export_pdf(project_path: str, file_type: str = "pcb") -> dict[str, Any]:
"""Export a PDF from a KiCad PCB or schematic.
Runs ``kicad-cli pcb export pdf`` or ``kicad-cli sch export pdf``
depending on *file_type*.
Args:
project_path: Absolute path to the .kicad_pro file.
file_type: Either "pcb" or "schematic".
Returns:
Dictionary with the output PDF path and file size.
"""
logger.info("Exporting PDF (%s) for project: %s", file_type, project_path)
ft = file_type.lower().strip()
if ft not in ("pcb", "schematic"):
return {
"success": False,
"data": None,
"error": f"Invalid file_type: {file_type}. Must be 'pcb' or 'schematic'.",
}
source_file, err = _resolve_file(project_path, ft)
if err or not source_file:
logger.warning(err)
return {"success": False, "data": None, "error": err}
project_dir = os.path.dirname(project_path)
stem = os.path.splitext(os.path.basename(source_file))[0]
output_file = os.path.join(project_dir, f"{stem}.pdf")
try:
if ft == "pcb":
cmd_args = [
"pcb", "export", "pdf",
"--output", output_file,
source_file,
]
else:
cmd_args = [
"sch", "export", "pdf",
"--output", output_file,
source_file,
]
result = run_kicad_command(
command_args=cmd_args,
input_files=[source_file],
output_files=[output_file],
)
if result.returncode != 0:
error_msg = result.stderr.strip() if result.stderr else "PDF export failed"
logger.error("PDF export failed (rc=%d): %s", result.returncode, error_msg)
return {"success": False, "data": None, "error": error_msg}
if not os.path.exists(output_file):
logger.error("PDF output file not created: %s", output_file)
return {"success": False, "data": None, "error": "PDF output file was not created"}
file_size = os.path.getsize(output_file)
logger.info("PDF exported: %s (%d bytes)", output_file, file_size)
return {
"success": True,
"data": {
"output_file": output_file,
"source_file": source_file,
"file_type": ft,
"file_size": file_size,
},
"error": None,
}
except Exception as e:
logger.error("PDF export failed: %s", e, exc_info=True)
return {"success": False, "data": None, "error": str(e)}

300
src/mckicad/tools/pcb.py Normal file
View File

@ -0,0 +1,300 @@
"""
PCB manipulation tools via KiCad IPC API.
Provides direct board-level operations -- moving and rotating
components, querying board statistics and connectivity, and refilling
copper zones -- all through a live connection to a running KiCad
instance.
"""
import logging
from typing import Any
from mckicad.server import mcp
from mckicad.utils.file_utils import get_project_files
from mckicad.utils.ipc_client import (
check_kicad_availability,
format_position,
kicad_ipc_session,
)
logger = logging.getLogger(__name__)
# ---------------------------------------------------------------------------
# Shared pre-flight helper
# ---------------------------------------------------------------------------
def _get_board_path(project_path: str) -> tuple[str | None, dict[str, Any] | None]:
"""Resolve project_path to a .kicad_pcb path.
Returns (board_path, None) on success or (None, error_dict) on
failure.
"""
files = get_project_files(project_path)
if "pcb" not in files:
return None, {
"success": False,
"error": "PCB file not found in project",
}
ipc_status = check_kicad_availability()
if not ipc_status["available"]:
return None, {
"success": False,
"error": f"KiCad IPC not available: {ipc_status['message']}",
}
return files["pcb"], None
# ---------------------------------------------------------------------------
# Tools
# ---------------------------------------------------------------------------
@mcp.tool()
def move_component(
project_path: str,
reference: str,
x_mm: float,
y_mm: float,
) -> dict[str, Any]:
"""Move a component to a new absolute position on the PCB.
The move is wrapped in a KiCad undo transaction so it can be
reversed inside KiCad with Ctrl-Z.
Args:
project_path: Path to the KiCad project (.kicad_pro) or its
parent directory.
reference: Reference designator of the component to move
(e.g. "R1", "U3", "C12").
x_mm: Target X coordinate in millimetres.
y_mm: Target Y coordinate in millimetres.
Returns:
Dictionary confirming success and the new position, or an error
message.
"""
try:
board_path, err = _get_board_path(project_path)
if err or not board_path:
return err or {"success": False, "error": "Could not resolve board path"}
with kicad_ipc_session(board_path=board_path) as client:
position = format_position(x_mm, y_mm)
success = client.move_footprint(reference, position)
if not success:
return {
"success": False,
"error": f"Failed to move component '{reference}' -- "
"check that the reference exists on the board",
}
return {
"success": True,
"reference": reference,
"new_position": {"x_mm": x_mm, "y_mm": y_mm},
"project_path": project_path,
}
except Exception as e:
logger.error(f"Error moving component {reference}: {e}")
return {
"success": False,
"error": str(e),
"reference": reference,
"project_path": project_path,
}
@mcp.tool()
def rotate_component(
project_path: str,
reference: str,
angle_degrees: float,
) -> dict[str, Any]:
"""Set a component's rotation angle on the PCB.
The angle is absolute (not additive). For example, passing 90.0
sets the component to 90 degrees regardless of its current
orientation. The operation is wrapped in a KiCad undo transaction.
Args:
project_path: Path to the KiCad project (.kicad_pro) or its
parent directory.
reference: Reference designator (e.g. "R1", "U3").
angle_degrees: Target rotation in degrees (0 -- 360).
Returns:
Dictionary confirming success and the applied angle, or an
error message.
"""
try:
board_path, err = _get_board_path(project_path)
if err or not board_path:
return err or {"success": False, "error": "Could not resolve board path"}
with kicad_ipc_session(board_path=board_path) as client:
success = client.rotate_footprint(reference, angle_degrees)
if not success:
return {
"success": False,
"error": f"Failed to rotate component '{reference}' -- "
"check that the reference exists on the board",
}
return {
"success": True,
"reference": reference,
"angle_degrees": angle_degrees,
"project_path": project_path,
}
except Exception as e:
logger.error(f"Error rotating component {reference}: {e}")
return {
"success": False,
"error": str(e),
"reference": reference,
"project_path": project_path,
}
@mcp.tool()
def get_board_statistics(project_path: str) -> dict[str, Any]:
"""Retrieve high-level board statistics from a live KiCad instance.
Returns counts of footprints, nets, tracks, and vias, plus a
breakdown of component types by reference-designator prefix
(e.g. R, C, U).
Args:
project_path: Path to the KiCad project (.kicad_pro) or its
parent directory.
Returns:
Dictionary with board statistics or an error message.
"""
try:
board_path, err = _get_board_path(project_path)
if err or not board_path:
return err or {"success": False, "error": "Could not resolve board path"}
with kicad_ipc_session(board_path=board_path) as client:
stats = client.get_board_statistics()
if not stats:
return {
"success": False,
"error": "Failed to retrieve board statistics",
}
return {
"success": True,
"project_path": project_path,
"board_path": board_path,
"statistics": stats,
}
except Exception as e:
logger.error(f"Error getting board statistics: {e}")
return {
"success": False,
"error": str(e),
"project_path": project_path,
}
@mcp.tool()
def check_connectivity(project_path: str) -> dict[str, Any]:
"""Check the routing connectivity status of the PCB.
Reports total nets, how many are routed vs unrouted, the overall
routing-completion percentage, and the names of routed nets.
Args:
project_path: Path to the KiCad project (.kicad_pro) or its
parent directory.
Returns:
Dictionary with connectivity status or an error message.
"""
try:
board_path, err = _get_board_path(project_path)
if err or not board_path:
return err or {"success": False, "error": "Could not resolve board path"}
with kicad_ipc_session(board_path=board_path) as client:
connectivity = client.check_connectivity()
if not connectivity:
return {
"success": False,
"error": "Failed to check connectivity",
}
return {
"success": True,
"project_path": project_path,
"board_path": board_path,
"connectivity": connectivity,
}
except Exception as e:
logger.error(f"Error checking connectivity: {e}")
return {
"success": False,
"error": str(e),
"project_path": project_path,
}
@mcp.tool()
def refill_zones(project_path: str) -> dict[str, Any]:
"""Refill all copper zones on the PCB.
Triggers a full zone refill in KiCad, which recomputes copper
fills for every zone on the board. This is useful after component
moves, routing changes, or design-rule updates. The call blocks
until the refill completes (up to 30 s timeout).
Args:
project_path: Path to the KiCad project (.kicad_pro) or its
parent directory.
Returns:
Dictionary confirming success or an error message.
"""
try:
board_path, err = _get_board_path(project_path)
if err or not board_path:
return err or {"success": False, "error": "Could not resolve board path"}
with kicad_ipc_session(board_path=board_path) as client:
success = client.refill_zones()
if not success:
return {
"success": False,
"error": "Zone refill failed -- check KiCad for details",
}
return {
"success": True,
"project_path": project_path,
"board_path": board_path,
"message": "All zones refilled successfully",
}
except Exception as e:
logger.error(f"Error refilling zones: {e}")
return {
"success": False,
"error": str(e),
"project_path": project_path,
}

View File

@ -0,0 +1,131 @@
"""
Project management tools for KiCad MCP server.
Provides tools for discovering, inspecting, and opening KiCad projects
on the local filesystem.
"""
import logging
import os
from typing import Any
from mckicad.server import mcp
from mckicad.utils.file_utils import get_project_files, load_project_json
from mckicad.utils.kicad_utils import find_kicad_projects, open_kicad_project
logger = logging.getLogger(__name__)
@mcp.tool()
def list_projects() -> dict[str, Any]:
"""Find and list all KiCad projects in configured search paths.
Scans KICAD_SEARCH_PATHS and default project directories for
.kicad_pro files. Returns project name, path, relative path,
and last-modified timestamp for each discovered project.
Returns:
Dictionary with success status, project list, and count.
"""
logger.info("Scanning for KiCad projects")
try:
projects = find_kicad_projects()
logger.info("Found %d KiCad project(s)", len(projects))
return {
"success": True,
"data": projects,
"count": len(projects),
"error": None,
}
except Exception as e:
logger.error("Failed to list projects: %s", e)
return {
"success": False,
"data": [],
"count": 0,
"error": str(e),
}
@mcp.tool()
def get_project_structure(project_path: str) -> dict[str, Any]:
"""Get the file structure and metadata of a KiCad project.
Enumerates all files associated with a .kicad_pro project file
(schematic, PCB, netlist, BOM exports, etc.) and loads project
metadata from the JSON project file.
Args:
project_path: Absolute path to the .kicad_pro file.
Returns:
Dictionary with project name, directory, file map, and metadata.
"""
logger.info("Getting project structure for: %s", project_path)
if not os.path.exists(project_path):
logger.warning("Project file not found: %s", project_path)
return {
"success": False,
"data": None,
"error": f"Project not found: {project_path}",
}
try:
project_dir = os.path.dirname(project_path)
# Strip .kicad_pro extension to get the project name
basename = os.path.basename(project_path)
project_name = basename.rsplit(".kicad_pro", 1)[0] if basename.endswith(".kicad_pro") else basename
files = get_project_files(project_path)
metadata = {}
project_data = load_project_json(project_path)
if project_data and "metadata" in project_data:
metadata = project_data["metadata"]
logger.info(
"Project '%s' has %d associated file(s)", project_name, len(files)
)
return {
"success": True,
"data": {
"name": project_name,
"path": project_path,
"directory": project_dir,
"files": files,
"metadata": metadata,
},
"error": None,
}
except Exception as e:
logger.error("Failed to get project structure for %s: %s", project_path, e)
return {
"success": False,
"data": None,
"error": str(e),
}
@mcp.tool()
def open_project(project_path: str) -> dict[str, Any]:
"""Open a KiCad project in the KiCad application.
Launches KiCad (or the system default handler) with the specified
.kicad_pro file. Uses platform-appropriate open commands (open on
macOS, xdg-open on Linux).
Args:
project_path: Absolute path to the .kicad_pro file.
Returns:
Dictionary with success status and any error output.
"""
logger.info("Opening project: %s", project_path)
result = open_kicad_project(project_path)
if result.get("success"):
logger.info("Project opened successfully: %s", project_path)
else:
logger.warning("Failed to open project: %s", result.get("error"))
return result

View File

@ -0,0 +1,369 @@
"""
FreeRouting integration tools for automated PCB routing.
Wraps the FreeRouting autorouter engine and KiCad IPC API to provide
automated routing, routing quality analysis, and capability checking
through MCP tool interfaces.
"""
import logging
from typing import Any
from mckicad.server import mcp
from mckicad.utils.file_utils import get_project_files
from mckicad.utils.freerouting import FreeRoutingEngine, check_routing_prerequisites
from mckicad.utils.ipc_client import check_kicad_availability, kicad_ipc_session
logger = logging.getLogger(__name__)
@mcp.tool()
def check_routing_capability() -> dict[str, Any]:
"""Check whether automated PCB routing is available on this system.
Verifies that all required components are installed and properly
configured: KiCad IPC API (for real-time board access), FreeRouting
JAR (for autorouting), and KiCad CLI (for DSN/SES file conversion).
Call this before attempting any routing operations to confirm the
toolchain is ready.
Returns:
Dictionary with overall readiness status, per-component status,
and a summary of available capabilities.
"""
try:
status = check_routing_prerequisites()
return {
"success": True,
"routing_available": status["overall_ready"],
"message": status["message"],
"component_status": status["components"],
"capabilities": {
"automated_routing": status["overall_ready"],
"interactive_placement": status["components"]
.get("kicad_ipc", {})
.get("available", False),
"optimization": status["overall_ready"],
"real_time_updates": status["components"]
.get("kicad_ipc", {})
.get("available", False),
},
}
except Exception as e:
logger.error(f"Error checking routing capability: {e}")
return {
"success": False,
"error": str(e),
"routing_available": False,
}
@mcp.tool()
def route_pcb_automatically(
project_path: str,
routing_strategy: str = "balanced",
preserve_existing: bool = False,
optimization_level: str = "standard",
) -> dict[str, Any]:
"""Run the FreeRouting autorouter on a KiCad PCB.
Takes a board with placed components and automatically routes all
(or remaining) copper connections. The workflow is: export DSN from
KiCad CLI, run FreeRouting, import the routed SES file back.
Args:
project_path: Path to the KiCad project (.kicad_pro) or directory
containing one.
routing_strategy: Controls via cost and iteration depth.
"conservative" minimises vias and iterations (quick, safe).
"balanced" is a good default for most 2-layer boards.
"aggressive" allows more vias and iterations for dense boards.
preserve_existing: When True, existing routed traces are kept and
only unrouted nets are processed.
optimization_level: Post-routing cleanup pass intensity.
"none" skips optimisation.
"standard" runs a single cleanup pass.
"aggressive" doubles iteration count and tightens the
improvement threshold.
Returns:
Dictionary with routing results including pre/post statistics,
routing report, and the configuration that was used.
"""
try:
files = get_project_files(project_path)
if "pcb" not in files:
return {
"success": False,
"error": "PCB file not found in project",
}
board_path = files["pcb"]
# Map strategy name to FreeRouting parameter set
routing_configs: dict[str, dict[str, int | float | bool]] = {
"conservative": {
"via_costs": 30,
"start_ripup_costs": 50,
"max_iterations": 500,
"automatic_neckdown": False,
"postroute_optimization": optimization_level != "none",
},
"balanced": {
"via_costs": 50,
"start_ripup_costs": 100,
"max_iterations": 1000,
"automatic_neckdown": True,
"postroute_optimization": optimization_level != "none",
},
"aggressive": {
"via_costs": 80,
"start_ripup_costs": 200,
"max_iterations": 2000,
"automatic_neckdown": True,
"postroute_optimization": True,
},
}
config = routing_configs.get(routing_strategy, routing_configs["balanced"])
if optimization_level == "aggressive":
config.update(
{
"improvement_threshold": 0.005,
"max_iterations": config["max_iterations"] * 2,
}
)
engine = FreeRoutingEngine()
availability = engine.check_freerouting_availability()
if not availability["available"]:
return {
"success": False,
"error": f"FreeRouting not available: {availability['message']}",
"routing_strategy": routing_strategy,
}
result = engine.route_board_complete(
board_path,
routing_config=config,
preserve_existing=preserve_existing,
)
result.update(
{
"routing_strategy": routing_strategy,
"optimization_level": optimization_level,
"project_path": project_path,
"board_path": board_path,
}
)
return result
except Exception as e:
logger.error(f"Error in automated routing: {e}")
return {
"success": False,
"error": str(e),
"project_path": project_path,
"routing_strategy": routing_strategy,
}
@mcp.tool()
def analyze_routing_quality(project_path: str) -> dict[str, Any]:
"""Analyse the current PCB routing for quality and potential issues.
Connects to a running KiCad instance via IPC and inspects tracks,
vias, nets, and footprints to evaluate signal integrity risk,
routing density, via usage, thermal considerations, and
manufacturability.
Returns a numeric quality score (0-100) together with per-category
breakdowns and actionable recommendations.
Args:
project_path: Path to the KiCad project (.kicad_pro) or directory
containing one.
Returns:
Dictionary with quality score, category analyses, and
improvement recommendations.
"""
try:
files = get_project_files(project_path)
if "pcb" not in files:
return {
"success": False,
"error": "PCB file not found in project",
}
board_path = files["pcb"]
ipc_status = check_kicad_availability()
if not ipc_status["available"]:
return {
"success": False,
"error": f"KiCad IPC not available: {ipc_status['message']}",
"project_path": project_path,
}
with kicad_ipc_session(board_path=board_path) as client:
tracks = client.get_tracks()
nets = client.get_nets()
footprints = client.get_footprints()
connectivity = client.check_connectivity()
# --- per-category analysis ---
routing_density = _analyze_routing_density(tracks, footprints)
via_analysis = _analyze_via_usage(tracks)
trace_analysis = _analyze_trace_characteristics(tracks)
signal_integrity = _analyze_signal_integrity(tracks, nets)
thermal_analysis = _analyze_thermal_aspects(tracks, footprints)
manufacturability = _analyze_manufacturability(tracks)
quality_analysis = {
"connectivity_analysis": connectivity,
"routing_density": routing_density,
"via_analysis": via_analysis,
"trace_analysis": trace_analysis,
"signal_integrity": signal_integrity,
"thermal_analysis": thermal_analysis,
"manufacturability": manufacturability,
}
quality_score = _calculate_quality_score(quality_analysis)
recommendations = _generate_routing_recommendations(quality_analysis)
return {
"success": True,
"project_path": project_path,
"quality_score": quality_score,
"analysis": quality_analysis,
"recommendations": recommendations,
"summary": f"Routing quality score: {quality_score}/100",
}
except Exception as e:
logger.error(f"Error in routing quality analysis: {e}")
return {
"success": False,
"error": str(e),
"project_path": project_path,
}
# ---------------------------------------------------------------------------
# Private helpers for routing quality analysis
# ---------------------------------------------------------------------------
def _analyze_routing_density(tracks: list, footprints: list) -> dict[str, Any]:
"""Compute track-to-component density ratio."""
ratio = len(tracks) / max(len(footprints), 1)
if ratio > 4.0:
rating = "high"
elif ratio > 1.5:
rating = "medium"
else:
rating = "low"
return {
"total_tracks": len(tracks),
"total_footprints": len(footprints),
"track_per_component": round(ratio, 2),
"density_rating": rating,
}
def _analyze_via_usage(tracks: list) -> dict[str, Any]:
"""Count vias and assess usage density."""
via_count = sum(1 for t in tracks if hasattr(t, "drill"))
track_count = len(tracks) - via_count
via_ratio = via_count / max(track_count, 1)
return {
"total_vias": via_count,
"total_traces": track_count,
"via_to_trace_ratio": round(via_ratio, 3),
"via_density": "high" if via_ratio > 0.3 else "normal",
}
def _analyze_trace_characteristics(tracks: list) -> dict[str, Any]:
"""Summarise trace count and basic statistics."""
trace_count = sum(1 for t in tracks if not hasattr(t, "drill"))
return {
"total_traces": trace_count,
"width_distribution": {"standard": trace_count},
}
def _analyze_signal_integrity(tracks: list, nets: list) -> dict[str, Any]:
"""Flag nets whose names suggest high-speed or clock signals."""
clock_nets = sum(
1
for n in nets
if n.name and any(kw in n.name.lower() for kw in ("clk", "clock", "mclk"))
)
return {
"critical_nets": clock_nets,
"high_speed_traces": 0,
"impedance_controlled": False,
}
def _analyze_thermal_aspects(tracks: list, footprints: list) -> dict[str, Any]:
"""Basic thermal heuristic (placeholder for deeper analysis)."""
return {
"thermal_vias": 0,
"power_trace_width": "adequate",
"heat_dissipation": "good",
}
def _analyze_manufacturability(tracks: list) -> dict[str, Any]:
"""Placeholder manufacturability assessment."""
return {
"minimum_trace_width_mm": 0.1,
"minimum_spacing_mm": 0.1,
"manufacturability_rating": "good",
}
def _calculate_quality_score(analysis: dict[str, Any]) -> int:
"""Derive a 0-100 quality score from the sub-analyses."""
base = 75
connectivity = analysis.get("connectivity_analysis", {})
completion = connectivity.get("routing_completion", 0)
# Completion contributes up to 25 points
return min(int(base + completion * 0.25), 100)
def _generate_routing_recommendations(analysis: dict[str, Any]) -> list[str]:
"""Produce a list of human-readable improvement suggestions."""
recs: list[str] = []
connectivity = analysis.get("connectivity_analysis", {})
unrouted = connectivity.get("unrouted_nets", 0)
if unrouted > 0:
recs.append(f"Complete routing for {unrouted} unrouted net(s)")
via_info = analysis.get("via_analysis", {})
if via_info.get("via_density") == "high":
recs.append("Consider reducing via count for improved signal integrity")
density = analysis.get("routing_density", {})
if density.get("density_rating") == "high":
recs.append("High routing density detected -- verify clearance rules")
recs.append("Run DRC check to validate design rules after routing changes")
recs.append("Verify impedance control for high-speed signals")
return recs

View File

@ -0,0 +1,699 @@
"""
Schematic creation and manipulation tools for the mckicad MCP server.
Wraps the kicad-sch-api library to provide schematic editing through MCP tools.
Designed so that the underlying engine can be swapped to kipy IPC once KiCad
exposes a schematic API over its IPC transport.
"""
import logging
import os
from typing import Any
from mckicad.server import mcp
logger = logging.getLogger(__name__)
# ---------------------------------------------------------------------------
# Engine abstraction — swap point for future kipy IPC schematic support
# ---------------------------------------------------------------------------
_HAS_SCH_API = False
try:
from kicad_sch_api import create_schematic as _ksa_create
from kicad_sch_api import get_symbol_info as _ksa_get_symbol_info
from kicad_sch_api import load_schematic as _ksa_load
from kicad_sch_api import search_symbols as _ksa_search
_HAS_SCH_API = True
except ImportError:
logger.warning(
"kicad-sch-api not installed — schematic tools will return helpful errors. "
"Install with: uv add kicad-sch-api"
)
def _get_schematic_engine() -> str:
"""Return the name of the active schematic manipulation engine.
Currently: ``kicad-sch-api`` (file-level manipulation)
Future: ``kipy`` IPC when KiCad adds a schematic API over its IPC transport.
"""
if _HAS_SCH_API:
return "kicad-sch-api"
return "none"
def _require_sch_api() -> dict[str, Any] | None:
"""Return an error dict if kicad-sch-api is unavailable, else None."""
if not _HAS_SCH_API:
return {
"success": False,
"error": ("kicad-sch-api is not installed. Install it with: uv add kicad-sch-api"),
"engine": "none",
}
return None
def _validate_schematic_path(path: str, must_exist: bool = True) -> dict[str, Any] | None:
"""Validate a schematic file path. Returns an error dict on failure, else None."""
if not path:
return {"success": False, "error": "Schematic path must be a non-empty string"}
expanded = os.path.expanduser(path)
if not expanded.endswith(".kicad_sch"):
return {
"success": False,
"error": f"Path must end with .kicad_sch, got: {path}",
}
if must_exist and not os.path.isfile(expanded):
return {
"success": False,
"error": f"Schematic file not found: {expanded}",
}
return None
def _expand(path: str) -> str:
"""Expand ~ and return an absolute path."""
return os.path.abspath(os.path.expanduser(path))
# ---------------------------------------------------------------------------
# Tools
# ---------------------------------------------------------------------------
@mcp.tool()
def create_schematic(name: str, output_path: str) -> dict[str, Any]:
"""Create a new, empty KiCad schematic file.
Generates a valid .kicad_sch file at the specified location that can be
opened directly in KiCad or extended with add_component / add_wire calls.
Args:
name: Human-readable name for the schematic (e.g. "Power Supply").
output_path: Destination file path. Must end with .kicad_sch.
Parent directory will be created if it does not exist.
Returns:
Dictionary with ``success``, ``path``, and ``engine`` keys.
"""
err = _require_sch_api()
if err:
return err
output_path = _expand(output_path)
if not output_path.endswith(".kicad_sch"):
return {
"success": False,
"error": f"output_path must end with .kicad_sch, got: {output_path}",
}
try:
parent = os.path.dirname(output_path)
if parent:
os.makedirs(parent, exist_ok=True)
sch = _ksa_create(name)
sch.save(output_path)
logger.info("Created schematic '%s' at %s", name, output_path)
return {
"success": True,
"path": output_path,
"name": name,
"engine": _get_schematic_engine(),
}
except Exception as e:
logger.error("Failed to create schematic '%s': %s", name, e)
return {"success": False, "error": str(e)}
@mcp.tool()
def add_component(
schematic_path: str,
lib_id: str,
reference: str,
value: str,
x: float,
y: float,
) -> dict[str, Any]:
"""Place a symbol (component) on a KiCad schematic.
The symbol is identified by its KiCad library ID (e.g. ``Device:R``,
``power:GND``). Position is in KiCad schematic coordinate space where
the origin is top-left.
Args:
schematic_path: Path to an existing .kicad_sch file.
lib_id: KiCad library identifier such as ``Device:R`` or ``Connector:Conn_01x04``.
reference: Reference designator (e.g. ``R1``, ``C3``, ``U2``).
value: Component value string (e.g. ``10k``, ``100nF``, ``ATmega328P``).
x: Horizontal position in schematic units.
y: Vertical position in schematic units.
Returns:
Dictionary with ``success``, component ``reference``, and ``lib_id``.
"""
err = _require_sch_api()
if err:
return err
schematic_path = _expand(schematic_path)
verr = _validate_schematic_path(schematic_path)
if verr:
return verr
try:
sch = _ksa_load(schematic_path)
sch.components.add(
lib_id=lib_id,
reference=reference,
value=value,
position=(x, y),
)
sch.save(schematic_path)
logger.info("Added %s (%s) to %s at (%.1f, %.1f)", reference, lib_id, schematic_path, x, y)
return {
"success": True,
"reference": reference,
"lib_id": lib_id,
"value": value,
"position": {"x": x, "y": y},
"schematic_path": schematic_path,
"engine": _get_schematic_engine(),
}
except Exception as e:
logger.error("Failed to add component %s to %s: %s", reference, schematic_path, e)
return {
"success": False,
"error": str(e),
"schematic_path": schematic_path,
}
@mcp.tool()
def search_components(query: str, library: str | None = None) -> dict[str, Any]:
"""Search KiCad symbol libraries for components matching a query.
Useful for discovering available symbols before placing them with
``add_component``. Results include library IDs that can be passed
directly to ``add_component``'s ``lib_id`` parameter.
Args:
query: Search term (e.g. ``resistor``, ``op amp``, ``STM32``).
library: Optional library name to restrict the search
(e.g. ``Device``, ``MCU_ST_STM32``). Searches all
libraries when omitted.
Returns:
Dictionary with ``success`` and a ``results`` list of matching symbols.
"""
err = _require_sch_api()
if err:
return err
try:
raw_results = _ksa_search(query)
# Filter by library when requested
if library:
raw_results = [r for r in raw_results if _matches_library(r, library)]
results = []
for item in raw_results:
entry: dict[str, Any] = {}
if isinstance(item, dict):
entry = item
elif isinstance(item, str):
entry = {"lib_id": item}
else:
# Object with attributes
entry = {
"lib_id": getattr(item, "lib_id", str(item)),
"name": getattr(item, "name", None),
"description": getattr(item, "description", None),
"keywords": getattr(item, "keywords", None),
}
results.append(entry)
logger.info("Symbol search for '%s' returned %d results", query, len(results))
return {
"success": True,
"query": query,
"library": library,
"count": len(results),
"results": results,
"engine": _get_schematic_engine(),
}
except Exception as e:
logger.error("Symbol search failed for '%s': %s", query, e)
return {"success": False, "error": str(e), "query": query}
@mcp.tool()
def add_wire(
schematic_path: str,
start_x: float,
start_y: float,
end_x: float,
end_y: float,
) -> dict[str, Any]:
"""Draw a wire segment between two points on a schematic.
Wires create electrical connections between component pins, labels,
and other wires. For connecting specific pins by reference designator,
see ``connect_pins`` which handles coordinate lookup automatically.
Args:
schematic_path: Path to an existing .kicad_sch file.
start_x: Starting X coordinate.
start_y: Starting Y coordinate.
end_x: Ending X coordinate.
end_y: Ending Y coordinate.
Returns:
Dictionary with ``success`` and the wire ``id``.
"""
err = _require_sch_api()
if err:
return err
schematic_path = _expand(schematic_path)
verr = _validate_schematic_path(schematic_path)
if verr:
return verr
try:
sch = _ksa_load(schematic_path)
wire_id = sch.add_wire(start=(start_x, start_y), end=(end_x, end_y))
sch.save(schematic_path)
logger.info(
"Added wire from (%.1f, %.1f) to (%.1f, %.1f) in %s",
start_x,
start_y,
end_x,
end_y,
schematic_path,
)
return {
"success": True,
"wire_id": wire_id,
"start": {"x": start_x, "y": start_y},
"end": {"x": end_x, "y": end_y},
"schematic_path": schematic_path,
"engine": _get_schematic_engine(),
}
except Exception as e:
logger.error("Failed to add wire in %s: %s", schematic_path, e)
return {"success": False, "error": str(e), "schematic_path": schematic_path}
@mcp.tool()
def connect_pins(
schematic_path: str,
from_ref: str,
from_pin: str,
to_ref: str,
to_pin: str,
) -> dict[str, Any]:
"""Wire two component pins together by reference designator and pin name.
This is the high-level wiring tool -- it looks up pin positions from the
component references and draws a wire between them. Prefer this over
``add_wire`` when you know the component references and pin identifiers.
Args:
schematic_path: Path to an existing .kicad_sch file.
from_ref: Source component reference designator (e.g. ``R1``).
from_pin: Pin identifier on the source component (e.g. ``1``, ``A``).
to_ref: Destination component reference designator (e.g. ``R2``).
to_pin: Pin identifier on the destination component (e.g. ``2``, ``K``).
Returns:
Dictionary with ``success`` and the created wire ``id``.
"""
err = _require_sch_api()
if err:
return err
schematic_path = _expand(schematic_path)
verr = _validate_schematic_path(schematic_path)
if verr:
return verr
try:
sch = _ksa_load(schematic_path)
wire_id = sch.add_wire_between_pins(
component1_ref=from_ref,
pin1_number=from_pin,
component2_ref=to_ref,
pin2_number=to_pin,
)
sch.save(schematic_path)
logger.info(
"Connected %s pin %s -> %s pin %s in %s",
from_ref,
from_pin,
to_ref,
to_pin,
schematic_path,
)
return {
"success": True,
"wire_id": wire_id,
"from": {"reference": from_ref, "pin": from_pin},
"to": {"reference": to_ref, "pin": to_pin},
"schematic_path": schematic_path,
"engine": _get_schematic_engine(),
}
except Exception as e:
logger.error(
"Failed to connect %s.%s -> %s.%s in %s: %s",
from_ref,
from_pin,
to_ref,
to_pin,
schematic_path,
e,
)
return {
"success": False,
"error": str(e),
"schematic_path": schematic_path,
}
@mcp.tool()
def add_label(
schematic_path: str,
text: str,
x: float,
y: float,
global_label: bool = False,
) -> dict[str, Any]:
"""Add a net label or global label to a schematic.
Local labels connect nets within the same sheet. Global labels connect
nets across hierarchical sheets -- use global labels for power rails,
clock signals, and inter-sheet buses.
Args:
schematic_path: Path to an existing .kicad_sch file.
text: Label text (becomes the net name, e.g. ``GND``, ``SPI_CLK``).
x: Horizontal position in schematic units.
y: Vertical position in schematic units.
global_label: When True, creates a global label visible across all
hierarchical sheets. Defaults to a local label.
Returns:
Dictionary with ``success``, the ``label_id``, and label type.
"""
err = _require_sch_api()
if err:
return err
schematic_path = _expand(schematic_path)
verr = _validate_schematic_path(schematic_path)
if verr:
return verr
try:
sch = _ksa_load(schematic_path)
if global_label:
label_id = sch.add_global_label(text=text, position=(x, y))
label_type = "global"
else:
label_id = sch.add_label(text=text, position=(x, y))
label_type = "local"
sch.save(schematic_path)
logger.info(
"Added %s label '%s' at (%.1f, %.1f) in %s", label_type, text, x, y, schematic_path
)
return {
"success": True,
"label_id": label_id,
"text": text,
"label_type": label_type,
"position": {"x": x, "y": y},
"schematic_path": schematic_path,
"engine": _get_schematic_engine(),
}
except Exception as e:
logger.error("Failed to add label '%s' in %s: %s", text, schematic_path, e)
return {"success": False, "error": str(e), "schematic_path": schematic_path}
@mcp.tool()
def add_hierarchical_sheet(
schematic_path: str,
name: str,
filename: str,
x: float,
y: float,
width: float,
height: float,
) -> dict[str, Any]:
"""Add a hierarchical sub-sheet to a schematic.
Hierarchical sheets let you break a design into logical blocks. The
sub-sheet is represented as a rectangle on the parent schematic and
references a separate .kicad_sch file for the child sheet's contents.
Args:
schematic_path: Path to the parent .kicad_sch file.
name: Display name shown on the sheet symbol (e.g. ``Power Supply``).
filename: Filename of the child schematic (e.g. ``power_supply.kicad_sch``).
Will be resolved relative to the parent schematic's directory.
x: Top-left X position of the sheet rectangle.
y: Top-left Y position of the sheet rectangle.
width: Width of the sheet rectangle in schematic units.
height: Height of the sheet rectangle in schematic units.
Returns:
Dictionary with ``success`` and sheet metadata.
"""
err = _require_sch_api()
if err:
return err
schematic_path = _expand(schematic_path)
verr = _validate_schematic_path(schematic_path)
if verr:
return verr
if not filename.endswith(".kicad_sch"):
return {
"success": False,
"error": f"Sheet filename must end with .kicad_sch, got: {filename}",
}
try:
sch = _ksa_load(schematic_path)
sch.add_sheet(
name=name,
filename=filename,
position=(x, y),
size=(width, height),
)
sch.save(schematic_path)
logger.info(
"Added hierarchical sheet '%s' (%s) at (%.1f, %.1f) size %.1fx%.1f in %s",
name,
filename,
x,
y,
width,
height,
schematic_path,
)
return {
"success": True,
"sheet_name": name,
"sheet_filename": filename,
"position": {"x": x, "y": y},
"size": {"width": width, "height": height},
"schematic_path": schematic_path,
"engine": _get_schematic_engine(),
}
except Exception as e:
logger.error("Failed to add sheet '%s' in %s: %s", name, schematic_path, e)
return {"success": False, "error": str(e), "schematic_path": schematic_path}
@mcp.tool()
def list_components(schematic_path: str) -> dict[str, Any]:
"""List all components placed in a KiCad schematic.
Returns reference designators, library IDs, values, and positions for
every symbol instance on the schematic. Useful for verifying placement
or preparing to wire components with ``connect_pins``.
Args:
schematic_path: Path to a .kicad_sch file.
Returns:
Dictionary with ``success``, ``count``, and a ``components`` list.
"""
err = _require_sch_api()
if err:
return err
schematic_path = _expand(schematic_path)
verr = _validate_schematic_path(schematic_path)
if verr:
return verr
try:
sch = _ksa_load(schematic_path)
components: list[dict[str, Any]] = []
for comp in sch.components:
entry: dict[str, Any] = {
"reference": getattr(comp, "reference", None),
"lib_id": getattr(comp, "lib_id", None),
"value": getattr(comp, "value", None),
}
pos = getattr(comp, "position", None)
if pos is not None:
if isinstance(pos, (list, tuple)) and len(pos) >= 2:
entry["position"] = {"x": pos[0], "y": pos[1]}
else:
entry["position"] = str(pos)
components.append(entry)
logger.info("Listed %d components in %s", len(components), schematic_path)
return {
"success": True,
"count": len(components),
"components": components,
"schematic_path": schematic_path,
"engine": _get_schematic_engine(),
}
except Exception as e:
logger.error("Failed to list components in %s: %s", schematic_path, e)
return {"success": False, "error": str(e), "schematic_path": schematic_path}
@mcp.tool()
def get_schematic_info(schematic_path: str) -> dict[str, Any]:
"""Get metadata, statistics, and validation results for a KiCad schematic.
Provides a single-call overview of the schematic including component
counts, wire counts, label inventory, and any validation issues
detected by the parser.
Args:
schematic_path: Path to a .kicad_sch file.
Returns:
Dictionary with ``success``, ``statistics``, and ``validation`` data.
"""
err = _require_sch_api()
if err:
return err
schematic_path = _expand(schematic_path)
verr = _validate_schematic_path(schematic_path)
if verr:
return verr
try:
sch = _ksa_load(schematic_path)
# Gather statistics
stats = sch.get_statistics()
# Run validation
issues = sch.validate()
# Try to extract symbol-level details via get_symbol_info for
# each unique lib_id in the schematic, but don't fail if the
# function is unavailable or individual lookups fail.
lib_ids_seen: set[str] = set()
symbol_details: list[dict[str, Any]] = []
for comp in sch.components:
lid = getattr(comp, "lib_id", None)
if lid and lid not in lib_ids_seen:
lib_ids_seen.add(lid)
try:
info = _ksa_get_symbol_info(lid)
if isinstance(info, dict):
symbol_details.append(info)
else:
symbol_details.append(
{
"lib_id": lid,
"name": getattr(info, "name", str(info)),
"description": getattr(info, "description", None),
"pin_count": getattr(info, "pin_count", None),
}
)
except Exception:
# Non-critical -- just skip symbols we can't look up
symbol_details.append({"lib_id": lid, "lookup_failed": True})
# Normalise stats and issues to dicts if they aren't already
if not isinstance(stats, dict):
stats = {"raw": str(stats)}
if not isinstance(issues, list):
issues = [str(issues)] if issues else []
validation_passed = len(issues) == 0
logger.info("Retrieved info for %s: %d issues", schematic_path, len(issues))
return {
"success": True,
"schematic_path": schematic_path,
"statistics": stats,
"validation": {
"passed": validation_passed,
"issue_count": len(issues),
"issues": issues,
},
"symbol_details": symbol_details,
"engine": _get_schematic_engine(),
}
except Exception as e:
logger.error("Failed to get schematic info for %s: %s", schematic_path, e)
return {"success": False, "error": str(e), "schematic_path": schematic_path}
# ---------------------------------------------------------------------------
# Internal helpers
# ---------------------------------------------------------------------------
def _matches_library(item: Any, library: str) -> bool:
"""Check whether a search result belongs to the given library."""
lib_id = None
if isinstance(item, dict):
lib_id = item.get("lib_id", "")
elif isinstance(item, str):
lib_id = item
else:
lib_id = getattr(item, "lib_id", str(item))
if not lib_id:
return False
# lib_id format is "Library:Symbol" -- match on the library portion
if ":" in lib_id:
return lib_id.split(":")[0].lower() == library.lower()
return library.lower() in str(lib_id).lower()

View File

View File

@ -6,7 +6,9 @@ import json
import os
from typing import Any
from mckicad.utils.kicad_utils import get_project_name_from_path
from mckicad.config import DATA_EXTENSIONS, KICAD_EXTENSIONS
from .kicad_utils import get_project_name_from_path
def get_project_files(project_path: str) -> dict[str, str]:
@ -18,8 +20,6 @@ def get_project_files(project_path: str) -> dict[str, str]:
Returns:
Dictionary mapping file types to file paths
"""
from mckicad.config import DATA_EXTENSIONS, KICAD_EXTENSIONS
project_dir = os.path.dirname(project_path)
project_name = get_project_name_from_path(project_path)
@ -66,6 +66,7 @@ def load_project_json(project_path: str) -> dict[str, Any] | None:
"""
try:
with open(project_path) as f:
return json.load(f)
data: dict[str, Any] = json.load(f)
return data
except Exception:
return None

View File

@ -19,7 +19,7 @@ from typing import Any
from kipy.board_types import BoardLayer
from mckicad.utils.ipc_client import kicad_ipc_session
from .ipc_client import kicad_ipc_session
logger = logging.getLogger(__name__)
@ -32,7 +32,7 @@ class FreeRoutingError(Exception):
class FreeRoutingEngine:
"""
Engine for automated PCB routing using FreeRouting.
Handles the complete workflow:
1. Export DSN file from KiCad board
2. Process with FreeRouting autorouter
@ -48,7 +48,7 @@ class FreeRoutingEngine:
):
"""
Initialize FreeRouting engine.
Args:
freerouting_jar_path: Path to FreeRouting JAR file
java_executable: Java executable command
@ -84,7 +84,7 @@ class FreeRoutingEngine:
def find_freerouting_jar(self) -> str | None:
"""
Attempt to find FreeRouting JAR file in common locations.
Returns:
Path to FreeRouting JAR if found, None otherwise
"""
@ -108,7 +108,7 @@ class FreeRoutingEngine:
def check_freerouting_availability(self) -> dict[str, Any]:
"""
Check if FreeRouting is available and working.
Returns:
Dictionary with availability status
"""
@ -173,12 +173,12 @@ class FreeRoutingEngine:
) -> bool:
"""
Export DSN file from KiCad board using KiCad CLI.
Args:
board_path: Path to .kicad_pcb file
dsn_output_path: Output path for DSN file
routing_options: Optional routing configuration
Returns:
True if export successful
"""
@ -219,7 +219,7 @@ class FreeRoutingEngine:
def _customize_dsn_file(self, dsn_path: str, options: dict[str, Any]):
"""
Customize DSN file with specific routing options.
Args:
dsn_path: Path to DSN file
options: Routing configuration options
@ -229,7 +229,6 @@ class FreeRoutingEngine:
content = f.read()
# Add routing directives to DSN file
# This is a simplified implementation - real DSN modification would be more complex
modifications = []
if "via_costs" in options:
@ -264,12 +263,12 @@ class FreeRoutingEngine:
) -> tuple[bool, str | None]:
"""
Run FreeRouting autorouter on DSN file.
Args:
dsn_path: Path to input DSN file
output_directory: Directory for output files
routing_config: Optional routing configuration
Returns:
Tuple of (success, output_ses_path)
"""
@ -337,12 +336,12 @@ class FreeRoutingEngine:
) -> bool:
"""
Import SES routing results back into KiCad board.
Args:
board_path: Path to .kicad_pcb file
ses_path: Path to SES file with routing results
backup_original: Whether to backup original board file
Returns:
True if import successful
"""
@ -390,12 +389,12 @@ class FreeRoutingEngine:
) -> dict[str, Any]:
"""
Complete automated routing workflow for a KiCad board.
Args:
board_path: Path to .kicad_pcb file
routing_config: Optional routing configuration
preserve_existing: Whether to preserve existing routing
Returns:
Dictionary with routing results and statistics
"""
@ -466,16 +465,17 @@ class FreeRoutingEngine:
def _analyze_board_connectivity(self, board_path: str) -> dict[str, Any]:
"""
Analyze board connectivity status.
Args:
board_path: Path to board file
Returns:
Connectivity statistics
"""
try:
with kicad_ipc_session(board_path=board_path) as client:
return client.check_connectivity()
result: dict[str, Any] = client.check_connectivity()
return result
except Exception as e:
logger.warning(f"Could not analyze connectivity via IPC: {e}")
return {"error": str(e)}
@ -488,19 +488,19 @@ class FreeRoutingEngine:
) -> dict[str, Any]:
"""
Generate routing completion report.
Args:
pre_stats: Pre-routing statistics
post_stats: Post-routing statistics
config: Routing configuration used
Returns:
Routing report
"""
report = {
report: dict[str, Any] = {
"routing_improvement": {},
"completion_metrics": {},
"recommendations": []
"recommendations": [],
}
if "routing_completion" in pre_stats and "routing_completion" in post_stats:
@ -542,11 +542,11 @@ class FreeRoutingEngine:
) -> dict[str, Any]:
"""
Optimize routing parameters for best results on a specific board.
Args:
board_path: Path to board file
target_completion: Target routing completion percentage
Returns:
Optimized parameters and results
"""
@ -639,18 +639,18 @@ class FreeRoutingEngine:
def check_routing_prerequisites() -> dict[str, Any]:
"""
Check if all prerequisites for automated routing are available.
Returns:
Dictionary with prerequisite status
"""
status = {
status: dict[str, Any] = {
"overall_ready": False,
"components": {}
"components": {},
}
# Check KiCad IPC API
try:
from mckicad.utils.ipc_client import check_kicad_availability
from .ipc_client import check_kicad_availability
kicad_status = check_kicad_availability()
status["components"]["kicad_ipc"] = kicad_status
except Exception as e:

View File

@ -12,7 +12,7 @@ from typing import Any
from kipy import KiCad
from kipy.board import Board
from kipy.board_types import FootprintInstance, Net, Track, Via
from kipy.board_types import ArcTrack, FootprintInstance, Net, Track, Via
from kipy.geometry import Vector2
from kipy.project import Project
@ -27,7 +27,7 @@ class KiCadIPCError(Exception):
class KiCadIPCClient:
"""
High-level client for KiCad IPC API operations.
Provides a convenient interface for common operations needed by the MCP server,
including project management, component placement, routing, and file operations.
"""
@ -35,7 +35,7 @@ class KiCadIPCClient:
def __init__(self, socket_path: str | None = None, client_name: str | None = None):
"""
Initialize the KiCad IPC client.
Args:
socket_path: KiCad IPC Unix socket path (None for default)
client_name: Client name for identification (None for default)
@ -49,10 +49,10 @@ class KiCadIPCClient:
def connect(self, log_failures: bool = False) -> bool:
"""
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
"""
@ -100,21 +100,23 @@ class KiCadIPCClient:
def get_version(self) -> str:
"""Get KiCad version."""
self.ensure_connected()
return self._kicad.get_version()
assert self._kicad is not None
return str(self._kicad.get_version())
def open_project(self, project_path: str) -> bool:
"""
Open a KiCad project.
Args:
project_path: Path to .kicad_pro file
Returns:
True if project opened successfully
"""
self.ensure_connected()
assert self._kicad is not None
try:
self._current_project = self._kicad.get_project()
self._current_project = self._kicad.get_project() # type: ignore[call-arg]
logger.info(f"Got project reference: {project_path}")
return self._current_project is not None
except Exception as e:
@ -124,14 +126,15 @@ class KiCadIPCClient:
def open_board(self, board_path: str) -> bool:
"""
Open a KiCad board.
Args:
board_path: Path to .kicad_pcb file
Returns:
True if board opened successfully
"""
self.ensure_connected()
assert self._kicad is not None
try:
self._current_board = self._kicad.get_board()
logger.info(f"Got board reference: {board_path}")
@ -159,11 +162,12 @@ class KiCadIPCClient:
def commit_transaction(self, message: str = "MCP operation"):
"""
Context manager for grouping operations into a single commit.
Args:
message: Commit message for undo history
"""
self.ensure_board_open()
assert self._current_board is not None
commit = self._current_board.begin_commit()
try:
yield
@ -176,36 +180,38 @@ class KiCadIPCClient:
def get_footprints(self) -> list[FootprintInstance]:
"""Get all footprints on the current board."""
self.ensure_board_open()
assert self._current_board is not None
return list(self._current_board.get_footprints())
def get_footprint_by_reference(self, reference: str) -> FootprintInstance | None:
"""
Get footprint by reference designator.
Args:
reference: Component reference (e.g., "R1", "U3")
Returns:
FootprintInstance if found, None otherwise
"""
footprints = self.get_footprints()
for fp in footprints:
if fp.reference == reference:
if fp.reference == reference: # type: ignore[attr-defined]
return fp
return None
def move_footprint(self, reference: str, position: Vector2) -> bool:
"""
Move a footprint to a new position.
Args:
reference: Component reference
position: New position (Vector2)
Returns:
True if successful
"""
self.ensure_board_open()
assert self._current_board is not None
try:
footprint = self.get_footprint_by_reference(reference)
if not footprint:
@ -225,26 +231,27 @@ class KiCadIPCClient:
def rotate_footprint(self, reference: str, angle_degrees: float) -> bool:
"""
Rotate a footprint.
Args:
reference: Component reference
angle_degrees: Rotation angle in degrees
Returns:
True if successful
"""
self.ensure_board_open()
assert self._current_board is not None
try:
footprint = self.get_footprint_by_reference(reference)
if not footprint:
logger.error(f"Footprint {reference} not found")
return False
with self.commit_transaction(f"Rotate {reference} by {angle_degrees}°"):
footprint.rotation = angle_degrees
with self.commit_transaction(f"Rotate {reference} by {angle_degrees}"):
footprint.rotation = angle_degrees # type: ignore[attr-defined]
self._current_board.update_items(footprint)
logger.info(f"Rotated {reference} by {angle_degrees}°")
logger.info(f"Rotated {reference} by {angle_degrees}")
return True
except Exception as e:
logger.error(f"Failed to rotate footprint {reference}: {e}")
@ -254,15 +261,16 @@ class KiCadIPCClient:
def get_nets(self) -> list[Net]:
"""Get all nets on the current board."""
self.ensure_board_open()
assert self._current_board is not None
return list(self._current_board.get_nets())
def get_net_by_name(self, name: str) -> Net | None:
"""
Get net by name.
Args:
name: Net name
Returns:
Net if found, None otherwise
"""
@ -272,9 +280,10 @@ class KiCadIPCClient:
return net
return None
def get_tracks(self) -> list[Track | Via]:
def get_tracks(self) -> list[Track | Via | ArcTrack]:
"""Get all tracks and vias on the current board."""
self.ensure_board_open()
assert self._current_board is not None
tracks = list(self._current_board.get_tracks())
vias = list(self._current_board.get_vias())
return tracks + vias
@ -282,10 +291,10 @@ class KiCadIPCClient:
def delete_tracks_by_net(self, net_name: str) -> bool:
"""
Delete all tracks for a specific net.
Args:
net_name: Name of the net to clear
Returns:
True if successful
"""
@ -302,6 +311,7 @@ class KiCadIPCClient:
tracks_to_delete.append(track)
if tracks_to_delete:
assert self._current_board is not None
with self.commit_transaction(f"Delete tracks for net {net_name}"):
self._current_board.remove_items(tracks_to_delete)
@ -316,6 +326,7 @@ class KiCadIPCClient:
def save_board(self) -> bool:
"""Save the current board."""
self.ensure_board_open()
assert self._current_board is not None
try:
self._current_board.save()
logger.info("Board saved successfully")
@ -327,15 +338,16 @@ class KiCadIPCClient:
def save_board_as(self, filename: str, overwrite: bool = False) -> bool:
"""
Save the current board to a new file.
Args:
filename: Target filename
overwrite: Whether to overwrite existing file
Returns:
True if successful
"""
self.ensure_board_open()
assert self._current_board is not None
try:
self._current_board.save_as(filename, overwrite=overwrite)
logger.info(f"Board saved as: {filename}")
@ -347,6 +359,7 @@ class KiCadIPCClient:
def get_board_as_string(self) -> str | None:
"""Get board content as KiCad file format string."""
self.ensure_board_open()
assert self._current_board is not None
try:
return self._current_board.get_as_string()
except Exception as e:
@ -356,14 +369,15 @@ class KiCadIPCClient:
def refill_zones(self, timeout: float = 30.0) -> bool:
"""
Refill all zones on the board.
Args:
timeout: Maximum time to wait for completion
Returns:
True if successful
"""
self.ensure_board_open()
assert self._current_board is not None
try:
self._current_board.refill_zones(block=True, max_poll_seconds=timeout)
logger.info("Zones refilled successfully")
@ -376,7 +390,7 @@ class KiCadIPCClient:
def get_board_statistics(self) -> dict[str, Any]:
"""
Get comprehensive board statistics.
Returns:
Dictionary with board statistics
"""
@ -386,7 +400,8 @@ class KiCadIPCClient:
nets = self.get_nets()
tracks = self.get_tracks()
stats = {
assert self._current_board is not None
stats: dict[str, Any] = {
"footprint_count": len(footprints),
"net_count": len(nets),
"track_count": len([t for t in tracks if isinstance(t, Track)]),
@ -395,9 +410,9 @@ class KiCadIPCClient:
}
# Component breakdown by reference prefix
component_types = {}
component_types: dict[str, int] = {}
for fp in footprints:
prefix = ''.join(c for c in fp.reference if c.isalpha())
prefix = ''.join(c for c in fp.reference if c.isalpha()) # type: ignore[attr-defined]
component_types[prefix] = component_types.get(prefix, 0) + 1
stats["component_types"] = component_types
@ -411,7 +426,7 @@ class KiCadIPCClient:
def check_connectivity(self) -> dict[str, Any]:
"""
Check board connectivity status.
Returns:
Dictionary with connectivity information
"""
@ -444,14 +459,14 @@ class KiCadIPCClient:
@contextmanager
def kicad_ipc_session(project_path: str = None, board_path: str = None):
def kicad_ipc_session(project_path: str | None = None, board_path: str | None = None):
"""
Context manager for KiCad IPC sessions.
Args:
project_path: Optional project file to open
board_path: Optional board file to open
Usage:
with kicad_ipc_session("/path/to/project.kicad_pro") as client:
client.move_footprint("R1", Vector2(10, 20))
@ -461,13 +476,11 @@ def kicad_ipc_session(project_path: str = None, board_path: str = None):
if not client.connect():
raise KiCadIPCError("Failed to connect to KiCad IPC server")
if project_path:
if not client.open_project(project_path):
raise KiCadIPCError(f"Failed to open project: {project_path}")
if project_path and not client.open_project(project_path):
raise KiCadIPCError(f"Failed to open project: {project_path}")
if board_path:
if not client.open_board(board_path):
raise KiCadIPCError(f"Failed to open board: {board_path}")
if board_path and not client.open_board(board_path):
raise KiCadIPCError(f"Failed to open board: {board_path}")
yield client
@ -479,7 +492,7 @@ 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
"""
@ -518,10 +531,10 @@ def check_kicad_availability() -> dict[str, Any]:
def get_project_board_path(project_path: str) -> str:
"""
Get the board file path from a project file path.
Args:
project_path: Path to .kicad_pro file
Returns:
Path to corresponding .kicad_pcb file
"""
@ -534,11 +547,11 @@ def get_project_board_path(project_path: str) -> str:
def format_position(x_mm: float, y_mm: float) -> Vector2:
"""
Create a Vector2 position from millimeter coordinates.
Args:
x_mm: X coordinate in millimeters
y_mm: Y coordinate in millimeters
Returns:
Vector2 position
"""

View File

@ -11,7 +11,7 @@ import platform
import shutil
import subprocess
from ..config import TIMEOUT_CONSTANTS
from mckicad.config import TIMEOUT_CONSTANTS
logger = logging.getLogger(__name__)
@ -68,7 +68,7 @@ class KiCadCLIManager:
logger.warning("KiCad CLI not found on this system")
return None
def get_cli_path(self, required: bool = True) -> str:
def get_cli_path(self, required: bool = True) -> str | None:
"""
Get KiCad CLI path, raising exception if not found and required.
@ -228,7 +228,9 @@ def find_kicad_cli(force_refresh: bool = False) -> str | None:
def get_kicad_cli_path(required: bool = True) -> str:
"""Convenience function to get KiCad CLI path."""
return get_cli_manager().get_cli_path(required)
cli_path = get_cli_manager().get_cli_path(required)
assert cli_path is not None
return cli_path
def is_kicad_cli_available() -> bool:

View File

@ -2,22 +2,19 @@
KiCad-specific utility functions.
"""
import logging # Import logging
import logging
import os
import subprocess
import sys # Add sys import
import sys
from typing import Any
from mckicad.config import (
ADDITIONAL_SEARCH_PATHS,
KICAD_APP_PATH,
KICAD_EXTENSIONS,
KICAD_USER_DIR,
get_kicad_app_path,
get_kicad_user_dir,
get_search_paths,
)
# Get PID for logging - Removed, handled by logging config
# _PID = os.getpid()
def find_kicad_projects() -> list[dict[str, Any]]:
"""Find KiCad projects in the user's directory.
@ -26,11 +23,15 @@ def find_kicad_projects() -> list[dict[str, Any]]:
List of dictionaries with project information
"""
projects = []
logging.info("Attempting to find KiCad projects...") # Log start
logging.info("Attempting to find KiCad projects...")
kicad_user_dir = get_kicad_user_dir()
additional_search_paths = get_search_paths()
# Search directories to look for KiCad projects
raw_search_dirs = [KICAD_USER_DIR] + ADDITIONAL_SEARCH_PATHS
logging.info(f"Raw KICAD_USER_DIR: '{KICAD_USER_DIR}'")
logging.info(f"Raw ADDITIONAL_SEARCH_PATHS: {ADDITIONAL_SEARCH_PATHS}")
raw_search_dirs = [kicad_user_dir] + additional_search_paths
logging.info(f"Raw kicad_user_dir: '{kicad_user_dir}'")
logging.info(f"Raw additional_search_paths: {additional_search_paths}")
logging.info(f"Raw search list before expansion: {raw_search_dirs}")
expanded_search_dirs = []
@ -47,7 +48,7 @@ def find_kicad_projects() -> list[dict[str, Any]]:
if not os.path.exists(search_dir):
logging.warning(
f"Expanded search directory does not exist: {search_dir}"
) # Use warning level
)
continue
logging.info(f"Scanning expanded directory: {search_dir}")
@ -79,7 +80,7 @@ def find_kicad_projects() -> list[dict[str, Any]]:
except OSError as e:
logging.error(
f"Error accessing project file {project_path}: {e}"
) # Use error level
)
continue # Skip if we can't access it
logging.info(f"Found {len(projects)} KiCad projects after scanning.")
@ -111,11 +112,13 @@ def open_kicad_project(project_path: str) -> dict[str, Any]:
if not os.path.exists(project_path):
return {"success": False, "error": f"Project not found: {project_path}"}
kicad_app_path = get_kicad_app_path()
try:
cmd = []
if sys.platform == "darwin": # macOS
# On MacOS, use the 'open' command to open the project in KiCad
cmd = ["open", "-a", KICAD_APP_PATH, project_path]
cmd = ["open", "-a", kicad_app_path, project_path]
elif sys.platform == "linux": # Linux
# On Linux, use 'xdg-open'
cmd = ["xdg-open", project_path]

View File

@ -10,7 +10,8 @@ import logging
import os
import subprocess # nosec B404 - subprocess usage is secured with validation
from ..config import TIMEOUT_CONSTANTS
from mckicad.config import TIMEOUT_CONSTANTS
from .kicad_cli import get_kicad_cli_path
from .path_validator import PathValidator, get_default_validator
@ -216,7 +217,7 @@ class SecureSubprocessRunner:
working_dir: str | None = None,
timeout: float = TIMEOUT_CONSTANTS["subprocess_default"],
capture_output: bool = True,
) -> subprocess.CompletedProcess:
) -> "subprocess.CompletedProcess[str]":
"""
Internal subprocess runner with consistent settings.
@ -232,21 +233,22 @@ class SecureSubprocessRunner:
Raises:
subprocess.SubprocessError: If command fails
"""
kwargs = {
"timeout": timeout,
"cwd": working_dir,
"text": True,
}
if capture_output:
kwargs.update(
{
"capture_output": True,
"check": False, # Don't raise on non-zero exit code
}
return subprocess.run( # nosec B603 - input is validated
command,
timeout=timeout,
cwd=working_dir,
text=True,
capture_output=True,
check=False,
)
else:
return subprocess.run( # nosec B603 - input is validated
command,
timeout=timeout,
cwd=working_dir,
text=True,
)
return subprocess.run(command, **kwargs) # nosec B603 - input is validated
# Global secure subprocess runner instance

View File

@ -1,3 +1,8 @@
#!/bin/bash
cd /home/rpm/claude/kicad-mcp
uv run python main.py "$@"
#!/usr/bin/env bash
# Start the mckicad MCP server
set -euo pipefail
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
cd "$SCRIPT_DIR"
exec uv run python main.py "$@"

46
tests/conftest.py Normal file
View File

@ -0,0 +1,46 @@
"""Shared test fixtures for mckicad tests."""
import tempfile
import pytest
@pytest.fixture
def tmp_project_dir(tmp_path):
"""Create a temporary directory with a minimal KiCad project structure."""
project_name = "test_project"
pro_file = tmp_path / f"{project_name}.kicad_pro"
pro_file.write_text('{"meta": {"filename": "test_project.kicad_pro"}}')
sch_file = tmp_path / f"{project_name}.kicad_sch"
sch_file.write_text("(kicad_sch (version 20230121))")
pcb_file = tmp_path / f"{project_name}.kicad_pcb"
pcb_file.write_text("(kicad_pcb (version 20230121))")
return tmp_path
@pytest.fixture
def project_path(tmp_project_dir):
"""Return path to the .kicad_pro file in the temp project."""
return str(tmp_project_dir / "test_project.kicad_pro")
@pytest.fixture
def schematic_path(tmp_project_dir):
"""Return path to the .kicad_sch file in the temp project."""
return str(tmp_project_dir / "test_project.kicad_sch")
@pytest.fixture
def tmp_output_dir():
"""Create a temporary output directory."""
with tempfile.TemporaryDirectory(prefix="mckicad_test_") as d:
yield d
@pytest.fixture(autouse=True)
def _set_test_search_paths(tmp_project_dir, monkeypatch):
"""Point KICAD_SEARCH_PATHS at the temp project directory for all tests."""
monkeypatch.setenv("KICAD_SEARCH_PATHS", str(tmp_project_dir))

View File

@ -1,302 +0,0 @@
#!/usr/bin/env python3
"""
REVOLUTIONARY MCP TOOLS DEMONSTRATION
Live demonstration of our EDA automation platform!
"""
import sys
import time
from pathlib import Path
# Add the mckicad module to path
sys.path.insert(0, str(Path(__file__).parent))
from mckicad.utils.ipc_client import KiCadIPCClient, check_kicad_availability
from mckicad.utils.file_utils import get_project_files
from mckicad.utils.netlist_parser import extract_netlist, analyze_netlist
from mckicad.tools.validation_tools import validate_project_boundaries
# Our new demo project
PROJECT_PATH = "/home/rpm/claude/Demo_PCB_Project/Smart_Sensor_Board.kicad_pro"
PCB_PATH = "/home/rpm/claude/Demo_PCB_Project/Smart_Sensor_Board.kicad_pcb"
SCHEMATIC_PATH = "/home/rpm/claude/Demo_PCB_Project/Smart_Sensor_Board.kicad_sch"
def print_banner(title, emoji="🎯"):
"""Print an impressive banner."""
width = 70
print("\n" + "=" * width)
print(f"{emoji} {title.center(width - 4)} {emoji}")
print("=" * width)
def print_section(title, emoji="🔸"):
"""Print a section header."""
print(f"\n{emoji} {title}")
print("-" * (len(title) + 4))
def demo_project_analysis():
"""Demonstrate MCP project analysis tools."""
print_section("MCP PROJECT ANALYSIS TOOLS", "🔍")
print("📁 MCP File Discovery:")
try:
files = get_project_files(PROJECT_PATH)
print(f" ✅ Project structure detected:")
for file_type, file_path in files.items():
print(f" {file_type}: {Path(file_path).name}")
print(f"\n🔍 MCP Project Validation:")
# Note: validate_project_boundaries is async, so we'll simulate results here
validation_result = {"status": "valid", "files_found": len(files)}
print(f" ✅ Project validation: {validation_result.get('status', 'unknown')}")
print(f" 📊 Files validated: {validation_result.get('files_found', 0)}")
return True
except Exception as e:
print(f" ❌ Analysis failed: {e}")
return False
def demo_ai_circuit_analysis():
"""Demonstrate AI-powered circuit analysis."""
print_section("AI CIRCUIT INTELLIGENCE", "🧠")
print("🤖 AI Circuit Pattern Recognition:")
try:
# Extract and analyze netlist with AI
netlist_data = extract_netlist(SCHEMATIC_PATH)
analysis = analyze_netlist(netlist_data)
print(f" ✅ AI Analysis Results:")
print(f" Components analyzed: {analysis['component_count']}")
print(f" Component categories: {len(analysis['component_types'])}")
print(f" Component types found: {list(analysis['component_types'].keys())[:8]}")
print(f" Power networks detected: {analysis['power_nets']}")
print(f" Signal integrity analysis: COMPLETE")
# Simulate AI suggestions
print(f"\n🎯 AI Design Recommendations:")
print(f" 💡 Suggested improvements:")
print(f" - Add more decoupling capacitors near high-speed ICs")
print(f" - Consider ground plane optimization for thermal management")
print(f" - Recommend differential pair routing for high-speed signals")
print(f" ⚡ AI confidence level: 95%")
return True
except Exception as e:
print(f" ❌ AI analysis failed: {e}")
return False
def demo_realtime_manipulation():
"""Demonstrate real-time KiCad manipulation via IPC."""
print_section("REAL-TIME BOARD MANIPULATION", "")
client = KiCadIPCClient()
try:
# Check availability first
availability = check_kicad_availability()
print(f"🔌 IPC Connection Status:")
print(f" KiCad IPC API: {'✅ Available' if availability.get('available') else '❌ Unavailable'}")
if not availability.get('available'):
print(f" Note: {availability.get('message', 'KiCad not running')}")
print(f" 📝 To use real-time features: Open KiCad with our Smart_Sensor_Board.kicad_pro")
return True # Don't fail the demo for this
# Connect to live KiCad
if not client.connect():
print(" ⚠️ KiCad connection not available (KiCad not running)")
print(" 📝 Demo: Real-time manipulation would show:")
print(" - Live component position updates")
print(" - Real-time routing modifications")
print(" - Interactive design rule checking")
return True
# Live board analysis
board = client._kicad.get_board()
print(f" ✅ Connected to live board: {board.name}")
# Real-time component analysis
footprints = board.get_footprints()
nets = board.get_nets()
tracks = board.get_tracks()
print(f" 📊 Live Board Statistics:")
print(f" Components: {len(footprints)}")
print(f" Networks: {len(nets)}")
print(f" Routed tracks: {len(tracks)}")
# Demonstrate component categorization
component_stats = {}
for fp in footprints:
try:
ref = fp.reference_field.text.value
if ref:
category = ref[0]
component_stats[category] = component_stats.get(category, 0) + 1
except:
continue
print(f" 🔧 Component Distribution:")
for category, count in sorted(component_stats.items()):
print(f" {category}-type: {count} components")
print(f" ⚡ Real-time manipulation READY!")
return True
except Exception as e:
print(f" ❌ Real-time manipulation demo failed: {e}")
return False
finally:
client.disconnect()
def demo_automated_modifications():
"""Demonstrate automated PCB modifications."""
print_section("AUTOMATED PCB MODIFICATIONS", "🔄")
print("🤖 AI-Powered Design Changes:")
print(" 📝 Simulated Modifications (would execute with live KiCad):")
print(" 1. ✅ Add bypass capacitors near power pins")
print(" 2. ✅ Optimize component placement for thermal management")
print(" 3. ✅ Route high-speed differential pairs")
print(" 4. ✅ Add test points for critical signals")
print(" 5. ✅ Update silkscreen with version info")
print(f"\n🚀 Automated Routing Preparation:")
print(" 📐 DSN export: READY")
print(" 🔧 FreeRouting engine: OPERATIONAL")
print(" ⚡ Routing optimization: CONFIGURED")
print(" 📥 SES import: READY")
print(f"\n✅ Automated modifications would complete in ~30 seconds!")
return True
def demo_manufacturing_export():
"""Demonstrate one-click manufacturing file generation."""
print_section("MANUFACTURING FILE GENERATION", "🏭")
print("📄 One-Click Manufacturing Export:")
try:
import subprocess
import tempfile
with tempfile.TemporaryDirectory() as temp_dir:
output_dir = Path(temp_dir) / "manufacturing"
output_dir.mkdir()
print(f" 🔧 Generating production files...")
# Gerber files
gerber_cmd = [
'kicad-cli', 'pcb', 'export', 'gerbers',
'--output', str(output_dir / 'gerbers'),
PCB_PATH
]
gerber_result = subprocess.run(gerber_cmd, capture_output=True, timeout=15)
if gerber_result.returncode == 0:
gerber_files = list((output_dir / 'gerbers').glob('*'))
print(f" ✅ Gerber files: {len(gerber_files)} layers generated")
# Drill files
drill_cmd = [
'kicad-cli', 'pcb', 'export', 'drill',
'--output', str(output_dir / 'drill'),
PCB_PATH
]
drill_result = subprocess.run(drill_cmd, capture_output=True, timeout=10)
if drill_result.returncode == 0:
print(f" ✅ Drill files: Generated")
# Position files
pos_cmd = [
'kicad-cli', 'pcb', 'export', 'pos',
'--output', str(output_dir / 'positions.csv'),
'--format', 'csv',
PCB_PATH
]
pos_result = subprocess.run(pos_cmd, capture_output=True, timeout=10)
if pos_result.returncode == 0:
print(f" ✅ Pick & place: positions.csv generated")
# BOM
bom_cmd = [
'kicad-cli', 'sch', 'export', 'bom',
'--output', str(output_dir / 'bom.csv'),
SCHEMATIC_PATH
]
bom_result = subprocess.run(bom_cmd, capture_output=True, timeout=10)
if bom_result.returncode == 0:
print(f" ✅ BOM: Component list generated")
print(f" 🎯 COMPLETE! Production-ready files generated in seconds!")
print(f" 🏭 Ready for: PCB fabrication, component assembly, quality control")
return True
except Exception as e:
print(f" ❌ Manufacturing export failed: {e}")
return False
def main():
"""Run the complete MCP tools demonstration."""
print_banner("REVOLUTIONARY MCP TOOLS DEMONSTRATION", "🚀")
print("Smart Sensor Board Project - Live EDA Automation")
print("Showcasing the world's most advanced KiCad integration!")
start_time = time.time()
# Run demonstrations
results = {
"project_analysis": demo_project_analysis(),
"ai_circuit_analysis": demo_ai_circuit_analysis(),
"realtime_manipulation": demo_realtime_manipulation(),
"automated_modifications": demo_automated_modifications(),
"manufacturing_export": demo_manufacturing_export()
}
total_time = time.time() - start_time
# Results summary
print_banner("MCP TOOLS DEMONSTRATION COMPLETE", "🎉")
passed_demos = sum(results.values())
total_demos = len(results)
print(f"📊 Demo Results: {passed_demos}/{total_demos} demonstrations successful")
print(f"⏱️ Total execution time: {total_time:.2f}s")
print(f"\n🎯 Capability Showcase:")
for demo_name, success in results.items():
status = "✅ SUCCESS" if success else "❌ ISSUE"
demo_title = demo_name.replace('_', ' ').title()
print(f" {status} {demo_title}")
if passed_demos == total_demos:
print_banner("🏆 REVOLUTIONARY PLATFORM PROVEN! 🏆", "🎉")
print("✨ All MCP tools working flawlessly!")
print("🚀 Complete EDA automation demonstrated!")
print("⚡ From concept to production in minutes!")
print("🔥 THE FUTURE OF PCB DESIGN IS HERE!")
elif passed_demos >= 4:
print_banner("🚀 OUTSTANDING SUCCESS! 🚀", "🌟")
print("💪 Advanced EDA automation capabilities confirmed!")
print("⚡ Revolutionary PCB workflow operational!")
else:
print_banner("✅ SOLID FOUNDATION! ✅", "🛠️")
print("🔧 Core MCP functionality demonstrated!")
return passed_demos >= 4
if __name__ == "__main__":
success = main()
sys.exit(0 if success else 1)

View File

@ -1,300 +0,0 @@
#!/usr/bin/env python3
"""
FINAL DEMONSTRATION: Revolutionary KiCad MCP Server
Complete EDA Automation Platform
This script demonstrates the full capabilities of our KiCad MCP server,
from basic project analysis to real-time board manipulation and routing.
"""
import sys
from pathlib import Path
# Add the mckicad module to path
sys.path.insert(0, str(Path(__file__).parent))
from mckicad.utils.ipc_client import KiCadIPCClient
from mckicad.utils.freerouting_engine import check_routing_prerequisites
from mckicad.utils.file_utils import get_project_files
from mckicad.utils.netlist_parser import extract_netlist, analyze_netlist
# Test project path
PROJECT_PATH = "/home/rpm/claude/MLX90640-Thermal-Camera/PCB/Thermal_Camera.kicad_pro"
def print_header(title):
"""Print a formatted header."""
print(f"\n{'='*60}")
print(f"🚀 {title}")
print('='*60)
def test_file_based_analysis():
"""Demonstrate file-based EDA analysis capabilities."""
print_header("FILE-BASED ANALYSIS")
results = {}
# 1. Project Validation
print("📁 Project Validation:")
try:
files = get_project_files(PROJECT_PATH)
print(f" ✓ Found {len(files)} project files:")
for file_type, path in files.items():
print(f" - {file_type}: {Path(path).name}")
results['project_validation'] = True
except Exception as e:
print(f" ✗ Project validation failed: {e}")
results['project_validation'] = False
# 2. BOM Analysis
print("\n📋 BOM Analysis:")
try:
bom_path = "/home/rpm/claude/MLX90640-Thermal-Camera/PCB/BOM/bom.csv"
if Path(bom_path).exists():
import csv
with open(bom_path, 'r') as f:
reader = csv.DictReader(f)
components = list(reader)
print(f" ✓ BOM loaded: {len(components)} component types")
# Analyze categories
categories = {}
total_components = 0
for comp in components:
designators = comp['Designator'].split(', ')
quantity = int(comp['Quantity'])
total_components += quantity
for des in designators:
category = des[0] if des else 'Unknown'
categories[category] = categories.get(category, 0) + 1
print(f" ✓ Total components: {total_components}")
print(f" ✓ Component categories: {len(categories)}")
for cat, count in sorted(categories.items()):
print(f" - {cat}: {count} types")
results['bom_analysis'] = True
else:
print(f" ✗ BOM file not found")
results['bom_analysis'] = False
except Exception as e:
print(f" ✗ BOM analysis failed: {e}")
results['bom_analysis'] = False
# 3. Circuit Pattern Analysis
print("\n🔍 Circuit Pattern Analysis:")
try:
schematic_path = files.get('schematic')
if schematic_path:
netlist_data = extract_netlist(schematic_path)
analysis = analyze_netlist(netlist_data)
print(f" ✓ Schematic parsed successfully")
print(f" ✓ Components analyzed: {analysis['component_count']}")
print(f" ✓ Component types: {len(analysis['component_types'])}")
print(f" ✓ Power nets detected: {len(analysis['power_nets'])}")
print(f" Power nets: {', '.join(analysis['power_nets'])}")
results['pattern_analysis'] = True
else:
print(" ✗ Schematic file not available")
results['pattern_analysis'] = False
except Exception as e:
print(f" ✗ Pattern analysis failed: {e}")
results['pattern_analysis'] = False
return results
def test_realtime_analysis():
"""Demonstrate real-time KiCad IPC analysis."""
print_header("REAL-TIME KiCad IPC ANALYSIS")
results = {}
client = KiCadIPCClient()
try:
# 1. IPC Connection
print("🔌 IPC Connection:")
if not client.connect():
print(" ✗ Failed to connect to KiCad IPC")
return {'connection': False}
print(f" ✓ Connected to KiCad {client.get_version()}")
results['connection'] = True
# 2. Board Access
print("\n📟 Board Access:")
try:
board = client._kicad.get_board()
print(f" ✓ Board loaded: {board.name}")
print(f" ✓ Project: {board.document.project.name}")
print(f" ✓ Path: {board.document.project.path}")
results['board_access'] = True
except Exception as e:
print(f" ✗ Board access failed: {e}")
results['board_access'] = False
return results
# 3. Component Analysis
print("\n🔧 Real-Time Component Analysis:")
try:
footprints = board.get_footprints()
print(f" ✓ Live footprint count: {len(footprints)}")
# Analyze footprint types
fp_types = {}
for fp in footprints:
ref = fp.reference
category = ref[0] if ref else 'Unknown'
fp_types[category] = fp_types.get(category, 0) + 1
print(f" ✓ Component categories found:")
for cat, count in sorted(fp_types.items()):
print(f" - {cat}: {count} components")
# Show sample components
print(f" ✓ Sample components:")
for fp in footprints[:5]:
print(f" - {fp.reference}: {fp.value} ({fp.footprint})")
results['component_analysis'] = True
except Exception as e:
print(f" ✗ Component analysis failed: {e}")
results['component_analysis'] = False
# 4. Network Analysis
print("\n🌐 Real-Time Network Analysis:")
try:
nets = board.get_nets()
print(f" ✓ Live network count: {len(nets)}")
# Analyze net types
power_nets = []
signal_nets = []
for net in nets:
if any(net.name.startswith(prefix) for prefix in ['VCC', 'VDD', 'GND', '+', '-']):
power_nets.append(net.name)
else:
signal_nets.append(net.name)
print(f" ✓ Power nets: {len(power_nets)}")
print(f" {', '.join(power_nets[:5])}")
print(f" ✓ Signal nets: {len(signal_nets)}")
print(f" {', '.join(signal_nets[:5])}")
results['network_analysis'] = True
except Exception as e:
print(f" ✗ Network analysis failed: {e}")
results['network_analysis'] = False
# 5. Routing Analysis
print("\n🛤️ Real-Time Routing Analysis:")
try:
tracks = board.get_tracks()
vias = board.get_vias()
print(f" ✓ Track segments: {len(tracks)}")
print(f" ✓ Vias: {len(vias)}")
# Get board dimensions
dimensions = board.get_dimensions()
print(f" ✓ Board dimensions: {dimensions}")
results['routing_analysis'] = True
except Exception as e:
print(f" ✗ Routing analysis failed: {e}")
results['routing_analysis'] = False
finally:
client.disconnect()
return results
def test_automation_readiness():
"""Test automation and routing readiness."""
print_header("AUTOMATION READINESS")
# Check routing prerequisites
print("🔧 Routing Prerequisites:")
try:
status = check_routing_prerequisites()
components = status.get("components", {})
for comp_name, comp_info in components.items():
available = comp_info.get("available", False)
status_icon = "" if available else ""
print(f" {status_icon} {comp_name}: {available}")
if not available and 'message' in comp_info:
print(f" {comp_info['message']}")
overall_ready = status.get("overall_ready", False)
print(f"\n Overall Readiness: {'✓ READY' if overall_ready else '⚠️ PARTIAL'}")
if not overall_ready:
print(" 💡 To enable full automation:")
if not components.get('freerouting', {}).get('available'):
print(" - Install FreeRouting: https://github.com/freerouting/freerouting/releases")
return {'automation_ready': overall_ready}
except Exception as e:
print(f" ✗ Prerequisites check failed: {e}")
return {'automation_ready': False}
def main():
"""Run the complete demonstration."""
print_header("REVOLUTIONARY KiCad MCP SERVER DEMONSTRATION")
print("Complete EDA Automation Platform")
print(f"Test Project: MLX90640 Thermal Camera")
print(f"Location: {PROJECT_PATH}")
# Run all tests
file_results = test_file_based_analysis()
realtime_results = test_realtime_analysis()
automation_results = test_automation_readiness()
# Combine results
all_results = {**file_results, **realtime_results, **automation_results}
# Final summary
print_header("FINAL SUMMARY")
passed = sum(all_results.values())
total = len(all_results)
print(f"🎯 Test Results: {passed}/{total} capabilities demonstrated")
print("\nCapabilities Demonstrated:")
for test_name, result in all_results.items():
status = "✅ WORKING" if result else "❌ NEEDS ATTENTION"
test_display = test_name.replace('_', ' ').title()
print(f" {status} {test_display}")
if passed >= 7: # Most capabilities working
print("\n🎉 REVOLUTIONARY KiCad MCP SERVER IS FULLY OPERATIONAL!")
print("✨ Capabilities Proven:")
print(" 🔍 Intelligent project analysis")
print(" 📊 Real-time component monitoring")
print(" 🌐 Live network topology analysis")
print(" 🛤️ Advanced routing analysis")
print(" 📋 Comprehensive BOM intelligence")
print(" 🔧 Circuit pattern recognition")
print(" 🚀 EDA automation readiness")
print("\n🔥 Ready for complete design automation!")
elif passed >= 4:
print("\n🚀 KiCad MCP SERVER IS SUBSTANTIALLY FUNCTIONAL!")
print(" Core EDA analysis capabilities proven")
print(" Real-time KiCad integration working")
print(" Ready for enhanced development")
else:
print("\n⚠️ KiCad MCP SERVER NEEDS DEBUGGING")
print(" Basic functionality needs attention")
return passed >= 4
if __name__ == "__main__":
success = main()
sys.exit(0 if success else 1)

View File

@ -1,208 +0,0 @@
#!/usr/bin/env python3
"""
PERFECT DEMONSTRATION: 100% EDA Automation Platform
Revolutionary KiCad MCP Server with FreeRouting Integration
This demonstrates the complete, fully-functional EDA automation platform.
"""
import sys
from pathlib import Path
# Add the mckicad module to path
sys.path.insert(0, str(Path(__file__).parent))
from mckicad.utils.ipc_client import KiCadIPCClient
from mckicad.utils.freerouting_engine import check_routing_prerequisites
from mckicad.utils.file_utils import get_project_files
from mckicad.utils.netlist_parser import extract_netlist, analyze_netlist
# Test project path
PROJECT_PATH = "/home/rpm/claude/MLX90640-Thermal-Camera/PCB/Thermal_Camera.kicad_pro"
def print_header(title, emoji="🚀"):
"""Print a formatted header."""
print(f"\n{'='*60}")
print(f"{emoji} {title}")
print('='*60)
def perfect_realtime_analysis():
"""Perfect real-time analysis with corrected API calls."""
print_header("PERFECT REAL-TIME ANALYSIS", "")
client = KiCadIPCClient()
try:
# Connect to KiCad
if not client.connect():
print("❌ Failed to connect to KiCad")
return False
print(f"✅ Connected to KiCad {client.get_version()}")
# Get board directly
board = client._kicad.get_board()
print(f"✅ Board: {board.name}")
print(f"✅ Project: {board.document.project.name}")
print(f"✅ Path: {board.document.project.path}")
# Perfect Component Analysis
print(f"\n🔧 Perfect Component Analysis:")
footprints = board.get_footprints()
print(f" ✅ Live footprint count: {len(footprints)}")
# Analyze with correct properties
component_types = {}
sample_components = []
for fp in footprints:
try:
ref = fp.reference_field.text.text
value = fp.value_field.text.text
footprint = fp.definition.id.entry_name
# Categorize
category = ref[0] if ref else 'Unknown'
component_types[category] = component_types.get(category, 0) + 1
# Collect samples
if len(sample_components) < 10:
sample_components.append({
'ref': ref,
'value': value,
'footprint': footprint
})
except Exception as e:
# Skip components with property access issues
pass
print(f" ✅ Component categories analyzed:")
for category, count in sorted(component_types.items()):
print(f" {category}: {count} components")
print(f" ✅ Sample components:")
for comp in sample_components:
print(f" {comp['ref']}: {comp['value']} ({comp['footprint']})")
# Perfect Network Analysis
print(f"\n🌐 Perfect Network Analysis:")
nets = board.get_nets()
print(f" ✅ Live network count: {len(nets)}")
power_nets = []
signal_nets = []
for net in nets:
if any(net.name.startswith(prefix) for prefix in ['+', '-', 'VCC', 'VDD', 'GND']):
power_nets.append(net.name)
elif net.name: # Skip empty net names
signal_nets.append(net.name)
print(f" ✅ Power nets ({len(power_nets)}): {', '.join(power_nets[:5])}")
print(f" ✅ Signal nets ({len(signal_nets)}): {', '.join(signal_nets[:5])}")
# Perfect Routing Analysis
print(f"\n🛤️ Perfect Routing Analysis:")
tracks = board.get_tracks()
vias = board.get_vias()
print(f" ✅ Track segments: {len(tracks)}")
print(f" ✅ Vias: {len(vias)}")
# Board info
try:
stackup = board.get_stackup()
print(f" ✅ Layer stackup available")
except:
print(f" Layer stackup: Custom analysis needed")
return True
except Exception as e:
print(f"❌ Real-time analysis error: {e}")
return False
finally:
client.disconnect()
def perfect_automation_readiness():
"""Perfect automation readiness check."""
print_header("PERFECT AUTOMATION READINESS", "🎯")
try:
status = check_routing_prerequisites()
components = status.get("components", {})
print("Component Status:")
all_available = True
for comp_name, comp_info in components.items():
available = comp_info.get("available", False)
all_available = all_available and available
status_icon = "" if available else ""
print(f" {status_icon} {comp_name.replace('_', ' ').title()}: {'Available' if available else 'Missing'}")
overall_ready = status.get("overall_ready", False)
if overall_ready:
print(f"\n🎉 STATUS: 100% AUTOMATION READY!")
print("🔥 Complete EDA workflow automation available!")
print(" • Real-time KiCad integration")
print(" • Automated PCB routing via FreeRouting")
print(" • Complete design-to-manufacturing pipeline")
else:
print(f"\n⚡ STATUS: CORE AUTOMATION READY!")
print("🚀 Advanced EDA analysis and manipulation available!")
return overall_ready
except Exception as e:
print(f"❌ Automation check error: {e}")
return False
def main():
"""Perfect demonstration of 100% EDA automation."""
print_header("ULTIMATE EDA AUTOMATION PLATFORM", "🏆")
print("Revolutionary KiCad MCP Server")
print("Complete Design-to-Manufacturing Automation")
print(f"Test Project: MLX90640 Thermal Camera")
# Run perfect tests
realtime_success = perfect_realtime_analysis()
automation_ready = perfect_automation_readiness()
# Perfect Summary
print_header("ULTIMATE SUCCESS SUMMARY", "🏆")
if realtime_success and automation_ready:
print("🌟 ACHIEVEMENT UNLOCKED: 100% EDA AUTOMATION!")
print("\n✨ Revolutionary Capabilities Proven:")
print(" 🔍 Perfect component analysis with live data")
print(" 🌐 Complete network topology monitoring")
print(" 🛤️ Advanced routing analysis and manipulation")
print(" 🤖 Full FreeRouting automation integration")
print(" ⚡ Real-time KiCad board manipulation")
print(" 📊 Comprehensive design intelligence")
print(f"\n🎯 IMPACT:")
print(" • Automate complete PCB design workflows")
print(" • Real-time design analysis and optimization")
print(" • Intelligent component placement and routing")
print(" • Automated manufacturing file generation")
print(f"\n🚀 The future of EDA automation is HERE!")
elif realtime_success:
print("⚡ MAJOR SUCCESS: Advanced EDA Automation Platform!")
print("\n🔥 Core capabilities fully operational:")
print(" • Real-time KiCad integration")
print(" • Advanced component analysis")
print(" • Network topology intelligence")
print(" • Routing analysis and monitoring")
else:
print("🔧 PARTIAL SUCCESS: Foundation established")
return realtime_success
if __name__ == "__main__":
success = main()
sys.exit(0 if success else 1)

View File

@ -1,389 +0,0 @@
#!/usr/bin/env python3
"""
ULTIMATE COMPREHENSIVE DEMONSTRATION
Revolutionary KiCad MCP Server - Complete EDA Automation Platform
This is the definitive test that proves our platform can handle
complete design-to-manufacturing workflows with AI intelligence.
"""
import sys
import time
from pathlib import Path
from datetime import datetime
# Add the mckicad module to path
sys.path.insert(0, str(Path(__file__).parent))
from mckicad.utils.ipc_client import KiCadIPCClient
from mckicad.utils.freerouting_engine import check_routing_prerequisites
from mckicad.utils.file_utils import get_project_files
from mckicad.utils.netlist_parser import extract_netlist, analyze_netlist
from mckicad.server import create_server
# Test project
PROJECT_PATH = "/home/rpm/claude/MLX90640-Thermal-Camera/PCB/Thermal_Camera.kicad_pro"
def print_banner(title, emoji="🎯"):
"""Print an impressive banner."""
width = 70
print("\n" + "=" * width)
print(f"{emoji} {title.center(width - 4)} {emoji}")
print("=" * width)
def print_section(title, emoji="🔸"):
"""Print a section header."""
print(f"\n{emoji} {title}")
print("-" * (len(title) + 4))
def comprehensive_project_analysis():
"""Comprehensive project analysis demonstrating all capabilities."""
print_section("COMPREHENSIVE PROJECT ANALYSIS", "🔍")
results = {}
start_time = time.time()
# 1. File-based Analysis
print("📁 File System Analysis:")
try:
files = get_project_files(PROJECT_PATH)
print(f" ✅ Project files: {list(files.keys())}")
results['file_analysis'] = True
except Exception as e:
print(f" ❌ File analysis: {e}")
results['file_analysis'] = False
# 2. Circuit Pattern Analysis
print("\n🧠 AI Circuit Intelligence:")
try:
schematic_path = files.get('schematic') if 'files' in locals() else None
if schematic_path:
netlist_data = extract_netlist(schematic_path)
analysis = analyze_netlist(netlist_data)
print(f" ✅ Components analyzed: {analysis['component_count']}")
print(f" ✅ Component types: {len(analysis['component_types'])}")
print(f" ✅ Power networks: {analysis['power_nets']}")
print(f" ✅ AI pattern recognition: OPERATIONAL")
results['ai_analysis'] = True
else:
results['ai_analysis'] = False
except Exception as e:
print(f" ❌ AI analysis: {e}")
results['ai_analysis'] = False
analysis_time = time.time() - start_time
print(f"\n⏱️ Analysis completed in {analysis_time:.2f}s")
return results
def realtime_board_manipulation():
"""Demonstrate real-time board manipulation capabilities."""
print_section("REAL-TIME BOARD MANIPULATION", "")
results = {}
client = KiCadIPCClient()
try:
# Connect to live KiCad
start_time = time.time()
if not client.connect():
print("❌ KiCad connection failed")
return {'connection': False}
connection_time = time.time() - start_time
print(f"🔌 Connected to KiCad in {connection_time:.3f}s")
# Get live board data
board = client._kicad.get_board()
print(f"📟 Live board: {board.name}")
print(f"📍 Project: {board.document.project.name}")
# Component analysis
start_time = time.time()
footprints = board.get_footprints()
# Advanced component categorization
component_stats = {}
position_data = []
for fp in footprints:
try:
ref = fp.reference_field.text.value
value = fp.value_field.text.value
pos = fp.position
if ref:
category = ref[0]
component_stats[category] = component_stats.get(category, 0) + 1
position_data.append({
'ref': ref,
'x': pos.x / 1000000, # Convert to mm
'y': pos.y / 1000000,
'value': value
})
except:
continue
analysis_time = time.time() - start_time
print(f"⚙️ Live Component Analysis ({analysis_time:.3f}s):")
print(f" 📊 Total components: {len(footprints)}")
print(f" 📈 Categories: {len(component_stats)}")
for cat, count in sorted(component_stats.items()):
print(f" {cat}: {count} components")
# Network topology analysis
nets = board.get_nets()
power_nets = [net for net in nets if net.name and any(net.name.startswith(p) for p in ['+', 'VCC', 'VDD', 'GND'])]
signal_nets = [net for net in nets if net.name and net.name not in [n.name for n in power_nets]]
print(f" 🌐 Network topology: {len(nets)} total nets")
print(f" Power: {len(power_nets)} | Signal: {len(signal_nets)}")
# Routing analysis
tracks = board.get_tracks()
vias = board.get_vias()
print(f" 🛤️ Routing status: {len(tracks)} tracks, {len(vias)} vias")
results.update({
'connection': True,
'component_analysis': True,
'network_analysis': True,
'routing_analysis': True,
'performance': analysis_time < 1.0 # Sub-second analysis
})
except Exception as e:
print(f"❌ Real-time manipulation error: {e}")
results['connection'] = False
finally:
client.disconnect()
return results
def automation_pipeline_readiness():
"""Demonstrate complete automation pipeline readiness."""
print_section("AUTOMATION PIPELINE READINESS", "🤖")
results = {}
# Routing automation readiness
print("🔧 Routing Automation Status:")
try:
routing_status = check_routing_prerequisites()
components = routing_status.get('components', {})
all_ready = True
for comp_name, comp_info in components.items():
available = comp_info.get('available', False)
all_ready = all_ready and available
icon = "" if available else ""
print(f" {icon} {comp_name.replace('_', ' ').title()}: {'Ready' if available else 'Missing'}")
overall_ready = routing_status.get('overall_ready', False)
print(f" 🎯 Overall routing: {'✅ READY' if overall_ready else '⚠️ PARTIAL'}")
results['routing_automation'] = overall_ready
except Exception as e:
print(f" ❌ Routing check failed: {e}")
results['routing_automation'] = False
# MCP Server readiness
print(f"\n🖥️ MCP Server Integration:")
try:
server = create_server()
print(f" ✅ Server creation: {type(server).__name__}")
print(f" ✅ Tool registration: Multiple categories")
print(f" ✅ Resource exposure: Project/DRC/BOM/Netlist")
print(f" ✅ Prompt templates: Design assistance")
results['mcp_server'] = True
except Exception as e:
print(f" ❌ MCP server issue: {e}")
results['mcp_server'] = False
# Manufacturing pipeline
print(f"\n🏭 Manufacturing Pipeline:")
try:
# Verify KiCad CLI capabilities (quick check)
import subprocess
result = subprocess.run(['kicad-cli', '--help'],
capture_output=True, text=True, timeout=5)
if result.returncode == 0:
print(f" ✅ Gerber generation: Ready")
print(f" ✅ Drill files: Ready")
print(f" ✅ Pick & place: Ready")
print(f" ✅ BOM export: Ready")
print(f" ✅ 3D export: Ready")
results['manufacturing'] = True
else:
results['manufacturing'] = False
except Exception as e:
print(f" ❌ Manufacturing check: {e}")
results['manufacturing'] = False
return results
def performance_benchmark():
"""Run performance benchmarks on key operations."""
print_section("PERFORMANCE BENCHMARKS", "🏃")
benchmarks = {}
# File analysis benchmark
print("📁 File Analysis Benchmark:")
start_time = time.time()
try:
for i in range(5):
files = get_project_files(PROJECT_PATH)
file_time = (time.time() - start_time) / 5
print(f" ⚡ Average file analysis: {file_time*1000:.1f}ms")
benchmarks['file_analysis'] = file_time
except Exception as e:
print(f" ❌ File benchmark failed: {e}")
benchmarks['file_analysis'] = float('inf')
# IPC connection benchmark
print(f"\n🔌 IPC Connection Benchmark:")
connection_times = []
for i in range(3):
client = KiCadIPCClient()
start_time = time.time()
try:
if client.connect():
connection_time = time.time() - start_time
connection_times.append(connection_time)
client.disconnect()
except:
pass
if connection_times:
avg_connection = sum(connection_times) / len(connection_times)
print(f" ⚡ Average connection: {avg_connection*1000:.1f}ms")
benchmarks['ipc_connection'] = avg_connection
else:
print(f" ❌ Connection benchmark failed")
benchmarks['ipc_connection'] = float('inf')
# Component analysis benchmark
print(f"\n⚙️ Component Analysis Benchmark:")
client = KiCadIPCClient()
try:
if client.connect():
board = client._kicad.get_board()
start_time = time.time()
footprints = board.get_footprints()
# Analyze all components
for fp in footprints:
try:
ref = fp.reference_field.text.value
pos = fp.position
value = fp.value_field.text.value
except:
continue
analysis_time = time.time() - start_time
print(f" ⚡ Full component analysis: {analysis_time*1000:.1f}ms ({len(footprints)} components)")
benchmarks['component_analysis'] = analysis_time
client.disconnect()
except Exception as e:
print(f" ❌ Component benchmark failed: {e}")
benchmarks['component_analysis'] = float('inf')
return benchmarks
def main():
"""Run the ultimate comprehensive demonstration."""
print_banner("ULTIMATE EDA AUTOMATION PLATFORM", "🏆")
print("Revolutionary KiCad MCP Server")
print("Complete Design-to-Manufacturing AI Integration")
print(f"Test Project: MLX90640 Thermal Camera")
print(f"Test Time: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}")
# Run comprehensive tests
overall_start = time.time()
analysis_results = comprehensive_project_analysis()
realtime_results = realtime_board_manipulation()
automation_results = automation_pipeline_readiness()
performance_results = performance_benchmark()
total_time = time.time() - overall_start
# Final assessment
print_banner("ULTIMATE SUCCESS ASSESSMENT", "🎯")
all_results = {**analysis_results, **realtime_results, **automation_results}
passed_tests = sum(all_results.values())
total_tests = len(all_results)
print(f"📊 Test Results: {passed_tests}/{total_tests} capabilities confirmed")
print(f"⏱️ Total execution time: {total_time:.2f}s")
# Detailed results
print(f"\n🔍 Capability Analysis:")
for category, results in [
("Project Analysis", analysis_results),
("Real-time Manipulation", realtime_results),
("Automation Pipeline", automation_results)
]:
category_passed = sum(results.values())
category_total = len(results)
status = "" if category_passed == category_total else "⚠️" if category_passed > 0 else ""
print(f" {status} {category}: {category_passed}/{category_total}")
# Performance assessment
print(f"\n⚡ Performance Analysis:")
for metric, time_val in performance_results.items():
if time_val != float('inf'):
if time_val < 0.1:
status = "🚀 EXCELLENT"
elif time_val < 0.5:
status = "✅ GOOD"
else:
status = "⚠️ ACCEPTABLE"
print(f" {status} {metric.replace('_', ' ').title()}: {time_val*1000:.1f}ms")
# Final verdict
success_rate = passed_tests / total_tests
if success_rate >= 0.95:
print_banner("🎉 PERFECTION ACHIEVED! 🎉", "🏆")
print("REVOLUTIONARY EDA AUTOMATION PLATFORM IS FULLY OPERATIONAL!")
print("✨ Complete design-to-manufacturing AI integration confirmed!")
print("🚀 Ready for production use by Claude Code users!")
print("🔥 The future of EDA automation is HERE!")
elif success_rate >= 0.85:
print_banner("🚀 OUTSTANDING SUCCESS! 🚀", "🏆")
print("ADVANCED EDA AUTOMATION PLATFORM IS OPERATIONAL!")
print("⚡ Core capabilities fully confirmed!")
print("🔥 Ready for advanced EDA workflows!")
elif success_rate >= 0.70:
print_banner("✅ SOLID SUCCESS! ✅", "🎯")
print("EDA AUTOMATION PLATFORM IS FUNCTIONAL!")
print("💪 Strong foundation for EDA automation!")
else:
print_banner("🔧 DEVELOPMENT SUCCESS! 🔧", "🛠️")
print("EDA PLATFORM FOUNDATION IS ESTABLISHED!")
print("📈 Ready for continued development!")
print(f"\n📈 Platform Readiness: {success_rate*100:.1f}%")
return success_rate >= 0.8
if __name__ == "__main__":
success = main()
sys.exit(0 if success else 1)

View File

@ -1,234 +0,0 @@
#!/usr/bin/env python3
"""
Test FreeRouting automation workflow.
This tests the complete automated PCB routing pipeline!
"""
import sys
from pathlib import Path
import os
import tempfile
import subprocess
# Add the mckicad module to path
sys.path.insert(0, str(Path(__file__).parent))
from mckicad.utils.ipc_client import KiCadIPCClient
from mckicad.utils.freerouting_engine import FreeRoutingEngine, check_routing_prerequisites
def test_routing_prerequisites():
"""Test routing prerequisites and components."""
print("🔧 Testing Routing Prerequisites")
print("-" * 40)
try:
status = check_routing_prerequisites()
components = status.get("components", {})
print("Component Status:")
all_ready = True
for comp_name, comp_info in components.items():
available = comp_info.get("available", False)
all_ready = all_ready and available
status_icon = "" if available else ""
print(f" {status_icon} {comp_name.replace('_', ' ').title()}: {'Ready' if available else 'Missing'}")
if not available and 'message' in comp_info:
print(f" {comp_info['message']}")
overall = status.get("overall_ready", False)
print(f"\n🎯 Overall Status: {'✅ READY' if overall else '⚠️ PARTIAL'}")
return overall
except Exception as e:
print(f"❌ Prerequisites check failed: {e}")
return False
def test_freerouting_engine():
"""Test FreeRouting engine initialization and capabilities."""
print("\n🚀 Testing FreeRouting Engine")
print("-" * 40)
try:
# Initialize engine
engine = FreeRoutingEngine()
print(f"✅ FreeRouting engine initialized")
# Test JAR file detection
jar_path = engine.find_freerouting_jar()
if jar_path:
print(f"✅ FreeRouting JAR found: {Path(jar_path).name}")
else:
print(f"❌ FreeRouting JAR not found")
return False
# Test Java availability
try:
result = subprocess.run(['java', '-version'],
capture_output=True, text=True, timeout=5)
if result.returncode == 0:
print(f"✅ Java runtime available")
else:
print(f"❌ Java runtime issue")
return False
except Exception as e:
print(f"❌ Java test failed: {e}")
return False
# Test FreeRouting help command
try:
result = subprocess.run(['java', '-jar', jar_path, '-h'],
capture_output=True, text=True, timeout=10)
print(f"✅ FreeRouting executable test successful")
except Exception as e:
print(f"⚠️ FreeRouting execution test: {e}")
# Don't fail here as the JAR might work differently
return True
except Exception as e:
print(f"❌ FreeRouting engine test failed: {e}")
return False
def test_dsn_export_capability():
"""Test DSN file export capability from KiCad."""
print("\n📄 Testing DSN Export Capability")
print("-" * 40)
try:
# Test KiCad CLI DSN export capability
pcb_path = "/home/rpm/claude/MLX90640-Thermal-Camera/PCB/Thermal_Camera.kicad_pcb"
if not Path(pcb_path).exists():
print(f"❌ PCB file not found: {pcb_path}")
return False
print(f"✅ PCB file found: {Path(pcb_path).name}")
# Test kicad-cli availability for export
try:
result = subprocess.run(['kicad-cli', '--help'],
capture_output=True, text=True, timeout=5)
if result.returncode == 0:
print(f"✅ KiCad CLI available for export")
else:
print(f"❌ KiCad CLI not working")
return False
except Exception as e:
print(f"❌ KiCad CLI test failed: {e}")
return False
# Test DSN export command format (dry run)
with tempfile.NamedTemporaryFile(suffix='.dsn', delete=False) as temp_dsn:
dsn_path = temp_dsn.name
print(f"📐 DSN Export Command Ready:")
print(f" Source: {Path(pcb_path).name}")
print(f" Target: {Path(dsn_path).name}")
print(f" ✅ Export pipeline prepared")
# Clean up temp file
os.unlink(dsn_path)
return True
except Exception as e:
print(f"❌ DSN export test failed: {e}")
return False
def test_routing_workflow_simulation():
"""Test complete routing workflow simulation."""
print("\n🔄 Testing Complete Routing Workflow (Simulation)")
print("-" * 40)
try:
client = KiCadIPCClient()
if not client.connect():
print("❌ Failed to connect to KiCad")
return False
board = client._kicad.get_board()
print(f"✅ Connected to board: {board.name}")
# Analyze current routing state
tracks_before = board.get_tracks()
vias_before = board.get_vias()
nets = board.get_nets()
print(f"📊 Current Board State:")
print(f" Tracks: {len(tracks_before)}")
print(f" Vias: {len(vias_before)}")
print(f" Networks: {len(nets)}")
# Analyze routing completeness
signal_nets = [net for net in nets if net.name and not any(net.name.startswith(p) for p in ['+', 'VCC', 'VDD', 'GND'])]
print(f" Signal nets: {len(signal_nets)}")
# Simulate routing workflow steps
print(f"\n🔄 Routing Workflow Simulation:")
print(f" 1. ✅ Export DSN file from KiCad board")
print(f" 2. ✅ Process with FreeRouting autorouter")
print(f" 3. ✅ Generate optimized SES file")
print(f" 4. ✅ Import routed traces back to KiCad")
print(f" 5. ✅ Verify routing completeness")
print(f"\n✅ Complete routing workflow READY!")
print(f" Input: {board.name} ({len(signal_nets)} nets to route)")
print(f" Engine: FreeRouting v1.9.0 automation")
print(f" Output: Fully routed PCB with optimized traces")
return True
except Exception as e:
print(f"❌ Workflow simulation failed: {e}")
return False
finally:
if 'client' in locals():
client.disconnect()
def main():
"""Test complete FreeRouting automation workflow."""
print("🚀 FREEROUTING AUTOMATION WORKFLOW TESTING")
print("=" * 55)
print("Testing complete automated PCB routing pipeline...")
results = {
"prerequisites": test_routing_prerequisites(),
"engine": test_freerouting_engine(),
"dsn_export": test_dsn_export_capability(),
"workflow": test_routing_workflow_simulation()
}
print("\n" + "=" * 55)
print("🎯 FREEROUTING WORKFLOW TEST RESULTS")
print("=" * 55)
passed = 0
for test_name, result in results.items():
status = "✅ PASS" if result else "❌ FAIL"
test_display = test_name.replace('_', ' ').title()
print(f"{status} {test_display}")
if result:
passed += 1
print(f"\n📊 Results: {passed}/{len(results)} tests passed")
if passed == len(results):
print("🎉 PERFECTION! FreeRouting automation FULLY OPERATIONAL!")
print("🔥 Complete automated PCB routing pipeline READY!")
print("⚡ From unrouted board to production-ready PCB in minutes!")
elif passed >= 3:
print("🚀 EXCELLENT! Core routing automation working!")
print("🔥 Advanced PCB routing capabilities confirmed!")
elif passed >= 2:
print("✅ GOOD! Basic routing infrastructure ready!")
else:
print("🔧 NEEDS WORK! Routing automation needs debugging!")
return passed >= 3
if __name__ == "__main__":
success = main()
sys.exit(0 if success else 1)

View File

@ -1,316 +0,0 @@
#!/usr/bin/env python3
"""
Test manufacturing file generation via KiCad CLI.
This tests the complete PCB-to-production pipeline!
"""
import sys
from pathlib import Path
import os
import tempfile
import subprocess
# Add the mckicad module to path
sys.path.insert(0, str(Path(__file__).parent))
PROJECT_PATH = "/home/rpm/claude/MLX90640-Thermal-Camera/PCB/Thermal_Camera.kicad_pro"
PCB_PATH = "/home/rpm/claude/MLX90640-Thermal-Camera/PCB/Thermal_Camera.kicad_pcb"
def test_gerber_generation():
"""Test Gerber file generation for PCB manufacturing."""
print("🏭 Testing Gerber File Generation")
print("-" * 40)
try:
# Create temp directory for output
with tempfile.TemporaryDirectory() as temp_dir:
output_dir = Path(temp_dir) / "gerbers"
output_dir.mkdir()
# Test Gerber generation command
cmd = [
'kicad-cli', 'pcb', 'export', 'gerbers',
'--output', str(output_dir),
PCB_PATH
]
print(f"📐 Gerber Generation Command:")
print(f" Command: {' '.join(cmd[:4])} ...")
print(f" Source: {Path(PCB_PATH).name}")
print(f" Output: {output_dir.name}/")
# Execute gerber generation
result = subprocess.run(cmd, capture_output=True, text=True, timeout=30)
if result.returncode == 0:
print(f"✅ Gerber generation successful!")
# Check generated files
gerber_files = list(output_dir.glob("*.g*"))
drill_files = list(output_dir.glob("*.drl"))
print(f"📋 Generated Files:")
print(f" Gerber layers: {len(gerber_files)}")
print(f" Drill files: {len(drill_files)}")
# Show some example files
for file in (gerber_files + drill_files)[:5]:
file_size = file.stat().st_size
print(f" {file.name}: {file_size} bytes")
if len(gerber_files) > 0:
print(f" ✅ Manufacturing-ready Gerber files generated!")
return True
else:
print(f" ❌ No Gerber files generated")
return False
else:
print(f"❌ Gerber generation failed:")
print(f" Error: {result.stderr}")
return False
except subprocess.TimeoutExpired:
print(f"❌ Gerber generation timed out")
return False
except Exception as e:
print(f"❌ Gerber generation test failed: {e}")
return False
def test_drill_file_generation():
"""Test drill file generation for PCB manufacturing."""
print("\n🔧 Testing Drill File Generation")
print("-" * 40)
try:
with tempfile.TemporaryDirectory() as temp_dir:
output_dir = Path(temp_dir) / "drill"
output_dir.mkdir()
# Test drill file generation
cmd = [
'kicad-cli', 'pcb', 'export', 'drill',
'--output', str(output_dir),
PCB_PATH
]
print(f"🔩 Drill Generation Command:")
print(f" Source: {Path(PCB_PATH).name}")
print(f" Output: {output_dir.name}/")
result = subprocess.run(cmd, capture_output=True, text=True, timeout=20)
if result.returncode == 0:
print(f"✅ Drill generation successful!")
# Check generated drill files
drill_files = list(output_dir.glob("*"))
print(f"📋 Generated Drill Files: {len(drill_files)}")
for file in drill_files:
file_size = file.stat().st_size
print(f" {file.name}: {file_size} bytes")
return len(drill_files) > 0
else:
print(f"❌ Drill generation failed: {result.stderr}")
return False
except Exception as e:
print(f"❌ Drill generation test failed: {e}")
return False
def test_position_file_generation():
"""Test component position file generation for pick & place."""
print("\n📍 Testing Position File Generation")
print("-" * 40)
try:
with tempfile.TemporaryDirectory() as temp_dir:
output_file = Path(temp_dir) / "positions.csv"
cmd = [
'kicad-cli', 'pcb', 'export', 'pos',
'--output', str(output_file),
'--format', 'csv',
PCB_PATH
]
print(f"🎯 Position Generation Command:")
print(f" Source: {Path(PCB_PATH).name}")
print(f" Output: {output_file.name}")
print(f" Format: CSV")
result = subprocess.run(cmd, capture_output=True, text=True, timeout=15)
if result.returncode == 0:
print(f"✅ Position file generation successful!")
if output_file.exists():
file_size = output_file.stat().st_size
print(f"📋 Position File: {file_size} bytes")
# Read and analyze position data
with open(output_file, 'r') as f:
lines = f.readlines()
print(f"📊 Position Data:")
print(f" Total lines: {len(lines)}")
print(f" Header: {lines[0].strip() if lines else 'None'}")
print(f" Sample: {lines[1].strip() if len(lines) > 1 else 'None'}")
print(f" ✅ Pick & place data ready for manufacturing!")
return True
else:
print(f"❌ Position file not created")
return False
else:
print(f"❌ Position generation failed: {result.stderr}")
return False
except Exception as e:
print(f"❌ Position generation test failed: {e}")
return False
def test_bom_generation():
"""Test BOM file generation."""
print("\n📋 Testing BOM Generation")
print("-" * 40)
try:
with tempfile.TemporaryDirectory() as temp_dir:
output_file = Path(temp_dir) / "bom.csv"
# Test schematic BOM export
sch_path = "/home/rpm/claude/MLX90640-Thermal-Camera/PCB/Thermal_Camera.kicad_sch"
cmd = [
'kicad-cli', 'sch', 'export', 'bom',
'--output', str(output_file),
sch_path
]
print(f"📊 BOM Generation Command:")
print(f" Source: {Path(sch_path).name}")
print(f" Output: {output_file.name}")
result = subprocess.run(cmd, capture_output=True, text=True, timeout=15)
if result.returncode == 0:
print(f"✅ BOM generation successful!")
if output_file.exists():
file_size = output_file.stat().st_size
print(f"📋 BOM File: {file_size} bytes")
# Analyze BOM content
with open(output_file, 'r') as f:
content = f.read()
lines = content.split('\n')
print(f"📊 BOM Analysis:")
print(f" Total lines: {len(lines)}")
# Count components in BOM
component_lines = [line for line in lines if line.strip() and not line.startswith('#')]
print(f" Component entries: {len(component_lines)}")
print(f" ✅ Manufacturing BOM ready!")
return True
else:
print(f"❌ BOM file not created")
return False
else:
print(f"❌ BOM generation failed: {result.stderr}")
return False
except Exception as e:
print(f"❌ BOM generation test failed: {e}")
return False
def test_3d_export():
"""Test 3D model export capability."""
print("\n🎲 Testing 3D Model Export")
print("-" * 40)
try:
with tempfile.TemporaryDirectory() as temp_dir:
output_file = Path(temp_dir) / "board_3d.step"
cmd = [
'kicad-cli', 'pcb', 'export', 'step',
'--output', str(output_file),
PCB_PATH
]
print(f"🔮 3D Export Command:")
print(f" Source: {Path(PCB_PATH).name}")
print(f" Output: {output_file.name}")
print(f" Format: STEP")
result = subprocess.run(cmd, capture_output=True, text=True, timeout=30)
if result.returncode == 0:
print(f"✅ 3D export successful!")
if output_file.exists():
file_size = output_file.stat().st_size
print(f"📋 3D Model: {file_size} bytes")
print(f" ✅ Mechanical CAD integration ready!")
return True
else:
print(f"❌ 3D file not created")
return False
else:
print(f"⚠️ 3D export: {result.stderr}")
# Don't fail here as 3D export might need additional setup
return True # Still consider as success for testing
except Exception as e:
print(f"⚠️ 3D export test: {e}")
return True # Don't fail the whole test for 3D issues
def main():
"""Test complete manufacturing file generation pipeline."""
print("🏭 MANUFACTURING FILE GENERATION TESTING")
print("=" * 50)
print("Testing complete PCB-to-production pipeline...")
results = {
"gerber_files": test_gerber_generation(),
"drill_files": test_drill_file_generation(),
"position_files": test_position_file_generation(),
"bom_generation": test_bom_generation(),
"3d_export": test_3d_export()
}
print("\n" + "=" * 50)
print("🎯 MANUFACTURING FILE TEST RESULTS")
print("=" * 50)
passed = 0
for test_name, result in results.items():
status = "✅ PASS" if result else "❌ FAIL"
test_display = test_name.replace('_', ' ').title()
print(f"{status} {test_display}")
if result:
passed += 1
print(f"\n📊 Results: {passed}/{len(results)} tests passed")
if passed == len(results):
print("🎉 PERFECTION! Manufacturing pipeline FULLY OPERATIONAL!")
print("🏭 Complete PCB-to-production automation READY!")
print("⚡ From KiCad design to factory-ready files!")
elif passed >= 4:
print("🚀 EXCELLENT! Core manufacturing capabilities working!")
print("🏭 Production-ready file generation confirmed!")
elif passed >= 3:
print("✅ GOOD! Essential manufacturing files ready!")
else:
print("🔧 PARTIAL! Some manufacturing capabilities need work!")
return passed >= 3
if __name__ == "__main__":
success = main()
sys.exit(0 if success else 1)

View File

@ -1,166 +0,0 @@
#!/usr/bin/env python3
"""
Test script for enhanced KiCad MCP server functionality.
This script tests the new routing capabilities, AI integration, and IPC API features
using the thermal camera project as a test case.
"""
import asyncio
import json
import logging
import sys
from pathlib import Path
# Add the mckicad module to path
sys.path.insert(0, str(Path(__file__).parent))
from mckicad.utils.freerouting_engine import check_routing_prerequisites
from mckicad.utils.ipc_client import check_kicad_availability
from mckicad.tools.analysis_tools import register_analysis_tools
from mckicad.tools.routing_tools import register_routing_tools
from mckicad.tools.ai_tools import register_ai_tools
# Set up logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
# Test project path
PROJECT_PATH = "/home/rpm/claude/MLX90640-Thermal-Camera/PCB/Thermal_Camera.kicad_pro"
def test_routing_prerequisites():
"""Test routing prerequisites check."""
logger.info("Testing routing prerequisites...")
try:
status = check_routing_prerequisites()
logger.info(f"Routing prerequisites status: {json.dumps(status, indent=2)}")
# Check individual components
components = status.get("components", {})
# KiCad IPC API
kicad_ipc = components.get("kicad_ipc", {})
logger.info(f"KiCad IPC API available: {kicad_ipc.get('available', False)}")
# FreeRouting
freerouting = components.get("freerouting", {})
logger.info(f"FreeRouting available: {freerouting.get('available', False)}")
# KiCad CLI
kicad_cli = components.get("kicad_cli", {})
logger.info(f"KiCad CLI available: {kicad_cli.get('available', False)}")
overall_ready = status.get("overall_ready", False)
logger.info(f"Overall routing readiness: {overall_ready}")
return status
except Exception as e:
logger.error(f"Error checking routing prerequisites: {e}")
return None
def test_kicad_ipc():
"""Test KiCad IPC API availability."""
logger.info("Testing KiCad IPC API...")
try:
status = check_kicad_availability()
logger.info(f"KiCad IPC status: {json.dumps(status, indent=2)}")
if status.get("available", False):
logger.info("✓ KiCad IPC API is available")
return True
else:
logger.warning("✗ KiCad IPC API is not available")
logger.warning(f"Reason: {status.get('message', 'Unknown')}")
return False
except Exception as e:
logger.error(f"Error testing KiCad IPC: {e}")
return False
def test_project_validation():
"""Test project validation with the thermal camera project."""
logger.info("Testing project validation...")
try:
from mckicad.utils.file_utils import get_project_files
if not Path(PROJECT_PATH).exists():
logger.error(f"Test project not found: {PROJECT_PATH}")
return False
files = get_project_files(PROJECT_PATH)
logger.info(f"Project files found: {list(files.keys())}")
required_files = ["project", "pcb", "schematic"]
missing_files = [f for f in required_files if f not in files]
if missing_files:
logger.error(f"Missing required files: {missing_files}")
return False
else:
logger.info("✓ All required project files found")
return True
except Exception as e:
logger.error(f"Error validating project: {e}")
return False
def test_enhanced_features():
"""Test enhanced MCP server features."""
logger.info("Testing enhanced features...")
results = {
"routing_prerequisites": test_routing_prerequisites(),
"kicad_ipc": test_kicad_ipc(),
"project_validation": test_project_validation()
}
return results
def main():
"""Main test function."""
logger.info("=== KiCad MCP Server Integration Test ===")
logger.info(f"Testing with project: {PROJECT_PATH}")
# Run tests
results = test_enhanced_features()
# Summary
logger.info("\n=== Test Summary ===")
for test_name, result in results.items():
status = "✓ PASS" if result else "✗ FAIL"
logger.info(f"{test_name}: {status}")
# Overall assessment
routing_ready = results["routing_prerequisites"] and results["routing_prerequisites"].get("overall_ready", False)
ipc_ready = results["kicad_ipc"]
project_valid = results["project_validation"]
logger.info(f"\nOverall Assessment:")
logger.info(f"- Project validation: {'' if project_valid else ''}")
logger.info(f"- KiCad IPC API: {'' if ipc_ready else ''}")
logger.info(f"- Routing capabilities: {'' if routing_ready else ''}")
if project_valid and ipc_ready:
logger.info("🎉 KiCad MCP server is ready for enhanced features!")
if not routing_ready:
logger.info("💡 To enable full routing automation, install FreeRouting:")
logger.info(" Download from: https://github.com/freerouting/freerouting/releases")
logger.info(" Place freerouting.jar in PATH or ~/freerouting.jar")
else:
logger.warning("⚠️ Some components need attention before full functionality")
return all([project_valid, ipc_ready])
if __name__ == "__main__":
success = main()
sys.exit(0 if success else 1)

View File

@ -1,268 +0,0 @@
#!/usr/bin/env python3
"""
Test MCP tools through the server interface.
This validates that our MCP server exposes all tools correctly.
"""
import sys
import json
from pathlib import Path
# Add the mckicad module to path
sys.path.insert(0, str(Path(__file__).parent))
# Import our server and tools
from mckicad.server import create_server
def test_server_initialization():
"""Test MCP server initialization and tool registration."""
print("🔧 Testing MCP Server Initialization")
print("-" * 40)
try:
# Create server instance
server = create_server()
print(f"✅ MCP server created: {server}")
# Check that server has the required components
print(f"✅ Server type: {type(server).__name__}")
return True
except Exception as e:
print(f"❌ Server initialization failed: {e}")
return False
def test_tool_registration():
"""Test that all tools are properly registered."""
print("\n📋 Testing Tool Registration")
print("-" * 40)
try:
# Import and test tool registration functions
from mckicad.tools.analysis_tools import register_analysis_tools
from mckicad.tools.project_tools import register_project_tools
from mckicad.tools.drc_tools import register_drc_tools
from mckicad.tools.bom_tools import register_bom_tools
from mckicad.tools.netlist_tools import register_netlist_tools
from mckicad.tools.pattern_tools import register_pattern_tools
from mckicad.tools.export_tools import register_export_tools
# Test that registration functions exist
registration_functions = [
("analysis_tools", register_analysis_tools),
("project_tools", register_project_tools),
("drc_tools", register_drc_tools),
("bom_tools", register_bom_tools),
("netlist_tools", register_netlist_tools),
("pattern_tools", register_pattern_tools),
("export_tools", register_export_tools),
]
print(f"📊 Tool Categories Available:")
for name, func in registration_functions:
print(f"{name}: {func.__name__}()")
print(f"✅ All tool registration functions available!")
return True
except ImportError as e:
print(f"❌ Tool import failed: {e}")
return False
except Exception as e:
print(f"❌ Tool registration test failed: {e}")
return False
def test_resource_registration():
"""Test that all resources are properly registered."""
print("\n📄 Testing Resource Registration")
print("-" * 40)
try:
# Import resource registration functions
from mckicad.resources.projects import register_project_resources
from mckicad.resources.files import register_file_resources
from mckicad.resources.drc_resources import register_drc_resources
from mckicad.resources.bom_resources import register_bom_resources
from mckicad.resources.netlist_resources import register_netlist_resources
resource_functions = [
("project_resources", register_project_resources),
("file_resources", register_file_resources),
("drc_resources", register_drc_resources),
("bom_resources", register_bom_resources),
("netlist_resources", register_netlist_resources),
]
print(f"📊 Resource Categories Available:")
for name, func in resource_functions:
print(f"{name}: {func.__name__}()")
print(f"✅ All resource registration functions available!")
return True
except ImportError as e:
print(f"❌ Resource import failed: {e}")
return False
except Exception as e:
print(f"❌ Resource registration test failed: {e}")
return False
def test_prompt_registration():
"""Test that all prompts are properly registered."""
print("\n💬 Testing Prompt Registration")
print("-" * 40)
try:
# Import prompt registration functions
from mckicad.prompts.templates import register_prompts
from mckicad.prompts.drc_prompt import register_drc_prompts
from mckicad.prompts.bom_prompts import register_bom_prompts
from mckicad.prompts.pattern_prompts import register_pattern_prompts
prompt_functions = [
("templates", register_prompts),
("drc_prompts", register_drc_prompts),
("bom_prompts", register_bom_prompts),
("pattern_prompts", register_pattern_prompts),
]
print(f"📊 Prompt Categories Available:")
for name, func in prompt_functions:
print(f"{name}: {func.__name__}()")
print(f"✅ All prompt registration functions available!")
return True
except ImportError as e:
print(f"❌ Prompt import failed: {e}")
return False
except Exception as e:
print(f"❌ Prompt registration test failed: {e}")
return False
def test_core_functionality():
"""Test core functionality imports and basic operations."""
print("\n⚙️ Testing Core Functionality")
print("-" * 40)
try:
# Test key utility imports
from mckicad.utils.file_utils import get_project_files
from mckicad.utils.ipc_client import KiCadIPCClient, check_kicad_availability
from mckicad.utils.freerouting_engine import check_routing_prerequisites
from mckicad.utils.netlist_parser import extract_netlist
print(f"📦 Core Utilities Available:")
print(f" ✅ file_utils: Project file management")
print(f" ✅ ipc_client: Real-time KiCad integration")
print(f" ✅ freerouting_engine: Automated routing")
print(f" ✅ netlist_parser: Circuit analysis")
# Test basic functionality
project_path = "/home/rpm/claude/MLX90640-Thermal-Camera/PCB/Thermal_Camera.kicad_pro"
if Path(project_path).exists():
files = get_project_files(project_path)
print(f" ✅ File analysis: {len(files)} project files detected")
# Test IPC availability (quick check)
ipc_status = check_kicad_availability()
print(f" ✅ IPC status: {'Available' if ipc_status.get('available') else 'Unavailable'}")
# Test routing prerequisites
routing_status = check_routing_prerequisites()
routing_ready = routing_status.get('overall_ready', False)
print(f" ✅ Routing status: {'Ready' if routing_ready else 'Partial'}")
print(f"✅ Core functionality operational!")
return True
except Exception as e:
print(f"❌ Core functionality test failed: {e}")
return False
def test_server_completeness():
"""Test that server has all expected components."""
print("\n🎯 Testing Server Completeness")
print("-" * 40)
try:
# Check that the main server creation works
from mckicad.server import create_server
from mckicad.config import KICAD_CLI_TIMEOUT
from mckicad.context import KiCadAppContext
print(f"📊 Server Components:")
print(f" ✅ create_server(): Main entry point")
print(f" ✅ Configuration: Timeout settings ({KICAD_CLI_TIMEOUT}s)")
print(f" ✅ Context management: {KiCadAppContext.__name__}")
# Verify key constants and configurations
from mckicad import config
config_items = [
'KICAD_CLI_TIMEOUT', 'DEFAULT_KICAD_PATHS',
'COMPONENT_LIBRARY_MAP', 'DEFAULT_FOOTPRINTS'
]
available_config = []
for item in config_items:
if hasattr(config, item):
available_config.append(item)
print(f" ✅ Configuration items: {len(available_config)}/{len(config_items)}")
print(f"✅ Server completeness confirmed!")
return True
except Exception as e:
print(f"❌ Server completeness test failed: {e}")
return False
def main():
"""Test complete MCP server interface."""
print("🖥️ MCP SERVER INTERFACE TESTING")
print("=" * 45)
print("Testing complete MCP server tool exposure...")
results = {
"server_init": test_server_initialization(),
"tool_registration": test_tool_registration(),
"resource_registration": test_resource_registration(),
"prompt_registration": test_prompt_registration(),
"core_functionality": test_core_functionality(),
"server_completeness": test_server_completeness()
}
print("\n" + "=" * 45)
print("🎯 MCP SERVER INTERFACE TEST RESULTS")
print("=" * 45)
passed = 0
for test_name, result in results.items():
status = "✅ PASS" if result else "❌ FAIL"
test_display = test_name.replace('_', ' ').title()
print(f"{status} {test_display}")
if result:
passed += 1
print(f"\n📊 Results: {passed}/{len(results)} tests passed")
if passed == len(results):
print("🎉 PERFECTION! MCP server interface FULLY OPERATIONAL!")
print("🖥️ Complete tool/resource/prompt exposure confirmed!")
print("⚡ Ready for Claude Code integration!")
elif passed >= 5:
print("🚀 EXCELLENT! MCP server core functionality working!")
print("🖥️ Advanced EDA automation interface ready!")
elif passed >= 4:
print("✅ GOOD! Essential MCP components operational!")
else:
print("🔧 PARTIAL! MCP interface needs refinement!")
return passed >= 4
if __name__ == "__main__":
success = main()
sys.exit(0 if success else 1)

33
tests/test_bom.py Normal file
View File

@ -0,0 +1,33 @@
"""Tests for BOM tools."""
import csv
import pytest
@pytest.mark.unit
def test_analyze_bom_no_csv(project_path):
"""analyze_bom with no CSV files should return empty results gracefully."""
from mckicad.tools.bom import analyze_bom
result = analyze_bom(project_path)
# Should succeed but find no data
assert isinstance(result, dict)
@pytest.mark.unit
def test_analyze_bom_with_csv(tmp_project_dir, project_path):
"""analyze_bom should parse a BOM CSV file."""
from mckicad.tools.bom import analyze_bom
# Create a simple BOM CSV
bom_path = tmp_project_dir / "test_project-bom.csv"
with open(bom_path, "w", newline="") as f:
writer = csv.writer(f)
writer.writerow(["Reference", "Value", "Footprint", "Qty"])
writer.writerow(["R1", "10k", "0805", "1"])
writer.writerow(["R2", "4.7k", "0805", "1"])
writer.writerow(["C1", "100nF", "0805", "1"])
result = analyze_bom(project_path)
assert isinstance(result, dict)

52
tests/test_config.py Normal file
View File

@ -0,0 +1,52 @@
"""Tests for mckicad.config — lazy configuration functions."""
def test_kicad_extensions_has_required_types():
from mckicad.config import KICAD_EXTENSIONS
assert "project" in KICAD_EXTENSIONS
assert "pcb" in KICAD_EXTENSIONS
assert "schematic" in KICAD_EXTENSIONS
assert KICAD_EXTENSIONS["project"] == ".kicad_pro"
def test_timeout_constants_are_positive():
from mckicad.config import TIMEOUT_CONSTANTS
for key, val in TIMEOUT_CONSTANTS.items():
assert val > 0, f"Timeout {key} must be positive"
def test_get_search_paths_reads_env(monkeypatch, tmp_path):
test_dir = str(tmp_path)
monkeypatch.setenv("KICAD_SEARCH_PATHS", test_dir)
from mckicad.config import get_search_paths
paths = get_search_paths()
assert test_dir in paths
def test_get_search_paths_filters_nonexistent(monkeypatch):
monkeypatch.setenv("KICAD_SEARCH_PATHS", "/nonexistent/path/abc123")
from mckicad.config import get_search_paths
paths = get_search_paths()
assert "/nonexistent/path/abc123" not in paths
def test_get_kicad_user_dir_env_override(monkeypatch):
monkeypatch.setenv("KICAD_USER_DIR", "/custom/kicad/dir")
from mckicad.config import get_kicad_user_dir
assert get_kicad_user_dir() == "/custom/kicad/dir"
def test_common_libraries_structure():
from mckicad.config import COMMON_LIBRARIES
assert "basic" in COMMON_LIBRARIES
assert "resistor" in COMMON_LIBRARIES["basic"]
assert "library" in COMMON_LIBRARIES["basic"]["resistor"]
assert "symbol" in COMMON_LIBRARIES["basic"]["resistor"]

32
tests/test_drc.py Normal file
View File

@ -0,0 +1,32 @@
"""Tests for DRC tools."""
import pytest
@pytest.mark.unit
def test_create_drc_rule_set_standard():
"""create_drc_rule_set should return rules for standard technology."""
from mckicad.tools.drc import create_drc_rule_set
result = create_drc_rule_set(name="test_rules", technology="standard")
assert result["success"] is True
assert "rules" in result["data"]
@pytest.mark.unit
def test_create_drc_rule_set_invalid_technology():
"""create_drc_rule_set should fail for unknown technology."""
from mckicad.tools.drc import create_drc_rule_set
result = create_drc_rule_set(name="test", technology="quantum")
assert result["success"] is False
@pytest.mark.unit
def test_get_manufacturing_constraints():
"""get_manufacturing_constraints should return constraints dict."""
from mckicad.tools.drc import get_manufacturing_constraints
result = get_manufacturing_constraints(technology="standard")
assert result["success"] is True
assert "constraints" in result["data"]

20
tests/test_project.py Normal file
View File

@ -0,0 +1,20 @@
"""Tests for project tools."""
def test_get_project_structure(project_path):
"""get_project_structure should return file dict for a valid project."""
from mckicad.tools.project import get_project_structure
result = get_project_structure(project_path)
assert result["success"] is True
assert "project" in result["data"]["files"]
def test_get_project_structure_missing():
"""get_project_structure should fail for nonexistent path."""
from mckicad.tools.project import get_project_structure
result = get_project_structure("/nonexistent/fake.kicad_pro")
assert result["success"] is False
assert "error" in result

48
tests/test_schematic.py Normal file
View File

@ -0,0 +1,48 @@
"""Tests for schematic tools (kicad-sch-api integration)."""
import os
import pytest
@pytest.mark.unit
def test_create_schematic(tmp_output_dir):
"""create_schematic should produce a .kicad_sch file."""
from mckicad.tools.schematic import create_schematic
output_path = os.path.join(tmp_output_dir, "test.kicad_sch")
result = create_schematic(name="test_circuit", output_path=output_path)
assert result["success"] is True
assert os.path.exists(output_path)
@pytest.mark.unit
def test_create_schematic_invalid_path():
"""create_schematic should fail gracefully for invalid paths."""
from mckicad.tools.schematic import create_schematic
result = create_schematic(name="x", output_path="/nonexistent/dir/test.kicad_sch")
assert result["success"] is False
assert "error" in result
@pytest.mark.unit
def test_search_components():
"""search_components should return results for common queries."""
from mckicad.tools.schematic import search_components
result = search_components(query="resistor")
# Should succeed even if no libs installed (returns empty results)
assert "success" in result
@pytest.mark.unit
def test_list_components_empty_schematic(tmp_output_dir):
"""list_components on new empty schematic should return empty list."""
from mckicad.tools.schematic import create_schematic, list_components
path = os.path.join(tmp_output_dir, "empty.kicad_sch")
create_schematic(name="empty", output_path=path)
result = list_components(schematic_path=path)
if result["success"]:
assert result.get("count", 0) == 0

View File

@ -1,234 +0,0 @@
"""
Tests for the mckicad.config module.
"""
import os
import platform
from unittest.mock import patch
class TestConfigModule:
"""Test config module constants and platform-specific behavior."""
def test_system_detection(self):
"""Test that system is properly detected."""
from mckicad.config import system
assert system in ['Darwin', 'Windows', 'Linux'] or isinstance(system, str)
assert system == platform.system()
def test_macos_paths(self):
"""Test macOS-specific path configuration."""
with patch('platform.system', return_value='Darwin'):
# Need to reload the config module after patching
import importlib
import mckicad.config
importlib.reload(mckicad.config)
from mckicad.config import KICAD_APP_PATH, KICAD_PYTHON_BASE, KICAD_USER_DIR
assert os.path.expanduser("~/Documents/KiCad") == KICAD_USER_DIR
assert KICAD_APP_PATH == "/Applications/KiCad/KiCad.app"
assert "Contents/Frameworks/Python.framework" in KICAD_PYTHON_BASE
def test_windows_paths(self):
"""Test Windows-specific path configuration."""
with patch('platform.system', return_value='Windows'):
import importlib
import mckicad.config
importlib.reload(mckicad.config)
from mckicad.config import KICAD_APP_PATH, KICAD_PYTHON_BASE, KICAD_USER_DIR
assert os.path.expanduser("~/Documents/KiCad") == KICAD_USER_DIR
assert KICAD_APP_PATH == r"C:\Program Files\KiCad"
assert KICAD_PYTHON_BASE == ""
def test_linux_paths(self):
"""Test Linux-specific path configuration."""
with patch('platform.system', return_value='Linux'):
import importlib
import mckicad.config
importlib.reload(mckicad.config)
from mckicad.config import KICAD_APP_PATH, KICAD_PYTHON_BASE, KICAD_USER_DIR
assert os.path.expanduser("~/KiCad") == KICAD_USER_DIR
assert KICAD_APP_PATH == "/usr/share/kicad"
assert KICAD_PYTHON_BASE == ""
def test_unknown_system_defaults_to_macos(self):
"""Test that unknown systems default to macOS paths."""
with patch('platform.system', return_value='FreeBSD'):
import importlib
import mckicad.config
importlib.reload(mckicad.config)
from mckicad.config import KICAD_APP_PATH, KICAD_USER_DIR
assert os.path.expanduser("~/Documents/KiCad") == KICAD_USER_DIR
assert KICAD_APP_PATH == "/Applications/KiCad/KiCad.app"
def test_kicad_extensions(self):
"""Test KiCad file extension mappings."""
from mckicad.config import KICAD_EXTENSIONS
expected_keys = ["project", "pcb", "schematic", "design_rules",
"worksheet", "footprint", "netlist", "kibot_config"]
for key in expected_keys:
assert key in KICAD_EXTENSIONS
assert isinstance(KICAD_EXTENSIONS[key], str)
assert KICAD_EXTENSIONS[key].startswith(('.', '_'))
def test_data_extensions(self):
"""Test data file extensions list."""
from mckicad.config import DATA_EXTENSIONS
assert isinstance(DATA_EXTENSIONS, list)
assert len(DATA_EXTENSIONS) > 0
expected_extensions = [".csv", ".pos", ".net", ".zip", ".drl"]
for ext in expected_extensions:
assert ext in DATA_EXTENSIONS
def test_circuit_defaults(self):
"""Test circuit default parameters."""
from mckicad.config import CIRCUIT_DEFAULTS
required_keys = ["grid_spacing", "component_spacing", "wire_width",
"text_size", "pin_length"]
for key in required_keys:
assert key in CIRCUIT_DEFAULTS
# Test specific types
assert isinstance(CIRCUIT_DEFAULTS["text_size"], list)
assert len(CIRCUIT_DEFAULTS["text_size"]) == 2
assert all(isinstance(x, (int, float)) for x in CIRCUIT_DEFAULTS["text_size"])
def test_common_libraries_structure(self):
"""Test common libraries configuration structure."""
from mckicad.config import COMMON_LIBRARIES
expected_categories = ["basic", "power", "connectors"]
for category in expected_categories:
assert category in COMMON_LIBRARIES
assert isinstance(COMMON_LIBRARIES[category], dict)
for component, info in COMMON_LIBRARIES[category].items():
assert "library" in info
assert "symbol" in info
assert isinstance(info["library"], str)
assert isinstance(info["symbol"], str)
def test_default_footprints_structure(self):
"""Test default footprints configuration structure."""
from mckicad.config import DEFAULT_FOOTPRINTS
# Test that at least some common components are present
common_components = ["R", "C", "LED", "D"]
for component in common_components:
assert component in DEFAULT_FOOTPRINTS
assert isinstance(DEFAULT_FOOTPRINTS[component], list)
assert len(DEFAULT_FOOTPRINTS[component]) > 0
# All footprints should be strings
for footprint in DEFAULT_FOOTPRINTS[component]:
assert isinstance(footprint, str)
assert ":" in footprint # Should be in format "Library:Footprint"
def test_timeout_constants(self):
"""Test timeout constants are reasonable values."""
from mckicad.config import TIMEOUT_CONSTANTS
required_keys = ["kicad_cli_version_check", "kicad_cli_export",
"application_open", "subprocess_default"]
for key in required_keys:
assert key in TIMEOUT_CONSTANTS
timeout = TIMEOUT_CONSTANTS[key]
assert isinstance(timeout, (int, float))
assert 0 < timeout <= 300 # Reasonable timeout range
def test_progress_constants(self):
"""Test progress constants are valid percentages."""
from mckicad.config import PROGRESS_CONSTANTS
required_keys = ["start", "detection", "setup", "processing",
"finishing", "validation", "complete"]
for key in required_keys:
assert key in PROGRESS_CONSTANTS
progress = PROGRESS_CONSTANTS[key]
assert isinstance(progress, int)
assert 0 <= progress <= 100
def test_display_constants(self):
"""Test display constants are reasonable values."""
from mckicad.config import DISPLAY_CONSTANTS
assert "bom_preview_limit" in DISPLAY_CONSTANTS
limit = DISPLAY_CONSTANTS["bom_preview_limit"]
assert isinstance(limit, int)
assert limit > 0
def test_empty_search_paths_environment(self):
"""Test behavior with empty KICAD_SEARCH_PATHS."""
with patch.dict(os.environ, {"KICAD_SEARCH_PATHS": ""}):
import importlib
import mckicad.config
importlib.reload(mckicad.config)
# Should still have default locations if they exist
from mckicad.config import ADDITIONAL_SEARCH_PATHS
assert isinstance(ADDITIONAL_SEARCH_PATHS, list)
def test_nonexistent_search_paths_ignored(self):
"""Test that nonexistent search paths are ignored."""
with patch.dict(os.environ, {"KICAD_SEARCH_PATHS": "/nonexistent/path1,/nonexistent/path2"}), \
patch('os.path.exists', return_value=False):
import importlib
import mckicad.config
importlib.reload(mckicad.config)
from mckicad.config import ADDITIONAL_SEARCH_PATHS
# Should not contain the nonexistent paths
assert "/nonexistent/path1" not in ADDITIONAL_SEARCH_PATHS
assert "/nonexistent/path2" not in ADDITIONAL_SEARCH_PATHS
def test_search_paths_expansion_and_trimming(self):
"""Test that search paths are expanded and trimmed."""
with patch.dict(os.environ, {"KICAD_SEARCH_PATHS": "~/test_path1, ~/test_path2 "}), \
patch('os.path.exists', return_value=True), \
patch('os.path.expanduser', side_effect=lambda x: x.replace("~", "/home/user")):
import importlib
import mckicad.config
importlib.reload(mckicad.config)
from mckicad.config import ADDITIONAL_SEARCH_PATHS
# Should contain expanded paths
assert "/home/user/test_path1" in ADDITIONAL_SEARCH_PATHS
assert "/home/user/test_path2" in ADDITIONAL_SEARCH_PATHS
def test_default_project_locations_expanded(self):
"""Test that default project locations are properly expanded."""
from mckicad.config import DEFAULT_PROJECT_LOCATIONS
assert isinstance(DEFAULT_PROJECT_LOCATIONS, list)
assert len(DEFAULT_PROJECT_LOCATIONS) > 0
# All should start with ~/
for location in DEFAULT_PROJECT_LOCATIONS:
assert location.startswith("~/")

View File

@ -1,229 +0,0 @@
"""
Tests for the mckicad.context module.
"""
from unittest.mock import Mock, patch
import pytest
from mckicad.context import KiCadAppContext, kicad_lifespan
class TestKiCadAppContext:
"""Test the KiCadAppContext dataclass."""
def test_context_creation(self):
"""Test basic context creation with required parameters."""
context = KiCadAppContext(
kicad_modules_available=True,
cache={}
)
assert context.kicad_modules_available is True
assert context.cache == {}
assert isinstance(context.cache, dict)
def test_context_with_cache_data(self):
"""Test context creation with pre-populated cache."""
test_cache = {"test_key": "test_value", "number": 42}
context = KiCadAppContext(
kicad_modules_available=False,
cache=test_cache
)
assert context.kicad_modules_available is False
assert context.cache == test_cache
assert context.cache["test_key"] == "test_value"
assert context.cache["number"] == 42
def test_context_immutable_fields(self):
"""Test that context fields behave as expected for a dataclass."""
context = KiCadAppContext(
kicad_modules_available=True,
cache={"initial": "value"}
)
# Should be able to modify the cache (it's mutable)
context.cache["new_key"] = "new_value"
assert context.cache["new_key"] == "new_value"
# Should be able to reassign fields
context.kicad_modules_available = False
assert context.kicad_modules_available is False
class TestKiCadLifespan:
"""Test the kicad_lifespan context manager."""
@pytest.fixture
def mock_server(self):
"""Create a mock FastMCP server."""
return Mock()
@pytest.mark.asyncio
async def test_lifespan_basic_flow(self, mock_server):
"""Test basic lifespan flow with successful initialization and cleanup."""
with patch('mckicad.context.logging') as mock_logging:
async with kicad_lifespan(mock_server, kicad_modules_available=True) as context:
# Check context is properly initialized
assert isinstance(context, KiCadAppContext)
assert context.kicad_modules_available is True
assert isinstance(context.cache, dict)
assert len(context.cache) == 0
# Add something to cache to test cleanup
context.cache["test"] = "value"
# Verify logging calls
mock_logging.info.assert_any_call("Starting KiCad MCP server initialization")
mock_logging.info.assert_any_call("KiCad MCP server initialization complete")
mock_logging.info.assert_any_call("Shutting down KiCad MCP server")
mock_logging.info.assert_any_call("KiCad MCP server shutdown complete")
@pytest.mark.asyncio
async def test_lifespan_kicad_modules_false(self, mock_server):
"""Test lifespan with KiCad modules unavailable."""
async with kicad_lifespan(mock_server, kicad_modules_available=False) as context:
assert context.kicad_modules_available is False
assert isinstance(context.cache, dict)
@pytest.mark.asyncio
async def test_lifespan_cache_operations(self, mock_server):
"""Test cache operations during lifespan."""
async with kicad_lifespan(mock_server, kicad_modules_available=True) as context:
# Test cache operations
context.cache["key1"] = "value1"
context.cache["key2"] = {"nested": "data"}
context.cache["key3"] = [1, 2, 3]
assert context.cache["key1"] == "value1"
assert context.cache["key2"]["nested"] == "data"
assert context.cache["key3"] == [1, 2, 3]
assert len(context.cache) == 3
@pytest.mark.asyncio
async def test_lifespan_cache_cleanup(self, mock_server):
"""Test that cache is properly cleared on shutdown."""
with patch('mckicad.context.logging') as mock_logging:
async with kicad_lifespan(mock_server, kicad_modules_available=True) as context:
# Populate cache
context.cache["test1"] = "value1"
context.cache["test2"] = "value2"
assert len(context.cache) == 2
# Verify cache cleanup was logged
mock_logging.info.assert_any_call("Clearing cache with 2 entries")
@pytest.mark.asyncio
async def test_lifespan_exception_handling(self, mock_server):
"""Test that cleanup happens even if an exception occurs."""
with patch('mckicad.context.logging') as mock_logging:
with pytest.raises(ValueError):
async with kicad_lifespan(mock_server, kicad_modules_available=True) as context:
context.cache["test"] = "value"
raise ValueError("Test exception")
# Verify cleanup still occurred
mock_logging.info.assert_any_call("Shutting down KiCad MCP server")
mock_logging.info.assert_any_call("KiCad MCP server shutdown complete")
@pytest.mark.asyncio
@pytest.mark.skip(reason="Mock setup complexity - temp dir cleanup not critical")
async def test_lifespan_temp_dir_cleanup(self, mock_server):
"""Test temporary directory cleanup functionality."""
with patch('mckicad.context.logging') as mock_logging, \
patch('mckicad.context.shutil') as mock_shutil:
async with kicad_lifespan(mock_server, kicad_modules_available=True) as context:
# The current implementation has an empty created_temp_dirs list
pass
# Verify shutil was imported (even if not used in current implementation)
# This tests the import doesn't fail
@pytest.mark.asyncio
@pytest.mark.skip(reason="Mock setup complexity - temp dir cleanup error handling not critical")
async def test_lifespan_temp_dir_cleanup_error_handling(self, mock_server):
"""Test error handling in temp directory cleanup."""
# Mock the created_temp_dirs to have some directories for testing
with patch('mckicad.context.logging') as mock_logging, \
patch('mckicad.context.shutil') as mock_shutil:
# Patch the created_temp_dirs list in the function scope
original_lifespan = kicad_lifespan
async def patched_lifespan(server, kicad_modules_available=False):
async with original_lifespan(server, kicad_modules_available) as context:
# Simulate having temp directories to clean up
context._temp_dirs = ["/tmp/test1", "/tmp/test2"] # Add test attribute
yield context
# Simulate cleanup with error
test_dirs = ["/tmp/test1", "/tmp/test2"]
mock_shutil.rmtree.side_effect = [None, OSError("Permission denied")]
for temp_dir in test_dirs:
try:
mock_shutil.rmtree(temp_dir, ignore_errors=True)
except Exception as e:
mock_logging.error(f"Error cleaning up temporary directory {temp_dir}: {str(e)}")
# The current implementation doesn't actually have temp dirs, so we test the structure
async with kicad_lifespan(mock_server) as context:
pass
@pytest.mark.asyncio
async def test_lifespan_default_parameters(self, mock_server):
"""Test lifespan with default parameters."""
async with kicad_lifespan(mock_server) as context:
# Default kicad_modules_available should be False
assert context.kicad_modules_available is False
assert isinstance(context.cache, dict)
assert len(context.cache) == 0
@pytest.mark.asyncio
async def test_lifespan_logging_messages(self, mock_server):
"""Test specific logging messages are called correctly."""
with patch('mckicad.context.logging') as mock_logging:
async with kicad_lifespan(mock_server, kicad_modules_available=True) as context:
context.cache["test"] = "data"
# Check specific log messages
expected_calls = [
"Starting KiCad MCP server initialization",
"KiCad Python module availability: True (Setup logic removed)",
"KiCad MCP server initialization complete",
"Shutting down KiCad MCP server",
"Clearing cache with 1 entries",
"KiCad MCP server shutdown complete"
]
for expected_call in expected_calls:
mock_logging.info.assert_any_call(expected_call)
@pytest.mark.asyncio
async def test_lifespan_empty_cache_no_cleanup_log(self, mock_server):
"""Test that empty cache doesn't log cleanup message."""
with patch('mckicad.context.logging') as mock_logging:
async with kicad_lifespan(mock_server, kicad_modules_available=False) as context:
# Don't add anything to cache
pass
# Should not log cache clearing for empty cache
calls = [call.args[0] for call in mock_logging.info.call_args_list]
cache_clear_calls = [call for call in calls if "Clearing cache" in call]
assert len(cache_clear_calls) == 0
@pytest.mark.asyncio
async def test_multiple_lifespan_instances(self, mock_server):
"""Test that multiple lifespan instances work independently."""
# Test sequential usage
async with kicad_lifespan(mock_server, kicad_modules_available=True) as context1:
context1.cache["instance1"] = "data1"
assert len(context1.cache) == 1
async with kicad_lifespan(mock_server, kicad_modules_available=False) as context2:
context2.cache["instance2"] = "data2"
assert len(context2.cache) == 1
assert context2.kicad_modules_available is False
# Should not have data from first instance
assert "instance1" not in context2.cache

View File

@ -1,368 +0,0 @@
"""
Tests for the mckicad.server module.
"""
import logging
import signal
from unittest.mock import Mock, call, patch
import pytest
from mckicad.server import (
add_cleanup_handler,
create_server,
main,
register_signal_handlers,
run_cleanup_handlers,
setup_logging,
shutdown_server,
)
class TestCleanupHandlers:
"""Test cleanup handler management."""
def setup_method(self):
"""Reset cleanup handlers before each test."""
from mckicad.server import cleanup_handlers
cleanup_handlers.clear()
def test_add_cleanup_handler(self):
"""Test adding cleanup handlers."""
def dummy_handler():
pass
add_cleanup_handler(dummy_handler)
from mckicad.server import cleanup_handlers
assert dummy_handler in cleanup_handlers
def test_add_multiple_cleanup_handlers(self):
"""Test adding multiple cleanup handlers."""
def handler1():
pass
def handler2():
pass
add_cleanup_handler(handler1)
add_cleanup_handler(handler2)
from mckicad.server import cleanup_handlers
assert handler1 in cleanup_handlers
assert handler2 in cleanup_handlers
assert len(cleanup_handlers) == 2
@patch('mckicad.server.logging')
def test_run_cleanup_handlers_success(self, mock_logging):
"""Test successful execution of cleanup handlers."""
handler1 = Mock()
handler1.__name__ = "handler1"
handler2 = Mock()
handler2.__name__ = "handler2"
add_cleanup_handler(handler1)
add_cleanup_handler(handler2)
run_cleanup_handlers()
handler1.assert_called_once()
handler2.assert_called_once()
mock_logging.info.assert_any_call("Running cleanup handlers...")
@patch('mckicad.server.logging')
@pytest.mark.skip(reason="Mock handler execution complexity - exception handling works in practice")
def test_run_cleanup_handlers_with_exception(self, mock_logging):
"""Test cleanup handlers with exceptions."""
def failing_handler():
raise ValueError("Test error")
failing_handler.__name__ = "failing_handler"
def working_handler():
pass
working_handler.__name__ = "working_handler"
add_cleanup_handler(failing_handler)
add_cleanup_handler(working_handler)
# Should not raise exception
run_cleanup_handlers()
mock_logging.error.assert_called()
# Should still log success for working handler
mock_logging.info.assert_any_call("Cleanup handler working_handler completed successfully")
@patch('mckicad.server.logging')
@pytest.mark.skip(reason="Global state management complexity - double execution prevention works")
def test_run_cleanup_handlers_prevents_double_execution(self, mock_logging):
"""Test that cleanup handlers don't run twice."""
handler = Mock()
handler.__name__ = "test_handler"
add_cleanup_handler(handler)
# Run twice
run_cleanup_handlers()
run_cleanup_handlers()
# Handler should only be called once
handler.assert_called_once()
class TestServerShutdown:
"""Test server shutdown functionality."""
def setup_method(self):
"""Reset server instance before each test."""
import mckicad.server
mckicad.server._server_instance = None
@patch('mckicad.server.logging')
def test_shutdown_server_with_instance(self, mock_logging):
"""Test shutting down server when instance exists."""
import mckicad.server
# Set up mock server instance
mock_server = Mock()
mckicad.server._server_instance = mock_server
shutdown_server()
mock_logging.info.assert_any_call("Shutting down KiCad MCP server")
mock_logging.info.assert_any_call("KiCad MCP server shutdown complete")
# Server instance should be cleared
assert mckicad.server._server_instance is None
@patch('mckicad.server.logging')
def test_shutdown_server_no_instance(self, mock_logging):
"""Test shutting down server when no instance exists."""
shutdown_server()
# Should not log anything since no server instance exists
mock_logging.info.assert_not_called()
class TestSignalHandlers:
"""Test signal handler registration."""
@patch('mckicad.server.signal.signal')
@patch('mckicad.server.logging')
def test_register_signal_handlers_success(self, mock_logging, mock_signal):
"""Test successful signal handler registration."""
mock_server = Mock()
register_signal_handlers(mock_server)
# Should register handlers for SIGINT and SIGTERM
expected_calls = [
call(signal.SIGINT, mock_signal.call_args_list[0][0][1]),
call(signal.SIGTERM, mock_signal.call_args_list[1][0][1])
]
assert mock_signal.call_count == 2
mock_logging.info.assert_any_call("Registered handler for signal 2") # SIGINT
mock_logging.info.assert_any_call("Registered handler for signal 15") # SIGTERM
@patch('mckicad.server.signal.signal')
@patch('mckicad.server.logging')
def test_register_signal_handlers_failure(self, mock_logging, mock_signal):
"""Test signal handler registration failure."""
mock_server = Mock()
mock_signal.side_effect = ValueError("Signal not supported")
register_signal_handlers(mock_server)
# Should log errors for failed registrations
mock_logging.error.assert_called()
@patch('mckicad.server.run_cleanup_handlers')
@patch('mckicad.server.shutdown_server')
@patch('mckicad.server.os._exit')
@patch('mckicad.server.logging')
def test_signal_handler_execution(self, mock_logging, mock_exit, mock_shutdown, mock_cleanup):
"""Test that signal handler executes cleanup and shutdown."""
mock_server = Mock()
with patch('mckicad.server.signal.signal') as mock_signal:
register_signal_handlers(mock_server)
# Get the registered handler function
handler_func = mock_signal.call_args_list[0][0][1]
# Call the handler
handler_func(signal.SIGINT, None)
# Verify cleanup sequence
mock_logging.info.assert_any_call("Received signal 2, initiating shutdown...")
mock_cleanup.assert_called_once()
mock_shutdown.assert_called_once()
mock_exit.assert_called_once_with(0)
class TestCreateServer:
"""Test server creation and configuration."""
@patch('mckicad.server.logging')
@patch('mckicad.server.FastMCP')
@patch('mckicad.server.register_signal_handlers')
@patch('mckicad.server.atexit.register')
@patch('mckicad.server.add_cleanup_handler')
def test_create_server_basic(self, mock_add_cleanup, mock_atexit, mock_register_signals, mock_fastmcp, mock_logging):
"""Test basic server creation."""
mock_server_instance = Mock()
mock_fastmcp.return_value = mock_server_instance
server = create_server()
# Verify FastMCP was created with correct parameters
mock_fastmcp.assert_called_once()
args, kwargs = mock_fastmcp.call_args
assert args[0] == "KiCad" # Server name
assert "lifespan" in kwargs
# Verify signal handlers and cleanup were registered
mock_register_signals.assert_called_once_with(mock_server_instance)
mock_atexit.assert_called_once()
mock_add_cleanup.assert_called()
assert server == mock_server_instance
@patch('mckicad.server.logging')
@patch('mckicad.server.FastMCP')
def test_create_server_logging(self, mock_fastmcp, mock_logging):
"""Test server creation logging."""
mock_server_instance = Mock()
mock_fastmcp.return_value = mock_server_instance
with patch('mckicad.server.register_signal_handlers'), \
patch('mckicad.server.atexit.register'), \
patch('mckicad.server.add_cleanup_handler'):
create_server()
# Verify logging calls
expected_log_calls = [
"Initializing KiCad MCP server",
"KiCad Python module setup removed; relying on kicad-cli for external operations.",
"Created FastMCP server instance with lifespan management",
"Registering resources...",
"Registering tools...",
"Registering prompts...",
"Server initialization complete"
]
for expected_call in expected_log_calls:
mock_logging.info.assert_any_call(expected_call)
@patch('mckicad.server.get_temp_dirs')
@patch('mckicad.server.os.path.exists')
@patch('mckicad.server.logging')
@pytest.mark.skip(reason="Complex mock setup for temp dir cleanup - functionality works in practice")
def test_temp_directory_cleanup_handler(self, mock_logging, mock_exists, mock_get_temp_dirs):
"""Test that temp directory cleanup handler works correctly."""
# Mock temp directories
mock_get_temp_dirs.return_value = ["/tmp/test1", "/tmp/test2"]
mock_exists.return_value = True
with patch('mckicad.server.FastMCP'), \
patch('mckicad.server.register_signal_handlers'), \
patch('mckicad.server.atexit.register'), \
patch('mckicad.server.add_cleanup_handler') as mock_add_cleanup, \
patch('mckicad.server.shutil.rmtree') as mock_rmtree:
create_server()
# Get the cleanup handler that was added
cleanup_calls = mock_add_cleanup.call_args_list
cleanup_handler = None
for call_args, call_kwargs in cleanup_calls:
if len(call_args) > 0 and hasattr(call_args[0], '__name__'):
if 'cleanup_temp_dirs' in str(call_args[0]):
cleanup_handler = call_args[0]
break
# Execute the cleanup handler manually to test it
if cleanup_handler:
cleanup_handler()
assert mock_get_temp_dirs.called
assert mock_rmtree.call_count == 2
class TestSetupLogging:
"""Test logging configuration."""
@patch('mckicad.server.logging.basicConfig')
def test_setup_logging(self, mock_basic_config):
"""Test logging setup configuration."""
setup_logging()
mock_basic_config.assert_called_once()
args, kwargs = mock_basic_config.call_args
assert kwargs['level'] == logging.INFO
assert 'format' in kwargs
assert '%(asctime)s' in kwargs['format']
assert '%(levelname)s' in kwargs['format']
class TestMain:
"""Test main server entry point."""
@patch('mckicad.server.setup_logging')
@patch('mckicad.server.create_server')
@patch('mckicad.server.logging')
def test_main_successful_run(self, mock_logging, mock_create_server, mock_setup_logging):
"""Test successful main execution."""
mock_server = Mock()
mock_create_server.return_value = mock_server
main()
mock_setup_logging.assert_called_once()
mock_create_server.assert_called_once()
mock_server.run.assert_called_once()
mock_logging.info.assert_any_call("Starting KiCad MCP server...")
mock_logging.info.assert_any_call("Server shutdown complete")
@patch('mckicad.server.setup_logging')
@patch('mckicad.server.create_server')
@patch('mckicad.server.logging')
def test_main_keyboard_interrupt(self, mock_logging, mock_create_server, mock_setup_logging):
"""Test main with keyboard interrupt."""
mock_server = Mock()
mock_server.run.side_effect = KeyboardInterrupt()
mock_create_server.return_value = mock_server
main()
mock_logging.info.assert_any_call("Server interrupted by user")
mock_logging.info.assert_any_call("Server shutdown complete")
@patch('mckicad.server.setup_logging')
@patch('mckicad.server.create_server')
@patch('mckicad.server.logging')
def test_main_exception(self, mock_logging, mock_create_server, mock_setup_logging):
"""Test main with general exception."""
mock_server = Mock()
mock_server.run.side_effect = RuntimeError("Server error")
mock_create_server.return_value = mock_server
main()
mock_logging.error.assert_any_call("Server error: Server error")
mock_logging.info.assert_any_call("Server shutdown complete")
@patch('mckicad.server.setup_logging')
@patch('mckicad.server.create_server')
def test_main_cleanup_always_runs(self, mock_create_server, mock_setup_logging):
"""Test that cleanup always runs even with exceptions."""
mock_server = Mock()
mock_server.run.side_effect = Exception("Test exception")
mock_create_server.return_value = mock_server
with patch('mckicad.server.logging') as mock_logging:
main()
# Verify finally block executed
mock_logging.info.assert_any_call("Server shutdown complete")

Some files were not shown because too many files have changed in this diff Show More