Playwright浏览器自动化服务器

一个基于Playwright的浏览器自动化服务器,提供网页交互、截图、测试代码生成和JavaScript执行功能,适用于AI助手和开发测试场景。

  • by executeautomation
  • TypeScript
  • 本地部署
  • 5676 星标
  • 更新于
在 GitHub 查看 executeautomation/mcp-playwright

使用说明

MseeP.ai Security Assessment

剧作家MCP服务器🎭

使用Playwright提供浏览器自动化功能的模型上下文协议服务器。此服务器使LLM能够在真实的浏览器环境中与网页交互、截图、生成测试代码、抓取网页并执行JavaScript。

✨ v1.0.10的新增功能

🎯 143个真实设备预设的设备仿真!

测试您的web应用程序 真实设备配置文件 用一个简单的命令:

// Test on iPhone 13 with automatic user-agent, touch support, and device pixel ratio
await playwright_resize({ device: "iPhone 13" });

// Switch to iPad with landscape orientation
await playwright_resize({ device: "iPad Pro 11", orientation: "landscape" });

// Test desktop view
await playwright_resize({ device: "Desktop Chrome" });

AI助手的自然语言支持:

  • “在iPhone 13上测试”
  • “切换到iPad视图”
  • “旋转到横向”

支持143台设备: iPhone、iPad、Pixel、Galaxy和Desktop浏览器,具有对视口、用户代理、触摸事件和设备像素比的适当模拟。

📚 查看设备快速参考 | 快速指南

截图

文档 | api参考

安装

您可以使用npm、mcp-get或Smithery安装该包:

使用npm:

npm install -g @executeautomation/playwright-mcp-server

使用mcp get:

npx @michaellatman/mcp-get@latest install @executeautomation/playwright-mcp-server

使用Smithery

通过以下方式自动安装Playwright MCP for Claude Desktop 史密瑟里:

npx @smithery/cli install @executeautomation/playwright-mcp-server --client claude

使用克劳德代码:

claude mcp add --transport stdio playwright npx @executeautomation/playwright-mcp-server

在VS代码中安装

使用以下按钮之一在VS Code中安装Playwright MCP服务器:

或者,您可以使用VS Code CLI安装Playwright MCP服务器:

# For VS Code
code --add-mcp '{"name":"playwright","command":"npx","args":["@executeautomation/playwright-mcp-server"]}'
# For VS Code Insiders
code-insiders --add-mcp '{"name":"playwright","command":"npx","args":["@executeautomation/playwright-mcp-server"]}'

安装后,ExecuteAutomation Playwright MCP服务器将可用于VS Code中的GitHub Copilot代理。

浏览器安装

自动安装(推荐)

剧作家MCP服务器 自动安装浏览器二进制文件 当您第一次使用它时。当服务器检测到浏览器丢失时,它将:

  1. 自动下载并安装所需的浏览器(Chromium、Firefox或WebKit)
  2. 在控制台中显示安装进度
  3. 安装完成后重试您的请求

无需手动设置! 只需开始使用服务器,它就会为您处理浏览器安装。

手动安装(可选)

如果您更喜欢手动安装浏览器或遇到自动安装的任何问题:

# Install all browsers
npx playwright install

# Or install specific browsers
npx playwright install chromium
npx playwright install firefox
npx playwright install webkit

浏览器存储位置

浏览器安装到:

  • 窗户: %USERPROFILE%\AppData\Local\ms-playwright
  • macOS: ~/Library/Caches/ms-playwright
  • Linux: ~/.cache/ms-playwright

使用Playwright服务器的配置

标准模式(stdio)

这是 Claude Desktop的推荐模式.

{
  "mcpServers": {
    "playwright": {
      "command": "npx",
      "args": ["-y", "@executeautomation/playwright-mcp-server"]
    }
  }
}

注: 在stdio模式下,日志记录会自动仅定向到文件(而不是控制台),以保持干净的JSON-RPC通信。日志被写入 ~/playwright-mcp-server.log.

HTTP模式(独立服务器)

