From b1cb48ecf77d3876169e76f73a05f5172e474d39 Mon Sep 17 00:00:00 2001 From: Lama Date: Fri, 21 Mar 2025 13:08:15 -0400 Subject: [PATCH] fix random import, etc issues --- kicad_mcp/prompts/pattern_prompts.py | 145 +++++++++++++++++++++++++++ kicad_mcp/tools/netlist_tools.py | 76 +------------- kicad_mcp/utils/component_utils.py | 2 +- 3 files changed, 151 insertions(+), 72 deletions(-) create mode 100644 kicad_mcp/prompts/pattern_prompts.py diff --git a/kicad_mcp/prompts/pattern_prompts.py b/kicad_mcp/prompts/pattern_prompts.py new file mode 100644 index 0000000..5a0548a --- /dev/null +++ b/kicad_mcp/prompts/pattern_prompts.py @@ -0,0 +1,145 @@ +""" +Prompt templates for circuit pattern analysis in KiCad. +""" +from mcp.server.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 diff --git a/kicad_mcp/tools/netlist_tools.py b/kicad_mcp/tools/netlist_tools.py index 1e2c8d0..fb33fee 100644 --- a/kicad_mcp/tools/netlist_tools.py +++ b/kicad_mcp/tools/netlist_tools.py @@ -2,7 +2,7 @@ Netlist extraction and analysis tools for KiCad schematics. """ import os -from typing import Dict, List, Any, Optional +from typing import Dict, Any from mcp.server.fastmcp import FastMCP, Context from kicad_mcp.utils.file_utils import get_project_files @@ -56,75 +56,6 @@ def register_netlist_tools(mcp: FastMCP) -> None: ctx.info(f"Error extracting netlist: {netlist_data['error']}") return {"success": False, "error": netlist_data['error']} - await ctx.report_progress(50, 100) - - # Check if the component exists - components = netlist_data.get("components", {}) - if component_ref not in components: - logger.error(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()) - } - - component_info = components[component_ref] - ctx.info(f"Found component: {component_ref} ({component_info.get('lib_id', 'unknown type')})") - - # Find all nets connected to this component - await ctx.report_progress(70, 100) - ctx.info("Analyzing component connections...") - - # Build connection information - connections = [] - connected_components = set() - - nets = netlist_data.get("nets", {}) - for net_name, pins in nets.items(): - # Check if any pin belongs to our component - component_pins = [pin for pin in pins if pin.get('component') == component_ref] - - if component_pins: - # This net connects to our component - net_info = { - "net_name": net_name, - "pins": component_pins, - "connected_to": [] - } - - # Find other components connected to this net - for pin in pins: - other_component = pin.get('component') - if other_component and other_component != component_ref: - connected_components.add(other_component) - net_info["connected_to"].append({ - "component": other_component, - "pin": pin.get('pin', 'unknown') - }) - - connections.append(net_info) - - await ctx.report_progress(90, 100) - - # Build result - result = { - "success": True, - "component_ref": component_ref, - "component_info": component_info, - "connections": connections, - "connected_component_count": len(connected_components), - "connected_components": list(connected_components) - } - - # Complete progress - await ctx.report_progress(100, 100) - ctx.info(f"Found {len(connections)} connections to component {component_ref}") - - return resultf"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") @@ -368,4 +299,7 @@ def register_netlist_tools(mcp: FastMCP) -> None: netlist_data = extract_netlist(schematic_path) if "error" in netlist_data: - logger.error( + logger.error("Failed to extract netlist") + + except Exception as e: + raise e diff --git a/kicad_mcp/utils/component_utils.py b/kicad_mcp/utils/component_utils.py index 0ed134b..9702688 100644 --- a/kicad_mcp/utils/component_utils.py +++ b/kicad_mcp/utils/component_utils.py @@ -2,7 +2,7 @@ Utility functions for working with KiCad component values and properties. """ import re -from typing import Optional, Tuple, Union, Dict +from typing import Any, Optional, Tuple, Union, Dict def extract_voltage_from_regulator(value: str) -> str: """Extract output voltage from a voltage regulator part number or description.