MCP Server是一个基于WebSocket的浏览器自动化服务器,它提供了一个简单的接口来控制Chrome浏览器。
- 基于WebSocket的通信协议
- 支持多会话管理
- 基于命令的API设计
- 支持浏览器启动和关闭
- 支持页面导航和交互
- 支持截图和DOM操作
- 支持JavaScript执行
# 克隆仓库
git clone https://github.com/yourusername/mcp-server.git
cd mcp-server
# 安装依赖
npm install
# 构建项目
npm run buildnpm start默认情况下,服务器将在http://127.0.0.1:3000上启动,WebSocket服务器将在同一地址上可用。
可以通过环境变量配置服务器:
PORT: HTTP服务器端口(默认:3000)HOST: HTTP服务器主机(默认:127.0.0.1)STATIC_PATH: 静态文件目录(默认:./public)HEADLESS: 是否以无头模式运行浏览器(默认:true)VIEWPORT_WIDTH: 浏览器视口宽度(默认:1280)VIEWPORT_HEIGHT: 浏览器视口高度(默认:800)LOG_LEVEL: 日志级别(默认:info)
所有WebSocket消息都是JSON格式,具有以下结构:
{
"messageId": "唯一消息ID",
"type": "消息类型",
"action": "操作名称",
"payload": "操作参数",
"timestamp": "时间戳"
}command: 客户端发送的命令response: 服务器对命令的响应notification: 服务器发送的通知error: 服务器发送的错误
{
"messageId": "msg1",
"type": "command",
"action": "browser.launch",
"payload": {
"headless": true
}
}{
"messageId": "msg2",
"type": "command",
"action": "page.navigate",
"payload": {
"url": "https://www.example.com",
"waitUntil": "networkidle0"
}
}{
"messageId": "msg3",
"type": "command",
"action": "element.click",
"payload": {
"selector": "#submit-button"
}
}{
"messageId": "msg4",
"type": "command",
"action": "element.type",
"payload": {
"selector": "#search-input",
"text": "搜索内容"
}
}{
"messageId": "msg5",
"type": "command",
"action": "page.screenshot",
"payload": {
"fullPage": true,
"encoding": "base64"
}
}{
"messageId": "msg6",
"type": "command",
"action": "execute.script",
"payload": {
"script": "return document.title",
"args": []
}
}除了JSON格式外,MCP Server还支持简单的文本命令格式:
action:param1=value1,param2=value2
例如:
page.navigate:url=https://www.example.com,waitUntil=networkidle0
element.click:selector=#submit-button
npm run devnpm testnpm run lintMIT