MCP Safari Server 是一个基于模型上下文协议(MCP)的服务器,可对 macOS 系统上的 Safari 浏览器进行编程式控制。它非常适合用于网页自动化、测试以及与 Claude 等 AI 助手协作进行调试工作。
cd mcp-safari-server
npm install
npm run build
将此服务器添加到你的 Claude 配置文件(.mcp.json 或 claude-code-config.json)中:
{
"mcpServers": {
"safari": {
"command": "node",
"args": ["/path/to/mcp-safari-server/dist/index.js"],
"env": {}
}
}
}
或者在开发环境中使用:
{
"mcpServers": {
"safari": {
"command": "npx",
"args": ["tsx", "/path/to/mcp-safari-server/src/index.ts"],
"env": {}
}
}
}
console.log、console.error 和 console.warn 消息。进入项目目录并安装所需依赖:
cd mcp-safari-server
npm install
运行以下命令构建服务器:
npm run build
将服务器配置添加到 Claude 配置文件中,可选择生产环境或开发环境的配置。
配置完成后,Claude 可以使用以下工具进行操作:
导航到指定 URL:
Use the safari navigate tool to open https://example.com
在当前页面执行 JavaScript 代码:
Use the safari execute_script tool to run: document.querySelector('h1').textContent
获取当前页面的 URL 和标题:
Use the safari get_page_info tool to see the current URL and title
对当前 Safari 窗口进行截图:
Use the safari take_screenshot tool to capture the current page
获取页面的控制台日志:
Use the safari get_console_logs tool to see any console messages
刷新当前页面:
Use the safari refresh_page tool
返回上一页或前进到下一页:
Use the safari go_back tool to go to the previous page
开始监控 JavaScript 错误并自动发送到 Claude Code:
Use the safari start_error_monitoring tool
选项:
interval:检查间隔(毫秒),默认值为 2000。autoSendToClaude:是否自动将错误发送到 Claude Code,默认值为 true。停止错误监控:
Use the safari stop_error_monitoring tool
点击页面上的元素:
Use the safari click_element tool with selector "#submit-button"
Use the safari click_element tool with selector "a.nav-link" and waitForNavigation true
在输入字段中输入文本:
Use the safari type_text tool with selector "#email" and text "user@example.com"
Use the safari type_text tool with selector "#password" and text "mypassword" and clearFirst false
滚动到特定元素或位置:
Use the safari scroll_to tool with selector "#footer"
Use the safari scroll_to tool with y 500 and behavior "smooth"
从下拉菜单中选择选项:
Use the safari select_option tool with selector "#country" and value "US"
Use the safari select_option tool with selector "#size" and text "Large"
Use the safari select_option tool with selector "#priority" and index 2
获取元素的文本内容:
Use the safari get_element_text tool with selector "h1"
Use the safari get_element_text tool with selector ".error-message"
等待元素出现:
Use the safari wait_for_element tool with selector "#loading-complete"
Use the safari wait_for_element tool with selector ".modal" and timeout 5000
Safari MCP 服务器可以自动监控网页应用中的 JavaScript 错误,并将其发送到 Claude Code 以便立即修复。具体工作流程如下:
启动错误监控后,服务器会执行以下操作:
window.onerror)。console.error 调用。navigate 工具在 Safari 中打开你的应用。start_error_monitoring 启动错误监控。stop_error_monitoring 停止监控。此服务器通过执行 AppleScript 命令来控制 Safari。请仅在受信任的环境中使用,并谨慎执行 JavaScript 代码。