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.

fix: match the tap hint to the screenshot's orientation (#454)

Nông Văn Tình3b51952bcd83
diff --git a/src/coordinate-mapping.ts b/src/coordinate-mapping.ts
index 7812d5b..21a3c20 100644
--- a/src/coordinate-mapping.ts
+++ b/src/coordinate-mapping.ts
@@ -6,6 +6,22 @@ const formatRatio = (ratio: number): string => Number(ratio.toFixed(RATIO_DECIMA
const isPositive = (size: Dimensions): boolean => size.width > 0 && size.height > 0;
+const isLandscape = (size: Dimensions): boolean => size.width > size.height;
+
+/**
+ * The screenshot always shows the display as it is rotated right now, but the
+ * reported screen size can be the device's natural (portrait) size even while
+ * a landscape app is in front. Taps land in the current rotation's coordinates,
+ * so when the two disagree the screen size is turned to match the screenshot.
+ */
+const orientLike = (screen: Dimensions, screenshot: Dimensions): Dimensions => {
+ if (isLandscape(screen) === isLandscape(screenshot)) {
+ return screen;
+ }
+
+ return { width: screen.height, height: screen.width };
+};
+
/**
* Plain-words instruction for a model that reads pixel positions off a
* screenshot and taps in screen coordinates. The screenshot is usually
@@ -15,11 +31,13 @@ const isPositive = (size: Dimensions): boolean => size.width > 0 && size.height
* Returns null when either size is unknown, in which case no instruction is
* better than a wrong one.
*/
-export const describeCoordinateMapping = (screenshot: Dimensions, screen: Dimensions): string | null => {
- if (!isPositive(screenshot) || !isPositive(screen)) {
+export const describeCoordinateMapping = (screenshot: Dimensions, reportedScreen: Dimensions): string | null => {
+ if (!isPositive(screenshot) || !isPositive(reportedScreen)) {
return null;
}
+ const screen = orientLike(reportedScreen, screenshot);
+
if (screenshot.width === screen.width && screenshot.height === screen.height) {
return `Screenshot is ${screenshot.width}x${screenshot.height} and its coordinates match the screen.`;
}
diff --git a/test/coordinate-mapping.ts b/test/coordinate-mapping.ts
index 80e6b3b..9dc8c9b 100644
--- a/test/coordinate-mapping.ts
+++ b/test/coordinate-mapping.ts
@@ -22,6 +22,28 @@ test.describe("coordinate mapping", () => {
expect(text).toBe("Screenshot is 1179x2556. Screen coordinates are 393x852. To tap something you see in this screenshot, multiply its x by 0.333 and y by 0.333.");
});
+ test("uses the rotated screen size when a landscape app is in front of a portrait-reported screen", () => {
+ // A 720x1600 Android phone running a landscape-locked app: the screenshot is landscape,
+ // but the device reports its natural portrait size.
+ const text = describeCoordinateMapping({ width: 1024, height: 461 }, { width: 720, height: 1600 });
+ expect(text).toBe("Screenshot is 1024x461. Screen coordinates are 1600x720. To tap something you see in this screenshot, multiply its x by 1.563 and y by 1.562.");
+ });
+
+ test("says coordinates match when a full-size landscape screenshot meets a portrait-reported screen", () => {
+ const text = describeCoordinateMapping({ width: 1600, height: 720 }, { width: 720, height: 1600 });
+ expect(text).toBe("Screenshot is 1600x720 and its coordinates match the screen.");
+ });
+
+ test("rotates a portrait screen measured in points to match a landscape pixel screenshot", () => {
+ const text = describeCoordinateMapping({ width: 2556, height: 1179 }, { width: 393, height: 852 });
+ expect(text).toBe("Screenshot is 2556x1179. Screen coordinates are 852x393. To tap something you see in this screenshot, multiply its x by 0.333 and y by 0.333.");
+ });
+
+ test("keeps a landscape-reported screen as is when the screenshot is landscape too", () => {
+ const text = describeCoordinateMapping({ width: 1024, height: 461 }, { width: 1600, height: 720 });
+ expect(text).toBe("Screenshot is 1024x461. Screen coordinates are 1600x720. To tap something you see in this screenshot, multiply its x by 1.563 and y by 1.562.");
+ });
+
test("returns nothing when the screen size is unknown", () => {
expect(describeCoordinateMapping({ width: 590, height: 1278 }, { width: 0, height: 0 })).toBeNull();
});