-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtool_call_method.go
More file actions
146 lines (125 loc) · 3.97 KB
/
tool_call_method.go
File metadata and controls
146 lines (125 loc) · 3.97 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
//go:build linux
package main
import (
"context"
"encoding/hex"
"encoding/json"
"fmt"
"strconv"
"github.com/facebookincubator/go-belt/tool/logger"
"github.com/mark3labs/mcp-go/mcp"
"github.com/mark3labs/mcp-go/server"
"github.com/AndroidGoLab/binder/binder"
"github.com/AndroidGoLab/binder/parcel"
"github.com/AndroidGoLab/binder/servicemanager"
)
// CallMethodResult describes the call_method response.
type CallMethodResult struct {
ReplySize int `json:"reply_size"`
ReplyHex string `json:"reply_hex"`
}
func (ts *ToolSet) registerCallMethod(s *server.MCPServer) {
tool := mcp.NewTool("call_method",
mcp.WithDescription(
"Call a method on a binder service using raw transaction. "+
"Accepts a service name and either a numeric transaction code "+
"or a method name (resolved via the AIDL descriptor). "+
"Optional hex-encoded parcel data can be provided as input. "+
"Returns the reply parcel as hex.",
),
mcp.WithString("service",
mcp.Required(),
mcp.Description("Service name (e.g. 'activity', 'power')"),
),
mcp.WithString("method",
mcp.Required(),
mcp.Description(
"Method to call: a numeric transaction code (decimal or 0x hex) "+
"or an AIDL method name (e.g. 'isInteractive')",
),
),
mcp.WithString("data",
mcp.Description("Hex-encoded parcel data to send (empty if omitted)"),
),
mcp.WithDestructiveHintAnnotation(true),
mcp.WithOpenWorldHintAnnotation(true),
)
s.AddTool(tool, ts.handleCallMethod)
}
func (ts *ToolSet) handleCallMethod(
ctx context.Context,
request mcp.CallToolRequest,
) (*mcp.CallToolResult, error) {
logger.Tracef(ctx, "handleCallMethod")
defer func() { logger.Tracef(ctx, "/handleCallMethod") }()
serviceName, err := request.RequireString("service")
if err != nil {
return mcp.NewToolResultError(err.Error()), nil
}
methodStr, err := request.RequireString("method")
if err != nil {
return mcp.NewToolResultError(err.Error()), nil
}
hexData := request.GetString("data", "")
svc, err := ts.sm.CheckService(ctx, servicemanager.ServiceName(serviceName))
if err != nil {
return mcp.NewToolResultError(fmt.Sprintf("checking service %q: %v", serviceName, err)), nil
}
if svc == nil {
return mcp.NewToolResultError(fmt.Sprintf("service %q not found", serviceName)), nil
}
code, err := resolveTransactionCode(ctx, svc, methodStr)
if err != nil {
return mcp.NewToolResultError(fmt.Sprintf("resolving method %q: %v", methodStr, err)), nil
}
var data *parcel.Parcel
switch hexData {
case "":
data = parcel.New()
default:
raw, decErr := hex.DecodeString(hexData)
if decErr != nil {
return mcp.NewToolResultError(fmt.Sprintf("decoding hex data: %v", decErr)), nil
}
data = parcel.FromBytes(raw)
}
defer data.Recycle()
reply, err := svc.Transact(ctx, code, 0, data)
if err != nil {
return mcp.NewToolResultError(fmt.Sprintf("transact failed: %v", err)), nil
}
defer reply.Recycle()
replyData := reply.Data()
result := CallMethodResult{
ReplySize: len(replyData),
ReplyHex: hex.EncodeToString(replyData),
}
out, err := json.Marshal(result)
if err != nil {
return nil, fmt.Errorf("marshaling call result: %w", err)
}
return mcp.NewToolResultText(string(out)), nil
}
// resolveTransactionCode converts a method string to a TransactionCode.
// It first tries to parse as a numeric code, then falls back to
// AIDL descriptor-based resolution via ResolveCode.
func resolveTransactionCode(
ctx context.Context,
svc binder.IBinder,
method string,
) (binder.TransactionCode, error) {
// Try numeric parse first (supports decimal and 0x prefix).
n, err := strconv.ParseUint(method, 0, 32)
if err == nil {
return binder.TransactionCode(n), nil
}
// Fall back to descriptor-based resolution.
descriptor := queryDescriptor(ctx, svc)
if descriptor == "" || descriptor == "(unknown)" {
return 0, fmt.Errorf(
"cannot resolve method name %q: service descriptor unknown; use a numeric code instead",
method,
)
}
return svc.ResolveCode(ctx, descriptor, method)
}