MCP Web自动化代理
使用智能网络自动化 模型上下文协议(MCP) 和 LangChain代理商 和 深度智能体 通过Playwright进行浏览器控制。
🌟 特性
- ✅ 两种代理类型:标准代理和深度代理(高级推理)
- 🌐 Web自动化:使用Playwright MCP服务器进行完全浏览器控制
- 🧠 AI驱动:浏览器操作的自然语言
- 🔄 智能规划:快照→ PLAN → ACT → 验证循环
- 🛡️ 安全:在采取不可逆转的行动之前进行确认
- 📧 多站点支持:Gmail、TripAdvisor、亚马逊和任何网站
______________________________________________________________________
📋 先决条件
- Python 3.8+
- OpenAI API密钥
- MCP服务器在端口8931上运行(或自行配置)
______________________________________________________________________
🚀 快速开始
1.️⃣ 克隆仓库
git clone
cd mcp2.️⃣ 创建虚拟环境
python -m venv venv
source venv/bin/activate # On Windows: venv\Scripts\activate3.️⃣ 安装依赖项
pip install -r requirements.txt4.️⃣ 设置环境变量
创建一个 .env 文件在 mcp 目录:
cat > .env << 'EOF'
OPENAI_API_KEY=your-openai-api-key-here
EOF5.️⃣ 设置并启动Playwright MCP服务器
确保您的Playwright MCP服务器在端口8931上运行:
# 1. Clone the Playwright MCP repository
git clone https://github.com/microsoft/playwright-mcp.git
cd playwright-mcp
# 2. Install dependencies
npx playwright install --with-deps
# 3.Start the MCP server on port 8931
npx @playwright/mcp@latest --port 8931验证MCP服务器是否正在运行
# Check if server is accessible
curl http://localhost:8931/mcp
# You should see a response indicating the MCP server is running保持此终端运行 -代理运行时,MCP服务器需要保持活动状态!
______________________________________________________________________
🎯 运行代理
选项1:标准代理(client.py)
最佳:一般web自动化任务
python client.py在哪里修改查询:
- 文件:
client.py - 线路: 139
- 位置:
{"role": "user", "content": "YOUR QUERY HERE"}查询示例:
# Gmail automation
"go to gmail and login with credentials user@example.com and password yourpass, then draft an email to recipient@example.com with subject 'Test' and body 'Hello'"
# Web search
"go to tripadvisor.in and search for hotels in delhi"
# General browsing
"navigate to amazon.in and search for wireless mouse"______________________________________________________________________
选项2:DeepAgent(client_deep_agent.py)
最佳:需要深入推理的复杂任务
python client_deep_agent.py在哪里修改查询:
- 文件:
client_deep_agent.py - 线路: 110
- 位置:
user_query = "YOUR QUERY HERE"查询示例:
# Travel planning
"go to tripadvisor.in and search for travel package for manali"
# Product research
"search for best laptops under 50000 on amazon and compare top 3"
# Multi-step tasks
"go to booking.com, search for hotels in Paris for next month, and get prices for top 5 hotels"______________________________________________________________________
📝 定制指南
更改查询
对于 client.py:
- 打开
client.py - 找到第139行:
{"role": "user", "content": "YOUR NEW QUERY HERE"}- 替换为您的任务
- 保存并运行:
python client.py
对于 client_deep_agent.py:
- 打开
client_deep_agent.py - 找到第110行:
user_query = "YOUR NEW QUERY HERE"- 替换为您的任务
- 保存并运行:
python client_deep_agent.py
______________________________________________________________________
更改MCP服务器URL
这两个文件都使用: http://localhost:8931/mcp
要更改它,请修改两个文件中的第60行:
client = MultiServerMCPClient({
"playwright": {"transport": "http", "url": "http://YOUR_SERVER:PORT/mcp"}
})______________________________________________________________________
🧠 代理比较
| 功能 | 标准代理 | 深度代理 |
|---|---|---|
| 文件 | client.py | client_deep_agent.py |
| 速度 | ⚡ 快速 | 🐢 更慢(思考更多) |
| 推理 | 基础 | 🧠 深度多步 |
| 规划 | 简单 | 📋 高级 |
| 最适合 | 快速任务 | 复杂研究 |
______________________________________________________________________
📖 系统提示功能
两个代理都使用高级系统提示:
1. 操作回路
SNAPSHOT → PLAN → ACT → VERIFY → (repeat)2. 安全规则
- 仅参考交互(无选择器猜测)
- 不可逆行动前的确认
- 验证码检测和暂停
3. 机敏性
- 表单字段目的映射
- 多步骤表单处理
- 使用替代方案进行错误恢复
- 安全意识(无密码泄露)
4. 特殊剧本
- 电子邮件撰写工作流程(如有需要,可在system_prompt中自定义)
- 搜索和提取
- 登录程序
______________________________________________________________________
📚 项目结构
Ideafoundation/
├── mcp/ # Main agent directory
│ ├── client.py # Standard agent
│ ├── client_deep_agent.py # DeepAgent (advanced)
│ ├── get_tools.py # Tool listing utility
│ ├── requirements.txt # Python dependencies
│ ├── .env # Environment variables (create this)
│ ├── README.md # This file
│ └── venv/ # Virtual environment
│
└── playwright-mcp/ # MCP Server (clone separately)
├── dist/ # Compiled files
├── src/ # Source code
├── package.json # NPM dependencies
└── index.js # MCP server entry point备注:克隆 playwright-mcp 在父目录中单独存储库
______________________________________________________________________
