HTML MCP服务器Go
MCP(模型上下文协议)服务器的高性能Go实现,用于从网页中获取和提取内容,包括JavaScript渲染的内容和Docsify文档网站。使用Go Fiber v2的清洁架构原则构建。
特性
- 双模式操作:同时用作MCP服务器(JSON-RPC)和REST API服务器
- HTML内容提取:从静态HTML页面提取文本内容
- JavaScript渲染:支持SPA和使用Chrome/Chromium动态渲染的内容
- 文档支持:Docsify文档网站的专业解析
- 清洁建筑:具有独立关注点的领域驱动设计
- 高性能:使用Go Fiber v2和异步处理构建
- Docker就绪:带有健康检查的容器化部署
- CORS支持:为API模式启用交叉原始请求
快速开始
先决条件
- 转到1.21或更高版本
- Chrome/Chromium(用于JavaScript渲染)
- Docker(可选,用于容器化部署)
地方发展
- 克隆和构建:
git clone
cd html-mcp-server-go
go mod download
go build -o html-mcp-server ./cmd- 作为REST API服务器运行:
./html-mcp-server -mode=api -port=8085- 测试API:
# Health check
curl http://localhost:8085/health
# Fetch regular webpage
curl -X POST http://localhost:8085/api/fetch \
-H "Content-Type: application/json" \
-d '{"url": "https://example.com"}'
# Fetch with JavaScript rendering
curl -X POST http://localhost:8085/api/fetch \
-H "Content-Type: application/json" \
-d '{"url": "https://docs.example.com", "use_js_rendering": true}'
# Fetch Docsify site
curl -X POST http://localhost:8085/api/fetch \
-H "Content-Type: application/json" \
-d '{"url": "https://docsify.js.org", "use_js_rendering": true, "parse_docsify": true}'- 以MCP服务器运行:
./html-mcp-server -mode=mcpDocker部署
- 使用Docker构建和运行:
docker build -t html-mcp-server-go .
docker run -p 8085:8085 html-mcp-server-go- 或者使用Docker Compose:
docker-compose up -dapi参考
REST API
GET/健康
返回服务器运行状况。
答复:
{
"status": "healthy",
"version": "1.0.0"
}POST/api/fetch
从网页中获取和提取内容。
请求正文:
{
"url": "https://example.com",
"extract_text_only": true,
"use_js_rendering": false,
"parse_docsify": false,
"timeout_seconds": 30,
"wait_for_selector": ".content"
}参数:
url(必填):要获取的URLextract_text_only:仅提取文本内容(默认值:true)use_js_rendering:使用Chrome进行JavaScript渲染(默认值:false)parse_docsify:解析Docsify特定内容(默认:自动检测)timeout_seconds:请求超时(默认值:30,最大值:300)wait_for_selector:要等待的CSS选择器(仅限JS渲染)
答复:
{
"url": "https://example.com",
"title": "Example Domain",
"text_content": "Example Domain This domain is for use...",
"raw_html": "...",
"metadata": {
"content_type": "text/html; charset=utf-8",
"status_code": 200,
"content_length": 1256,
"is_js_rendered": false,
"is_docsify": false
},
"docsify_data": {
"sidebar_content": "...",
"navbar_content": "...",
"main_content": "...",
"config_data": {...}
}
}MCP协议
服务器使用以下工具实现MCP(模型上下文协议):
fetch_html
从网页中提取内容。
参数:
url(必填):要获取的URLextract_text_only:仅提取文本内容use_js_rendering:使用JavaScript渲染parse_docsify:解析Docsify内容timeout_seconds:请求超时wait_for_selector:要等待的CSS选择器
MCP客户端配置
将以下配置之一添加到MCP客户端设置中:
本地开发(已安装Go)
{
"mcpServers": {
"html-mcp-server-go": {
"command": "go",
"args": ["run", "./cmd", "-mode=mcp"],
"cwd": "/path/to/html-mcp-server-go"
}
}
}二进制执行(编译二进制)
{
"mcpServers": {
"html-mcp-server-go": {
"command": "/path/to/html-mcp-server",
"args": ["-mode=mcp"]
}
}
}Docker容器
{
"mcpServers": {
"html-mcp-server-go": {
"command": "docker",
"args": ["run", "--rm", "-i", "html-mcp-server-go", "-mode=mcp"]
}
}
}注: 替换 /path/to/html-mcp-server-go 和 /path/to/html-mcp-server 分别显示项目目录和二进制文件的实际路径。
建筑
该项目遵循清洁建筑原则:
├── cmd/ # Application entry point
├── domain/ # Core business logic
│ ├── model/ # Domain entities and value objects
│ └── port/ # Interfaces for external dependencies
├── application/ # Business logic and use cases
│ ├── service/ # Application services
│ └── usecase/ # Use case implementations
├── infrastructure/ # External adapters
│ ├── adapter/ # HTML parser adapter
│ ├── api/ # Fiber REST API server
│ ├── client/ # HTTP client with Chrome support
│ └── mcp/ # MCP server implementation
└── docker-compose.yaml # Container orchestration配置
环境变量
CHROME_BIN:Chrome/Chromium二进制文件的路径CHROME_PATH:Chrome的替代路径
命令行标志
-mode:服务器模式(mcp、api、auto)-默认值:auto-port:API服务器端口-默认值:8085
Docker配置
Docker镜像包括:
- Alpine Linux基础映像
- Chrome/Chromium浏览器
- 非root用户安全
- 健康检查
- 资源限制(1GB内存,1个CPU核)
发展
添加新功能
- 在中定义域模型
domain/model/ - 在中创建接口
domain/port/ - 在中实现业务逻辑
application/service/ - 在中创建适配器
infrastructure/ - 导线依赖关系
cmd/main.go
测试
# Run tests
go test ./...
# Run specific package tests
go test ./domain/model
go test ./application/service
# Integration testing with Docker
docker-compose up -d
curl http://localhost:8085/health
docker-compose down建筑
# Development build
go build -o html-mcp-server ./cmd
# Production build
CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o html-mcp-server ./cmd
# Docker build
docker build -t html-mcp-server-go .文档支持
服务器为Docsify文档站点提供专门支持:
特性
- 自动检测:自动检测Docsify站点
- 内容提取:将侧边栏、导航栏和主要内容分开
- 配置解析:提取Docsify配置数据
- JavaScript渲染:处理动态内容加载
用法示例
curl -X POST http://localhost:8085/api/fetch \
-H "Content-Type: application/json" \
-d '{
"url": "https://docsify.js.org/#/quickstart",
"use_js_rendering": true,
"parse_docsify": true,
"wait_for_selector": ".markdown-section"
}'Docsify响应结构
{
"docsify_data": {
"sidebar_content": "Quick start\nWriting content...",
"navbar_content": "GitHub\nGitee",
"main_content": "Quick start\nIt is recommended...",
"config_data": {
"name": "docsify",
"repo": "https://github.com/docsifyjs/docsify/",
"loadSidebar": true
}
}
}错误处理
服务器提供全面的错误处理:
HTTP状态代码(API模式)
200 OK:请求成功400 Bad Request:请求参数无效408 Request Timeout:请求超时500 Internal Server Error:服务器端错误
错误响应格式
{
"error": "ERROR_CODE",
"message": "Human-readable error description"
}常见错误代码
INVALID_URL:URL为空或格式错误FETCH_ERROR:网络或HTTP错误PARSE_ERROR:HTML解析失败TIMEOUT:请求超时JS_RENDERING_ERROR:JavaScript渲染失败DOCSIFY_PARSE_ERROR:文档解析失败
演出
- 并发处理:基于Goroutine的异步处理
- 连接池:HTTP客户端重用连接
- 内存效率高:流式HTML处理
- 资源限制:可配置的超时和限制
- 浏览器优化:Chrome以最低的资源使用率推出
安全
- 非根容器:Docker以非root用户身份运行
- 资源限制:Docker中的内存和CPU限制
- 输入验证:URL验证和净化
- 超时保护:请求和浏览器超时
- CORS配置:可配置的跨来源政策
许可证
这个项目是开源的,可以在MIT许可证下使用。
