关于 playwright-mcp
Playwright MCP 是面向大模型的浏览器自动化服务,使用结构化无障碍快照而非基于截图的交互。结构化数据支持确定性的页面操作,无需视觉模型。
主要功能
- 01通过无障碍快照进行交互
- 02使用 Playwright 自动执行浏览器操作
- 03读取浏览器控制台消息
- 04使用持久或隔离的配置文件
- 05通过扩展连接现有标签页
适用场景
- 01为大模型自动执行网页交互
- 02使用隔离存储运行浏览器会话
- 03使用已登录的现有浏览器标签页
MCP 相关
向 MCP 客户端提供 Playwright 浏览器自动化和页面检查工具,包括导航、交互、脚本执行与控制台访问。可在本地运行,也可提供 HTTP MCP 端点。
配置
本地服务的配置在各客户端中通用,复制后粘贴到客户端的 MCP 配置文件即可。
工具
22收录的工具列表 · 更新于 2 周前
此工具无需参数。
详细介绍
Playwright MCP
一个使用 Playwright 提供浏览器自动化功能的模型上下文协议(MCP)服务。此服务让大模型能够通过结构化的无障碍快照与网页交互,无需使用屏幕截图或针对视觉调优的模型。
主要功能
- 快速轻量。使用 Playwright 的无障碍树,而非基于像素的输入。
- 适合大模型使用。无需视觉模型,完全基于结构化数据运行。
- 工具调用具有确定性。避免基于屏幕截图的方法常见的歧义。
要求
- Node.js 18 或更高版本
- VS Code、Cursor、Windsurf、Claude Desktop、Goose 或任何其他 MCP 客户端
开始使用
首先,使用你的客户端安装 Playwright MCP 服务。
标准配置适用于大多数工具:
{
"mcpServers": {
"playwright": {
"command": "npx",
"args": [
"@playwright/mcp@latest"
]
}
}
}
Amp
通过 Amp VS Code 扩展设置界面添加,或更新你的 settings.json 文件:
"amp.mcpServers": {
"playwright": {
"command": "npx",
"args": [
"@playwright/mcp@latest"
]
}
}
Amp CLI 设置:
通过下面的 amp mcp addcommand 添加
amp mcp add playwright -- npx @playwright/mcp@latest
Claude Code
使用 Claude Code CLI 添加 Playwright MCP 服务:
claude mcp add playwright npx @playwright/mcp@latest
Claude Desktop
按照 MCP 安装指南操作,并使用上面的标准配置。
Codex
使用 Codex CLI 添加 Playwright MCP 服务:
codex mcp add playwright npx "@playwright/mcp@latest"
或者,创建或编辑配置文件 ~/.codex/config.toml 并添加:
[mcp_servers.playwright]
command = "npx"
args = ["@playwright/mcp@latest"]
如需了解更多信息,请参阅 Codex MCP 文档。
Cursor
点击按钮进行安装:
或手动安装:
前往 Cursor Settings -> MCP -> Add new MCP Server。名称可自行设置,类型选择 command,命令使用 npx @playwright/mcp@latest。你也可以点击 Edit 检查配置或添加类似命令的参数。
Factory
使用 Factory CLI 添加 Playwright MCP 服务:
droid mcp add playwright "npx @playwright/mcp@latest"
或者,在 Factory droid 中输入 /mcp,打开用于管理 MCP 服务的交互式界面。
如需了解更多信息,请参阅 Factory MCP 文档。
Gemini CLI
按照 MCP 安装指南操作,并使用上面的标准配置。
Goose
点击按钮进行安装:
或手动安装:
前往 Advanced settings -> Extensions -> Add custom extension。名称可自定义,类型选择 STDIO,并将 command 设置为 npx @playwright/mcp。点击“Add Extension”。
Kiro
请参阅 MCP Servers 文档。例如,在 .kiro/settings/mcp.json 中:
{
"mcpServers": {
"playwright": {
"command": "npx",
"args": [
"@playwright/mcp@latest"
]
}
}
}
opencode
请参阅 MCP Servers 文档。例如,在 ~/.config/opencode/opencode.json 中:
{
"$schema": "https://opencode.ai/config.json",
"mcp": {
"playwright": {
"type": "local",
"command": [
"npx",
"@playwright/mcp@latest"
],
"enabled": true
}
}
}
Qodo Gen
在 VSCode 或 IntelliJ 中打开 Qodo Gen 聊天面板 → Connect more tools → + Add new MCP → 粘贴上述标准配置。
点击 Save。
VS Code
点击按钮安装:
或手动安装:
请参阅 MCP 安装指南,并使用上述标准配置。你也可以使用 VS Code CLI 安装 Playwright MCP 服务:
# For VS Code
code --add-mcp '{"name":"playwright","command":"npx","args":["@playwright/mcp@latest"]}'
安装完成后,Playwright MCP 服务即可与 VS Code 中的 GitHub Copilot Agent 配合使用。
Warp
前往 Settings -> AI -> Manage MCP Servers -> + Add,以添加 MCP 服务。使用上述标准配置。
或者,在 Warp 提示词中使用斜杠命令 /add-mcp,并粘贴上述标准配置:
{
"mcpServers": {
"playwright": {
"command": "npx",
"args": [
"@playwright/mcp@latest"
]
}
}
}
Windsurf
请参阅 Windsurf MCP 文档。使用上述标准配置。
配置
Playwright MCP 服务支持以下参数。可以将这些参数作为上述 JSON 配置中 "args" 列表的一部分提供:
> npx @playwright/mcp@latest --help
--allowed-hosts <hosts...> comma-separated list of hosts this
server is allowed to serve from.
Defaults to the host the server is bound
to. Pass '*' to disable the host check.
--block-service-workers block service workers
--browser <browser> browser or chrome channel to use,
possible values: chrome, firefox,
webkit, msedge.
--caps <caps> comma-separated list of additional
capabilities to enable, possible values:
vision, pdf.
--cdp-endpoint <endpoint> CDP endpoint to connect to.
--cdp-header <headers...> CDP headers to send with the connect
request, multiple can be specified.
--config <path> path to the configuration file.
--device <device> device to emulate, for example: "iPhone
15"
--executable-path <path> path to the browser executable.
--extension Connect to a running browser instance
(Edge/Chrome only). Requires the
"Playwright MCP Bridge" browser
extension to be installed.
--grant-permissions <permissions...> List of permissions to grant to the
browser context, for example
"geolocation", "clipboard-read",
"clipboard-write".
--headless run browser in headless mode, headed by
default
--host <host> host to bind server to. Default is
localhost. Use 0.0.0.0 to bind to all
interfaces.
--ignore-https-errors ignore https errors
--init-page <path...> path to TypeScript file to evaluate on
Playwright page object
--init-script <path...> path to JavaScript file to add as an
initialization script. The script will
be evaluated in every page before any of
the page's scripts. Can be specified
multiple times.
--isolated keep the browser profile in memory, do
not save it to disk.
--image-responses <mode> whether to send image responses to the
client. Can be "allow" or "omit",
Defaults to "allow".
--no-sandbox disable the sandbox for all process
types that are normally sandboxed.
--output-dir <path> path to the directory for output files.
--port <port> port to listen on for SSE transport.
--proxy-bypass <bypass> comma-separated domains to bypass proxy,
for example
".com,chromium.org,.domain.com"
--proxy-server <proxy> specify proxy server, for example
"http://myproxy:3128" or
"socks5://myproxy:8080"
--save-session Whether to save the Playwright MCP
session into the output directory.
--save-trace Whether to save the Playwright Trace of
the session into the output directory.
--save-video <size> Whether to save the video of the session
into the output directory. For example
"--save-video=800x600"
--secrets <path> path to a file containing secrets in the
dotenv format
--shared-browser-context reuse the same browser context between
all connected HTTP clients.
--storage-state <path> path to the storage state file for
isolated sessions.
--test-id-attribute <attribute> specify the attribute to use for test
ids, defaults to "data-testid"
--timeout-action <timeout> specify action timeout in milliseconds,
defaults to 5000ms
--timeout-navigation <timeout> specify navigation timeout in
milliseconds, defaults to 60000ms
--user-agent <ua string> specify user agent string
--user-data-dir <path> path to the user data directory. If not
specified, a temporary directory will be
created.
--viewport-size <size> specify browser viewport size in pixels,
for example "1280x720"
用户配置文件
你可以像使用普通浏览器一样,使用持久化配置文件运行 Playwright MCP(默认模式);也可以在隔离上下文中进行测试会话,或通过浏览器扩展连接到现有浏览器。
持久化配置文件
所有登录信息都会存储在持久化配置文件中。你可以在会话之间删除它,以清除离线状态。
持久化配置文件位于以下位置,你也可以使用 --user-data-dir 参数覆盖该位置。
# Windows
%USERPROFILE%\AppData\Local\ms-playwright\mcp-{channel}-profile
# macOS
- ~/Library/Caches/ms-playwright/mcp-{channel}-profile
# Linux
- ~/.cache/ms-playwright/mcp-{channel}-profile
隔离模式
在隔离模式下,每个会话都会在隔离配置文件中启动。每次你要求 MCP 关闭浏览器时,
会话都会关闭,并且该会话的所有存储状态都会丢失。你可以通过配置中的 contextOptions 或 --storage-state 参数向浏览器提供初始存储状态。点击此处了解有关存储状态的更多信息。
{
"mcpServers": {
"playwright": {
"command": "npx",
"args": [
"@playwright/mcp@latest",
"--isolated",
"--storage-state={path/to/storage.json}"
]
}
}
}
浏览器扩展
Playwright MCP Chrome Extension 可让你连接到现有浏览器标签页,并利用已登录的会话和浏览器状态。请参阅 extension/README.md,了解安装和设置说明。
初始状态
有多种方式可以为浏览器上下文或页面提供初始状态。
对于存储状态,你可以:
- 使用
--user-data-dir参数启动,并指定用户数据目录。这样会在不同会话之间保留所有浏览器数据。 - 使用
--storage-state参数启动,并指定存储状态文件。这样会将文件中的 Cookie 和本地存储加载到隔离的浏览器上下文中。
对于页面状态,你可以使用:
- 使用
--init-page指定一个 TypeScript 文件,该文件将在 Playwright 页面对象上执行。这样你便可以运行任意代码来设置页面。
// init-page.ts
export default async ({ page }) => {
await page.context().grantPermissions(['geolocation']);
await page.context().setGeolocation({ latitude: 37.7749, longitude: -122.4194 });
await page.setViewportSize({ width: 1280, height: 720 });
};
- 使用
--init-script指定一个 JavaScript 文件,该文件将作为初始化脚本添加。该脚本会在每个页面的其他脚本运行之前执行。 这适用于覆盖浏览器 API 或设置环境。
// init-script.js
window.isPlaywrightMCP = true;
配置文件
可以使用 JSON 配置文件配置 Playwright MCP 服务。你可以使用 --config 命令行选项指定配置文件:
npx @playwright/mcp@latest --config path/to/config.json
配置文件架构
{
// Browser configuration
browser?: {
// Browser type to use (chromium, firefox, or webkit)
browserName?: 'chromium' | 'firefox' | 'webkit';
// Keep the browser profile in memory, do not save it to disk.
isolated?: boolean;
// Path to user data directory for browser profile persistence
userDataDir?: string;
// Browser launch options (see Playwright docs)
// @see https://playwright.dev/docs/api/class-browsertype#browser-type-launch
launchOptions?: {
channel?: string; // Browser channel (e.g. 'chrome')
headless?: boolean; // Run in headless mode
executablePath?: string; // Path to browser executable
// ... other Playwright launch options
};
// Browser context options
// @see https://playwright.dev/docs/api/class-browser#browser-new-context
contextOptions?: {
viewport?: { width: number, height: number };
// ... other Playwright context options
};
// CDP endpoint for connecting to existing browser
cdpEndpoint?: string;
// Remote Playwright server endpoint
remoteEndpoint?: string;
},
// Server configuration
server?: {
port?: number; // Port to listen on
host?: string; // Host to bind to (default: localhost)
},
// List of additional capabilities
capabilities?: Array<
'tabs' | // Tab management
'install' | // Browser installation
'pdf' | // PDF generation
'vision' | // Coordinate-based interactions
>;
// Directory for output files
outputDir?: string;
// Network configuration
network?: {
// List of origins to allow the browser to request. Default is to allow all. Origins matching both `allowedOrigins` and `blockedOrigins` will be blocked.
allowedOrigins?: string[];
// List of origins to block the browser to request. Origins matching both `allowedOrigins` and `blockedOrigins` will be blocked.
blockedOrigins?: string[];
};
/**
* Whether to send image responses to the client. Can be "allow" or "omit".
* Defaults to "allow".
*/
imageResponses?: 'allow' | 'omit';
}
独立 MCP 服务
在没有显示器的系统上运行有头浏览器,或从 IDE 的工作进程中运行时,
请在设置了 DISPLAY 的环境中运行 MCP 服务,并传入 --port 标志以启用 HTTP 传输。
npx @playwright/mcp@latest --port 8931
然后,在 MCP 客户端配置中,将 url 设置为 HTTP 端点:
{
"mcpServers": {
"playwright": {
"url": "http://localhost:8931/mcp"
}
}
}
Docker
**注意:**目前,Docker 实现仅支持无头 Chromium。
{
"mcpServers": {
"playwright": {
"command": "docker",
"args": ["run", "-i", "--rm", "--init", "--pull=always", "mcr.microsoft.com/playwright/mcp"]
}
}
}
或者,如果你希望将容器作为长期运行的服务,而不是让 MCP 客户端启动它,请使用:
docker run -d -i --rm --init --pull=always \
--entrypoint node \
--name playwright \
-p 8931:8931 \
mcr.microsoft.com/playwright/mcp \
cli.js --headless --browser chromium --no-sandbox --port 8931
服务将在主机端口 8931 上监听,任何 MCP 客户端都可以访问它。
你也可以自行构建 Docker 镜像。
docker build -t mcr.microsoft.com/playwright/mcp .
编程方式使用
import http from 'http';
import { createConnection } from '@playwright/mcp';
import { SSEServerTransport } from '@modelcontextprotocol/sdk/server/sse.js';
http.createServer(async (req, res) => {
// ...
// Creates a headless Playwright MCP server with SSE transport
const connection = await createConnection({ browser: { launchOptions: { headless: true } } });
const transport = new SSEServerTransport('/messages', res);
await connection.connect(transport);
// ...
});
工具
核心自动化
-
browser_click
- 标题:点击
- 描述:在网页上执行点击操作
- 参数:
element(字符串):用于获取与该元素交互权限的易读元素描述ref(字符串):页面快照中的精确目标元素引用doubleClick(布尔值,可选):是否执行双击而不是单击button(字符串,可选):要点击的鼠标按键,默认为左键modifiers(数组,可选):要按下的修饰键
- 只读:false
-
browser_close
- 标题:关闭浏览器
- 描述:关闭页面
- 参数:无
- 只读:false
-
browser_console_messages
- 标题:获取控制台消息
- 描述:返回所有控制台消息
- 参数:
onlyErrors(boolean,可选):仅返回错误消息
- 只读:true
-
browser_drag
- 标题:拖动鼠标
- 描述:在两个元素之间执行拖放操作
- 参数:
startElement(string):用于获取与该元素交互权限的人类可读源元素描述startRef(string):页面快照中的精确源元素引用endElement(string):用于获取与该元素交互权限的人类可读目标元素描述endRef(string):页面快照中的精确目标元素引用
- 只读:false
-
browser_evaluate
- 标题:执行 JavaScript
- 描述:在页面或元素上执行 JavaScript 表达式
- 参数:
function(string):() => { /* code */ },或在提供元素时使用(element) => { /* code */ }element(string,可选):用于获取与该元素交互权限的人类可读元素描述ref(string,可选):页面快照中的精确目标元素引用
- 只读:false
-
browser_file_upload
- 标题:上传文件
- 描述:上传一个或多个文件
- 参数:
paths(array,可选):要上传文件的绝对路径。可以是单个文件,也可以是多个文件。如果省略,则取消文件选择器。
- 只读:false
-
browser_fill_form
- 标题:填写表单
- 描述:填写多个表单字段
- 参数:
fields(array):要填写的字段
- 只读:false
-
browser_handle_dialog
- 标题:处理对话框
- 描述:处理对话框
- 参数:
accept(boolean):是否接受对话框。promptText(string,可选):如果是提示对话框,要输入的提示文本。
- 只读:false
-
browser_hover
- 标题:悬停鼠标
- 描述:将鼠标悬停在页面元素上
- 参数:
element(string):用于获取与该元素交互权限的人类可读元素描述ref(string):页面快照中的精确目标元素引用
- 只读:false
-
browser_navigate
- 标题:导航到 URL
- 描述:导航到 URL
- 参数:
url(string):要导航到的 URL
- 只读:false
-
browser_navigate_back
- 标题:返回
- 描述:返回上一页
- 参数:无
- 只读:false
-
browser_network_requests
- 标题:列出网络请求
- 描述:返回页面加载以来的所有网络请求
- 参数:无
- 只读:true
-
browser_press_key
- 标题:按键
- 描述:按下键盘上的按键
- 参数:
key(string):要按下的按键名称或要生成的字符,例如ArrowLeft或a
- 只读:false
-
browser_resize
- 标题:调整浏览器窗口大小
- 描述:调整浏览器窗口大小
- 参数:
width(number):浏览器窗口宽度height(number):浏览器窗口高度
- 只读:false
-
browser_run_code
- 标题:运行 Playwright 代码
- 描述:运行 Playwright 代码片段
- 参数:
code(string):要运行的 Playwright 代码片段。该片段应访问page对象以与页面交互。可以包含多条语句。例如:await page.getByRole('button', { name: 'Submit' }).click();
- 只读:false
-
browser_select_option
- 标题:选择选项
- 描述:在下拉菜单中选择一个选项
- 参数:
element(string):用于获取与该元素交互权限的、人类可读的元素描述ref(string):页面快照中的精确目标元素引用values(array):要在下拉菜单中选择的值数组。可以是单个值,也可以是多个值。
- 只读:false
-
browser_snapshot
- 标题:页面快照
- 描述:捕获当前页面的无障碍快照,这比截图更好
- 参数:无
- 只读:true
-
browser_take_screenshot
- 标题:截图
- 描述:截取当前页面的截图。你不能根据截图执行操作;请使用 browser_snapshot 执行操作。
- 参数:
type(string, optional):截图的图像格式。默认为 png。filename(string, optional):用于保存截图的文件名。如果未指定,默认为page-{timestamp}.{png|jpeg}。建议使用相对文件名,以确保文件保存在输出目录中。element(string, optional):用于获取对该元素截图权限的、人类可读的元素描述。如果未提供,则截取视口。若提供了 element,也必须提供 ref。ref(string, optional):页面快照中的精确目标元素引用。如果未提供,则截取视口。若提供了 ref,也必须提供 element。fullPage(boolean, optional):为 true 时,截取整个可滚动页面,而非当前可见视口。不能与元素截图同时使用。
- 只读:true
-
browser_type
- 标题:输入文本
- 描述:在可编辑元素中输入文本
- 参数:
element(string):用于获取与该元素交互权限的、人类可读的元素描述ref(string):页面快照中的精确目标元素引用text(string):要输入到元素中的文本submit(boolean, optional):是否提交输入的文本(之后按 Enter)slowly(boolean, optional):是否逐字输入。适用于触发页面中的按键处理程序。默认情况下会一次性填入整个文本。
- 只读:false
-
browser_wait_for
- 标题:等待
- 描述:等待文本出现或消失,或等待指定时长
- 参数:
time(number, optional):等待时间(秒)text(string, optional):要等待出现的文本textGone(string, optional):要等待消失的文本
- 只读:false
标签页管理
- browser_tabs
- 标题:管理标签页
- 描述:列出、创建、关闭或选择浏览器标签页。
- 参数:
action(string):要执行的操作index(number, optional):用于关闭或选择的标签页索引。关闭时若省略,则关闭当前标签页。
- 只读:false
浏览器安装
- browser_install
- 标题:安装浏览器
- 描述:安装配置中指定的浏览器。如果出现浏览器未安装的错误,请调用此工具。
- 参数:无
- 只读:false
基于坐标(通过 --caps=vision 启用)
-
browser_mouse_click_xy
- 标题:点击
- 描述:在给定位置单击鼠标左键
- 参数:
element(string):用于获取与该元素交互权限的、人类可读的元素描述x(number):X 坐标y(number):Y 坐标
- 只读:false
-
browser_mouse_drag_xy
- Title:拖动鼠标
- Description:将鼠标左键拖动到指定位置
- Parameters:
element(string):用于获取与该元素交互权限的易于理解的元素描述startX(number):起始 X 坐标startY(number):起始 Y 坐标endX(number):结束 X 坐标endY(number):结束 Y 坐标
- Read-only:false
-
browser_mouse_move_xy
- Title:移动鼠标
- Description:将鼠标移动到指定位置
- Parameters:
element(string):用于获取与该元素交互权限的易于理解的元素描述x(number):X 坐标y(number):Y 坐标
- Read-only:false
PDF 生成(通过 --caps=pdf 选择启用)
- browser_pdf_save
- Title:另存为 PDF
- Description:将页面另存为 PDF
- Parameters:
filename(string, optional):要将 PDF 保存到的文件名。如果未指定,默认使用page-{timestamp}.pdf。建议使用相对文件名,以确保文件保存在输出目录中。
- Read-only:true
测试断言(通过 --caps=testing 选择启用)
-
browser_generate_locator
- Title:为元素创建定位器
- Description:为指定元素生成用于测试的定位器
- Parameters:
element(string):用于获取与该元素交互权限的易于理解的元素描述ref(string):页面快照中目标元素的精确引用
- Read-only:true
-
browser_verify_element_visible
- Title:验证元素可见
- Description:验证元素在页面上可见
- Parameters:
role(string):元素的 ROLE。可在快照中通过以下形式找到:- {ROLE} "Accessible Name":accessibleName(string):元素的 ACCESSIBLE_NAME。可在快照中通过以下形式找到:- role "{ACCESSIBLE_NAME}"
- Read-only:false
-
browser_verify_list_visible
- Title:验证列表可见
- Description:验证列表在页面上可见
- Parameters:
element(string):易于理解的列表描述ref(string):指向该列表的目标元素精确引用items(array):要验证的项目
- Read-only:false
-
browser_verify_text_visible
- Title:验证文本可见
- Description:验证文本在页面上可见。如果可行,优先使用 browser_verify_element_visible。
- Parameters:
text(string):要验证的 TEXT。可在快照中通过以下形式找到:- role "Accessible Name": {TEXT}或- text: {TEXT}
- Read-only:false
-
browser_verify_value
- Title:验证值
- Description:验证元素值
- Parameters:
type(string):元素类型element(string):易于理解的元素描述ref(string):指向该元素的目标元素精确引用value(string):要验证的值。对于复选框,使用 "true" 或 "false"。
- Read-only:false
跟踪(通过 --caps=tracing 选择启用)
-
browser_start_tracing
- Title:开始跟踪
- Description:开始记录跟踪
- Parameters:无
- Read-only:true
-
browser_stop_tracing
- Title:停止跟踪
- Description:停止记录跟踪
- Parameters:无
- Read-only:true
评论
一个开放协议,规定了 AI 应用如何以统一方式连接外部工具与数据源。客户端负责与模型对话,MCP 服务负责提供工具、资源和提示模板,两者通过标准消息通信,同一个服务可以被不同客户端复用。