使用MCP服务器控制浏览器的AI代理

简介

MCP(Multi-Control Protocol)服务器是一种允许AI代理控制浏览器的工具,通过使用browser-use包实现浏览器自动化。它支持两种传输模式:SSE(Server-Sent Events)和stdio。

基础知识点

  1. 浏览器自动化:使用Playwright等工具来控制浏览器的行为,如打开网页、点击按钮等。
  2. MCP协议:一种用于AI代理与浏览器之间通信的协议,支持SSE和stdio两种传输方式。
  3. VNC流媒体:允许实时观看浏览器自动化过程。

安装步骤

1. 安装依赖

首先,需要安装uv工具和mcp-proxy:

bash 复制代码
curl -LsSf https://astral.sh/uv/install.sh | sh
uv tool install mcp-proxy
uv tool update-shell

2. 配置环境

创建一个.env文件,设置必要的环境变量:

bash 复制代码
OPENAI_API_KEY=your-api-key
CHROME_PATH=optional/path/to/chrome
PATIENT=false  # 设置为true时,API调用将等待任务完成

3. 安装依赖包

使用uv安装必要的Python包:

bash 复制代码
uv sync
uv pip install playwright
uv run playwright install --with-deps --no-shell chromium

运行服务器

SSE模式

直接从源代码运行服务器:

bash 复制代码
uv run server --port 8000

stdio模式

  1. 构建并安装全局包:
bash 复制代码
uv build
uv tool uninstall browser-use-mcp-server 2>/dev/null || true
uv tool install dist/browser_use_mcp_server-*.whl
  1. 运行服务器:
bash 复制代码
browser-use-mcp-server run server --port 8000 --stdio --proxy-port 9000

客户端配置

SSE模式客户端配置

json 复制代码
{
  "mcpServers": {
    "browser-use-mcp-server": {
      "url": "http://localhost:8000/sse"
    }
  }
}

stdio模式客户端配置

json 复制代码
{
  "mcpServers": {
    "browser-server": {
      "command": "browser-use-mcp-server",
      "args": [
        "run",
        "server",
        "--port",
        "8000",
        "--stdio",
        "--proxy-port",
        "9000"
      ],
      "env": {
        "OPENAI_API_KEY": "your-api-key"
      }
    }
  }
}

Docker部署

使用Docker可以提供一个一致且隔离的环境来运行服务器。

构建Docker镜像

perl 复制代码
bash
docker build -t browser-use-mcp-server .

运行容器

bash 复制代码
docker run --rm -p8000:8000 -p5900:5900 browser-use-mcp-server

自定义VNC密码

创建一个包含密码的文件,然后将其挂载到容器中:

bash 复制代码
echo "your-secure-password" > vnc_password.txt
docker run --rm -p8000:8000 -p5900:5900 \
  -v $(pwd)/vnc_password.txt:/run/secrets/vnc_password:ro \
  browser-use-mcp-server

VNC查看器

使用noVNC作为浏览器查看器:

bash 复制代码
git clone https://github.com/novnc/noVNC
cd noVNC
./utils/novnc_proxy --vnc localhost:5900

默认密码为browser-use,除非使用自定义密码方法覆盖。

示例用法

尝试使用AI代理打开网页并获取顶级文章:

bash 复制代码
open https://news.ycombinator.com and return the top ranked article
相关推荐
OpenTiny社区5 分钟前
我用Vue 100%复制DeepSeek开源的网页App,代码更少,功能还更多!
前端·vue.js·github
乌暮7 分钟前
少写重复 CRUD:用大白话搞懂 MyBatis-Plus
java·开发语言·spring boot·面试·mybatis
云起SAAS25 分钟前
手机靓号选号商城H5源码 完整前后台可直接运行
前端·javascript·后端
CoderYanger29 分钟前
Java EE 进阶:9.MyBatis 操作数据库(进阶)(第四弹):图书管理系统(用户登录+添加图书)
java·数据库·spring boot·mysql·面试·java-ee·mybatis
Bigger31 分钟前
我扒了 GitHub Trending 前 20,8 个在做同一件事:给 Coding Agent 造基础设施
开源·github·ai编程
the局外人1 小时前
Python 实战检索优化:BM25、向量与重排
数据库·后端·python
苏三说技术1 小时前
分库分表要被淘汰了?
后端
__underline__1 小时前
冷门Python知识点006篇:Python 类体里的名字,能被"凭空造出来"
后端·python
nice_lcj5201 小时前
【从零理解计算机网络 · 05】TCP 四次挥手:为什么挥手要四次、TIME_WAIT 在等什么
后端·tcp/ip·计算机网络·面试·tcp·四次挥手·time_wait
Moment1 小时前
RSI 是怎样让 AI 改进自己的 ❓❓❓
前端·后端·面试