Electron MCP 服务器是一款强大的模型上下文协议(MCP)服务器,为 Electron 应用程序提供全面的自动化、调试和可观测性功能。通过集成 Chrome DevTools 协议,借助人工智能驱动的自动化,为你的 Electron 开发工作流程注入强大动力。
点击下面的按钮,在 VS Code 中使用 NPX 进行安装:
在你的 VS Code MCP 设置中添加以下内容:
{
"mcp": {
"servers": {
"electron": {
"command": "npx",
"args": ["-y", "electron-mcp-server"]
}
}
}
}
在 ~/Library/Application Support/Claude/claude_desktop_config.json 中添加以下内容:
{
"mcpServers": {
"electron": {
"command": "npx",
"args": ["-y", "electron-mcp-server"]
}
}
}
npm install -g electron-mcp-server
要让 MCP 服务器与你的 Electron 应用配合使用,需要启用远程调试。在 Electron 应用的主进程中添加以下代码:
const { app } = require('electron');
const isDev = process.env.NODE_ENV === 'development' || process.argv.includes('--dev');
// 在开发模式下启用远程调试
if (isDev) {
app.commandLine.appendSwitch('remote-debugging-port', '9222');
}
也可以使用以下替代方法:
# 启动应用并启用调试
electron . --remote-debugging-port=9222
# 或者通过 npm 脚本
npm run dev -- --remote-debugging-port=9222
注意:MCP 服务器会自动扫描 9222 - 9225 端口,以检测启用了远程调试的运行中的 Electron 应用程序。
点击下面的按钮,在 VS Code 中使用 NPX 进行安装:
在你的 VS Code MCP 设置中添加以下内容:
{
"mcp": {
"servers": {
"electron": {
"command": "npx",
"args": ["-y", "electron-mcp-server"]
}
}
}
}
在 ~/Library/Application Support/Claude/claude_desktop_config.json 中添加以下内容:
{
"mcpServers": {
"electron": {
"command": "npx",
"args": ["-y", "electron-mcp-server"]
}
}
}
npm install -g electron-mcp-server
// 1. 首先,了解页面结构
await send_command_to_electron({
command: 'get_page_structure',
});
// 2. 通过文本点击按钮(比选择器更可靠)
await send_command_to_electron({
command: 'click_by_text',
args: {
text: 'Login', // 查找文本、aria-label 或标题中包含 "Login" 的按钮
},
});
// 3. 通过标签或占位符文本填充输入框
await send_command_to_electron({
command: 'fill_input',
args: {
text: 'username', // 查找标签为 "Username" 或占位符为 "Enter username" 的输入框
value: 'john.doe@example.com',
},
});
await send_command_to_electron({
command: 'fill_input',
args: {
text: 'password',
value: 'secretpassword',
},
});
// 4. 通过可见文本选择下拉选项
await send_command_to_electron({
command: 'select_option',
args: {
text: 'country', // 查找标签包含 "country" 的选择框
value: 'United States', // 选择具有此文本的选项
},
});
// 5. 拍摄截图以验证结果
await take_screenshot();
// 查找所有具有详细信息的交互式元素
await send_command_to_electron({
command: 'find_elements',
});
// 返回每个可点击元素和输入框的详细信息
// {
// "type": "clickable",
// "text": "Submit Form",
// "id": "submit-btn",
// "className": "btn btn-primary",
// "ariaLabel": "Submit the registration form",
// "position": { "x": 100, "y": 200, "width": 120, "height": 40 },
// "visible": true
// }
// 在开发模式下启动应用
await launch_electron_app({
appPath: '/path/to/app',
devMode: true,
});
// 拍摄截图
await take_screenshot();
// 以编程方式点击按钮
await send_command_to_electron({
command: 'eval',
args: {
code: "document.querySelector('#submit-btn').click()",
},
});
// 验证结果
await send_command_to_electron({
command: 'get_title',
});
// 获取窗口信息
const windowInfo = await get_electron_window_info();
// 提取应用程序数据
await send_command_to_electron({
command: 'eval',
args: {
code: 'JSON.stringify(window.appState, null, 2)',
},
});
// 监控日志
await read_electron_logs({
logType: 'all',
lines: 100,
});
// 获取系统信息
await send_command_to_electron({
command: 'eval',
args: {
code: '({memory: performance.memory, timing: performance.timing})',
},
});
// 定期拍摄截图进行视觉回归测试
await take_screenshot({
outputPath: '/tests/screenshots/current.png',
});
避免使用原始的 JavaScript eval,而是使用以下安全命令:
// ✅ 安全的按钮点击
{
"command": "click_by_text",
"args": { "text": "Create New Encyclopedia" }
}
// ✅ 安全的元素选择
{
"command": "click_by_selector",
"args": { "selector": "button[title='Create']" }
}
// ✅ 安全的键盘快捷键
{
"command": "send_keyboard_shortcut",
"args": { "text": "Ctrl+N" }
}
// ✅ 安全的导航
{
"command": "navigate_to_hash",
"args": { "text": "create" }
}
详细的安全文档请参阅 SECURITY_CONFIG.md。
使用此 MCP 服务器时最常见的错误是 send_command_to_electron 工具的参数结构不正确。
{
"command": "click_by_selector",
"args": "button.submit-btn" // ❌ 原始字符串 - 错误!
}
{
"command": "click_by_selector",
"args": {
"selector": "button.submit-btn" // ✅ 包含选择器属性的对象
}
}
| 命令 | 必需参数 | 示例 |
|---|---|---|
click_by_selector |
{"selector": "css-selector"} |
{"selector": "button.primary"} |
click_by_text |
{"text": "button text"} |
{"text": "Submit"} |
fill_input |
{"value": "text", "selector": "..."} 或 {"value": "text", "placeholder": "..."} |
{"placeholder": "Enter name", "value": "John"} |
send_keyboard_shortcut |
{"text": "key combination"} |
{"text": "Ctrl+N"} |
eval |
{"code": "javascript"} |
{"code": "document.title"} |
get_title, get_url, get_body_text |
无需参数 | {} 或省略参数 |
get_page_structure 或 debug_elements 开始。// 步骤 1: 了解页面结构
{
"command": "get_page_structure"
}
// 步骤 2: 通过文本点击按钮(最可靠)
{
"command": "click_by_text",
"args": {
"text": "Create New Encyclopedia"
}
}
// 步骤 3: 填充表单字段
{
"command": "fill_input",
"args": {
"placeholder": "Enter encyclopedia name",
"value": "AI and Machine Learning"
}
}
// 步骤 4: 使用选择器提交
{
"command": "click_by_selector",
"args": {
"selector": "button[type='submit']"
}
}
| 错误 | 原因 | 解决方案 |
|---|---|---|
| "The provided selector is empty" | 传递字符串而不是对象 | 使用 {"selector": "..."} |
| "Element not found" | 选择器错误 | 先使用 get_page_structure |
| "Command blocked" | 安全限制 | 检查安全级别设置 |
| "Click prevented - too soon" | 连续快速点击 | 等待后重试 |
ELECTRON_RUN_AS_NODE 和其他环境变量。src/
├── handlers.ts # MCP 工具处理程序
├── index.ts # 服务器入口点
├── tools.ts # 工具定义
├── screenshot.ts # 截图功能
├── utils/
│ ├── process.ts # 进程管理与 DevTools 协议
│ ├── logs.ts # 日志管理
│ └── project.ts # 项目脚手架
└── schemas/ # 用于验证的 JSON 模式
本项目采用 MIT 许可证,详情请参阅 LICENSE 文件。
我们欢迎贡献!请参阅我们的 贡献指南 了解详细信息。
git checkout -b feature/awesome-feature)。git commit -m 'Add awesome feature')。git push origin feature/awesome-feature)。如果这个项目对你有帮助,考虑请我喝杯咖啡! ☕
你的支持有助于我维护和改进这个项目。感谢! 🙏
准备好借助人工智能驱动的自动化为你的 Electron 开发加速了吗? 安装 MCP 服务器,立即开始构建更智能的工作流程! 🚀