Skip to content

Mobile MCP: let AI agents drive your iOS and Android apps. MCP server for simulators, emulators and real devices. Works with Claude Code, Codex, Cursor and any MCP client.

feat: add screenrecording to mp4 (#282)

Gil Megidish5b875bd6d4e0
diff --git a/package-lock.json b/package-lock.json
index b2dec17..7cd7984 100644
--- a/package-lock.json
+++ b/package-lock.json
@@ -9,6 +9,7 @@
"version": "0.0.1",
"license": "Apache-2.0",
"dependencies": {
+ "@mobilenext/mobilecli": "^0.1.60",
"@modelcontextprotocol/sdk": "1.26.0",
"ajv": "^8.18.0",
"commander": "14.0.0",
@@ -46,7 +47,7 @@
"node": ">=18"
},
"optionalDependencies": {
- "@mobilenext/mobilecli": "0.1.59"
+ "@mobilenext/mobilecli": "^0.1.60"
}
},
"node_modules/@babel/code-frame": {
@@ -755,9 +756,9 @@
}
},
"node_modules/@mobilenext/mobilecli": {
- "version": "0.1.59",
- "resolved": "https://registry.npmjs.org/@mobilenext/mobilecli/-/mobilecli-0.1.59.tgz",
- "integrity": "sha512-kzLw4v1nbHQgfiyUYsQQ39aR8p6La+W1v8YBtQPk7Z+IGL7FMWvHK+sK5vWrXSj7MuQXp22/ZY2rOrcndW+drQ==",
+ "version": "0.1.60",
+ "resolved": "https://registry.npmjs.org/@mobilenext/mobilecli/-/mobilecli-0.1.60.tgz",
+ "integrity": "sha512-XF3JfJH1xKSqCegYkT8sdDi8gv/8ovRp9Kn+L58Rw4np/wL/D065uzumcFF6IyDmreSUj9+kefbjMPYUQx5rSA==",
"license": "MIT",
"optional": true,
"bin": {
diff --git a/package.json b/package.json
index f35b8bc..1324011 100644
--- a/package.json
+++ b/package.json
@@ -34,7 +34,7 @@
"zod-to-json-schema": "3.25.0"
},
"optionalDependencies": {
- "@mobilenext/mobilecli": "0.1.59"
+ "@mobilenext/mobilecli": "0.1.60"
},
"devDependencies": {
"@eslint/eslintrc": "^3.2.0",
diff --git a/src/mobilecli.ts b/src/mobilecli.ts
index 2af0f43..ddaa898 100644
--- a/src/mobilecli.ts
+++ b/src/mobilecli.ts
@@ -1,6 +1,6 @@
import { existsSync } from "node:fs";
import { dirname, join, sep } from "node:path";
-import { execFileSync } from "node:child_process";
+import { execFileSync, spawn, ChildProcess } from "node:child_process";
export interface MobilecliDevicesOptions {
includeOffline?: boolean;
@@ -41,6 +41,13 @@ export class Mobilecli {
return execFileSync(path, args, { encoding: "utf8" }).toString().trim();
}
+ public spawnCommand(args: string[]): ChildProcess {
+ const binaryPath = this.getPath();
+ return spawn(binaryPath, args, {
+ stdio: ["ignore", "ignore", "ignore"],
+ });
+ }
+
public executeCommandBuffer(args: string[]): Buffer {
const path = this.getPath();
return execFileSync(path, args, {
diff --git a/src/server.ts b/src/server.ts
index c341b02..c8bd321 100644
--- a/src/server.ts
+++ b/src/server.ts
@@ -2,7 +2,9 @@ import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
import { z } from "zod";
import fs from "node:fs";
import os from "node:os";
+import path from "node:path";
import crypto from "node:crypto";
+import { ChildProcess } from "node:child_process";
import { error, trace } from "./logger";
import { AndroidRobot, AndroidDeviceManager } from "./android";
@@ -26,6 +28,12 @@ interface MobilecliDevicesResponse {
devices: MobilecliDevice[];
}
+interface ActiveRecording {
+ process: ChildProcess;
+ outputPath: string;
+ startedAt: number;
+}
+
export const getAgentVersion = (): string => {
const json = require("../package.json");
return json.version;
@@ -130,6 +138,7 @@ export const createMcpServer = (): McpServer => {
};
const mobilecli = new Mobilecli();
+ const activeRecordings = new Map<string, ActiveRecording>();
posthog("launch", {}).then();
const ensureMobilecliAvailable = (): void => {
@@ -185,7 +194,7 @@ export const createMcpServer = (): McpServer => {
tool(
"mobile_list_available_devices",
"List Devices",
- "List all available devices. This includes both physical devices and simulators. If there is more than one device returned, you need to let the user select one of them.",
+ "List all available devices. This includes both physical mobile devices and mobile simulators and emulators. It returns both Android and iOS devices.",
{},
{ readOnlyHint: true },
async ({}) => {
@@ -678,5 +687,92 @@ export const createMcpServer = (): McpServer => {
}
);
+ tool(
+ "mobile_start_screen_recording",
+ "Start Screen Recording",
+ "Start recording the screen of a mobile device. The recording runs in the background until stopped with mobile_stop_screen_recording. Returns the path where the recording will be saved.",
+ {
+ device: z.string().describe("The device identifier to use. Use mobile_list_available_devices to find which devices are available to you."),
+ output: z.string().optional().describe("The file path to save the recording to. If not provided, a temporary path will be used."),
+ timeLimit: z.number().optional().describe("Maximum recording duration in seconds. The recording will stop automatically after this time."),
+ },
+ { destructiveHint: true },
+ async ({ device, output, timeLimit }) => {
+ getRobotFromDevice(device);
+
+ if (activeRecordings.has(device)) {
+ throw new ActionableError(`Device "${device}" is already being recorded. Stop the current recording first with mobile_stop_screen_recording.`);
+ }
+
+ const outputPath = output || path.join(os.tmpdir(), `screen-recording-${Date.now()}.mp4`);
+
+ const args = ["screenrecord", "--device", device, "--output", outputPath, "--silent"];
+ if (timeLimit !== undefined) {
+ args.push("--time-limit", String(timeLimit));
+ }
+
+ const child = mobilecli.spawnCommand(args);
+
+ const cleanup = () => {
+ activeRecordings.delete(device);
+ };
+
+ child.on("error", cleanup);
+ child.on("exit", cleanup);
+
+ activeRecordings.set(device, {
+ process: child,
+ outputPath,
+ startedAt: Date.now(),
+ });
+
+ return `Screen recording started. Output will be saved to: ${outputPath}`;
+ }
+ );
+
+ tool(
+ "mobile_stop_screen_recording",
+ "Stop Screen Recording",
+ "Stop an active screen recording on a mobile device. Returns the file path, size, and approximate duration of the recording.",
+ {
+ device: z.string().describe("The device identifier to use. Use mobile_list_available_devices to find which devices are available to you."),
+ },
+ { destructiveHint: true },
+ async ({ device }) => {
+ const recording = activeRecordings.get(device);
+ if (!recording) {
+ throw new ActionableError(`No active recording found for device "${device}". Start a recording first with mobile_start_screen_recording.`);
+ }
+
+ const { process: child, outputPath, startedAt } = recording;
+ activeRecordings.delete(device);
+
+ child.kill("SIGINT");
+
+ await new Promise<void>(resolve => {
+ const timeout = setTimeout(() => {
+ child.kill("SIGKILL");
+ resolve();
+ }, 5 * 60 * 1000);
+
+ child.on("close", () => {
+ clearTimeout(timeout);
+ resolve();
+ });
+ });
+
+ const durationSeconds = Math.round((Date.now() - startedAt) / 1000);
+
+ if (!fs.existsSync(outputPath)) {
+ return `Recording stopped after ~${durationSeconds}s but the output file was not found at: ${outputPath}`;
+ }
+
+ const stats = fs.statSync(outputPath);
+ const fileSizeMB = (stats.size / (1024 * 1024)).toFixed(2);
+
+ return `Recording stopped. File: ${outputPath} (${fileSizeMB} MB, ~${durationSeconds}s)`;
+ }
+ );
+
return server;
};