-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtool_get_wifi_state.go
More file actions
80 lines (66 loc) · 1.91 KB
/
tool_get_wifi_state.go
File metadata and controls
80 lines (66 loc) · 1.91 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
80
//go:build linux
package main
import (
"context"
"encoding/json"
"fmt"
"strings"
"github.com/facebookincubator/go-belt/tool/logger"
"github.com/mark3labs/mcp-go/mcp"
"github.com/mark3labs/mcp-go/server"
)
// WifiStateResult holds the get_wifi_state response.
type WifiStateResult struct {
Enabled bool `json:"enabled"`
SSID string `json:"ssid,omitempty"`
Error string `json:"error,omitempty"`
}
func registerGetWifiState(s *server.MCPServer) {
tool := mcp.NewTool("get_wifi_state",
mcp.WithDescription(
"Get the current WiFi state (enabled/disabled and connected SSID). "+
"Uses 'cmd wifi status' on the device.",
),
mcp.WithReadOnlyHintAnnotation(true),
mcp.WithDestructiveHintAnnotation(false),
mcp.WithIdempotentHintAnnotation(true),
)
s.AddTool(tool, handleGetWifiState)
}
func handleGetWifiState(
ctx context.Context,
_ mcp.CallToolRequest,
) (*mcp.CallToolResult, error) {
logger.Tracef(ctx, "handleGetWifiState")
defer func() { logger.Tracef(ctx, "/handleGetWifiState") }()
out, err := shellExec("cmd wifi status")
if err != nil {
return mcp.NewToolResultError(fmt.Sprintf("wifi status: %v", err)), nil
}
result := parseWifiStatus(out)
data, err := json.Marshal(result)
if err != nil {
return nil, fmt.Errorf("marshaling wifi state: %w", err)
}
return mcp.NewToolResultText(string(data)), nil
}
func parseWifiStatus(output string) WifiStateResult {
result := WifiStateResult{}
for _, line := range strings.Split(output, "\n") {
line = strings.TrimSpace(line)
switch {
case line == "Wifi is enabled":
result.Enabled = true
case line == "Wifi is disabled":
result.Enabled = false
case strings.HasPrefix(line, "Wifi is connected to"):
// Line format: Wifi is connected to "SSID_NAME"
if q1 := strings.Index(line, "\""); q1 >= 0 {
if q2 := strings.Index(line[q1+1:], "\""); q2 >= 0 {
result.SSID = line[q1+1 : q1+1+q2]
}
}
}
}
return result
}