Skip to content

springnick/mcp01

Repository files navigation

MCP Server (Managed Chrome Protocol)

MCP Server是一个基于WebSocket的浏览器自动化服务器,它提供了一个简单的接口来控制Chrome浏览器。

功能特点

  • 基于WebSocket的通信协议
  • 支持多会话管理
  • 基于命令的API设计
  • 支持浏览器启动和关闭
  • 支持页面导航和交互
  • 支持截图和DOM操作
  • 支持JavaScript执行

安装

# 克隆仓库
git clone https://github.com/yourusername/mcp-server.git
cd mcp-server

# 安装依赖
npm install

# 构建项目
npm run build

使用方法

启动服务器

npm 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)

API参考

WebSocket消息格式

所有WebSocket消息都是JSON格式,具有以下结构:

{
  "messageId": "唯一消息ID",
  "type": "消息类型",
  "action": "操作名称",
  "payload": "操作参数",
  "timestamp": "时间戳"
}

消息类型

  • command: 客户端发送的命令
  • response: 服务器对命令的响应
  • notification: 服务器发送的通知
  • error: 服务器发送的错误

命令示例

启动浏览器

{
  "messageId": "msg1",
  "type": "command",
  "action": "browser.launch",
  "payload": {
    "headless": true
  }
}

导航到URL

{
  "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"
  }
}

执行JavaScript

{
  "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 dev

运行测试

npm test

代码检查

npm run lint

许可证

MIT

About

broswer mcp servver

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors