MCP Web 浏览器服务器
由 Playwright 提供支持的模型上下文协议 (MCP) 的高级 Web 浏览服务器,通过灵活、安全的 API 实现无头浏览器交互。
🌐 功能
- 无头 Web 浏览:通过 SSL 证书验证绕过导航到任何网站
- 整页内容提取:检索完整的 HTML 内容,包括动态加载的 JavaScript
- 多标签支持:创建、管理和在多个浏览器标签之间切换
- 高级Web交互工具:
- 提取文本内容
- 点击页面元素
- 在表单字段中输入文本
- 截取屏幕截图
- 使用过滤功能提取页面链接
- 向任意方向滚动页面
- 在页面上执行 JavaScript
- 刷新页面
- 等待导航完成
- 资源管理:闲置后自动清理未使用的资源
- 增强页面信息:获取有关当前页面的详细元数据
🚀 快速入门
先决条件
安装
# Install MCP and Playwright
pip install mcp playwright
# Install browser dependencies
playwright install
Claude Desktop 的配置
添加到您的claude_desktop_config.json
:
{
"mcpServers": {
"web-browser": {
"command": "python",
"args": [
"/path/to/your/server.py"
]
}
}
}
💡 使用示例
基本网页导航
# Browse to a website
page_content = browse_to("https://example.com")
# Extract page text
text_content = extract_text_content()
# Extract text from a specific element
title_text = extract_text_content("h1.title")
网络互动
# Navigate to a page
browse_to("https://example.com/login")
# Input text into a form
input_text("#username", "your_username")
input_text("#password", "your_password")
# Click a login button
click_element("#login-button")
屏幕截图
# Capture full page screenshot
full_page_screenshot = get_page_screenshots(full_page=True)
# Capture specific element screenshot
element_screenshot = get_page_screenshots(selector="#main-content")
链接提取
# Get all links on the page
page_links = get_page_links()
# Get links matching a pattern
filtered_links = get_page_links(filter_pattern="contact")
多标签浏览
# Create a new tab
tab_id = create_new_tab("https://example.com")
# Create another tab
another_tab_id = create_new_tab("https://example.org")
# List all open tabs
tabs = list_tabs()
# Switch between tabs
switch_tab(tab_id)
# Close a tab
close_tab(another_tab_id)
高级交互
# Scroll the page
scroll_page(direction="down", amount="page")
# Execute JavaScript on the page
result = execute_javascript("return document.title")
# Get detailed page information
page_info = get_page_info()
# Refresh the current page
refresh_page()
# Wait for navigation to complete
wait_for_navigation(timeout_ms=5000)
🛡️ 安全功能
- SSL 证书验证绕过
- 安全浏览器上下文管理
- 自定义用户代理配置
- 错误处理和全面日志记录
- 可配置超时设置
- CSP旁路控制
- 防止 cookie 被窃取
🔧 故障排除
常见问题
- SSL 证书错误:自动绕过
- 页面加载缓慢:调整
browse_to()
方法中的超时时间 - 未找到元素:仔细验证选择器
- 浏览器资源使用情况:闲置一段时间后自动清理
日志记录
所有重要事件均记录详细信息,以便于调试。
📋 工具参数
browse_to(url: str, context: Optional[Any] = None)
url
:要导航到的网站context
:可选上下文对象(当前未使用)
selector
:可选的 CSS 选择器,用于提取特定内容context
:可选上下文对象(当前未使用)
click_element(selector: str, context: Optional[Any] = None)
selector
:要点击的元素的 CSS 选择器context
:可选上下文对象(当前未使用)
get_page_screenshots(full_page: bool = False, selector: Optional[str] = None, context: Optional[Any] = None)
full_page
:捕获整个页面截图selector
:屏幕截图的可选元素context
:可选上下文对象(当前未使用)
get_page_links(filter_pattern: Optional[str] = None, context: Optional[Any] = None)
filter_pattern
:用于过滤链接的可选文本模式context
:可选上下文对象(当前未使用)
input_text(selector: str, text: str, context: Optional[Any] = None)
selector
:输入元素的 CSS 选择器text
:要输入的文本context
:可选上下文对象(当前未使用)
create_new_tab(url: Optional[str] = None, context: Optional[Any] = None)
url
:在新标签页中导航到的可选 URLcontext
:可选上下文对象(当前未使用)
switch_tab(tab_id: str, context: Optional[Any] = None)
tab_id
:要切换到的选项卡的 IDcontext
:可选上下文对象(当前未使用)
list_tabs(context: Optional[Any] = None)
close_tab(tab_id: Optional[str] = None, context: Optional[Any] = None)
tab_id
:要关闭的选项卡的可选 ID(默认为当前选项卡)context
:可选上下文对象(当前未使用)
refresh_page(context: Optional[Any] = None)
get_page_info(context: Optional[Any] = None)
scroll_page(direction: str = "down", amount: str = "page", context: Optional[Any] = None)
direction
:滚动方向(‘向上’,‘向下’,‘向左’,‘向右’)amount
:滚动量(‘页’、‘一半’或数字)context
:可选上下文对象(当前未使用)
wait_for_navigation(timeout_ms: int = 10000, context: Optional[Any] = None)
timeout_ms
:等待的最长时间(以毫秒为单位)context
:可选上下文对象(当前未使用)
execute_javascript(script: str, context: Optional[Any] = None)
script
:要执行的 JavaScript 代码context
:可选上下文对象(当前未使用)
🤝 贡献
欢迎贡献代码!欢迎提交 Pull 请求。
开发设置
# Clone the repository
git clone https://github.com/random-robbie/mcp-web-browser.git
# Create virtual environment
python -m venv venv
source venv/bin/activate # On Windows use `venv\Scripts\activate`
# Install dependencies
pip install -e .[dev]
📄 许可证
MIT 许可证
🔗 相关项目
💬 支持
如有问题或疑问,请在 GitHub 上打开问题。