A Model Context Protocol (MCP) server that provides Playwright automation capabilities specifically for Electron applications.
This project extends the functionality of the official Playwright MCP server to support Electron applications. While the official server focuses on web browsers, this implementation provides dedicated tools for automating Electron desktop applications.
This is a TypeScript project using Yarn as the package manager.
- Official Playwright MCP: Does not currently support Electron automation
- Playwright Electron Support: Playwright has experimental support for Electron automation (documentation)
- This Project: Bridges the gap by providing MCP tools specifically designed for Electron application automation
One of the key challenges in implementing this MCP server is maintaining Electron application instance references between tool calls. Unlike typical MCP tools that are stateless, Electron automation requires:
- Keeping the
ElectronApplicationinstance alive across multiple tool calls - Maintaining references to opened windows
- Preserving application state and context
- Handling cleanup when the session ends
Potential Solutions:
- Instance caching with unique identifiers
- Session-based state management
- Automatic cleanup mechanisms
- Connection pooling for multiple Electron apps
This architectural challenge needs to be carefully designed to ensure robust automation workflows while maintaining the MCP protocol's principles.
The official Playwright MCP server provides a proven reference for state management that we can adapt for Electron:
-
Context Class - Central state manager that:
- Uses lazy initialization for browser contexts (
_browserContextPromise) - Maintains persistent browser context once created
- Manages array of Tab objects and current active tab
- Uses lazy initialization for browser contexts (
-
Connection-Level Isolation - Each MCP connection gets its own Context instance with isolated browser state
-
Persistent Browser Context - Browser context stays alive for entire session until explicitly closed, supporting both isolated and persistent modes
-
Tab State Management - Direct references to Playwright Page objects that persist throughout tab lifecycle
-
Modal State Tracking - Tracks blocking states (dialogs, file choosers) per-tab to prevent incompatible tool execution
The official server uses lazy initialization with persistent references - browser contexts are created on-demand then cached, and direct Playwright object references (Page, BrowserContext) are maintained across tool invocations.
For our Electron MCP server, we need similar patterns but adapted for:
ElectronApplicationinstances instead of browser contexts- Electron app lifecycle management instead of browser lifecycle
- Multiple Electron windows instead of browser tabs
- Electron-specific modal states and dialogs
This server reproduces all the tools from the official Playwright MCP server but adapted for Electron applications:
-
Application Management
- Launch Electron applications
- Close Electron applications
- Application context evaluation
-
Window Management
- Access first window
- Multiple window handling
- Window navigation and control
-
UI Interaction
- Click elements
- Type text
- Key presses
- Mouse interactions (hover, drag)
- File uploads
- Dialog handling
-
Content Analysis
- Take screenshots
- Extract text content
- Element inspection
- Wait for conditions
-
Advanced Features
- HAR recording
- Video recording
- Trace collection
- Console monitoring
Based on Playwright's experimental Electron support:
- v12.2.0+
- v13.4.0+
- v14+
yarn installThe server provides MCP tools that can be used by any MCP-compatible client to automate Electron applications.
// Launch an Electron app and interact with it
const electronApp = await electron.launch({ args: ['main.js'] });
const window = await electronApp.firstWindow();
await window.click('text=Click me');
await electronApp.close();electron_launch- Launch an Electron applicationelectron_close- Close the Electron applicationelectron_evaluate- Evaluate code in the Electron context
electron_first_window- Get the first windowelectron_new_window- Handle new windowselectron_window_list- List all windows
electron_click- Click on elementselectron_type- Type textelectron_press_key- Press keyboard keyselectron_hover- Hover over elementselectron_drag- Drag and dropelectron_file_upload- Upload fileselectron_handle_dialog- Handle dialogs
electron_screenshot- Take screenshotselectron_get_text- Extract text contentelectron_wait_for- Wait for conditionselectron_console_log- Monitor console output
electron_start_recording- Start video recordingelectron_stop_recording- Stop video recordingelectron_start_trace- Start trace recordingelectron_stop_trace- Stop trace recording
-
Project Foundation
- Set up TypeScript project with Yarn
- Configure build system and development scripts
- Set up basic MCP server structure
-
State Management Layer
- Implement
ElectronContextclass (analogous to official server's Context) - Create
ElectronWindowclass (analogous to Tab) - Implement lazy initialization for ElectronApplication instances
- Add connection-level state isolation
- Implement
-
ElectronApplication Management
- Implement
electron_launchtool with configurable launch options - Add persistent application instance caching
- Implement
electron_closetool with proper cleanup - Handle application restart scenarios
- Implement
-
Window Management
- Implement window discovery and tracking
- Add support for multiple window instances
- Create window switching and focus management
- Handle window lifecycle events (open/close)
-
UI Interaction Tools
electron_click- Element clicking with selector supportelectron_type- Text input with focus managementelectron_press_key- Keyboard interactionelectron_hover- Mouse hover eventselectron_drag- Drag and drop operations
-
Content Analysis Tools
electron_screenshot- Window/element screenshotselectron_get_text- Text content extractionelectron_wait_for- Condition waiting (elements, timeouts)electron_evaluate- JavaScript execution in renderer/main process
-
Dialog and File Handling
electron_handle_dialog- Modal dialog managementelectron_file_upload- File selection dialogs- Modal state tracking system
-
Recording and Debugging
electron_console_log- Console output monitoringelectron_start_trace/electron_stop_trace- Trace recordingelectron_start_recording/electron_stop_recording- Video recording
-
Test Suite
- Unit tests for state management
- Integration tests with sample Electron apps
- Error handling and edge case testing
-
Documentation and Examples
- API documentation for all tools
- Usage examples and best practices
- Migration guide from browser automation
// Core classes structure
class ElectronContext {
private _electronAppPromise?: Promise<ElectronApplication>;
private _windows: ElectronWindow[] = [];
private _currentWindow?: ElectronWindow;
private _modalStates = new Set<string>();
}
class ElectronWindow {
private _page: Page;
private _consoleMessages: any[] = [];
private _requests: any[] = [];
}- Replace
BrowserContextwithElectronApplication - Replace
TabwithElectronWindow - Adapt navigation tools for Electron window management
- Handle Electron-specific events and lifecycle
const electronClickTool = {
name: 'electron_click',
handle: async (context: ElectronContext, params) => {
const window = await context.ensureWindow();
await window.click(params.selector);
}
};- Node.js 18+
- Yarn package manager
- TypeScript
yarn install
yarn build
yarn start├── README.md
├── package.json
├── yarn.lock
├── tsconfig.json
├── src/
│ ├── index.ts # Main MCP server
│ ├── electron-tools.ts # Electron automation tools
│ ├── state-manager.ts # Instance state management
│ └── types.ts # Type definitions
├── examples/
│ └── sample-usage.js # Usage examples
└── tests/
└── electron-test.ts # Test cases
This project uses the official Playwright MCP server as a reference implementation, adapting all browser-focused tools to work with Electron applications instead.
The key differences:
- State Management: Electron instances must persist between tool calls
- Application Lifecycle: Managing Electron app launch/close cycles
- TypeScript Implementation: Full type safety for Electron APIs
- Yarn Workspace: Modern package management
- Ensure that
nodeCliInspect(FuseV1Options.EnableNodeCliInspectArguments) fuse is not set tofalsein your Electron app - Some features may require specific Electron versions
- Experimental support means API may change
- Fork the repository
- Create a feature branch
- Make your changes
- Add tests
- Submit a pull request
MIT License