feat: Implement comprehensive MCP capabilities with tool schemas and resources
This major update brings the KiCAD MCP server to full MCP 2025-06-18 spec
compliance with proper tool schemas and resources capability.
## Dependencies Updated
- @modelcontextprotocol/sdk: 1.10.0 → 1.21.0 (critical update)
- dotenv: 16.0.3 → 17.0.0 (latest stable)
- typescript: 5.2.2 → 5.9.3
- zod: 3.22.2 → 3.25.0
- @types/node: 20.5.6 → 20.19.0
- @types/express: 5.0.1 → 5.0.5
- requests (Python): 2.31.0 → 2.32.5
- Added @cfworker/json-schema for MCP SDK compatibility
## Tool Schemas (52 tools)
Created comprehensive JSON Schema definitions for all tools organized by category:
- **Project Tools** (4): create_project, open_project, save_project, get_project_info
- **Board Tools** (9): set_board_size, add_board_outline, add_layer, etc.
- **Component Tools** (10): place_component, move_component, rotate_component, etc.
- **Routing Tools** (8): add_net, route_trace, add_via, route_differential_pair, etc.
- **Library Tools** (4): list_libraries, search_footprints, etc.
- **Design Rule Tools** (4): set_design_rules, run_drc, get_drc_violations, etc.
- **Export Tools** (5): export_gerber, export_pdf, export_svg, export_3d, export_bom
- **Schematic Tools** (6): create_schematic, add_schematic_component, etc.
- **UI Tools** (2): check_kicad_ui, launch_kicad_ui
Each tool now has:
- Detailed descriptions explaining purpose
- Complete JSON Schema for input validation
- Required/optional parameter specifications
- Type constraints and validation rules
## Resources Capability (8 resources)
Implemented MCP resources to expose project state:
- `kicad://project/current/info` - Project metadata
- `kicad://project/current/board` - Board properties
- `kicad://project/current/components` - Component list
- `kicad://project/current/nets` - Electrical nets
- `kicad://project/current/layers` - Layer stack
- `kicad://project/current/design-rules` - Design rules
- `kicad://project/current/drc-report` - DRC violations
- `kicad://board/preview.png` - Board preview image
## Protocol Compliance
- Updated initialize response with proper capabilities declaration
- Added `tools: { listChanged: true }`
- Added `resources: { subscribe: false, listChanged: true }`
- Enhanced serverInfo with title and version
- Added instructions field for user guidance
- Implemented resources/list method
- Implemented resources/read method with proper error handling
- All responses follow MCP 2025-06-18 spec exactly
## Benefits
✅ Claude/LLMs can now understand what each tool does
✅ Automatic parameter validation via JSON Schema
✅ Better error messages for invalid inputs
✅ Access to project state via resources (no need to call tools)
✅ Full MCP protocol compliance
✅ Better developer experience with latest SDK features
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
7
python/resources/__init__.py
Normal file
7
python/resources/__init__.py
Normal file
@@ -0,0 +1,7 @@
|
||||
"""
|
||||
Resource definitions for KiCAD MCP Server
|
||||
"""
|
||||
|
||||
from .resource_definitions import RESOURCE_DEFINITIONS, handle_resource_read
|
||||
|
||||
__all__ = ['RESOURCE_DEFINITIONS', 'handle_resource_read']
|
||||
313
python/resources/resource_definitions.py
Normal file
313
python/resources/resource_definitions.py
Normal file
@@ -0,0 +1,313 @@
|
||||
"""
|
||||
Resource definitions for exposing KiCAD project state via MCP
|
||||
|
||||
Resources follow the MCP 2025-06-18 specification, providing
|
||||
read-only access to project data for LLM context.
|
||||
"""
|
||||
|
||||
import json
|
||||
import base64
|
||||
from typing import Dict, Any, List, Optional
|
||||
import logging
|
||||
|
||||
logger = logging.getLogger('kicad_interface')
|
||||
|
||||
# =============================================================================
|
||||
# RESOURCE DEFINITIONS
|
||||
# =============================================================================
|
||||
|
||||
RESOURCE_DEFINITIONS = [
|
||||
{
|
||||
"uri": "kicad://project/current/info",
|
||||
"name": "Current Project Information",
|
||||
"description": "Metadata about the currently open KiCAD project including paths, name, and status",
|
||||
"mimeType": "application/json"
|
||||
},
|
||||
{
|
||||
"uri": "kicad://project/current/board",
|
||||
"name": "Board Properties",
|
||||
"description": "Comprehensive board information including dimensions, layer count, and design rules",
|
||||
"mimeType": "application/json"
|
||||
},
|
||||
{
|
||||
"uri": "kicad://project/current/components",
|
||||
"name": "Component List",
|
||||
"description": "List of all components on the board with references, footprints, values, and positions",
|
||||
"mimeType": "application/json"
|
||||
},
|
||||
{
|
||||
"uri": "kicad://project/current/nets",
|
||||
"name": "Electrical Nets",
|
||||
"description": "List of all electrical nets and their connections",
|
||||
"mimeType": "application/json"
|
||||
},
|
||||
{
|
||||
"uri": "kicad://project/current/layers",
|
||||
"name": "Layer Stack",
|
||||
"description": "Board layer configuration and properties",
|
||||
"mimeType": "application/json"
|
||||
},
|
||||
{
|
||||
"uri": "kicad://project/current/design-rules",
|
||||
"name": "Design Rules",
|
||||
"description": "Current design rule settings for clearances, track widths, and constraints",
|
||||
"mimeType": "application/json"
|
||||
},
|
||||
{
|
||||
"uri": "kicad://project/current/drc-report",
|
||||
"name": "DRC Violations",
|
||||
"description": "Design Rule Check violations and warnings from last DRC run",
|
||||
"mimeType": "application/json"
|
||||
},
|
||||
{
|
||||
"uri": "kicad://board/preview.png",
|
||||
"name": "Board Preview Image",
|
||||
"description": "2D rendering of the current board state",
|
||||
"mimeType": "image/png"
|
||||
}
|
||||
]
|
||||
|
||||
# =============================================================================
|
||||
# RESOURCE READ HANDLERS
|
||||
# =============================================================================
|
||||
|
||||
def handle_resource_read(uri: str, interface) -> Dict[str, Any]:
|
||||
"""
|
||||
Handle reading a resource by URI
|
||||
|
||||
Args:
|
||||
uri: Resource URI to read
|
||||
interface: KiCADInterface instance with access to board state
|
||||
|
||||
Returns:
|
||||
Dict with resource contents following MCP spec
|
||||
"""
|
||||
logger.info(f"Reading resource: {uri}")
|
||||
|
||||
handlers = {
|
||||
"kicad://project/current/info": _get_project_info,
|
||||
"kicad://project/current/board": _get_board_info,
|
||||
"kicad://project/current/components": _get_components,
|
||||
"kicad://project/current/nets": _get_nets,
|
||||
"kicad://project/current/layers": _get_layers,
|
||||
"kicad://project/current/design-rules": _get_design_rules,
|
||||
"kicad://project/current/drc-report": _get_drc_report,
|
||||
"kicad://board/preview.png": _get_board_preview
|
||||
}
|
||||
|
||||
handler = handlers.get(uri)
|
||||
if handler:
|
||||
try:
|
||||
return handler(interface)
|
||||
except Exception as e:
|
||||
logger.error(f"Error reading resource {uri}: {str(e)}")
|
||||
return {
|
||||
"contents": [{
|
||||
"uri": uri,
|
||||
"mimeType": "text/plain",
|
||||
"text": f"Error: {str(e)}"
|
||||
}]
|
||||
}
|
||||
else:
|
||||
return {
|
||||
"contents": [{
|
||||
"uri": uri,
|
||||
"mimeType": "text/plain",
|
||||
"text": f"Unknown resource: {uri}"
|
||||
}]
|
||||
}
|
||||
|
||||
# =============================================================================
|
||||
# INDIVIDUAL RESOURCE HANDLERS
|
||||
# =============================================================================
|
||||
|
||||
def _get_project_info(interface) -> Dict[str, Any]:
|
||||
"""Get current project information"""
|
||||
result = interface.project_commands.get_project_info({})
|
||||
|
||||
if result.get("success"):
|
||||
return {
|
||||
"contents": [{
|
||||
"uri": "kicad://project/current/info",
|
||||
"mimeType": "application/json",
|
||||
"text": json.dumps(result.get("project", {}), indent=2)
|
||||
}]
|
||||
}
|
||||
else:
|
||||
return {
|
||||
"contents": [{
|
||||
"uri": "kicad://project/current/info",
|
||||
"mimeType": "text/plain",
|
||||
"text": "No project currently open"
|
||||
}]
|
||||
}
|
||||
|
||||
def _get_board_info(interface) -> Dict[str, Any]:
|
||||
"""Get board properties and metadata"""
|
||||
result = interface.board_commands.get_board_info({})
|
||||
|
||||
if result.get("success"):
|
||||
return {
|
||||
"contents": [{
|
||||
"uri": "kicad://project/current/board",
|
||||
"mimeType": "application/json",
|
||||
"text": json.dumps(result.get("board", {}), indent=2)
|
||||
}]
|
||||
}
|
||||
else:
|
||||
return {
|
||||
"contents": [{
|
||||
"uri": "kicad://project/current/board",
|
||||
"mimeType": "text/plain",
|
||||
"text": "No board currently loaded"
|
||||
}]
|
||||
}
|
||||
|
||||
def _get_components(interface) -> Dict[str, Any]:
|
||||
"""Get list of all components"""
|
||||
result = interface.component_commands.get_component_list({})
|
||||
|
||||
if result.get("success"):
|
||||
components = result.get("components", [])
|
||||
return {
|
||||
"contents": [{
|
||||
"uri": "kicad://project/current/components",
|
||||
"mimeType": "application/json",
|
||||
"text": json.dumps({
|
||||
"count": len(components),
|
||||
"components": components
|
||||
}, indent=2)
|
||||
}]
|
||||
}
|
||||
else:
|
||||
return {
|
||||
"contents": [{
|
||||
"uri": "kicad://project/current/components",
|
||||
"mimeType": "application/json",
|
||||
"text": json.dumps({"count": 0, "components": []}, indent=2)
|
||||
}]
|
||||
}
|
||||
|
||||
def _get_nets(interface) -> Dict[str, Any]:
|
||||
"""Get list of electrical nets"""
|
||||
result = interface.routing_commands.get_nets_list({})
|
||||
|
||||
if result.get("success"):
|
||||
nets = result.get("nets", [])
|
||||
return {
|
||||
"contents": [{
|
||||
"uri": "kicad://project/current/nets",
|
||||
"mimeType": "application/json",
|
||||
"text": json.dumps({
|
||||
"count": len(nets),
|
||||
"nets": nets
|
||||
}, indent=2)
|
||||
}]
|
||||
}
|
||||
else:
|
||||
return {
|
||||
"contents": [{
|
||||
"uri": "kicad://project/current/nets",
|
||||
"mimeType": "application/json",
|
||||
"text": json.dumps({"count": 0, "nets": []}, indent=2)
|
||||
}]
|
||||
}
|
||||
|
||||
def _get_layers(interface) -> Dict[str, Any]:
|
||||
"""Get layer stack information"""
|
||||
result = interface.board_commands.get_layer_list({})
|
||||
|
||||
if result.get("success"):
|
||||
layers = result.get("layers", [])
|
||||
return {
|
||||
"contents": [{
|
||||
"uri": "kicad://project/current/layers",
|
||||
"mimeType": "application/json",
|
||||
"text": json.dumps({
|
||||
"count": len(layers),
|
||||
"layers": layers
|
||||
}, indent=2)
|
||||
}]
|
||||
}
|
||||
else:
|
||||
return {
|
||||
"contents": [{
|
||||
"uri": "kicad://project/current/layers",
|
||||
"mimeType": "application/json",
|
||||
"text": json.dumps({"count": 0, "layers": []}, indent=2)
|
||||
}]
|
||||
}
|
||||
|
||||
def _get_design_rules(interface) -> Dict[str, Any]:
|
||||
"""Get design rule settings"""
|
||||
result = interface.design_rule_commands.get_design_rules({})
|
||||
|
||||
if result.get("success"):
|
||||
return {
|
||||
"contents": [{
|
||||
"uri": "kicad://project/current/design-rules",
|
||||
"mimeType": "application/json",
|
||||
"text": json.dumps(result.get("rules", {}), indent=2)
|
||||
}]
|
||||
}
|
||||
else:
|
||||
return {
|
||||
"contents": [{
|
||||
"uri": "kicad://project/current/design-rules",
|
||||
"mimeType": "text/plain",
|
||||
"text": "Design rules not available"
|
||||
}]
|
||||
}
|
||||
|
||||
def _get_drc_report(interface) -> Dict[str, Any]:
|
||||
"""Get DRC violations"""
|
||||
result = interface.design_rule_commands.get_drc_violations({})
|
||||
|
||||
if result.get("success"):
|
||||
violations = result.get("violations", [])
|
||||
return {
|
||||
"contents": [{
|
||||
"uri": "kicad://project/current/drc-report",
|
||||
"mimeType": "application/json",
|
||||
"text": json.dumps({
|
||||
"count": len(violations),
|
||||
"violations": violations
|
||||
}, indent=2)
|
||||
}]
|
||||
}
|
||||
else:
|
||||
return {
|
||||
"contents": [{
|
||||
"uri": "kicad://project/current/drc-report",
|
||||
"mimeType": "application/json",
|
||||
"text": json.dumps({
|
||||
"count": 0,
|
||||
"violations": [],
|
||||
"message": "Run DRC first to get violations"
|
||||
}, indent=2)
|
||||
}]
|
||||
}
|
||||
|
||||
def _get_board_preview(interface) -> Dict[str, Any]:
|
||||
"""Get board preview as PNG image"""
|
||||
result = interface.board_commands.get_board_2d_view({"width": 800, "height": 600})
|
||||
|
||||
if result.get("success") and "imageData" in result:
|
||||
# Image data should already be base64 encoded
|
||||
image_data = result.get("imageData", "")
|
||||
return {
|
||||
"contents": [{
|
||||
"uri": "kicad://board/preview.png",
|
||||
"mimeType": "image/png",
|
||||
"blob": image_data # Base64 encoded PNG
|
||||
}]
|
||||
}
|
||||
else:
|
||||
# Return a placeholder message
|
||||
return {
|
||||
"contents": [{
|
||||
"uri": "kicad://board/preview.png",
|
||||
"mimeType": "text/plain",
|
||||
"text": "Board preview not available"
|
||||
}]
|
||||
}
|
||||
Reference in New Issue
Block a user