-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtool_get_uptime.go
More file actions
79 lines (64 loc) · 1.86 KB
/
tool_get_uptime.go
File metadata and controls
79 lines (64 loc) · 1.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
//go:build linux
package main
import (
"context"
"encoding/json"
"fmt"
"strconv"
"strings"
"github.com/facebookincubator/go-belt/tool/logger"
"github.com/mark3labs/mcp-go/mcp"
"github.com/mark3labs/mcp-go/server"
)
// UptimeResult holds parsed uptime data.
type UptimeResult struct {
UptimeSeconds float64 `json:"uptime_seconds"`
IdleSeconds float64 `json:"idle_seconds"`
Formatted string `json:"formatted"`
}
func registerGetUptime(s *server.MCPServer) {
tool := mcp.NewTool("get_uptime",
mcp.WithDescription(
"Get device uptime and idle time from /proc/uptime. "+
"Returns uptime in seconds and a human-readable formatted string.",
),
mcp.WithReadOnlyHintAnnotation(true),
mcp.WithDestructiveHintAnnotation(false),
mcp.WithIdempotentHintAnnotation(true),
)
s.AddTool(tool, handleGetUptime)
}
func handleGetUptime(
ctx context.Context,
_ mcp.CallToolRequest,
) (*mcp.CallToolResult, error) {
logger.Tracef(ctx, "handleGetUptime")
defer func() { logger.Tracef(ctx, "/handleGetUptime") }()
out, err := shellExec("cat /proc/uptime")
if err != nil {
return mcp.NewToolResultError(fmt.Sprintf("uptime: %v", err)), nil
}
result := parseUptime(out)
data, err := json.Marshal(result)
if err != nil {
return nil, fmt.Errorf("marshaling uptime: %w", err)
}
return mcp.NewToolResultText(string(data)), nil
}
func parseUptime(output string) UptimeResult {
parts := strings.Fields(output)
result := UptimeResult{}
if len(parts) >= 1 {
result.UptimeSeconds, _ = strconv.ParseFloat(parts[0], 64)
}
if len(parts) >= 2 {
result.IdleSeconds, _ = strconv.ParseFloat(parts[1], 64)
}
totalSec := int(result.UptimeSeconds)
days := totalSec / 86400
hours := (totalSec % 86400) / 3600
minutes := (totalSec % 3600) / 60
seconds := totalSec % 60
result.Formatted = fmt.Sprintf("%dd %dh %dm %ds", days, hours, minutes, seconds)
return result
}