MCP浏览器代理是一款强大的Model Context Protocol(MCP)集成工具,它赋予Claude Desktop自主的浏览器自动化能力,可帮助用户更高效地完成各类网页操作和API请求。
要使用MCP浏览器代理,你需要满足以下要求:
npm init playwright@latest
此软件包包含Playwright和运行浏览器自动化所需的依赖项。当你运行npm install时,将安装所需的Playwright依赖项。该软件包支持以下浏览器:
首次使用某种浏览器类型时,Playwright将根据需要自动安装相应的浏览器驱动程序。你也可以使用以下命令手动安装它们:
npx playwright install chrome
npx playwright install firefox
npx playwright install webkit
npx playwright install msedge
⚠️ 重要提示
- 关于Safari:Playwright不直接支持Safari浏览器,而是使用WebKit,它是Safari的浏览器引擎。
- 关于Edge:选择Edge作为浏览器类型时,代理实际上将启动Microsoft Edge(非Chromium)。从技术上讲,在Playwright中,Edge是使用Chromium浏览器实例并带有'msedge'通道参数启动的,因为Microsoft Edge基于Chromium。
git clone https://github.com/imprvhub/mcp-browser-agent
cd mcp-browser-agent
npm install
npm run build
有两种方法可以运行MCP服务器:
node dist/index.js
在使用Claude Desktop时,请保持此终端窗口打开。服务器将一直运行,直到你关闭终端。
Claude Desktop可以在需要时自动启动MCP服务器。要进行设置,请按以下步骤操作:
Claude Desktop配置文件位于:
~/Library/Application Support/Claude/claude_desktop_config.json%APPDATA%\Claude\claude_desktop_config.json~/.config/Claude/claude_desktop_config.json编辑此文件以添加浏览器代理MCP配置。如果文件不存在,请创建它:
{
"mcpServers": {
"browserAgent": {
"command": "node",
"args": ["ABSOLUTE_PATH_TO_DIRECTORY/mcp-browser-agent/dist/index.js",
"--browser",
"chrome"
]
}
}
}
重要提示:请将ABSOLUTE_PATH_TO_DIRECTORY替换为你安装MCP的完整绝对路径:
/Users/username/mcp-browser-agentC:\\Users\\username\\mcp-browser-agent如果你已经配置了其他MCP,只需在mcpServers对象中添加browserAgent部分。以下是一个包含多个MCP的配置示例:
{
"mcpServers": {
"otherMcp1": {
"command": "...",
"args": ["..."]
},
"otherMcp2": {
"command": "...",
"args": ["..."]
},
"browserAgent": {
"command": "node",
"args": [
"ABSOLUTE_PATH_TO_DIRECTORY/mcp-browser-agent/dist/index.js",
"--browser",
"chrome"
]
}
}
}
MCP浏览器代理支持多种浏览器类型。默认情况下,它使用Chrome,但你可以通过以下几种方式指定不同的浏览器:
在你的主目录中创建或编辑文件.mcp_browser_agent_config.json:
{
"browserType": "chrome"
}
browserType支持的值包括:
chrome - 使用已安装的Chrome(默认)firefox - 使用Firefox 'Nightly'浏览器webkit - 使用WebKit引擎(注意:这不是Safari本身,而是驱动Safari的WebKit渲染引擎)edge - 使用Microsoft Edge手动启动MCP服务器时,你可以指定浏览器类型:
node dist/index.js --browser firefox
设置MCP_BROWSER_TYPE环境变量:
MCP_BROWSER_TYPE=firefox node dist/index.js
在Claude Desktop的claude_desktop_config.json中配置MCP时,你可以指定浏览器类型:
{
"mcpServers": {
"browserAgent": {
"command": "node",
"args": [
"ABSOLUTE_PATH_TO_DIRECTORY/mcp-browser-agent/dist/index.js",
"--browser",
"chrome"
]
}
}
}
以下是一些使用MCP浏览器代理与Claude的实际示例:
Navigate to the Google homepage at https://www.google.com
Take a screenshot of the current page and name it "google-homepage"
Type "weather forecast" in the search box
Navigate to https://www.wikipedia.org and search for "Model Context Protocol"
Go to https://the-internet.herokuapp.com/dropdown and select the option "Option 1" from the dropdown
Navigate to https://the-internet.herokuapp.com/login and fill in the username field with "tomsmith" and the password field with "SuperSecretPassword!"
Go to https://the-internet.herokuapp.com/login, fill in the username and password fields, then click the login button
Go to https://example.com and execute a JavaScript script to return the page title
Navigate to https://www.google.com and execute a JavaScript script to count the number of links on the page
Perform a GET request to https://jsonplaceholder.typicode.com/todos/1
Make a POST request to https://jsonplaceholder.typicode.com/posts with appropriate JSON data
| 工具名称 | 描述 | 参数 |
|---|---|---|
browser_navigate |
导航到URL | url(必需), timeout, waitUntil |
browser_screenshot |
捕获屏幕截图 | name(必需), selector, fullPage, mask, savePath |
browser_click |
点击元素 | selector(必需) |
browser_fill |
填充表单输入 | selector(必需), value(必需) |
browser_select |
选择下拉选项 | selector(必需), value(必需) |
browser_hover |
悬停在元素上 | selector(必需) |
browser_evaluate |
执行JavaScript | script(必需) |
| 工具名称 | 描述 | 参数 |
|---|---|---|
api_get |
GET请求 | url(必需), headers |
api_post |
POST请求 | url(必需), data(必需), headers |
api_put |
PUT请求 | url(必需), data(必需), headers |
api_patch |
PATCH请求 | url(必需), data(必需), headers |
api_delete |
DELETE请求 | url(必需), headers |
MCP浏览器代理公开了以下资源:
browser://logs - 访问浏览器控制台日志screenshot://[name] - 按名称访问屏幕截图MCP浏览器代理基于Model Context Protocol构建,使Claude能够通过Playwright与有头浏览器进行交互。该实现包含四个主要组件:
与基本集成不同,MCP浏览器代理作为真正的AI代理,具有以下特点:
如果你在Claude Desktop中看到错误 "MCP Browser Agent: Server disconnected":
node dist/index.js。claude_desktop_config.json中的绝对路径对于你的系统是正确的。\\)。如果浏览器未启动或你看不到它:
Chromium和Chrome浏览器存在已知问题,有时使用后进程不会正确终止。如果你遇到此问题:
ps aux | grep chrome或ps aux | grep chromium查找进程,然后使用kill
终止它。⚠️ 重要提示
此MCP集成基于Playwright构建,Playwright存在已知问题和错误,可能会影响其操作。请将你在浏览器自动化中遇到的任何问题报告给 Playwright的GitHub问题。Playwright团队正在不断努力解决这些问题,但尽管存在这些限制,此代理仍为Claude Desktop提供了浏览器自动化功能的基础。
src/index.ts:主入口点和MCP服务器初始化。src/tools.ts:工具模式和注册。src/handlers.ts:工具和资源的MCP请求处理程序。src/executor.ts:使用Playwright的工具实现逻辑。npm run build
npm run watch
项目包含测试以验证核心功能和浏览器处理。
npm test # 运行测试
npm run test:watch # 监视模式
npm run test:coverage # 覆盖率报告
测试验证配置完整性、浏览器自动化功能、错误处理和进程清理。由于Chrome/Chromium终止存在已知问题,测试套件特别关注确保正确处理浏览器进程。
⚠️ 重要提示
此MCP集成赋予Claude自主的浏览器控制能力。请查看我们的 安全策略,了解有关禁止使用、安全影响和最佳实践的重要信息。
MCP浏览器代理旨在用于合法的自动化任务,但可能会被滥用。用户有责任确保其使用符合所有适用的法律、服务条款和道德准则。有关更多信息,请参阅我们详细的 安全策略。
欢迎为MCP浏览器代理做出贡献!你可以在以下方面提供帮助:
本项目采用Mozilla公共许可证2.0 - 有关详细信息,请参阅 LICENSE 文件。