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.
TypeScript agentandroidandroid-automationappium-alternativeclaude-codecodexcursoremulatoriosios-automationmcpmcp-servermobilemobile-automationmobile-testingreal-devicesimulatorskills
feat: report the focused activity in mobile_get_foreground_app (#512)
Mobile Kevin Bot!9c4a48123ba0
diff --git a/src/mobile-device.ts b/src/mobile-device.tsindex 12cc1f5..6b99fd6 100644--- a/src/mobile-device.ts+++ b/src/mobile-device.ts@@ -1,5 +1,5 @@import { Mobilecli } from "./mobilecli";-import { ActionableError, Button, FoldPosture, InstalledApp, Orientation, Robot, ScreenElement, ScreenSize, ScreenshotOptions, SwipeDirection } from "./robot";+import { ActionableError, Button, FoldPosture, ForegroundApp, InstalledApp, Orientation, Robot, ScreenElement, ScreenSize, ScreenshotOptions, SwipeDirection } from "./robot";interface InstalledAppsResponse {status: "ok",@@ -64,6 +64,7 @@ interface ForegroundAppResponse {data: {packageName: string;appName?: string;+ activity?: string;};}@@ -215,12 +216,18 @@ export class MobileDevice implements Robot {})) as InstalledApp[];}- public async getForegroundApp(): Promise<InstalledApp> {+ public async getForegroundApp(): Promise<ForegroundApp> {const response = this.runJsonCommand<ForegroundAppResponse>(["apps", "foreground"]);- return {+ const app: ForegroundApp = {appName: response.data.appName || response.data.packageName,packageName: response.data.packageName,};++ if (response.data.activity) {+ app.activity = response.data.activity;+ }++ return app;}public async launchApp(packageName: string, locale?: string): Promise<void> {diff --git a/src/robot.ts b/src/robot.tsindex 30f98b4..b494ede 100644--- a/src/robot.ts+++ b/src/robot.ts@@ -12,6 +12,11 @@ export interface InstalledApp {appName: string;}+export interface ForegroundApp extends InstalledApp {+ // activity class on android, view controller class on ios+ activity?: string;+}+export type SwipeDirection = "up" | "down" | "left" | "right";export type Button = "HOME" | "BACK" | "VOLUME_UP" | "VOLUME_DOWN" | "ENTER" | "DPAD_CENTER" | "DPAD_UP" | "DPAD_DOWN" | "DPAD_LEFT" | "DPAD_RIGHT";@@ -92,7 +97,7 @@ export interface Robot {/*** Get the app currently in the foreground. Optional, not all robots support it.*/- getForegroundApp?(): Promise<InstalledApp>;+ getForegroundApp?(): Promise<ForegroundApp>;/*** Override the GPS location reported by the device. Optional, not all robots support it.diff --git a/src/server.ts b/src/server.tsindex 348abe1..e919c2a 100644--- a/src/server.ts+++ b/src/server.ts@@ -573,7 +573,7 @@ export const createMcpServer = (): McpServer => {tool("mobile_get_foreground_app","Get Foreground App",- "Get the app currently in the foreground on the device. Use this to verify which app or screen you are on before interacting with it.",+ "Get the app currently in the foreground on the device, including the focused activity class on Android or view controller class on iOS when available. Use this to verify which app or screen you are on before interacting with it.",{device: z.string().describe("The device identifier to use. Use mobile_list_available_devices to find which devices are available to you.")},@@ -585,7 +585,8 @@ export const createMcpServer = (): McpServer => {}const app = await robot.getForegroundApp();- return `Foreground app: ${app.appName} (${app.packageName})`;+ const activity = app.activity ? `, activity: ${app.activity}` : "";+ return `Foreground app: ${app.appName} (${app.packageName})${activity}`;});diff --git a/test/mobile-device.test.ts b/test/mobile-device.test.tsindex b68fffc..b7b242b 100644--- a/test/mobile-device.test.ts+++ b/test/mobile-device.test.ts@@ -163,6 +163,13 @@ test.describe("MobileDevice", () => {expect(calls[0]).toEqual(["apps", "foreground", "--device", "test-device"]);expect(app).toEqual({ appName: "com.example.app", packageName: "com.example.app" });});++ test("getForegroundApp should include the focused activity reported by mobilecli", async () => {+ const { device } = createMockMobileDevice(JSON.stringify({ status: "ok", data: { packageName: "com.android.settings", appName: "Settings", activity: "com.android.settings.SubSettings" } }));+ const app = await device.getForegroundApp();++ expect(app).toEqual({ appName: "Settings", packageName: "com.android.settings", activity: "com.android.settings.SubSettings" });+ });});test.describe("fold", () => {diff --git a/test/server-foreground-app.test.ts b/test/server-foreground-app.test.tsnew file mode 100644index 0000000..f1b72b3--- /dev/null+++ b/test/server-foreground-app.test.ts@@ -0,0 +1,62 @@+import { expect, test } from "@playwright/test";+import { Client, InMemoryTransport } from "@modelcontextprotocol/client";+import { createMcpServer } from "../src/server";+import { Mobilecli } from "../src/mobilecli";++const ANDROID_DEVICE = "emulator-5554";++type ForegroundAppData = {+ packageName: string;+ appName?: string;+ activity?: string;+};++const originalExecuteCommand = Mobilecli.prototype.executeCommand;++// answers the mobilecli commands that mobile_get_foreground_app runs, as if one android emulator is attached+const pretendMobilecliReportsForegroundApp = (foreground: ForegroundAppData) => {+ Mobilecli.prototype.executeCommand = function(args: string[]): string {+ if (args[0] === "--version") {+ return "mobilecli version 1.0.18";+ }++ if (args[0] === "devices") {+ return JSON.stringify({ status: "ok", data: { devices: [{ id: ANDROID_DEVICE, name: "Pixel", platform: "android", type: "emulator", version: "15", state: "online" }] } });+ }++ if (args[0] === "apps" && args[1] === "foreground") {+ return JSON.stringify({ status: "ok", data: foreground });+ }++ throw new Error(`unexpected mobilecli command: ${args.join(" ")}`);+ };+};++const getForegroundAppText = async (): Promise<string> => {+ process.env.MOBILEMCP_DISABLE_TELEMETRY = "1";+ const server = createMcpServer();+ const client = new Client({ name: "foreground-app-test", version: "1.0.0" });+ const [clientTransport, serverTransport] = InMemoryTransport.createLinkedPair();+ await server.connect(serverTransport);+ await client.connect(clientTransport);+ try {+ const result: any = await client.callTool({ name: "mobile_get_foreground_app", arguments: { device: ANDROID_DEVICE } });+ return result.content[0].text;+ } finally {+ await client.close();+ }+};++test.afterEach(() => {+ Mobilecli.prototype.executeCommand = originalExecuteCommand;+});++test("includes the focused activity when mobilecli reports one", async () => {+ pretendMobilecliReportsForegroundApp({ packageName: "com.android.settings", appName: "Settings", activity: "com.android.settings.SubSettings" });+ expect(await getForegroundAppText()).toBe("Foreground app: Settings (com.android.settings), activity: com.android.settings.SubSettings");+});++test("leaves out the activity when mobilecli does not report one", async () => {+ pretendMobilecliReportsForegroundApp({ packageName: "com.android.settings", appName: "Settings" });+ expect(await getForegroundAppText()).toBe("Foreground app: Settings (com.android.settings)");+});