MCP浏览器服务器是一个基于模型上下文协议(MCP)的服务器,借助Playwright实现浏览器自动化功能。该服务器能让AI助手通过标准化接口与网页进行交互,在网页自动化、测试和调试工作流中表现出色。
它适用于各类AI助手,包括:
npm install
npm run build
npx playwright install
sudo npx playwright install-deps
在VS Code中配置MCP服务器,可将以下内容添加到settings.json或工作区配置中:
"mcp": {
"servers": {
"browser-automation": {
"command": "node",
"args": [
"/home/yourUserName/mcp-browser-server/build/index.js"
],
"env": {}
}
}
}
配置完成后,Chat.fans代理和GitHub Copilot Chat即可使用浏览器自动化工具进行网页测试、数据抓取和自动化任务。
Ctrl+Shift+P,选择 "Tasks: Run Task",然后选择 "build"。Ctrl+Shift+P,选择 "Tasks: Run Task",然后选择 "dev"。Ctrl+Shift+P,选择 "Tasks: Run Task",然后选择 "test-mcp-server"。以下是一个网页应用测试的示例:
// 以有头模式启动浏览器,便于可视化调试
await launch_browser({ browser: "chromium", headless: false });
// 导航到登录页面
await navigate({ url: "http://localhost:3000/login" });
// 填写凭证
await type_text({ selector: "input[type='email']", text: "user@example.com" });
await type_text({ selector: "input[type='password']", text: "password123" });
// 提交表单
await click_element({ selector: "button[type='submit']" });
// 等待登录成功
await wait_for_element({ selector: ".dashboard", timeout: 10000 });
// 检查登录期间是否有控制台错误
await get_console_logs({ level: "error" });
// 截取仪表盘截图
await screenshot({ fullPage: true, path: "dashboard.png" });
// 获取所有控制台日志进行调试
await get_console_logs();
// 向下滚动以查看更多内容
await scroll({ direction: "down", pixels: 500, behavior: "smooth" });
// 检查页面是否可以垂直滚动
await check_scrollability({ direction: "vertical" });
// 滚动回顶部
await scroll({ direction: "up", pixels: 500 });
MCP浏览器服务器提供了全面的滚动工具,用于导航长页面和检查滚动能力。
scroll 工具允许你以细粒度控制页面在任何方向上滚动:
// 默认向下滚动100px
await scroll();
// 按指定方向和自定义距离滚动
await scroll({ direction: "down", pixels: 300, behavior: "smooth" });
await scroll({ direction: "up", pixels: 200, behavior: "auto" });
await scroll({ direction: "left", pixels: 150 });
await scroll({ direction: "right", pixels: 150 });
// 平滑滚动以提升用户体验
await scroll({ direction: "down", pixels: 500, behavior: "smooth" });
参数说明:
direction:可选值为 "up"、"down"、"left"、"right"(默认值为 "down")。pixels:滚动的像素数(默认值为100)。behavior:可选值为 "auto" 或 "smooth"(默认值为 "auto")。check_scrollability 工具用于确定页面是否可以在特定方向上滚动:
// 检查垂直和水平滚动能力
await check_scrollability({ direction: "both" });
// 仅检查垂直滚动能力
await check_scrollability({ direction: "vertical" });
// 仅检查水平滚动能力
await check_scrollability({ direction: "horizontal" });
响应内容包括:
analyze_screenshot 工具通过Ollama使用本地Gemma3模型对网页进行AI分析。该功能可以描述页面上可见的内容、分析页面结构,并根据上下文查找特定元素。
ollama pull gemma3:4b
ollama serve
// 基本截图分析
await analyze_screenshot({
fullPage: true,
model: "gemma3:4b"
});
// 详细结构分析
await analyze_screenshot({
detailed: true,
pretext: "Focus on navigation elements and form fields"
});
// 特定上下文分析
await analyze_screenshot({
pretext: "Check if there are any error messages or broken layouts",
path: "error-check.png"
});
参数说明:
支持的模型:
gemma3:4b(默认,速度和质量平衡较好)。# 一键设置(安装依赖、浏览器并构建项目)
npm run setup
# 或分步操作
npm install
npx playwright install
npm run build
# 构建项目
npm run build
# 以开发模式运行
npm run dev
# 启动服务器
npm run start
# 开发助手(显示所有可用命令)
npm run dev-helper help
项目在 tests/ 目录中包含了全面的测试:
# 运行基本通信测试
npm run test
# 运行浏览器自动化演示
npm run test:demo
# 运行AI分析测试(需要Ollama)
npm run test:ai-simple
# 检查系统状态
npm run test:status
# 运行所有测试
npm run test:all
使用开发助手进行常见任务:
# 显示所有可用命令
npm run dev-helper help
# 从头开始快速设置
npm run dev-helper setup
# 运行全面测试
npm run dev-helper test
# 清理生成的文件
npm run dev-helper clean
更多测试详情,请参阅 tests/README.md。
mcp-browser-server/
├── src/ # TypeScript源代码
│ └── index.ts # 主MCP服务器实现
├── build/ # 编译后的JavaScript输出
├── tests/ # 测试脚本和文档
│ ├── README.md # 测试文档
│ ├── simple-test.mjs # 基本通信测试
│ ├── demo-test.mjs # 浏览器自动化演示
│ └── *.mjs # 其他测试文件
├── screenshots/ # 测试生成的截图
├── package.json # 项目配置
└── README.md # 本文件
双重许可:
完整条款请参阅 LICENSE。如需商业许可咨询,请联系我们。