-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtool_input_text.go
More file actions
68 lines (56 loc) · 1.71 KB
/
Copy pathtool_input_text.go
File metadata and controls
68 lines (56 loc) · 1.71 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
//go:build linux
package main
import (
"context"
"fmt"
"strings"
"github.com/facebookincubator/go-belt/tool/logger"
"github.com/mark3labs/mcp-go/mcp"
"github.com/mark3labs/mcp-go/server"
)
func registerInputText(s *server.MCPServer) {
tool := mcp.NewTool("input_text",
mcp.WithDescription(
"Type text on the device using 'input text'. "+
"Spaces and special characters are escaped automatically. "+
"The device must have an active text field focused.",
),
mcp.WithString("text",
mcp.Required(),
mcp.Description("Text to type"),
),
mcp.WithDestructiveHintAnnotation(true),
mcp.WithIdempotentHintAnnotation(false),
)
s.AddTool(tool, handleInputText)
}
func handleInputText(
ctx context.Context,
request mcp.CallToolRequest,
) (*mcp.CallToolResult, error) {
logger.Tracef(ctx, "handleInputText")
defer func() { logger.Tracef(ctx, "/handleInputText") }()
text, err := request.RequireString("text")
if err != nil {
return mcp.NewToolResultError(err.Error()), nil
}
// Android's 'input text' requires spaces to be escaped as %s
// and other special shell characters to be handled.
escaped := escapeInputText(text)
cmd := fmt.Sprintf("input text %s", shellQuote(escaped))
out, err := shellExec(cmd)
if err != nil {
return mcp.NewToolResultError(fmt.Sprintf("input text: %v", err)), nil
}
if out == "" {
out = fmt.Sprintf("typed %d characters", len(text))
}
return mcp.NewToolResultText(out), nil
}
// escapeInputText escapes text for Android's 'input text' command.
// Spaces must be replaced with %s, and certain characters need escaping.
func escapeInputText(text string) string {
// Android input text uses %s for space.
text = strings.ReplaceAll(text, " ", "%s")
return text
}