forked from DevOps/deploy.stack
实现一个基于Golang的MCP时间服务器,提供获取当前时间和日期功能 包含客户端示例、安装脚本和详细文档 refactor: 优化磁盘巡检脚本以支持SAS和SSD硬盘 增强磁盘巡检脚本的兼容性,改进SMART信息解析逻辑 添加硬盘类型检测和更全面的错误处理 docs: 更新README和安装说明 添加MCP时间服务器的使用文档和API说明 完善磁盘巡检报告格式和内容
152 lines
3.6 KiB
Markdown
152 lines
3.6 KiB
Markdown
# MCP Time Server
|
||
|
||
一个使用Golang实现的MCP(Model Context Protocol)服务器,提供获取当前系统时间和SSE(Server-Sent Events)实时时间流功能。
|
||
|
||
## 项目概述
|
||
|
||
这个服务器实现了自定义的MCP协议,允许客户端通过HTTP接口获取当前系统时间,并且支持通过SSE技术订阅实时时间更新流。服务器使用slog库进行日志记录,日志仅输出到控制台。
|
||
|
||
## 功能特性
|
||
|
||
- 提供获取当前系统时间的MCP工具(`get_current_time`)
|
||
- 支持自定义时间格式
|
||
- 实现SSE(Server-Sent Events)功能,提供实时时间流订阅(`subscribe_time_stream`)
|
||
- 使用slog进行结构化日志记录
|
||
- 基于HTTP实现MCP协议
|
||
- 提供健康检查接口
|
||
- 支持优雅关闭
|
||
|
||
## 目录结构
|
||
|
||
```
|
||
mcpTimeServer/
|
||
├── main.go # 服务器主程序
|
||
├── go.mod # Go模块定义
|
||
├── install.sh # 安装脚本
|
||
└── README.md # 项目说明文档
|
||
```
|
||
|
||
## 安装与配置
|
||
|
||
### 前提条件
|
||
|
||
- Go 1.21或更高版本
|
||
|
||
### 安装步骤
|
||
|
||
1. 克隆项目或进入项目目录
|
||
|
||
2. 运行安装脚本:
|
||
```bash
|
||
chmod +x install.sh
|
||
./install.sh
|
||
```
|
||
|
||
安装脚本会检查Go环境、安装依赖并编译项目。
|
||
|
||
## 使用方法
|
||
|
||
### 启动服务器
|
||
|
||
```bash
|
||
./mcp_time_server
|
||
```
|
||
|
||
服务器启动后,会在8080端口监听HTTP请求。
|
||
|
||
## API说明
|
||
|
||
### MCP协议接口
|
||
|
||
#### 提交MCP请求
|
||
|
||
- **URL**: `/mcp/v1/submit`
|
||
- **方法**: `POST`
|
||
- **Content-Type**: `application/json`
|
||
|
||
**请求体示例**:
|
||
```json
|
||
{
|
||
"data": {"format": "2006-01-02 15:04:05"}, // 可选参数
|
||
"type": "get_current_time",
|
||
"timestamp": 1699999999
|
||
}
|
||
```
|
||
|
||
#### 可用工具
|
||
|
||
1. **get_current_time**
|
||
- **参数**: `format`(可选)- 时间格式字符串,使用Go的时间格式语法(如"2006-01-02 15:04:05")
|
||
- **返回结果**:
|
||
```json
|
||
{
|
||
"current_time": "格式化的时间字符串",
|
||
"timestamp": 时间戳(Unix时间,秒)
|
||
}
|
||
```
|
||
|
||
2. **subscribe_time_stream**
|
||
- **参数**:
|
||
- `interval`(可选)- 时间更新间隔(秒),默认为1秒
|
||
- `format`(可选)- 时间格式字符串
|
||
- **返回结果**:
|
||
```json
|
||
{
|
||
"stream_id": "唯一的流标识符",
|
||
"sse_url": "http://localhost:8080/sse",
|
||
"interval": 更新间隔(秒)
|
||
}
|
||
```
|
||
|
||
### SSE接口
|
||
|
||
- **URL**: `/sse`
|
||
- **方法**: `GET`
|
||
- **Content-Type**: `text/event-stream`
|
||
|
||
连接后,服务器会以指定的间隔发送时间更新事件。
|
||
|
||
### 健康检查接口
|
||
|
||
- **URL**: `/health`
|
||
- **方法**: `GET`
|
||
- **返回**: 状态码200表示服务器正常运行
|
||
|
||
## 测试方法
|
||
|
||
服务器启动后,可以使用以下方式测试:
|
||
|
||
1. **健康检查**:
|
||
```bash
|
||
curl http://localhost:8080/health
|
||
```
|
||
|
||
2. **获取当前时间**:
|
||
```bash
|
||
curl -X POST http://localhost:8080/mcp/v1/submit -H 'Content-Type: application/json' -d '{"data":{},"type":"get_current_time","timestamp":'$(date +%s)'}'
|
||
```
|
||
|
||
3. **使用自定义格式获取当前时间**:
|
||
```bash
|
||
curl -X POST http://localhost:8080/mcp/v1/submit -H 'Content-Type: application/json' -d '{"data":{"format":"2006-01-02 15:04:05"},"type":"get_current_time","timestamp":'$(date +%s)'}'
|
||
```
|
||
|
||
4. **订阅时间流**:
|
||
```bash
|
||
curl http://localhost:8080/sse
|
||
```
|
||
或直接在浏览器中访问 `http://localhost:8080/sse`
|
||
|
||
## 依赖说明
|
||
|
||
- github.com/google/uuid v1.6.0:用于生成唯一标识符
|
||
|
||
## 注意事项
|
||
|
||
- 服务器默认监听在8080端口
|
||
- 日志仅输出到控制台,不会写入文件
|
||
- SSE连接会在客户端断开或服务器关闭时终止
|
||
|
||
## License
|
||
|
||
MIT |