PlayMCP是一个全面的MCP(模型上下文协议)服务器,它借助Playwright实现浏览器自动化。该服务器为网页数据抓取、测试和自动化操作提供了强大的工具。
| 属性 | 详情 |
|---|---|
| 工具 | 描述 |
| 必需参数 | - |
openBrowser |
启动浏览器实例 |
navigate |
导航到URL |
click |
点击元素 |
type |
在元素中输入文本 |
moveMouse |
将鼠标移动到坐标位置 |
scroll |
滚动页面 |
screenshot |
进行截图 |
getPageSource |
获取HTML源代码 |
getPageText |
获取文本内容 |
getPageTitle |
获取页面标题 |
getPageUrl |
获取当前URL |
getScripts |
获取JavaScript代码 |
getStylesheets |
获取CSS样式表 |
getMetaTags |
获取元标签 |
getLinks |
获取所有链接 |
getImages |
获取所有图像 |
getForms |
获取所有表单 |
getElementContent |
获取元素内容 |
executeJavaScript |
运行JavaScript |
closeBrowser |
关闭浏览器 |
前提条件
克隆并设置
git clone
cd PlayMCP
npm install
npm run build
安装Playwright浏览器
npx playwright install
这将下载必要的浏览器二进制文件(Chromium、Firefox、Safari)。
验证安装
npm run start
如果一切正常,你应该会看到 "Browser Automation MCP Server starting..."。
git clone
cd PlayMCP
npm install && npm run build && npx playwright install
添加到你的MCP配置文件中:
标准MCP配置:
{
"servers": {
"playmcp-browser": {
"type": "stdio",
"command": "node",
"args": ["./dist/server.js"],
"cwd": "/path/to/PlayMCP",
"description": "使用Playwright的浏览器自动化服务器"
}
}
}
替代配置(适用于VS Code GitHub Copilot):
{
"servers": {
"playmcp-browser": {
"type": "stdio",
"command": "node",
"args": ["/absolute/path/to/PlayMCP/dist/server.js"]
}
}
}
Windows用户:
{
"servers": {
"playmcp-browser": {
"type": "stdio",
"command": "node",
"args": ["C:\\path\\to\\PlayMCP\\dist\\server.js"]
}
}
}
此MCP服务器与VS Code GitHub Copilot完全兼容。将上述配置添加到你的MCP设置后,你可以直接在VS Code中使用所有浏览器自动化工具。
Claude桌面版(config.json位置):
%APPDATA%\Claude\config.json~/Library/Application Support/Claude/config.json~/.config/Claude/config.jsonVS Code MCP扩展: 添加到你的VS Code settings.json或MCP配置文件中。
完整配置示例:
{
"mcpServers": {
"playmcp-browser": {
"type": "stdio",
"command": "node",
"args": ["/Users/username/PlayMCP/dist/server.js"],
"description": "使用Playwright进行浏览器自动化"
}
}
}
// 打开浏览器并导航
await openBrowser({ headless: false, debug: true })
await navigate({ url: "https://example.com" })
// 提取内容
const title = await getPageTitle()
const links = await getLinks()
const forms = await getForms()
// 表单自动化
// 填写表单
await click({ selector: "#login-button" })
await type({ selector: "#username", text: "user@example.com" })
await type({ selector: "#password", text: "password123" })
await click({ selector: "#submit" })
// 页面交互
// 滚动和交互
await scroll({ x: 0, y: 500 })
await moveMouse({ x: 100, y: 200 })
await click({ selector: ".dropdown-menu" })
// 高级JavaScript执行
// 运行自定义JavaScript
await executeJavaScript({
script: "document.querySelectorAll('h1').length"
})
// 修改页面内容
await executeJavaScript({
script: "document.body.style.backgroundColor = 'lightblue'"
})
// 提取复杂数据
await executeJavaScript({
script: `
Array.from(document.querySelectorAll('article')).map(article => ({
title: article.querySelector('h2')?.textContent,
summary: article.querySelector('p')?.textContent
}))
`
})
// 截图和文档记录
// 进行截图
await screenshot({ path: "./full-page.png", type: "page" })
await screenshot({ path: "./element.png", type: "element", selector: "#main-content" })
安装并设置:
git clone && cd PlayMCP
npm install && npm run build && npx playwright install
添加到你的MCP客户端配置
开始自动化操作:
await openBrowser({ debug: true })
await navigate({ url: "https://news.ycombinator.com" })
const links = await getLinks()
console.log(`找到 ${links.length} 个链接`)
| 属性 | 详情 |
|---|---|
| 模型类型 | - |
| 系统要求 | Node.js 16+(推荐LTS版本) |
| 操作系统 | Windows、macOS或Linux |
| 内存 | 至少2GB RAM(大量使用时推荐4GB以上) |
| 磁盘空间 | 浏览器二进制文件和依赖项约需500MB |
| 属性 | 详情 |
|---|---|
| 依赖项 | - |
| Playwright | 处理浏览器自动化(自动安装) |
| TypeScript | 用于编译(开发依赖项) |
| 浏览器二进制文件 | 通过 npx playwright install 下载 |
“Browser not initialized” 错误
openBrowserPlaywright安装失败
# 尝试手动安装浏览器
npx playwright install chromium
# 或安装所有浏览器
npx playwright install
Linux/macOS上的权限错误
# 确保脚本可执行
chmod +x dist/server.js
MCP配置中的路径问题
C:\\path\\to\\PlayMCP\\dist\\server.jsnode /path/to/PlayMCP/dist/server.js浏览器崩溃或超时
headless: false 模式运行进行调试# 直接测试服务器
echo '{"jsonrpc":"2.0","id":1,"method":"tools/list","params":{}}' | node ./dist/server.js
你应该会看到一个列出所有可用工具的JSON响应。
本项目采用MIT许可证。