mac mcp服务器
](https://www.npmjs.com/package/mac-mcp-server)  ](https://nodejs.org/) 
macOS AppleScript MCP(模型上下文协议)服务器,使Claude Code和其他AI助手能够通过AppleScript和JXA(JavaScript for Automation)自动化macOS。
概述
此MCP服务器为全面的macOS自动化提供了44个工具,使AI助手能够:
- 检索系统信息(硬件、电池、显示器)
- 管理应用程序(启动、退出、激活、列出正在运行的应用程序)
- 控制窗口(移动、调整大小、聚焦、最小化)
- 模拟键盘和鼠标输入
- 通过辅助功能API与UI元素交互
- 捕获屏幕截图(针对API兼容性进行了自动优化)
- 访问剪贴板和通知
- 控制音频设置
- 导航菜单和状态栏项目
服务器通过stdio进行通信,以实现无缝的克劳德代码集成,并使用AppleScript/JXA进行系统自动化。
快速开始
先决条件
- macOS 10.15(Catalina)或更高版本
- Node.js 20.11.0或更高版本
从npm安装
npm install -g mac-mcp-server配置Claude代码
将以下内容添加到您的Claude Code MCP配置中(~/.claude.json 或克劳德桌面设置):
{
"mcpServers": {
"mac-mcp-server": {
"command": "npx",
"args": ["-y", "mac-mcp-server"]
}
}
}重新启动Claude Code以加载MCP服务器。
授予macOS权限
首次使用时,macOS会提示您授予所需的权限。请参阅 权限指南 详见下文。
工具参考
系统信息
| 工具 | 说明 |
|---|---|
get_system_info | 检索macOS版本、硬件型号、处理器和内存 |
get_battery_status | 获取电池百分比和充电状态(MacBook) |
get_display_info | 列出连接的显示器及其分辨率信息 |
音频控制
| 工具 | 说明 |
|---|---|
get_volume | 获取当前系统卷(0-100) |
set_volume | 将系统卷设置为指定的百分比 |
get_mute_status | 检查系统音频是否已静音 |
set_mute | 将系统音频静音或取消静音 |
剪贴板和通知
| 工具 | 说明 |
|---|---|
get_clipboard | 读取当前剪贴板内容(文本、图像或文件) |
set_clipboard | 将剪贴板设置为指定文本 |
send_notification | 显示带有可选字幕和声音的macOS通知 |
应用管理
| 工具 | 说明 |
|---|---|
list_running_apps | 列出所有正在运行的GUI应用程序及其捆绑ID和PID |
launch_app | 按名称启动应用程序 |
quit_app | 优雅地退出应用程序 |
activate_app | 将应用程序置于前台 |
Finder集成
| 工具 | 说明 |
|---|---|
reveal_in_finder | 打开Finder并选择指定的文件或文件夹 |
get_selected_files | 获取Finder中当前选定文件的路径 |
get_finder_window_path | 获取最前面的Finder窗口的路径 |
窗口管理
| 工具 | 说明 |
|---|---|
list_windows | 列出所有可见窗口的位置和大小 |
focus_window | 将特定窗口带到前面 |
move_window | 将窗口移动到指定坐标 |
resize_window | 将窗口调整为指定尺寸 |
minimize_window | 最小化Dock的窗口 |
鼠标控制
| 工具 | 说明 |
|---|---|
click | 执行鼠标单击(左键、右键或中键) |
double_click | 执行双击操作 |
move_mouse | 移动光标而不单击 |
drag | 从开始坐标到结束坐标执行拖动操作 |
键盘输入
| 工具 | 说明 |
|---|---|
type_text | 在当前光标位置键入文本 |
press_key | 按名称按键(Enter、Tab、Escape、F1-F12等) |
key_combination | 按下组合键(例如Cmd+C、Cmd+Shift+S) |
滚动和导航
| 工具 | 说明 |
|---|---|
scroll | 沿指定方向(上、下、左、右)滚动 |
scroll_to_element | 滚动直到UI元素可见 |
截图
| 工具 | 说明 |
|---|---|
take_screenshot | 以PNG/JPEG格式捕获屏幕、显示器、窗口或区域 |
屏幕截图功能:
- 自动调整大小:屏幕截图的大小调整为最大1600px(可配置),以实现API兼容性
- 自动压缩:需要时使用JPEG压缩将文件大小限制为1.8MB
- 全分辨率:使用
rawFile: true用于以全分辨率输出文件 - 禁用处理:设置
maxDimension: 0和maxFileSize: 0禁用所有处理
UI元素交互
| 工具 | 说明 |
|---|---|
get_ui_elements | 检索应用程序的UI元素树 |
click_ui_element | 按路径单击UI元素 |
get_ui_element_value | 获取UI元素的值 |
set_ui_element_value | 设置可编辑UI元素的值 |
focus_ui_element | 将键盘焦点设置为UI元素 |
菜单栏操作
| 工具 | 说明 |
|---|---|
list_menu_items | 获取应用程序的菜单层次结构 |
click_menu_item | 按路径单击菜单项(例如,“文件>保存”) |
get_menu_item_state | 获取菜单项的启用/选中状态 |
list_status_bar_items | 列出可见的状态栏项目 |
click_status_bar_item | 单击状态栏项以打开其菜单 |
click_status_bar_menu_item | 单击状态栏菜单中的菜单项 |
get_menu_bar_structure | 获取进程的完整菜单栏层次结构 |
macOS权限
此服务器需要特定的macOS权限才能运行。首次使用某些工具时,macOS会提示您授予访问权限。
必需的权限
1.无障碍
需要: 键盘输入、鼠标控制、UI元素交互、窗口管理
授予访问权限:
- 打开 系统设置>隐私和安全>辅助功能
- 点击 + 按钮并添加您的终端应用程序(终端、iTerm2或克劳德代码)
- 切换开关以启用访问
- 重新启动终端 授予许可后
快速访问:
open "x-apple.systempreferences:com.apple.preference.security?Privacy_Accessibility"2.自动化
需要: 控制其他应用程序(Finder、Safari等)
授予访问权限:
- 打开 系统设置>隐私和安全>自动化
- 在列表中查找您的终端应用程序
- 为要控制的目标应用程序启用复选框
- 如果首次使用时出现提示,请单击 好的 允许
快速访问:
open "x-apple.systempreferences:com.apple.preference.security?Privacy_Automation"3.屏幕录制
需要: 截图
授予访问权限:
- 打开 系统设置>隐私和安全>屏幕录制
- 点击 + 按钮并添加您的终端应用程序
- 切换开关以启用访问
- 重新启动终端 授予许可后
快速访问:
open "x-apple.systempreferences:com.apple.preference.security?Privacy_ScreenCapture"权限疑难解答
| 问题 | 解决方案 |
|---|---|
| 已添加权限但不起作用 | 完全重新启动终端应用程序(Cmd+Q,然后重新打开) |
| 应用程序未出现在权限列表中 | 运行一次操作以触发权限对话框 |
| 授予权限后仍被拒绝 | 从列表中删除应用程序,重新启动,然后重新添加 |
| 所有权限问题仍然存在 | 重新启动macOS |
| “辅助访问”错误 | 授予 无障碍 具体许可 |
| “-1743”错误代码 | 格兰特 自动化 目标应用程序的权限 |
| 截图返回黑色图像 | Grant 屏幕录制 权限和重新启动 |
权限检查表
在使用mac mcp服务器之前,请验证是否授予了这些权限:
- \[ \] 无障碍 -终端/Claude代码已添加并启用
- \[ \] 自动化 -在您的终端下启用的目标应用程序
- \[ \] 屏幕录制 -终端/Claude代码已添加并启用
- \[ \] 应用程序已重新启动 授予权限后
使用示例
系统信息
// Get system info
await get_system_info({})
// Returns: { macOSVersion: "15.0", hardwareModel: "MacBook Pro", ... }
// Get battery status
await get_battery_status({})
// Returns: { percentage: 85, isCharging: true, isDesktop: false }应用管理
// List running applications
await list_running_apps({})
// Returns: [{ name: "Safari", bundleId: "com.apple.Safari", processId: 1234 }, ...]
// Launch an application
await launch_app({ name: 'Safari' })
// Quit an application
await quit_app({ name: 'Safari' })窗口管理
// List all windows
await list_windows({})
// Focus a specific window
await focus_window({ appName: 'Finder', windowIndex: 1 })
// Move a window
await move_window({ appName: 'Finder', x: 100, y: 100 })
// Resize a window
await resize_window({ appName: 'Finder', width: 800, height: 600 })键盘和鼠标
// Type text
await type_text({ text: 'Hello, World!' })
// Press a key
await press_key({ key: 'enter' })
// Key combination (Cmd+C)
await key_combination({ modifiers: ['command'], key: 'c' })
// Click at coordinates
await click({ x: 500, y: 300 })
// Right-click
await click({ x: 500, y: 300, button: 'right' })
// Cmd+click
await click({ x: 500, y: 300, modifiers: ['command'] })截图
// Capture full screen (auto-resized and compressed for API)
await take_screenshot({})
// Capture to file (also auto-processed by default)
await take_screenshot({ filePath: '/tmp/screenshot.png' })
// Capture at full resolution (no processing)
await take_screenshot({ filePath: '/tmp/full.png', rawFile: true })
// Capture specific region
await take_screenshot({
region: { x: 100, y: 100, width: 800, height: 600 },
})
// Custom compression settings
await take_screenshot({
maxDimension: 1920, // Max 1920px
maxFileSize: 1_000_000, // Max 1MB
quality: 70, // JPEG quality 70
})
// Disable all processing for base64 output
await take_screenshot({ maxDimension: 0, maxFileSize: 0 })UI元素交互
// Get UI element tree
await get_ui_elements({ appName: 'Safari', maxDepth: 3 })
// Click a button by path
await click_ui_element({
appName: 'Safari',
elementPath: 'window1/button1',
})
// Set text field value
await set_ui_element_value({
appName: 'TextEdit',
elementPath: 'window1/textfield1',
value: 'New text',
})菜单操作
// List application menus
await list_menu_items({ appName: 'Finder' })
// Click a menu item
await click_menu_item({
appName: 'Finder',
menuPath: 'File > New Finder Window',
})
// Get menu item state
await get_menu_item_state({
appName: 'Finder',
menuPath: 'View > Show Path Bar',
})从源安装
对于开发或定制:
git clone https://github.com/laststance/mac-mcp-server.git
cd mac-mcp-server
pnpm install
pnpm build然后配置Claude Code以使用本地构建:
{
"mcpServers": {
"mac-mcp-server": {
"command": "node",
"args": ["/path/to/mac-mcp-server/dist/index.js"]
}
}
}发展
命令
| 命令 | 描述 |
|---|---|
pnpm build | 为生产而建 |
pnpm dev | 开发模式(手表) |
pnpm test | 运行测试 |
pnpm typecheck | TypeScript类型检查 |
pnpm lint | Lint代码 |
pnpm lint:fix | 棉绒和自动修复 |
pnpm format | 使用Prettier格式化代码 |
项目结构
mac-mcp-server/
├── src/
│ ├── index.ts # Entry point, MCP server setup
│ ├── lib/
│ │ ├── server.ts # MCP server configuration
│ │ ├── executor.ts # AppleScript execution
│ │ ├── permission.ts # Permission checking and guidance
│ │ └── sanitizer.ts # Input sanitization
│ └── tools/
│ ├── system.ts # System information tools
│ ├── clipboard.ts # Clipboard management
│ ├── notification.ts # Notification display
│ ├── audio.ts # Audio control
│ ├── application.ts # Application lifecycle
│ ├── finder.ts # Finder integration
│ ├── window.ts # Window management
│ ├── mouse.ts # Mouse control
│ ├── keyboard.ts # Keyboard input
│ ├── scroll.ts # Scroll operations
│ ├── screenshot.ts # Screenshot capture
│ ├── ui-element.ts # UI element interaction
│ └── menu.ts # Menu bar operations
├── dist/ # Compiled output
├── package.json
└── tsconfig.json安全
此MCP服务器的设计考虑了安全性:
- 仅执行AppleScript:所有自动化都是通过AppleScript和JXA执行的。不会执行任意shell命令。
- 输入净化:所有用户输入在包含在AppleScript中之前都经过消毒,以防止注入攻击。
- 无数据持久性:服务器不存储任何用户数据或凭据。
- 权限门控访问:所有敏感操作都需要明确的macOS权限授予。
- 自动化范围:每个工具都有特定的、有限的用途,而不是提供一般的系统访问。
需求
| 要求 | 版本 |
|---|---|
| macOS | 10.15(Catalina)或更高版本 |
| Node.js | 20.11.0或更高版本 |
| pnpm | 10.28.0(用于开发) |
许可证
麻省理工学院
作者
贡献
欢迎捐款。请先打开一个问题,讨论您想更改什么。
- 分叉存储库
- 创建功能分支(
git checkout -b feature/amazing-feature) - 提交您的更改(
git commit -m 'Add some amazing feature') - 推到分支(
git push origin feature/amazing-feature) - 打开拉取请求
