-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtool_is_element_present.go
More file actions
77 lines (63 loc) · 2.22 KB
/
Copy pathtool_is_element_present.go
File metadata and controls
77 lines (63 loc) · 2.22 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
//go:build linux
package main
import (
"context"
"encoding/json"
"fmt"
"github.com/facebookincubator/go-belt/tool/logger"
"github.com/mark3labs/mcp-go/mcp"
"github.com/mark3labs/mcp-go/server"
)
// ElementPresenceResult holds the is_element_present response.
type ElementPresenceResult struct {
Present bool `json:"present"`
Count int `json:"count"`
}
func registerIsElementPresent(s *server.MCPServer) {
tool := mcp.NewTool("is_element_present",
mcp.WithDescription(
"Check whether a UI element matching the given criteria exists on the current screen. "+
"Returns a boolean 'present' field and the count of matches.",
),
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.WithReadOnlyHintAnnotation(true),
mcp.WithDestructiveHintAnnotation(false),
mcp.WithIdempotentHintAnnotation(true),
)
s.AddTool(tool, handleIsElementPresent)
}
func handleIsElementPresent(
ctx context.Context,
request mcp.CallToolRequest,
) (*mcp.CallToolResult, error) {
logger.Tracef(ctx, "handleIsElementPresent")
defer func() { logger.Tracef(ctx, "/handleIsElementPresent") }()
textFilter := request.GetString("text", "")
resourceIDFilter := request.GetString("resource_id", "")
contentDescFilter := request.GetString("content_desc", "")
if textFilter == "" && resourceIDFilter == "" && contentDescFilter == "" {
return mcp.NewToolResultError("at least one search filter is required (text, resource_id, or content_desc)"), nil
}
xmlData, err := dumpUIXML()
if err != nil {
return mcp.NewToolResultError(fmt.Sprintf("dumping UI: %v", err)), nil
}
elements := parseAndFilterUI(xmlData, textFilter, resourceIDFilter, contentDescFilter, "")
result := ElementPresenceResult{
Present: len(elements) > 0,
Count: len(elements),
}
data, err := json.Marshal(result)
if err != nil {
return nil, fmt.Errorf("marshaling presence result: %w", err)
}
return mcp.NewToolResultText(string(data)), nil
}