spex-mcp
Version:
MCP server for Figma SpeX plugin and Cursor AI integration
196 lines (142 loc) ⢠5.3 kB
Markdown
# Module Separation Summary
This document summarizes how the SpeX MCP Server functions have been divided into two distinct types: Figma SpeX Plugin functions and MCP Cursor AI tools.
## š Refactoring Overview
The monolithic `src/index.js` has been refactored into a modular architecture:
### Before (Monolithic)
```
src/index.js (212 lines)
āāā Server setup
āāā WebSocket handling
āāā MCP tool definitions
āāā Tool handlers
āāā Figma SpeX message handling
āāā Connection management
```
### After (Modular)
```
src/
āāā index.js (95 lines) - Main orchestration
āāā figma-functions.js (120+ lines) - Figma SpeX plugin management
āāā mcp-tools.js (70+ lines) - MCP tools for Cursor AI
```
## š Module Breakdown
### 1. `src/figma-functions.js` - Figma SpeX Plugin Functions
**Purpose**: Handle all Figma SpeX plugin communication via WebSocket
**Key Features**:
- ā
WebSocket server management (port 8080)
- ā
Connection lifecycle management
- ā
Message routing and handling
- ā
SpeX plugin communication protocols
- ā
Broadcasting to multiple SpeX plugins
**Functions**:
- `setupWebSocketServer()` - Initialize WebSocket server
- `handleFigmaMessage(ws, message)` - Route incoming messages
- `pullSpecsFromFigma()` - Request design specifications
- `broadcastToFigmaPlugins(message)` - Send to all SpeX plugins
- `shutdown()` - Graceful shutdown
**Message Types**:
- `hello-world` - Connectivity test
- `specs-data` - Design specifications
### 2. `src/mcp-tools.js` - MCP Cursor AI Tools
**Purpose**: Provide tools for Cursor AI via Model Context Protocol
**Key Features**:
- ā
MCP tool definitions and schemas
- ā
Tool execution handlers
- ā
Error handling and responses
**Available Tools**:
- `hello-world` - Test connectivity
- `pull-specs` - Get design specifications
### 3. `src/index.js` - Main Orchestration
**Purpose**: Coordinate both Figma SpeX and MCP functionalities
**Key Features**:
- ā
Initialize both managers
- ā
Setup MCP request handlers
- ā
Lifecycle management
- ā
Graceful shutdown
- ā
Error handling
## š Inter-module Communication
```
Cursor AI ā MCPToolsManager ā FigmaPluginManager ā Figma SpeX Plugins
```
- **MCPToolsManager** receives requests from Cursor AI
- **MCPToolsManager** calls **FigmaPluginManager** methods
- **FigmaPluginManager** communicates with Figma SpeX plugins via WebSocket
- Results flow back through the chain
## š Benefits of Separation
### 1. **Maintainability**
- ā
Clear separation of concerns
- ā
Easier to debug specific functionality
- ā
Independent testing of modules
### 2. **Extensibility**
- ā
Add new Figma SpeX functions without touching MCP code
- ā
Add new MCP tools without touching WebSocket code
- ā
Simple and focused architecture
### 3. **Reusability**
- ā
FigmaPluginManager can be used in other SpeX projects
- ā
MCPToolsManager patterns can be reused
- ā
Clean separation allows for easy integration
### 4. **Testing**
- ā
Unit test each module independently
- ā
Mock dependencies easily
- ā
Focused integration tests
## š Usage Examples
### Adding a New Figma SpeX Function
```javascript
// In figma-functions.js
handleFigmaMessage(ws, message) {
switch (message.name || message.type) {
// ... existing cases
case 'new-spex-function':
this.handleNewSpeXFunction(message.data);
break;
}
}
handleNewSpeXFunction(data) {
// Implementation
}
```
### Adding a New MCP Tool
```javascript
// In mcp-tools.js
getToolsList() {
return [
// ... existing tools
{
name: "new-mcp-tool",
description: "Description of new tool",
inputSchema: { /* schema */ }
}
];
}
async handleToolCall(name, args) {
switch (name) {
// ... existing cases
case "new-mcp-tool":
return this.handleNewMCPTool(args);
}
}
```
## š Future Enhancements
With this simplified modular structure, we can easily add:
1. **Database Integration** - Store design specs persistently
2. **Authentication** - Secure WebSocket connections
3. **Plugin Marketplace** - Support multiple SpeX plugin types
4. **Real-time Sync** - Live updates between Figma SpeX and code
5. **Version Control** - Track design changes over time
6. **Team Collaboration** - Multi-user design workflows
7. **Enhanced Tools** - More sophisticated MCP tools
8. **Monitoring** - Health checks and metrics
## šÆ Current Architecture Benefits
The simplified architecture provides:
1. **Focus** - Core functionality without unnecessary complexity
2. **Reliability** - Fewer moving parts, fewer potential issues
3. **Performance** - Lightweight and efficient
4. **Clarity** - Easy to understand and maintain
5. **Flexibility** - Room to grow in multiple directions
## š§ Next Steps
1. **Testing** - Add comprehensive unit tests for each module
2. **Documentation** - Expand API documentation with examples
3. **Examples** - Create more Figma SpeX plugin integration examples
4. **Performance** - Optimize WebSocket handling and message processing
5. **Security** - Add authentication and input validation
6. **Monitoring** - Add logging, metrics, and health checks