一个使用Playwright提供浏览器自动化功能的模型上下文协议(MCP)服务器。该服务器使AI助手能够通过标准化接口与网页进行交互。
非常适合使用AI助手进行的Web自动化、测试和调试工作流程,包括:
npm install
npm run build
确保安装了Playwright浏览器:
npx playwright install
对于系统依赖项(Linux):
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可以使用浏览器自动化工具进行Web测试、抓取和自动化任务。
Ctrl+Shift+P → "Tasks: 运行任务" → "build"Ctrl+Shift+P → "Tasks: 运行任务" → "dev"Ctrl+Shift+P → "Tasks: 运行任务" → "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工具允许您以细粒度控制的方式在任何方向上滚动页面:
// 默认向下滚动(100像素)
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工具使用本地Gemma3模型通过Ollama提供AI驱动的网页分析。此功能可以根据上下文描述页面上可见的内容,分析页面结构,并查找特定元素。
ollama pull gemma3:4b
ollama serve
// 使用AI进行截图和分析
await analyze_screenshot({
fullPage: true,
model: "gemma3:4b"
});
// 获取页面结构的详细分析
await analyze_screenshot({
detailed: true,
pretext: "关注导航元素和表单字段"
});
// 查找特定元素或问题
await analyze_screenshot({
pretext: "检查是否存在任何错误消息或布局损坏",
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中的完整条款。对于商业许可咨询,请联系我们。