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: support orientation changes (#28)

Gil Megidish450349de69d3
diff --git a/src/android.ts b/src/android.ts
index 4da601f..407e764 100644
--- a/src/android.ts
+++ b/src/android.ts
@@ -3,7 +3,7 @@ import { execFileSync } from "child_process";
import * as xml from "fast-xml-parser";
-import { ActionableError, Button, InstalledApp, Robot, ScreenElement, ScreenElementRect, ScreenSize, SwipeDirection } from "./robot";
+import { ActionableError, Button, InstalledApp, Robot, ScreenElement, ScreenElementRect, ScreenSize, SwipeDirection, Orientation } from "./robot";
interface UiAutomatorXmlNode {
node: UiAutomatorXmlNode[];
@@ -195,6 +195,48 @@ export class AndroidRobot implements Robot {
public async tap(x: number, y: number): Promise<void> {
this.adb("shell", "input", "tap", `${x}`, `${y}`);
}
+
+ public async setOrientation(orientation: Orientation): Promise<void> {
+ // Android uses numbers for orientation:
+ // 0 - Portrait
+ // 1 - Landscape
+ const orientationValue = orientation === "portrait" ? 0 : 1;
+
+ // Set orientation using content provider
+ this.adb(
+ "shell",
+ "content",
+ "insert",
+ "--uri",
+ "content://settings/system",
+ "--bind",
+ "name:s:user_rotation",
+ "--bind",
+ `value:i:${orientationValue}`
+ );
+
+ // Force the orientation change
+ this.adb(
+ "shell",
+ "settings",
+ "put",
+ "system",
+ "accelerometer_rotation",
+ "0"
+ );
+ }
+
+ public async getOrientation(): Promise<Orientation> {
+ const rotation = this.adb(
+ "shell",
+ "settings",
+ "get",
+ "system",
+ "user_rotation"
+ ).toString().trim();
+
+ return rotation === "0" ? "portrait" : "landscape";
+ }
}
export const getConnectedDevices = (): string[] => {
diff --git a/src/ios.ts b/src/ios.ts
index cb01055..282a909 100644
--- a/src/ios.ts
+++ b/src/ios.ts
@@ -6,7 +6,7 @@ import { execFileSync } from "child_process";
import { Socket } from "net";
import { WebDriverAgent } from "./webdriver-agent";
-import { ActionableError, Button, InstalledApp, Robot, ScreenSize, SwipeDirection, ScreenElement } from "./robot";
+import { ActionableError, Button, InstalledApp, Robot, ScreenSize, SwipeDirection, ScreenElement, Orientation } from "./robot";
const WDA_PORT = 8100;
const IOS_TUNNEL_PORT = 60105;
@@ -164,6 +164,16 @@ export class IosRobot implements Robot {
unlinkSync(tmpFilename);
return buffer;
}
+
+ public async setOrientation(orientation: Orientation): Promise<void> {
+ const wda = await this.wda();
+ await wda.setOrientation(orientation);
+ }
+
+ public async getOrientation(): Promise<Orientation> {
+ const wda = await this.wda();
+ return await wda.getOrientation();
+ }
}
export class IosManager {
diff --git a/src/iphone-simulator.ts b/src/iphone-simulator.ts
index 8e8d640..55bbdeb 100644
--- a/src/iphone-simulator.ts
+++ b/src/iphone-simulator.ts
@@ -1,7 +1,7 @@
import { execFileSync } from "child_process";
import { WebDriverAgent } from "./webdriver-agent";
-import { ActionableError, Button, InstalledApp, Robot, ScreenElement, ScreenSize, SwipeDirection } from "./robot";
+import { ActionableError, Button, InstalledApp, Robot, ScreenElement, ScreenSize, SwipeDirection, Orientation } from "./robot";
export interface Simulator {
name: string;
@@ -158,6 +158,16 @@ export class Simctl implements Robot {
const wda = await this.wda();
return wda.getElementsOnScreen();
}
+
+ public async setOrientation(orientation: Orientation): Promise<void> {
+ const wda = await this.wda();
+ return wda.setOrientation(orientation);
+ }
+
+ public async getOrientation(): Promise<Orientation> {
+ const wda = await this.wda();
+ return wda.getOrientation();
+ }
}
export class SimctlManager {
diff --git a/src/robot.ts b/src/robot.ts
index 9e868a0..3b276c8 100644
--- a/src/robot.ts
+++ b/src/robot.ts
@@ -1,4 +1,3 @@
-
export interface Dimensions {
width: number;
height: number;
@@ -38,6 +37,8 @@ export class ActionableError extends Error {
}
}
+export type Orientation = "portrait" | "landscape";
+
export interface Robot {
/**
* Get the screen size of the device in pixels.
@@ -98,4 +99,15 @@ export interface Robot {
* return a filtered list of elements that make sense to interact with.
*/
getElementsOnScreen(): Promise<ScreenElement[]>;
+
+ /**
+ * Change the screen orientation of the device.
+ * @param orientation The desired orientation ("portrait" or "landscape")
+ */
+ setOrientation(orientation: Orientation): Promise<void>;
+
+ /**
+ * Get the current screen orientation.
+ */
+ getOrientation(): Promise<Orientation>;
}
diff --git a/src/server.ts b/src/server.ts
index 82f641d..a1f9f3f 100644
--- a/src/server.ts
+++ b/src/server.ts
@@ -285,5 +285,29 @@ export const createMcpServer = (): McpServer => {
}
);
+ tool(
+ "mobile_set_orientation",
+ "Change the screen orientation of the device",
+ {
+ orientation: z.enum(["portrait", "landscape"]).describe("The desired orientation"),
+ },
+ async ({ orientation }) => {
+ requireRobot();
+ await robot!.setOrientation(orientation);
+ return `Changed device orientation to ${orientation}`;
+ }
+ );
+
+ tool(
+ "mobile_get_orientation",
+ "Get the current screen orientation of the device",
+ {},
+ async () => {
+ requireRobot();
+ const orientation = await robot!.getOrientation();
+ return `Current device orientation is ${orientation}`;
+ }
+ );
+
return server;
};
diff --git a/src/webdriver-agent.ts b/src/webdriver-agent.ts
index fabd014..656af38 100644
--- a/src/webdriver-agent.ts
+++ b/src/webdriver-agent.ts
@@ -1,4 +1,4 @@
-import { ActionableError, SwipeDirection, ScreenSize, ScreenElement } from "./robot";
+import { ActionableError, SwipeDirection, ScreenSize, ScreenElement, Orientation } from "./robot";
export interface SourceTreeElementRect {
x: number;
@@ -249,4 +249,26 @@ export class WebDriverAgent {
});
});
}
+
+ public async setOrientation(orientation: Orientation): Promise<void> {
+ await this.withinSession(async sessionUrl => {
+ const url = `${sessionUrl}/orientation`;
+ await fetch(url, {
+ method: "POST",
+ headers: { "Content-Type": "application/json" },
+ body: JSON.stringify({
+ orientation: orientation.toUpperCase()
+ })
+ });
+ });
+ }
+
+ public async getOrientation(): Promise<Orientation> {
+ return this.withinSession(async sessionUrl => {
+ const url = `${sessionUrl}/orientation`;
+ const response = await fetch(url);
+ const json = await response.json();
+ return json.value.toLowerCase() as Orientation;
+ });
+ }
}