浏览器自动化服务器

一个基于Playwright的浏览器自动化服务器,支持REST API和MCP协议,部署在Google Cloud Run上,提供网页导航、截图、表单填写等功能。

  • by Matt-MFG
  • HTML
  • 本地部署
  • 更新于
在 GitHub 查看 matt-mfg/browser-use-mcp

使用说明

浏览器使用MCP服务器

使用Playwright实现浏览器自动化的模型上下文协议(MCP)服务器,部署在Google Cloud Run上。该项目通过REST API和MCP协议端点提供浏览器自动化功能。

特性

  • 🌐 浏览器自动化工具:

    • 导航到URL
    • 截图
    • 点击元素
    • 填写表单字段
    • 阅读页面内容
  • 🔧 双协议支持:

    • 用于直接HTTP访问的REST API端点
    • 用于AI代理集成的MCP协议
  • ☁️ 云原生设计:

    • 部署在Google Cloud Run上
    • Docker容器化
    • 自动扩展和无服务器
  • 🔒 安全功能:

    • 域分配列表
    • Google Cloud身份令牌身份验证
    • 审核日志记录

快速开始

地方发展

  1. 克隆存储库:
git clone https://github.com/yourusername/browser-use-mcp.git
cd browser-use-mcp
  1. 设置Python环境:
python3 -m venv .venv
source .venv/bin/activate  # On Windows: .venv\Scripts\activate
pip install -r requirements.txt
playwright install chromium
  1. 运行MCP服务器:
uvicorn app:app --host 0.0.0.0 --port 8080
  1. 使用聊天界面进行测试:
# In another terminal
python3 -m http.server 8090
# Open http://localhost:8090/chat_interface_v2.html

使用MCP服务器

REST API示例

# Take a screenshot
curl -X POST http://localhost:8080/mcp/tools/screenshot \
  -H "Content-Type: application/json" \
  -d '{"url": "https://example.com", "full_page": false}'

# Navigate to a URL
curl -X POST http://localhost:8080/mcp/tools/navigate \
  -H "Content-Type: application/json" \
  -d '{"url": "https://github.com"}'

# Read page content
curl -X POST http://localhost:8080/mcp/tools/read \
  -H "Content-Type: application/json" \
  -d '{"url": "https://example.com", "selector": "h1"}'

Python客户端示例

import asyncio
import aiohttp

async def take_screenshot():
    async with aiohttp.ClientSession() as session:
        async with session.post(
            "http://localhost:8080/mcp/tools/screenshot",
            json={"url": "https://github.com", "full_page": False}
        ) as resp:
            result = await resp.json()
            # result contains base64 encoded screenshot

asyncio.run(take_screenshot())

部署

谷歌云运行

  1. 构建和部署:
gcloud builds submit --config cloudbuild.yaml
  1. 环境变量:
    • ALLOWED_DOMAINS:逗号分隔的允许域列表
    • LOG_LEVEL:日志记录级别(默认值:INFO)

项目结构

browser-use-mcp/
├── app.py                    # Main FastAPI application
├── mcp_tools.py             # Browser automation tool implementations
├── browser_manager.py       # Centralized browser lifecycle management
├── security_middleware.py   # Google Cloud logging integration
├── chat_interface_v2.html   # Web-based chat interface
├── requirements.txt         # Python dependencies
├── Dockerfile              # Container configuration
├── cloudbuild.yaml         # CI/CD pipeline
└── agents/                 # ADK agent configurations

可用工具

截图

捕获网页的屏幕截图。

{
  "url": "https://example.com",
  "full_page": false
}

导航

导航到URL并返回页面信息。

{
  "url": "https://example.com"
}

点击

单击页面上的元素。

{
  "url": "https://example.com",
  "selector": "button#submit",
  "return_screenshot": false
}

填充

用文本填充表单字段。

{
  "url": "https://example.com",
  "selector": "input[name='email']",
  "text": "user@example.com",
  "return_screenshot": false
}

阅读

从页面中提取文本内容。

{
  "url": "https://example.com",
  "selector": "article"
}

发展

运行测试

python test_all_tools.py

构建Docker镜像

docker build -t browser-use-mcp .
docker run -p 8080:8080 browser-use-mcp

故障排除

常见问题

  1. 浏览器中的 CORS 错误:服务器包括CORS中间件。确保您从允许的来源访问。

  2. Playwright浏览器问题:确保安装了Chromium:

    playwright install chromium
    
  3. 端口已在使用中:检查正在运行的进程:

    lsof -i :8080
    

贡献

  1. 分叉存储库
  2. 创建要素分支
  3. 提交您的更改
  4. 推到分支
  5. 创建拉取请求

许可证

此项目根据MIT许可证获得许可-有关详细信息,请参阅许可证文件。

致谢