@mobilenext/mobile-mcp 1.0.1 → 1.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.ja.md +572 -0
- package/README.md +38 -11
- package/README.zh-CN.md +572 -0
- package/lib/format-elements.js +82 -0
- package/lib/mobile-device.js +106 -19
- package/lib/mobilecli.js +18 -14
- package/lib/server.js +219 -86
- package/package.json +2 -2
- package/lib/image-utils.js +0 -155
package/lib/mobile-device.js
CHANGED
|
@@ -2,6 +2,27 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.MobileDevice = void 0;
|
|
4
4
|
const mobilecli_1 = require("./mobilecli");
|
|
5
|
+
const robot_1 = require("./robot");
|
|
6
|
+
const flattenUIElement = (element) => {
|
|
7
|
+
const screenElement = {
|
|
8
|
+
type: element.type,
|
|
9
|
+
label: element.label,
|
|
10
|
+
text: element.text,
|
|
11
|
+
name: element.name,
|
|
12
|
+
value: element.value,
|
|
13
|
+
identifier: element.identifier,
|
|
14
|
+
rect: element.rect,
|
|
15
|
+
ref: element.ref,
|
|
16
|
+
focused: element.focused,
|
|
17
|
+
selected: element.selected,
|
|
18
|
+
checked: element.checked,
|
|
19
|
+
enabled: element.enabled,
|
|
20
|
+
};
|
|
21
|
+
if (!element.children) {
|
|
22
|
+
return [screenElement];
|
|
23
|
+
}
|
|
24
|
+
return [screenElement, ...element.children.flatMap(child => flattenUIElement(child))];
|
|
25
|
+
};
|
|
5
26
|
class MobileDevice {
|
|
6
27
|
deviceId;
|
|
7
28
|
mobilecli;
|
|
@@ -13,8 +34,17 @@ class MobileDevice {
|
|
|
13
34
|
const fullArgs = [...args, "--device", this.deviceId];
|
|
14
35
|
return this.mobilecli.executeCommand(fullArgs);
|
|
15
36
|
}
|
|
37
|
+
runJsonCommand(args) {
|
|
38
|
+
const output = this.runCommand(args);
|
|
39
|
+
try {
|
|
40
|
+
return JSON.parse(output);
|
|
41
|
+
}
|
|
42
|
+
catch {
|
|
43
|
+
throw new robot_1.ActionableError(`Failed to parse JSON response from mobilecli ${args.join(" ")}`);
|
|
44
|
+
}
|
|
45
|
+
}
|
|
16
46
|
async getScreenSize() {
|
|
17
|
-
const response =
|
|
47
|
+
const response = this.runJsonCommand(["device", "info"]);
|
|
18
48
|
if (response.data.device.screenSize) {
|
|
19
49
|
return response.data.device.screenSize;
|
|
20
50
|
}
|
|
@@ -67,19 +97,36 @@ class MobileDevice {
|
|
|
67
97
|
endX = x + swipeDistance;
|
|
68
98
|
break;
|
|
69
99
|
}
|
|
70
|
-
this.runCommand(["io", "swipe", `${x},${y},${endX},${endY}`]);
|
|
100
|
+
this.runCommand(["io", "swipe", `${Math.round(x)},${Math.round(y)},${Math.round(endX)},${Math.round(endY)}`]);
|
|
71
101
|
}
|
|
72
|
-
async getScreenshot() {
|
|
73
|
-
const
|
|
102
|
+
async getScreenshot(options) {
|
|
103
|
+
const format = options?.format || "png";
|
|
104
|
+
const fullArgs = ["screenshot", "--device", this.deviceId, "--format", format, "--output", "-"];
|
|
105
|
+
if (format === "jpeg" && options?.quality !== undefined) {
|
|
106
|
+
fullArgs.push("--quality", `${options.quality}`);
|
|
107
|
+
}
|
|
108
|
+
if (options?.maxSize !== undefined) {
|
|
109
|
+
fullArgs.push("--max-size", `${options.maxSize}`);
|
|
110
|
+
}
|
|
111
|
+
if (options?.scale !== undefined && options.scale !== 1) {
|
|
112
|
+
fullArgs.push("--scale", `${options.scale}`);
|
|
113
|
+
}
|
|
74
114
|
return this.mobilecli.executeCommandBuffer(fullArgs);
|
|
75
115
|
}
|
|
76
116
|
async listApps() {
|
|
77
|
-
const response =
|
|
117
|
+
const response = this.runJsonCommand(["apps", "list"]);
|
|
78
118
|
return response.data.map(app => ({
|
|
79
119
|
appName: app.appName || app.packageName,
|
|
80
120
|
packageName: app.packageName,
|
|
81
121
|
}));
|
|
82
122
|
}
|
|
123
|
+
async getForegroundApp() {
|
|
124
|
+
const response = this.runJsonCommand(["apps", "foreground"]);
|
|
125
|
+
return {
|
|
126
|
+
appName: response.data.appName || response.data.packageName,
|
|
127
|
+
packageName: response.data.packageName,
|
|
128
|
+
};
|
|
129
|
+
}
|
|
83
130
|
async launchApp(packageName, locale) {
|
|
84
131
|
const args = ["apps", "launch", packageName];
|
|
85
132
|
if (locale) {
|
|
@@ -106,7 +153,11 @@ class MobileDevice {
|
|
|
106
153
|
this.runCommand(["io", "button", button]);
|
|
107
154
|
}
|
|
108
155
|
async tap(x, y) {
|
|
109
|
-
|
|
156
|
+
// mobilecli rejects fractional coordinates ("x and y must be integers")
|
|
157
|
+
this.runCommand(["io", "tap", `${Math.round(x)},${Math.round(y)}`]);
|
|
158
|
+
}
|
|
159
|
+
async tapByRef(ref) {
|
|
160
|
+
this.runCommand(["io", "tap", ref]);
|
|
110
161
|
}
|
|
111
162
|
async doubleTap(x, y) {
|
|
112
163
|
// TODO: should move into mobilecli itself as "io doubletap"
|
|
@@ -114,27 +165,63 @@ class MobileDevice {
|
|
|
114
165
|
await this.tap(x, y);
|
|
115
166
|
}
|
|
116
167
|
async longPress(x, y, duration) {
|
|
117
|
-
this.runCommand(["io", "longpress", `${x},${y}`, "--duration", `${duration}`]);
|
|
168
|
+
this.runCommand(["io", "longpress", `${Math.round(x)},${Math.round(y)}`, "--duration", `${duration}`]);
|
|
118
169
|
}
|
|
119
170
|
async getElementsOnScreen() {
|
|
120
|
-
const response =
|
|
121
|
-
return response.data.elements.
|
|
122
|
-
type: element.type,
|
|
123
|
-
label: element.label,
|
|
124
|
-
text: element.text,
|
|
125
|
-
name: element.name,
|
|
126
|
-
value: element.value,
|
|
127
|
-
identifier: element.identifier,
|
|
128
|
-
rect: element.rect,
|
|
129
|
-
focused: element.focused,
|
|
130
|
-
}));
|
|
171
|
+
const response = this.runJsonCommand(["dump", "ui"]);
|
|
172
|
+
return response.data.elements.flatMap(element => flattenUIElement(element));
|
|
131
173
|
}
|
|
132
174
|
async setOrientation(orientation) {
|
|
133
175
|
this.runCommand(["device", "orientation", "set", orientation]);
|
|
134
176
|
}
|
|
135
177
|
async getOrientation() {
|
|
136
|
-
const response =
|
|
178
|
+
const response = this.runJsonCommand(["device", "orientation", "get"]);
|
|
137
179
|
return response.data.orientation;
|
|
138
180
|
}
|
|
181
|
+
async setLocation(latitude, longitude) {
|
|
182
|
+
this.runCommand(["device", "location", "set", `${latitude},${longitude}`]);
|
|
183
|
+
}
|
|
184
|
+
async clearLocation() {
|
|
185
|
+
this.runCommand(["device", "location", "clear"]);
|
|
186
|
+
}
|
|
187
|
+
async getClipboard() {
|
|
188
|
+
const response = this.runJsonCommand(["io", "clipboard", "get"]);
|
|
189
|
+
return response.data.text;
|
|
190
|
+
}
|
|
191
|
+
async setClipboard(text) {
|
|
192
|
+
this.runCommand(["io", "clipboard", "set", text]);
|
|
193
|
+
}
|
|
194
|
+
getLogs(limit, filters, timeoutMs) {
|
|
195
|
+
const args = ["device", "logs", "--limit", String(limit), ...filters.flatMap(filter => ["--filter", filter]), "--device", this.deviceId];
|
|
196
|
+
const child = this.mobilecli.spawnCommand(args, true);
|
|
197
|
+
return new Promise((resolve, reject) => {
|
|
198
|
+
const stdout = [];
|
|
199
|
+
const stderr = [];
|
|
200
|
+
let silenceTimer;
|
|
201
|
+
// kill the stream once no log entry arrived for timeoutMs, then return what was captured
|
|
202
|
+
const restartSilenceTimer = () => {
|
|
203
|
+
clearTimeout(silenceTimer);
|
|
204
|
+
silenceTimer = setTimeout(() => child.kill(), timeoutMs);
|
|
205
|
+
};
|
|
206
|
+
child.stdout?.on("data", (chunk) => {
|
|
207
|
+
stdout.push(chunk);
|
|
208
|
+
restartSilenceTimer();
|
|
209
|
+
});
|
|
210
|
+
child.stderr?.on("data", (chunk) => stderr.push(chunk));
|
|
211
|
+
child.on("error", err => {
|
|
212
|
+
clearTimeout(silenceTimer);
|
|
213
|
+
reject(err);
|
|
214
|
+
});
|
|
215
|
+
child.on("close", code => {
|
|
216
|
+
clearTimeout(silenceTimer);
|
|
217
|
+
if (code !== 0 && !child.killed) {
|
|
218
|
+
reject(new Error(`mobilecli device logs failed with code ${code}: ${Buffer.concat(stderr).toString().trim()}`));
|
|
219
|
+
return;
|
|
220
|
+
}
|
|
221
|
+
resolve(Buffer.concat(stdout).toString().trim());
|
|
222
|
+
});
|
|
223
|
+
restartSilenceTimer();
|
|
224
|
+
});
|
|
225
|
+
}
|
|
139
226
|
}
|
|
140
227
|
exports.MobileDevice = MobileDevice;
|
package/lib/mobilecli.js
CHANGED
|
@@ -24,10 +24,10 @@ class Mobilecli {
|
|
|
24
24
|
}
|
|
25
25
|
return (0, node_child_process_1.execFileSync)(path, args, options).toString().trim();
|
|
26
26
|
}
|
|
27
|
-
spawnCommand(args) {
|
|
27
|
+
spawnCommand(args, captureOutput = false) {
|
|
28
28
|
const binaryPath = this.getPath();
|
|
29
29
|
return (0, node_child_process_1.spawn)(binaryPath, args, {
|
|
30
|
-
stdio: ["ignore", "ignore", "ignore"],
|
|
30
|
+
stdio: ["ignore", captureOutput ? "pipe" : "ignore", captureOutput ? "pipe" : "ignore"],
|
|
31
31
|
});
|
|
32
32
|
}
|
|
33
33
|
executeCommandBuffer(args) {
|
|
@@ -47,26 +47,30 @@ class Mobilecli {
|
|
|
47
47
|
const normalizedPlatform = platform === "win32" ? "windows" : platform;
|
|
48
48
|
const normalizedArch = arch === "arm64" ? "arm64" : "amd64";
|
|
49
49
|
const ext = platform === "win32" ? ".exe" : "";
|
|
50
|
-
const
|
|
50
|
+
const scopedPackage = `mobilecli-${normalizedPlatform}-${normalizedArch}`;
|
|
51
|
+
const binaryName = `${scopedPackage}${ext}`;
|
|
52
|
+
const nodeModulesRoots = [];
|
|
51
53
|
// Check if mobile-mcp is installed as a package
|
|
52
54
|
const currentPath = __filename;
|
|
53
55
|
const pathParts = currentPath.split(node_path_1.sep);
|
|
54
56
|
const lastNodeModulesIndex = pathParts.lastIndexOf("node_modules");
|
|
55
57
|
if (lastNodeModulesIndex !== -1) {
|
|
56
58
|
// We're inside node_modules, go to the last node_modules in the path
|
|
57
|
-
|
|
58
|
-
const lastNodeModulesPath = nodeModulesParts.join(node_path_1.sep);
|
|
59
|
-
const mobilecliPath = (0, node_path_1.join)(lastNodeModulesPath, "mobilecli", "bin", binaryName);
|
|
60
|
-
if ((0, node_fs_1.existsSync)(mobilecliPath)) {
|
|
61
|
-
return mobilecliPath;
|
|
62
|
-
}
|
|
59
|
+
nodeModulesRoots.push(pathParts.slice(0, lastNodeModulesIndex + 1).join(node_path_1.sep));
|
|
63
60
|
}
|
|
64
61
|
// Not in node_modules, look one directory up from current script
|
|
65
|
-
|
|
66
|
-
const
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
62
|
+
nodeModulesRoots.push((0, node_path_1.join)((0, node_path_1.dirname)((0, node_path_1.dirname)(__filename)), "node_modules"));
|
|
63
|
+
for (const root of nodeModulesRoots) {
|
|
64
|
+
// mobilecli <= 1.0.6 shipped the binary in its own bin directory
|
|
65
|
+
const legacyPath = (0, node_path_1.join)(root, "mobilecli", "bin", binaryName);
|
|
66
|
+
if ((0, node_fs_1.existsSync)(legacyPath)) {
|
|
67
|
+
return legacyPath;
|
|
68
|
+
}
|
|
69
|
+
// mobilecli >= 1.0.7 ships per-platform binaries in scoped packages
|
|
70
|
+
const scopedPath = (0, node_path_1.join)(root, "@mobilenext", scopedPackage, binaryName);
|
|
71
|
+
if ((0, node_fs_1.existsSync)(scopedPath)) {
|
|
72
|
+
return scopedPath;
|
|
73
|
+
}
|
|
70
74
|
}
|
|
71
75
|
throw new Error(`Could not find mobilecli binary for platform: ${platform}`);
|
|
72
76
|
}
|