在没有显示器的系统上或从IDE的工作进程运行headed浏览器时,您可以将MCP服务器作为独立的HTTP服务器运行:

Claude桌面用户注意事项: Claude Desktop当前需要stdio模式(命令/args配置)。建议将HTTP模式用于VS Code、自定义客户端和远程部署。看 CLAUDE_DESKTOP_CONFIG.md 了解详情。

启动HTTP服务器

# Using npx
npx @executeautomation/playwright-mcp-server --port 8931

# Or after global installation
playwright-mcp-server --port 8931

服务器将启动并显示可用终结点:

==============================================
Playwright MCP Server (HTTP Mode)
==============================================
Port: 8931

ENDPOINTS:
- SSE Stream:     GET  http://localhost:8931/sse
- Messages:       POST http://localhost:8931/messages?sessionId=<id>
- MCP (unified):  GET  http://localhost:8931/mcp
- MCP (unified):  POST http://localhost:8931/mcp?sessionId=<id>
- Health Check:   GET  http://localhost:8931/health
==============================================

HTTP模式的客户端配置

⚠️ 关键: 这 "type": "http" 字段是 必需 用于HTTP/SSE传输!

对于VS代码GitHub副本:

{
  "github.copilot.chat.mcp.servers": {
    "playwright": {
      "url": "http://localhost:8931/mcp",
      "type": "http"
    }
  }
}

对于自定义MCP客户端:

{
  "mcpServers": {
    "playwright": {
      "url": "http://localhost:8931/mcp",
      "type": "http"
    }
  }
}

重要提示: 没有 "type": "http",连接将失败。

对于Claude Desktop: 改用stdio模式(请参阅上面的标准模式)

HTTP模式的用例

  • 在没有显示器的系统上运行有头浏览器(例如远程服务器)
  • 与VS Code GitHub Copilot集成
  • 将服务器作为后台服务运行
  • 从多个客户端访问服务器
  • 使用调试 /health 端点
  • 自定义MCP客户端集成

监控: 服务器包括一个监控系统,该系统在动态分配的端口上启动(避免冲突)。检查实际端口的控制台输出。

注: 对于Claude Desktop,现在继续使用stdio模式(上面的标准模式)。

故障排除

“找不到会话ID的传输”错误

症状: 400错误,消息为“错误请求:找不到会话ID的传输”

解决方案:

  1. 检查配置包括 "type": "http"

    {
      "url": "http://localhost:8931/mcp",
      "type": "http"  // ← This is REQUIRED!
    }
    
  2. 验证服务器日志是否显示连接:

    # Should see these in order:
    # 1. "Incoming request" - GET /mcp
    # 2. "Transport registered" - with sessionId
    # 3. "POST message received" - with same sessionId
    
  3. 重新启动服务器和客户端

连接问题

  • 服务器未启动: 检查端口8931是否可用
  • 外部访问被阻止: 这是设计(安全)。服务器仅绑定到本地主机
  • 对于远程访问: 使用SSH隧道:
    ssh -L 8931:localhost:8931 user@remote-server
    

测试

此项目使用Jest进行测试。测试位于 src/__tests__ 目录。

运行测试

您可以使用以下命令之一运行测试:

# Run tests using the custom script (with coverage)
node run-tests.cjs

# Run tests using npm scripts
npm test           # Run tests without coverage
npm run test:coverage  # Run tests with coverage
npm run test:custom    # Run tests with custom script (same as node run-tests.cjs)

测试覆盖率报告将在 coverage 目录。

运行评估

evals包加载一个mcp客户端,然后运行index.ts文件,因此不需要在测试之间重建。您可以通过在npx命令前加前缀来加载环境变量。可以找到完整的文档 这里.

OPENAI_API_KEY=your-key  npx mcp-eval src/evals/evals.ts src/tools/codegen/index.ts

贡献

添加新工具时,请注意工具名称的长度。一些客户端(如Cursor)对服务器和工具的组合名称有60个字符的限制(server_name:tool_name).

我们的服务器名称是 playwright-mcp。请确保您的工具名称足够短,不会超过此限制。

明星历史