@mobilenext/mobile-mcp 1.0.2 → 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.
@@ -2,6 +2,7 @@
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");
5
6
  const flattenUIElement = (element) => {
6
7
  const screenElement = {
7
8
  type: element.type,
@@ -11,7 +12,11 @@ const flattenUIElement = (element) => {
11
12
  value: element.value,
12
13
  identifier: element.identifier,
13
14
  rect: element.rect,
15
+ ref: element.ref,
14
16
  focused: element.focused,
17
+ selected: element.selected,
18
+ checked: element.checked,
19
+ enabled: element.enabled,
15
20
  };
16
21
  if (!element.children) {
17
22
  return [screenElement];
@@ -29,8 +34,17 @@ class MobileDevice {
29
34
  const fullArgs = [...args, "--device", this.deviceId];
30
35
  return this.mobilecli.executeCommand(fullArgs);
31
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
+ }
32
46
  async getScreenSize() {
33
- const response = JSON.parse(this.runCommand(["device", "info"]));
47
+ const response = this.runJsonCommand(["device", "info"]);
34
48
  if (response.data.device.screenSize) {
35
49
  return response.data.device.screenSize;
36
50
  }
@@ -85,17 +99,34 @@ class MobileDevice {
85
99
  }
86
100
  this.runCommand(["io", "swipe", `${Math.round(x)},${Math.round(y)},${Math.round(endX)},${Math.round(endY)}`]);
87
101
  }
88
- async getScreenshot() {
89
- const fullArgs = ["screenshot", "--device", this.deviceId, "--format", "png", "--output", "-"];
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
+ }
90
114
  return this.mobilecli.executeCommandBuffer(fullArgs);
91
115
  }
92
116
  async listApps() {
93
- const response = JSON.parse(this.runCommand(["apps", "list"]));
117
+ const response = this.runJsonCommand(["apps", "list"]);
94
118
  return response.data.map(app => ({
95
119
  appName: app.appName || app.packageName,
96
120
  packageName: app.packageName,
97
121
  }));
98
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
+ }
99
130
  async launchApp(packageName, locale) {
100
131
  const args = ["apps", "launch", packageName];
101
132
  if (locale) {
@@ -125,6 +156,9 @@ class MobileDevice {
125
156
  // mobilecli rejects fractional coordinates ("x and y must be integers")
126
157
  this.runCommand(["io", "tap", `${Math.round(x)},${Math.round(y)}`]);
127
158
  }
159
+ async tapByRef(ref) {
160
+ this.runCommand(["io", "tap", ref]);
161
+ }
128
162
  async doubleTap(x, y) {
129
163
  // TODO: should move into mobilecli itself as "io doubletap"
130
164
  await this.tap(x, y);
@@ -134,15 +168,60 @@ class MobileDevice {
134
168
  this.runCommand(["io", "longpress", `${Math.round(x)},${Math.round(y)}`, "--duration", `${duration}`]);
135
169
  }
136
170
  async getElementsOnScreen() {
137
- const response = JSON.parse(this.runCommand(["dump", "ui"]));
171
+ const response = this.runJsonCommand(["dump", "ui"]);
138
172
  return response.data.elements.flatMap(element => flattenUIElement(element));
139
173
  }
140
174
  async setOrientation(orientation) {
141
175
  this.runCommand(["device", "orientation", "set", orientation]);
142
176
  }
143
177
  async getOrientation() {
144
- const response = JSON.parse(this.runCommand(["device", "orientation", "get"]));
178
+ const response = this.runJsonCommand(["device", "orientation", "get"]);
145
179
  return response.data.orientation;
146
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
+ }
147
226
  }
148
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 binaryName = `mobilecli-${normalizedPlatform}-${normalizedArch}${ext}`;
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
- const nodeModulesParts = pathParts.slice(0, lastNodeModulesIndex + 1);
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
- const scriptDir = (0, node_path_1.dirname)(__filename);
66
- const parentDir = (0, node_path_1.dirname)(scriptDir);
67
- const mobilecliPath = (0, node_path_1.join)(parentDir, "node_modules", "mobilecli", "bin", binaryName);
68
- if ((0, node_fs_1.existsSync)(mobilecliPath)) {
69
- return mobilecliPath;
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
  }