-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtool_find_ui_element.go
More file actions
215 lines (190 loc) · 6.04 KB
/
tool_find_ui_element.go
File metadata and controls
215 lines (190 loc) · 6.04 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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
//go:build linux
package main
import (
"context"
"encoding/json"
"encoding/xml"
"fmt"
"strconv"
"strings"
"github.com/facebookincubator/go-belt/tool/logger"
"github.com/mark3labs/mcp-go/mcp"
"github.com/mark3labs/mcp-go/server"
)
// UIElement represents a matched UI element from the hierarchy.
type UIElement struct {
Text string `json:"text,omitempty"`
ResourceID string `json:"resource_id,omitempty"`
Class string `json:"class,omitempty"`
ContentDesc string `json:"content_desc,omitempty"`
Bounds string `json:"bounds"`
CenterX int `json:"center_x"`
CenterY int `json:"center_y"`
Clickable bool `json:"clickable"`
Enabled bool `json:"enabled"`
}
func registerFindUIElement(s *server.MCPServer) {
tool := mcp.NewTool("find_ui_element",
mcp.WithDescription(
"Search the UI hierarchy for elements matching the given criteria. "+
"Dumps the UI via uiautomator and parses the XML to find "+
"elements by text, resource-id, content-desc, or class. "+
"Returns matching elements with their bounds and center coordinates.",
),
mcp.WithString("text",
mcp.Description("Match elements whose text contains this substring (case-insensitive)"),
),
mcp.WithString("resource_id",
mcp.Description("Match elements whose resource-id contains this substring"),
),
mcp.WithString("content_desc",
mcp.Description("Match elements whose content-desc contains this substring (case-insensitive)"),
),
mcp.WithString("class",
mcp.Description("Match elements whose class contains this substring"),
),
mcp.WithReadOnlyHintAnnotation(true),
mcp.WithDestructiveHintAnnotation(false),
mcp.WithIdempotentHintAnnotation(true),
)
s.AddTool(tool, handleFindUIElement)
}
func handleFindUIElement(
ctx context.Context,
request mcp.CallToolRequest,
) (*mcp.CallToolResult, error) {
logger.Tracef(ctx, "handleFindUIElement")
defer func() { logger.Tracef(ctx, "/handleFindUIElement") }()
textFilter := request.GetString("text", "")
resourceIDFilter := request.GetString("resource_id", "")
contentDescFilter := request.GetString("content_desc", "")
classFilter := request.GetString("class", "")
if textFilter == "" && resourceIDFilter == "" && contentDescFilter == "" && classFilter == "" {
return mcp.NewToolResultError("at least one search filter is required (text, resource_id, content_desc, or class)"), nil
}
xmlData, err := dumpUIXML()
if err != nil {
return mcp.NewToolResultError(fmt.Sprintf("dumping UI: %v", err)), nil
}
elements := parseAndFilterUI(xmlData, textFilter, resourceIDFilter, contentDescFilter, classFilter)
data, err := json.Marshal(elements)
if err != nil {
return nil, fmt.Errorf("marshaling UI elements: %w", err)
}
return mcp.NewToolResultText(string(data)), nil
}
// dumpUIXML runs uiautomator dump and returns the XML content.
func dumpUIXML() (string, error) {
cmd := fmt.Sprintf("uiautomator dump %s >/dev/null 2>&1 && cat %s",
shellQuote(uiDumpPath), shellQuote(uiDumpPath))
return shellExec(cmd)
}
// xmlNode represents a node element in the uiautomator XML.
type xmlNode struct {
XMLName xml.Name `xml:"node"`
Text string `xml:"text,attr"`
ResourceID string `xml:"resource-id,attr"`
Class string `xml:"class,attr"`
ContentDesc string `xml:"content-desc,attr"`
Bounds string `xml:"bounds,attr"`
Clickable string `xml:"clickable,attr"`
Enabled string `xml:"enabled,attr"`
Children []xmlNode `xml:"node"`
}
// xmlHierarchy is the root of the uiautomator XML.
type xmlHierarchy struct {
XMLName xml.Name `xml:"hierarchy"`
Children []xmlNode `xml:"node"`
}
// parseAndFilterUI parses the UI XML and filters nodes by the given criteria.
func parseAndFilterUI(
xmlData string,
textFilter string,
resourceIDFilter string,
contentDescFilter string,
classFilter string,
) []UIElement {
var hierarchy xmlHierarchy
if err := xml.Unmarshal([]byte(xmlData), &hierarchy); err != nil {
return nil
}
var results []UIElement
for i := range hierarchy.Children {
collectMatchingNodes(
&hierarchy.Children[i],
textFilter, resourceIDFilter, contentDescFilter, classFilter,
&results,
)
}
return results
}
func collectMatchingNodes(
node *xmlNode,
textFilter string,
resourceIDFilter string,
contentDescFilter string,
classFilter string,
results *[]UIElement,
) {
if matchesFilters(node, textFilter, resourceIDFilter, contentDescFilter, classFilter) {
cx, cy := parseBoundsCenter(node.Bounds)
*results = append(*results, UIElement{
Text: node.Text,
ResourceID: node.ResourceID,
Class: node.Class,
ContentDesc: node.ContentDesc,
Bounds: node.Bounds,
CenterX: cx,
CenterY: cy,
Clickable: node.Clickable == "true",
Enabled: node.Enabled == "true",
})
}
for i := range node.Children {
collectMatchingNodes(
&node.Children[i],
textFilter, resourceIDFilter, contentDescFilter, classFilter,
results,
)
}
}
func matchesFilters(
node *xmlNode,
textFilter string,
resourceIDFilter string,
contentDescFilter string,
classFilter string,
) bool {
if textFilter != "" && !containsIgnoreCase(node.Text, textFilter) {
return false
}
if resourceIDFilter != "" && !strings.Contains(node.ResourceID, resourceIDFilter) {
return false
}
if contentDescFilter != "" && !containsIgnoreCase(node.ContentDesc, contentDescFilter) {
return false
}
if classFilter != "" && !strings.Contains(node.Class, classFilter) {
return false
}
return true
}
func containsIgnoreCase(s, substr string) bool {
return strings.Contains(strings.ToLower(s), strings.ToLower(substr))
}
// parseBoundsCenter parses Android bounds format "[x1,y1][x2,y2]" and
// returns the center coordinates.
func parseBoundsCenter(bounds string) (int, int) {
// Format: [x1,y1][x2,y2]
parts := strings.FieldsFunc(bounds, func(r rune) bool {
return r == '[' || r == ']' || r == ','
})
if len(parts) < 4 {
return 0, 0
}
x1, _ := strconv.Atoi(parts[0])
y1, _ := strconv.Atoi(parts[1])
x2, _ := strconv.Atoi(parts[2])
y2, _ := strconv.Atoi(parts[3])
return (x1 + x2) / 2, (y1 + y2) / 2
}