Successfully implemented a complete Model Context Protocol (MCP) server for debugging and automating native Electron applications on macOS.
-
MCP Server (
src/server.ts,src/index.ts)- Built using
@modelcontextprotocol/sdkv1.21.0 - Stdio transport for communication
- 24 registered tools across 4 categories
- Built using
-
CDP Client (
src/lib/cdp-client.ts)- Chrome DevTools Protocol client using
chrome-remote-interface - Manages connections to Electron apps
- Supports DOM inspection, JavaScript execution, screenshots
- Chrome DevTools Protocol client using
-
Accessibility Manager (
src/lib/accessibility.ts)- macOS UI automation using
robotjs - Mouse control (move, click, drag)
- Keyboard control (type, press keys)
- Screenshot capture
- macOS UI automation using
-
Permissions Manager (
src/lib/permissions.ts)- Checks and manages macOS system permissions
- Uses
node-mac-permissions - Provides user-friendly setup instructions
list_electron_targets- Discover Electron windowsconnect_to_electron_target- Establish CDP connectionget_dom_tree- Retrieve DOM structurequery_selector- Find elements by CSS selectorquery_selector_all- Find all matching elementsexecute_javascript- Run code in Electron contexttake_electron_screenshot- Capture Electron window
get_mouse_position- Get cursor positionmove_mouse- Move cursorclick- Click (left/right/middle)double_click- Double clickdrag- Drag and droptype_text- Type textpress_key- Press keys with modifierstake_screenshot- Capture screen/regionget_screen_size- Get display dimensions
trigger_hotkey- Custom keyboard shortcutstrigger_common_macos_hotkey- System hotkeys (Spotlight, Mission Control, etc.)simulate_app_shortcut- App-specific shortcutssend_key_sequence- Complex key sequences
check_permission- Check single permissioncheck_all_permissions- Check all permissionsget_permission_instructions- Get setup guiderequest_permission- Request permission
Created a test application (examples/test-electron-app/) with:
- Interactive UI elements (buttons, inputs)
- Event logging
- Styled interface
- Pre-configured for debugging (
--inspect=9222)
Core:
@modelcontextprotocol/sdk^1.21.0 - MCP protocol implementationzod^3.23.8 - Schema validation
Electron Debugging:
chrome-remote-interface^0.33.2 - CDP client
macOS Automation:
robotjs^0.6.0 - Native UI automationnode-mac-permissions^2.3.0 - Permission management
Development:
typescript^5.7.2tsx^4.19.2eslint^8.57.1prettier^3.4.2
electron-native-mcp/
├── src/
│ ├── index.ts # Entry point (stdio transport)
│ ├── server.ts # MCP server configuration
│ ├── types/
│ │ └── index.ts # TypeScript type definitions
│ ├── lib/
│ │ ├── cdp-client.ts # Chrome DevTools Protocol client
│ │ ├── accessibility.ts # macOS UI automation
│ │ └── permissions.ts # Permission management
│ └── tools/
│ ├── dom/ # DOM inspection tools
│ ├── ui/ # UI automation tools
│ ├── hotkey/ # Hotkey tools
│ └── permissions/ # Permission tools
├── examples/
│ └── test-electron-app/ # Test Electron application
├── dist/ # Compiled JavaScript
├── package.json
├── tsconfig.json
├── README.md
├── USAGE.md
└── IMPLEMENTATION.md
- Connect to any Electron app running with
--inspectflag - Query DOM using CSS selectors
- Execute arbitrary JavaScript
- Take screenshots of web content
- Control mouse and keyboard
- Works with any macOS application
- Pixel-perfect positioning
- Support for all mouse buttons and keyboard modifiers
- Trigger system-wide keyboard shortcuts
- Pre-configured common macOS hotkeys
- Custom key combinations
- Complex key sequences
- Check permission status
- Request permissions programmatically
- User-friendly setup instructions
- Supports all required macOS permissions
Problem: nut.js is no longer publicly available on npm
Solution: Switched to robotjs for UI automation
Problem: Tool registration API differs from documentation
Solution: Used registerTool() method with proper schema structure
Problem: ToolResult type didn't match MCP SDK expectations
Solution: Added index signature [x: string]: unknown to ToolResult interface
Problem: macOS requires multiple system permissions
Solution: Created comprehensive permission manager with instructions
-
Build the server:
npm install npm run build
-
Run the test Electron app:
cd examples/test-electron-app npm install npm start -
Test DOM inspection:
- List targets
- Connect to test app
- Query selectors
- Execute JavaScript
-
Test UI automation:
- Check permissions
- Move mouse
- Click buttons
- Type text
-
Test hotkeys:
- Trigger Spotlight
- Trigger custom shortcuts
-
Enhanced Image Recognition
- Add OCR capabilities
- Template matching for UI elements
-
Recording & Playback
- Record user actions
- Replay automation sequences
-
Better Error Handling
- More descriptive error messages
- Automatic retry logic
-
Performance Optimization
- Connection pooling for CDP
- Caching of DOM queries
-
Additional Platforms
- Windows support
- Linux support
-
Advanced CDP Features
- Network interception
- Console log capture
- Performance profiling
- macOS Only: Currently only supports macOS due to permission system and robotjs
- RobotJS Dependencies: Requires native compilation (Xcode Command Line Tools)
- CDP Port: Assumes default port 9222 (configurable)
- Screenshot Format: Limited to PNG/JPEG for Electron, PNG for native
- Permission Prompts: Some permissions require manual user action
Successfully delivered a fully functional MCP server that meets all requirements:
✅ See native Electron app DOM (via CDP)
✅ Click buttons in native Electron app (via robotjs)
✅ Trigger global hotkeys on macOS (via robotjs)
The server provides 24 tools across 4 categories, comprehensive documentation, and a test application for validation.
- Core implementation: 13 TypeScript files
- Configuration: 5 files (package.json, tsconfig.json, eslint, prettier, gitignore)
- Documentation: 3 files (README.md, USAGE.md, IMPLEMENTATION.md)
- Example app: 4 files (package.json, main.js, index.html, README.md)
Total: 25 files, ~2500 lines of code