Go MCP Server 项目初始化:工具注册架构 + 编译运行通过

- Tool 接口 + Registry 注册中心,工具模块化插拔
- system 工具:health / info
- database 工具:db_query / db_tables / db_table_info / db_explain(四层 SQL 安全)
- docker 工具:docker_ps / docker_logs / docker_inspect(白名单 + 大小限制)
- middleware:auth(JWT+APIKey)/ ratelimit(令牌桶)/ audit(slog 结构化)
- stdio / SSE 双传输模式,Viper 多环境配置
- Dockerfile 多阶段构建(golang:alpine → scratch),~15MB 镜像
- docker-compose.yml 一键部署 + 安全加固(read_only / no-new-privileges / cap_drop)
- go build ./... / go vet ./... / go test ./... 全部通过

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
yangzhaohan
2026-07-06 10:26:52 +08:00
parent 68ca67a98b
commit fb2c3a6cae
26 changed files with 1854 additions and 1 deletions
+80 -1
View File
@@ -1 +1,80 @@
ops-mcp server
# ops-mcp
生产环境 OPS MCP Server —— 安全的数据库查询 + Docker 容器日志工具。
Go 编译为 ~15MB 单二进制,scratch 镜像部署。
## 快速开始
```bash
make build && ./ops-mcp -env=dev
```
## Claude Desktop 配置
```json
{
"mcpServers": {
"ops-mcp": {
"command": "/path/to/ops-mcp",
"args": ["-env=dev"]
}
}
}
```
## Docker Compose 部署(推荐)
```bash
# 1. 配置密钥
cp .env.example .env
# 编辑 .env,填入真实的 DB_PASSWORD 和 JWT_SECRET
# 2. 启动(仅 MCP 服务)
make up
# 3. 启动(带本地 PostgreSQL,用于开发调试)
make up-dev
# 4. 查看日志
make logs
# 5. 更新部署
git pull && make up # 重新 build 镜像并重启
```
**docker-compose.yml 做了什么:**
| 项目 | 说明 |
|---|---|
| `build.context` | 从当前目录 `Dockerfile` 构建 |
| `ports` | 映射 `8080`SSE 模式) |
| `volumes` | 只读挂载 `/var/run/docker.sock`,挂载 `prod.yaml` |
| `read_only: true` | 容器文件系统只读 + tmpfs `/tmp` |
| `security_opt` | `no-new-privileges` 禁止提权 |
| `cap_drop: [ALL]` | 移除所有 Linux capabilities |
| `healthcheck` | 30s 间隔自检 |
| `profiles: [dev]` | PG 仅 `--profile dev` 时启动 |
## MCP 工具列表
| 工具 | 说明 |
|---|---|
| `health` | 服务健康检查、内存、goroutine、连接状态 |
| `info` | 版本号、Go 版本、已加载工具列表 |
| `db_query` | 参数化只读 SQL 查询(四层安全防护) |
| `db_tables` | 列出数据库所有表 |
| `db_table_info` | 查看表结构 |
| `db_explain` | EXPLAIN 分析查询计划 |
| `docker_ps` | 列出 Docker 容器 |
| `docker_logs` | 获取容器日志(白名单 + 大小限制) |
| `docker_inspect` | 查看容器详情(不泄露 Env 密钥) |
## 开发
```bash
make build # 编译
make test # 测试
make lint # 代码检查(需安装 golangci-lint
make image # 仅构建 Docker 镜像
```