-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtool_get_device_properties.go
More file actions
78 lines (64 loc) · 1.84 KB
/
tool_get_device_properties.go
File metadata and controls
78 lines (64 loc) · 1.84 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
//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"
)
func registerGetDeviceProperties(s *server.MCPServer) {
tool := mcp.NewTool("get_device_properties",
mcp.WithDescription(
"Get all Android system properties (getprop) as a JSON object "+
"of key-value pairs. Returns properties like ro.build.model, "+
"ro.build.version.sdk, ro.product.manufacturer, etc.",
),
mcp.WithReadOnlyHintAnnotation(true),
mcp.WithDestructiveHintAnnotation(false),
mcp.WithIdempotentHintAnnotation(true),
)
s.AddTool(tool, handleGetDeviceProperties)
}
func handleGetDeviceProperties(
ctx context.Context,
_ mcp.CallToolRequest,
) (*mcp.CallToolResult, error) {
logger.Tracef(ctx, "handleGetDeviceProperties")
defer func() { logger.Tracef(ctx, "/handleGetDeviceProperties") }()
out, err := shellExec("getprop")
if err != nil {
return mcp.NewToolResultError(fmt.Sprintf("getprop: %v", err)), nil
}
props := parseGetpropOutput(out)
data, err := json.Marshal(props)
if err != nil {
return nil, fmt.Errorf("marshaling properties: %w", err)
}
return mcp.NewToolResultText(string(data)), nil
}
// parseGetpropOutput parses `getprop` output lines of the form
// "[key]: [value]" into a map.
func parseGetpropOutput(output string) map[string]string {
props := make(map[string]string)
for _, line := range strings.Split(output, "\n") {
line = strings.TrimSpace(line)
if line == "" {
continue
}
// Format: [key]: [value]
colonIdx := strings.Index(line, "]: [")
if colonIdx < 0 {
continue
}
if !strings.HasPrefix(line, "[") || !strings.HasSuffix(line, "]") {
continue
}
key := line[1:colonIdx]
value := line[colonIdx+4 : len(line)-1]
props[key] = value
}
return props
}