使用说明
剧作家MCP服务器
基于Python的MCP(模型上下文协议)服务器,用于浏览器自动化 剧作家 和 FastMCP.
特性
- 多浏览器支持:Chromium、Firefox和WebKit
- 会话管理:具有持久会话的多个浏览器页面
- 导航:转到URL,后退/前进,重新加载
- 元素交互:单击、填写、键入、悬停、选择选项、选中/取消选中
- 内容提取:获取文本、HTML、属性、查询元素
- 截图:整页、视口或元素屏幕截图
- 键盘支持:按键、组合键
- 等待实用程序:等待选择器、加载状态、URL
- JavaScript评估:在浏览器上下文中执行自定义JS
- 文件上传:将文件上传到输入元素
- PDF导出:将页面另存为PDF(仅限Chromium)
- 对话框处理:处理警报、确认和提示对话框
安装
先决条件
- Python 3.10或更高版本
- pip或uv包管理器
从源代码安装
# Clone or navigate to the project
cd Project_MCP
# Install dependencies
pip install -e .
# Install Playwright browsers
python -m playwright install
仅安装依赖项
pip install -r requirements.txt
python -m playwright install
用法
运行服务器
服务器支持两种传输模式: 标准 (默认)和 上海证券交易所 (服务器发送的事件)。
标准传输(默认)
# Run directly
python -m playwright_mcp.server
# Or use the installed command
playwright-mcp
# Or with FastMCP CLI
fastmcp run src/playwright_mcp/server.py
SSE传输(HTTP服务器)
SSE传输将MCP服务器作为HTTP服务器运行,这对于基于web的客户端或远程连接非常有用。
# Run with SSE on default host (0.0.0.0) and port (8000)
python -m playwright_mcp.server --sse
# Run with custom port
python -m playwright_mcp.server --sse --port=3000
# Run with custom host and port
python -m playwright_mcp.server --sse --host=127.0.0.1 --port=8080
# Or use the dedicated SSE command (default: 0.0.0.0:8000)
playwright-mcp-sse
SSE端点:
GET /sse-SSE事件流POST /messages-向服务器发送消息
Claude桌面配置
添加到您的Claude Desktop配置文件中:
macOS: ~/Library/Application Support/Claude/claude_desktop_config.json
视窗: %APPDATA%\Claude\claude_desktop_config.json
{
"mcpServers": {
"playwright": {
"command": "python",
"args": ["-m", "playwright_mcp.server"],
"cwd": "/path/to/Project_MCP"
}
}
}
或使用紫外线:
{
"mcpServers": {
"playwright": {
"command": "uv",
"args": ["run", "playwright-mcp"],
"cwd": "/path/to/Project_MCP"
}
}
}
可用工具
浏览器管理
| 工具 | 说明 |
|---|---|
browser_launch |
启动浏览器(chromium/firefox/webkit) |
browser_close |
关闭浏览器并清理资源 |
page_new |
创建新的浏览器选项卡/页面 |
page_close |
关闭特定页面 |
page_list |
列出所有打开的页面 |
导航
| 工具 | 说明 |
|---|---|
navigate |
导航到URL |
go_back |
返回历史记录 |
go_forward |
在历史中向前导航 |
reload |
重新加载当前页面 |
元素交互
| 工具 | 说明 |
|---|---|
click |
单击元素 |
fill |
在输入框中填写文本 |
type_text |
逐个字符键入文本 |
press_key |
按键盘键 |
hover |
将鼠标悬停在元素上 |
select_option |
选择下拉选项 |
check |
选中/取消选中复选框或单选框 |
focus |
聚焦一个元素 |
内容提取
| 工具 | 说明 |
|---|---|
get_text |
获取元素的文本内容 |
get_inner_html |
获取元素的内部HTML |
get_attribute |
获取元素属性值 |
get_page_content |
获取整页HTML |
query_selector_all |
查找所有匹配的元素 |
截图
| 工具 | 说明 |
|---|---|
screenshot |
截图(返回base64) |
screenshot_to_file |
将屏幕截图保存到文件 |
等待实用程序
| 工具 | 说明 |
|---|---|
wait_for_selector |
等待元素状态 |
wait_for_load_state |
等待页面加载状态 |
wait_for_url |
等待URL模式 |
凭证管理
| 工具 | 说明 |
|---|---|
get_credentials |
从env-vars获取域的用户名/密码 |
list_credential_env_vars |
列出所有配置的凭据环境变量 |
高级
| 工具 | 说明 |
|---|---|
evaluate |
执行JavaScript |
save_as_pdf |
将页面另存为PDF |
scroll |
滚动页面 |
set_viewport |
设置视口大小 |
handle_dialog |
处理JS对话框 |
upload_file |
上传文件 |
is_visible |
检查元件可见性 |
is_enabled |
检查元件是否启用 |
is_checked |
检查复选框是否已选中 |
示例工作流
基本导航和屏幕截图
1. browser_launch(browser_type="chromium", headless=true)
2. navigate(url="https://example.com")
3. screenshot(page_id="page_1", full_page=true)
4. browser_close()
表格填写
1. browser_launch()
2. navigate(url="https://example.com/login")
3. fill(page_id="page_1", selector="#username", value="user@example.com")
4. fill(page_id="page_1", selector="#password", value="password123")
5. click(page_id="page_1", selector="button[type='submit']")
6. wait_for_url(page_id="page_1", url_pattern="**/dashboard")
数据提取
1. browser_launch()
2. navigate(url="https://news.ycombinator.com")
3. query_selector_all(page_id="page_1", selector=".titleline > a")
4. get_text(page_id="page_1", selector=".titleline")
使用存储的凭据进行身份验证
1. get_credentials(domain="github.com") # Returns username & password from env vars
2. browser_launch()
3. navigate(url="https://github.com/login")
4. fill(page_id="page_1", selector="#login_field", value=<username from step 1>)
5. fill(page_id="page_1", selector="#password", value=<password from step 1>)
6. click(page_id="page_1", selector="input[type='submit']")
凭证管理
使用基于域的命名模式将凭据安全地存储在环境变量中。
环境变量模式
对于域,删除所有非字母数字字符并附加 _username 或 _password:
| 域 | 用户名环境变量 | 密码环境变量 |
|---|---|---|
github.com |
githubcom_username |
githubcom_password |
example.com |
examplecom_username |
examplecom_password |
my-app.io |
myappio_username |
myappio_password |
login.site.org |
loginsiteorg_username |
loginsiteorg_password |
sub.domain.co.uk |
subdomaincouk_username |
subdomaincouk_password |
设置凭据
选项1:在shell中导出
export githubcom_username="your-username"
export githubcom_password="your-password"
选项2:使用.env文件 (使用类似工具 dotenv)
# .env file
githubcom_username=your-username
githubcom_password=your-password
examplecom_username=user@example.com
examplecom_password=secret123
选项3:在Claude Desktop配置中设置
{
"mcpServers": {
"playwright": {
"command": "python",
"args": ["-m", "playwright_mcp.server"],
"env": {
"githubcom_username": "your-username",
"githubcom_password": "your-password"
}
}
}
}
凭证工具
get_credentials(domain="github.com")-检索域的用户名和密码list_credential_env_vars()-列出所有已配置的凭据环境变量(仅名称,不包括值)
选择器类型
这些工具支持各种选择器类型:
- 层叠样式表:
button.submit,#login-btn,[data-testid='submit'] - XPath:
xpath=//button[@type='submit'] - 文本:
text=Sign In,text=Click here - 角色:
role=button[name='Submit']
发展
运行测试
pip install -e ".[dev]"
pytest
项目结构
Project_MCP/
├── src/
│ └── playwright_mcp/
│ ├── __init__.py
│ └── server.py
├── pyproject.toml
├── requirements.txt
└── README.md
许可证
MIT许可证