093511bbdd
- 移除 database/middleware 等复杂模块,先聚焦核心功能 - docker_logs:封装 docker logs --tail N <container> - 修复 go.mod 版本为 1.24 匹配 Docker 构建镜像 - Dockerfile 改用 alpine + docker-cli,避免 scratch 无 shell 问题 - docker-compose.yml 简化为单服务定义 Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
79 lines
2.2 KiB
Go
79 lines
2.2 KiB
Go
package tool
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"runtime"
|
|
"time"
|
|
|
|
"github.com/mark3labs/mcp-go/mcp"
|
|
"github.com/mark3labs/mcp-go/server"
|
|
)
|
|
|
|
var StartTime = time.Now()
|
|
|
|
// HealthChecker 用于 system 工具检查其他工具的连通性。
|
|
type HealthChecker interface {
|
|
HealthCheckAll(ctx context.Context) map[string]error
|
|
}
|
|
|
|
type SystemTool struct {
|
|
version string
|
|
registry HealthChecker
|
|
}
|
|
|
|
func NewSystemTool(version string, reg HealthChecker) *SystemTool {
|
|
return &SystemTool{version: version, registry: reg}
|
|
}
|
|
|
|
func (s *SystemTool) Name() string { return "system" }
|
|
func (s *SystemTool) Description() string { return "服务健康检查和信息查询" }
|
|
func (s *SystemTool) Initialize(_ context.Context) error { return nil }
|
|
func (s *SystemTool) Shutdown(_ context.Context) error { return nil }
|
|
func (s *SystemTool) HealthCheck(_ context.Context) error { return nil }
|
|
|
|
func (s *SystemTool) Register(mcpServer *server.MCPServer) error {
|
|
mcpServer.AddTool(mcp.NewTool("health",
|
|
mcp.WithDescription("服务健康检查:运行时间、内存、连接状态"),
|
|
), s.handleHealth)
|
|
|
|
mcpServer.AddTool(mcp.NewTool("info",
|
|
mcp.WithDescription("服务信息:版本号、Go 版本、已加载工具"),
|
|
), s.handleInfo)
|
|
|
|
return nil
|
|
}
|
|
|
|
func (s *SystemTool) handleHealth(ctx context.Context, _ mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
|
var m runtime.MemStats
|
|
runtime.ReadMemStats(&m)
|
|
|
|
result := map[string]any{
|
|
"status": "ok",
|
|
"uptime": time.Since(StartTime).String(),
|
|
"goroutines": runtime.NumGoroutine(),
|
|
"heap_mb": float64(m.HeapAlloc) / 1024 / 1024,
|
|
}
|
|
|
|
if s.registry != nil {
|
|
result["connections"] = s.registry.HealthCheckAll(ctx)
|
|
}
|
|
|
|
data, _ := json.MarshalIndent(result, "", " ")
|
|
return mcp.NewToolResultText(string(data)), nil
|
|
}
|
|
|
|
func (s *SystemTool) handleInfo(ctx context.Context, _ mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
|
result := map[string]any{
|
|
"version": s.version,
|
|
"go_version": runtime.Version(),
|
|
"os": runtime.GOOS,
|
|
"arch": runtime.GOARCH,
|
|
"cpus": runtime.NumCPU(),
|
|
"start_time": StartTime.Format(time.RFC3339),
|
|
}
|
|
data, _ := json.MarshalIndent(result, "", " ")
|
|
return mcp.NewToolResultText(fmt.Sprintf("```json\n%s\n```", string(data))), nil
|
|
}
|