@mobilenext/mobile-mcp 1.0.0 → 1.0.1
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/lib/mobilecli.js +35 -4
- package/lib/server.js +96 -21
- package/package.json +1 -1
package/lib/mobilecli.js
CHANGED
|
@@ -6,6 +6,7 @@ const node_path_1 = require("node:path");
|
|
|
6
6
|
const node_child_process_1 = require("node:child_process");
|
|
7
7
|
const TIMEOUT = 30000;
|
|
8
8
|
const MAX_BUFFER_SIZE = 1024 * 1024 * 8;
|
|
9
|
+
const DEFAULT_ALLOCATE_TIMEOUT_SECONDS = 900; // matches mobilecli's own "remote allocate --wait" default
|
|
9
10
|
class Mobilecli {
|
|
10
11
|
path = null;
|
|
11
12
|
constructor() { }
|
|
@@ -15,9 +16,13 @@ class Mobilecli {
|
|
|
15
16
|
}
|
|
16
17
|
return this.path;
|
|
17
18
|
}
|
|
18
|
-
executeCommand(args) {
|
|
19
|
+
executeCommand(args, timeoutMs) {
|
|
19
20
|
const path = this.getPath();
|
|
20
|
-
|
|
21
|
+
const options = { encoding: "utf8" };
|
|
22
|
+
if (timeoutMs !== undefined) {
|
|
23
|
+
options.timeout = timeoutMs;
|
|
24
|
+
}
|
|
25
|
+
return (0, node_child_process_1.execFileSync)(path, args, options).toString().trim();
|
|
21
26
|
}
|
|
22
27
|
spawnCommand(args) {
|
|
23
28
|
const binaryPath = this.getPath();
|
|
@@ -77,11 +82,37 @@ class Mobilecli {
|
|
|
77
82
|
return "failed " + error.message;
|
|
78
83
|
}
|
|
79
84
|
}
|
|
85
|
+
spawnRemoteLogin() {
|
|
86
|
+
const binaryPath = this.getPath();
|
|
87
|
+
return (0, node_child_process_1.spawn)(binaryPath, ["auth", "login", "--provider", "mobilenext"], {
|
|
88
|
+
stdio: ["ignore", "pipe", "pipe"],
|
|
89
|
+
});
|
|
90
|
+
}
|
|
80
91
|
remoteListDevices() {
|
|
81
92
|
return this.executeCommand(["remote", "list-devices"]);
|
|
82
93
|
}
|
|
83
|
-
remoteAllocate(
|
|
84
|
-
|
|
94
|
+
remoteAllocate(options) {
|
|
95
|
+
const args = ["remote", "allocate", "--platform", options.platform];
|
|
96
|
+
if (options.name) {
|
|
97
|
+
args.push("--name", options.name);
|
|
98
|
+
}
|
|
99
|
+
for (const version of options.version ?? []) {
|
|
100
|
+
args.push("--version", version);
|
|
101
|
+
}
|
|
102
|
+
if (options.type) {
|
|
103
|
+
args.push("--type", options.type);
|
|
104
|
+
}
|
|
105
|
+
if (options.wait) {
|
|
106
|
+
args.push("--wait");
|
|
107
|
+
}
|
|
108
|
+
if (options.timeoutSeconds !== undefined) {
|
|
109
|
+
args.push("--timeout", String(options.timeoutSeconds));
|
|
110
|
+
}
|
|
111
|
+
// only "--wait" makes this call block for as long as mobilecli's own allocation timeout
|
|
112
|
+
const execTimeoutMs = options.wait
|
|
113
|
+
? (options.timeoutSeconds ?? DEFAULT_ALLOCATE_TIMEOUT_SECONDS) * 1000 + 5000
|
|
114
|
+
: undefined;
|
|
115
|
+
return this.executeCommand(args, execTimeoutMs);
|
|
85
116
|
}
|
|
86
117
|
remoteRelease(deviceId) {
|
|
87
118
|
return this.executeCommand(["remote", "release", "--device", deviceId]);
|
package/lib/server.js
CHANGED
|
@@ -21,6 +21,7 @@ const mobile_device_1 = require("./mobile-device");
|
|
|
21
21
|
const utils_1 = require("./utils");
|
|
22
22
|
const ALLOWED_SCREENSHOT_EXTENSIONS = [".png", ".jpg", ".jpeg"];
|
|
23
23
|
const ALLOWED_RECORDING_EXTENSIONS = [".mp4"];
|
|
24
|
+
const LOGIN_PROMPT_TIMEOUT_MS = 15000;
|
|
24
25
|
const getAgentVersion = () => {
|
|
25
26
|
const json = require("../package.json");
|
|
26
27
|
return json.version;
|
|
@@ -122,6 +123,7 @@ const createMcpServer = () => {
|
|
|
122
123
|
const mobilecli = new mobilecli_1.Mobilecli();
|
|
123
124
|
const activeRecordings = new Map();
|
|
124
125
|
const agentVerifiedSimulators = new Set();
|
|
126
|
+
const activeLoginProcesses = [];
|
|
125
127
|
posthog("launch", {}).then();
|
|
126
128
|
const ensureMobilecliAvailable = () => {
|
|
127
129
|
try {
|
|
@@ -180,7 +182,8 @@ const createMcpServer = () => {
|
|
|
180
182
|
}
|
|
181
183
|
throw new robot_1.ActionableError(`Device "${deviceId}" not found. Use the mobile_list_available_devices tool to see available devices.`);
|
|
182
184
|
};
|
|
183
|
-
tool("mobile_list_available_devices", "List Devices", "List all available devices. This includes both physical mobile devices and mobile simulators and emulators. It returns both Android and iOS devices."
|
|
185
|
+
tool("mobile_list_available_devices", "List Devices", "List all available devices. This includes both physical mobile devices and mobile simulators and emulators. It returns both Android and iOS devices. " +
|
|
186
|
+
"These are local devices already connected to this machine, ready to use immediately at no cost - for devices from the shared remote cloud fleet, use mobile_list_remote_devices instead.", {}, { readOnlyHint: true }, async ({}, telemetry) => {
|
|
184
187
|
// from today onward, we must have mobilecli working
|
|
185
188
|
ensureMobilecliAvailable();
|
|
186
189
|
const devices = [];
|
|
@@ -255,27 +258,99 @@ const createMcpServer = () => {
|
|
|
255
258
|
const out = { devices };
|
|
256
259
|
return JSON.stringify(out);
|
|
257
260
|
});
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
}
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
261
|
+
tool("mobile_login_to_cloud_provider", "Login to Cloud Provider", "Start authenticating this machine with the remote device cloud provider. This is required once before mobile_list_remote_devices or mobile_allocate_remote_device will work; if either of those fails with an authentication error, call this tool and then retry. " +
|
|
262
|
+
"This starts a browser-based device-code login and returns quickly with a URL and a one-time code - it does NOT wait for the login to complete. Show the URL and code to the user verbatim and ask them to open the URL and enter the code in their own browser. " +
|
|
263
|
+
"The login keeps running in the background after this tool returns; once the user confirms they've completed it, retry the remote devices tool that originally failed. " +
|
|
264
|
+
"Only call this after the user has explicitly asked to connect to, log into, or use remote/cloud devices - never call it speculatively, since it interrupts the user to act in their browser.", {}, {}, async ({}) => {
|
|
265
|
+
ensureMobilecliAvailable();
|
|
266
|
+
const child = mobilecli.spawnRemoteLogin();
|
|
267
|
+
activeLoginProcesses.push(child);
|
|
268
|
+
const forget = () => {
|
|
269
|
+
const index = activeLoginProcesses.indexOf(child);
|
|
270
|
+
if (index !== -1) {
|
|
271
|
+
activeLoginProcesses.splice(index, 1);
|
|
272
|
+
}
|
|
273
|
+
};
|
|
274
|
+
return new Promise((resolve, reject) => {
|
|
275
|
+
let output = "";
|
|
276
|
+
let settled = false;
|
|
277
|
+
const promptTimeout = setTimeout(() => {
|
|
278
|
+
if (!settled) {
|
|
279
|
+
settled = true;
|
|
280
|
+
forget();
|
|
281
|
+
child.kill();
|
|
282
|
+
reject(new robot_1.ActionableError(`Timed out waiting for the login prompt from mobilecli. Output so far: ${output.trim() || "(none)"}`));
|
|
283
|
+
}
|
|
284
|
+
}, LOGIN_PROMPT_TIMEOUT_MS);
|
|
285
|
+
child.stdout?.on("data", (chunk) => {
|
|
286
|
+
output += chunk.toString();
|
|
287
|
+
if (!settled && output.includes("Waiting for authorization")) {
|
|
288
|
+
settled = true;
|
|
289
|
+
clearTimeout(promptTimeout);
|
|
290
|
+
const urlMatch = output.match(/(https?:\/\/\S+)/);
|
|
291
|
+
const codeMatch = output.match(/enter the code:\s*(\S+)/i);
|
|
292
|
+
if (urlMatch && codeMatch) {
|
|
293
|
+
resolve(`Authentication to mobilenext.ai started, please open ${urlMatch[1]} and enter the code: ${codeMatch[1]}`);
|
|
294
|
+
}
|
|
295
|
+
else {
|
|
296
|
+
resolve(`Authentication started, please check the output: ${output.trim()}`);
|
|
297
|
+
}
|
|
298
|
+
}
|
|
299
|
+
});
|
|
300
|
+
child.stderr?.on("data", (chunk) => {
|
|
301
|
+
output += chunk.toString();
|
|
302
|
+
});
|
|
303
|
+
child.on("error", (err) => {
|
|
304
|
+
forget();
|
|
305
|
+
if (!settled) {
|
|
306
|
+
settled = true;
|
|
307
|
+
clearTimeout(promptTimeout);
|
|
308
|
+
reject(new robot_1.ActionableError(`Failed to start login: ${err.message}`));
|
|
309
|
+
}
|
|
310
|
+
});
|
|
311
|
+
child.on("exit", (code) => {
|
|
312
|
+
forget();
|
|
313
|
+
if (!settled) {
|
|
314
|
+
settled = true;
|
|
315
|
+
clearTimeout(promptTimeout);
|
|
316
|
+
reject(new robot_1.ActionableError(`mobilecli auth login exited early (code ${code}). Output: ${output.trim() || "(none)"}`));
|
|
317
|
+
}
|
|
318
|
+
});
|
|
277
319
|
});
|
|
278
|
-
}
|
|
320
|
+
});
|
|
321
|
+
tool("mobile_list_remote_devices", "List Remote Devices", "List the catalog of device models (make, platform, OS version) available to reserve from the remote cloud device fleet. " +
|
|
322
|
+
"This is different from mobile_list_available_devices, which lists real devices and simulators/emulators already connected to this local machine and ready to use immediately at no cost. " +
|
|
323
|
+
"Remote devices live in a shared cloud fleet: they are not usable until reserved with mobile_allocate_remote_device, and reserving one may be a limited/billed resource. " +
|
|
324
|
+
"Requires mobile_login_to_cloud_provider to have been called first; if this fails with an authentication error, call that tool then retry.", {}, { readOnlyHint: true }, async ({}) => {
|
|
325
|
+
ensureMobilecliAvailable();
|
|
326
|
+
const result = mobilecli.remoteListDevices();
|
|
327
|
+
return result;
|
|
328
|
+
});
|
|
329
|
+
tool("mobile_allocate_remote_device", "Allocate Remote Device", "Reserve a physical device from the remote cloud fleet for exclusive use, returning a device identifier usable with the other mobile_* tools. " +
|
|
330
|
+
"Unlike local devices, a remote device is a shared and billed resource borrowed for the session - only call this after the user has explicitly asked to use a remote/cloud device, never speculatively or as a fallback when a local device isn't found. " +
|
|
331
|
+
"Requires mobile_login_to_cloud_provider to have been called first; if this fails with an authentication error, call that tool then retry. " +
|
|
332
|
+
"Use mobile_list_remote_devices first to see which names and versions actually exist in the fleet before filtering by them. " +
|
|
333
|
+
"Release the device with mobile_release_remote_device once the whole task is finished - releasing wipes the device's state, so do not release and reallocate between steps of the same task just to be tidy.", {
|
|
334
|
+
platform: zod_1.z.enum(["ios", "android"]).describe("The platform to allocate a device for"),
|
|
335
|
+
name: zod_1.z.string().optional().describe("Filter by device name/model. Supports a trailing * for prefix match (e.g. \"iPhone*\"), or an exact name (e.g. \"iPhone 16\")."),
|
|
336
|
+
version: zod_1.z.array(zod_1.z.string()).optional().describe("Filter by OS version. Supports comparison prefixes >=, >, <=, < (e.g. \">=18\"), or an exact version (e.g. \"18.6.2\"). Multiple values are ANDed together."),
|
|
337
|
+
type: zod_1.z.enum(["real"]).optional().describe("Device type filter. Currently only \"real\" (physical devices) is supported by the fleet."),
|
|
338
|
+
wait: zod_1.z.boolean().optional().describe("If true, block until the device has finished allocating and is ready to use, up to timeoutSeconds. If false/omitted, this returns as soon as the reservation is made, but the device may not be immediately ready."),
|
|
339
|
+
timeoutSeconds: zod_1.z.coerce.number().int().positive().optional().describe("Seconds to wait for allocation when wait is true. Defaults to 900 (15 minutes). Only relevant when wait is true."),
|
|
340
|
+
}, { destructiveHint: true }, async ({ platform, name, version, type, wait, timeoutSeconds }) => {
|
|
341
|
+
ensureMobilecliAvailable();
|
|
342
|
+
const result = mobilecli.remoteAllocate({ platform, name, version, type, wait, timeoutSeconds });
|
|
343
|
+
return result;
|
|
344
|
+
});
|
|
345
|
+
tool("mobile_release_remote_device", "Release Remote Device", "Release a device previously reserved with mobile_allocate_remote_device back to the remote cloud fleet so it becomes available to others. " +
|
|
346
|
+
"Releasing is destructive to the device's state: apps installed, files pushed, and any other changes made during this session are lost, and a later mobile_allocate_remote_device call may take time and could return a different physical unit. " +
|
|
347
|
+
"Only release once the whole task is finished - if there is more work to do on the same device shortly, keep holding it rather than releasing and reallocating.", {
|
|
348
|
+
device: zod_1.z.string().describe("The device identifier to release back to the remote fleet"),
|
|
349
|
+
}, { destructiveHint: true }, async ({ device }) => {
|
|
350
|
+
ensureMobilecliAvailable();
|
|
351
|
+
const result = mobilecli.remoteRelease(device);
|
|
352
|
+
return result;
|
|
353
|
+
});
|
|
279
354
|
tool("mobile_list_apps", "List Apps", "List all the installed apps on the device", {
|
|
280
355
|
device: zod_1.z.string().describe("The device identifier to use. Use mobile_list_available_devices to find which devices are available to you.")
|
|
281
356
|
}, { readOnlyHint: true }, async ({ device }) => {
|