Files
deploy.stack/mcp_server_go/client_example.go
cnphpbb 226726eecd feat: 初始化MCP服务器Go版本实现
添加MCP服务器Go版本的核心功能,包括:
- 配置文件管理
- 日志系统
- OpenAI API集成
- HTTP服务器和API端点
- 健康检查和监控
- 安装脚本和文档
2025-09-10 22:23:34 +08:00

125 lines
3.4 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package main
import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
"time"
)
// MCPRequest 定义MCP请求结构
type MCPRequest struct {
Data interface{} `json:"data"` Type string `json:"type"` Metadata map[string]string `json:"metadata,omitempty"` Timestamp int64 `json:"timestamp"`
}
// MCPResponse 定义MCP响应结构
type MCPResponse struct {
Success bool `json:"success"` Message string `json:"message,omitempty"` Data interface{} `json:"data,omitempty"` AIResult interface{} `json:"ai_result,omitempty"` RequestID string `json:"request_id,omitempty"` Timestamp int64 `json:"timestamp"`
}
func main() {
// 准备测试数据
diskData := map[string]interface{}{
"disk_id": "sda",
"smart_data": map[string]interface{}{
"temperature": 38,
"power_on_hours": 12345,
"read_errors": 0,
"write_errors": 0,
"reallocated_sectors": 0,
},
"performance_data": map[string]interface{}{
"read_speed": 120,
"write_speed": 90,
"io_wait": 0.5,
},
}
// 创建MCP请求
mcpRequest := MCPRequest{
Data: diskData,
Type: "disk_inspection",
Metadata: map[string]string{"server_id": "test-server", "location": "test-lab"},
Timestamp: time.Now().Unix(),
}
// 序列化请求数据
jsonData, err := json.Marshal(mcpRequest)
if err != nil {
fmt.Printf("序列化请求数据失败: %v\n", err)
return
}
// 发送HTTP请求到MCP服务器
resp, err := http.Post("http://localhost:8080/mcp/v1/submit", "application/json", bytes.NewBuffer(jsonData))
if err != nil {
fmt.Printf("发送请求失败: %v\n", err)
fmt.Println("请确保MCP服务器正在运行并且监听在正确的端口上")
return
}
defer resp.Body.Close()
// 读取响应
body, err := io.ReadAll(resp.Body)
if err != nil {
fmt.Printf("读取响应失败: %v\n", err)
return
}
// 打印响应状态和内容
fmt.Printf("响应状态码: %d\n", resp.StatusCode)
// 如果响应成功,解析并显示详细信息
if resp.StatusCode == http.StatusOK {
var mcpResponse MCPResponse
if err := json.Unmarshal(body, &mcpResponse); err != nil {
fmt.Printf("解析响应数据失败: %v\n", err)
fmt.Printf("原始响应内容: %s\n", string(body))
return
}
// 格式化输出响应数据
fmt.Println("\nMCP服务器响应:")
fmt.Printf(" 成功状态: %v\n", mcpResponse.Success)
fmt.Printf(" 消息: %s\n", mcpResponse.Message)
fmt.Printf(" 请求ID: %s\n", mcpResponse.RequestID)
fmt.Printf(" 时间戳: %d (%s)\n",
mcpResponse.Timestamp,
time.Unix(mcpResponse.Timestamp, 0).Format("2006-01-02 15:04:05"))
// 输出AI分析结果如果有
if mcpResponse.AIResult != nil {
fmt.Println("\nAI分析结果:")
aiResultJSON, _ := json.MarshalIndent(mcpResponse.AIResult, " ", " ")
fmt.Println(string(aiResultJSON))
}
} else {
fmt.Printf("请求失败,响应内容: %s\n", string(body))
}
// 测试健康检查接口
testHealthCheck()
}
// 测试健康检查接口
func testHealthCheck() {
fmt.Println("\n测试健康检查接口...")
resp, err := http.Get("http://localhost:8080/health")
if err != nil {
fmt.Printf("健康检查失败: %v\n", err)
return
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
fmt.Printf("读取健康检查响应失败: %v\n", err)
return
}
fmt.Printf("健康检查状态码: %d\n", resp.StatusCode)
fmt.Printf("健康检查响应: %s\n", string(body))
}