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
fix: round coordinates before passing them to mobilecli (#403)
Chienae5a261c3600
diff --git a/src/mobile-device.ts b/src/mobile-device.tsindex d171c8a..261953a 100644--- a/src/mobile-device.ts+++ b/src/mobile-device.ts@@ -153,7 +153,7 @@ export class MobileDevice implements Robot {break;}- this.runCommand(["io", "swipe", `${x},${y},${endX},${endY}`]);+ this.runCommand(["io", "swipe", `${Math.round(x)},${Math.round(y)},${Math.round(endX)},${Math.round(endY)}`]);}public async getScreenshot(): Promise<Buffer> {@@ -203,7 +203,8 @@ export class MobileDevice implements Robot {}public async tap(x: number, y: number): Promise<void> {- this.runCommand(["io", "tap", `${x},${y}`]);+ // mobilecli rejects fractional coordinates ("x and y must be integers")+ this.runCommand(["io", "tap", `${Math.round(x)},${Math.round(y)}`]);}public async doubleTap(x: number, y: number): Promise<void> {@@ -213,7 +214,7 @@ export class MobileDevice implements Robot {}public async longPress(x: number, y: number, duration: number): Promise<void> {- this.runCommand(["io", "longpress", `${x},${y}`, "--duration", `${duration}`]);+ this.runCommand(["io", "longpress", `${Math.round(x)},${Math.round(y)}`, "--duration", `${duration}`]);}public async getElementsOnScreen(): Promise<ScreenElement[]> {diff --git a/test/mobile-device.test.ts b/test/mobile-device.test.tsnew file mode 100644index 0000000..9358264--- /dev/null+++ b/test/mobile-device.test.ts@@ -0,0 +1,52 @@+import { test, expect } from "@playwright/test";++import { MobileDevice } from "../src/mobile-device";++function createMockMobileDevice(mockResponse: string): { device: MobileDevice; calls: string[][] } {+ const device = new MobileDevice("test-device");+ const calls: string[][] = [];++ (device as any).mobilecli.executeCommand = function(args: string[]): string {+ calls.push(args);+ return mockResponse;+ };++ return { device, calls };+}++test.describe("MobileDevice", () => {++ test.describe("coordinate rounding", () => {++ test("tap should round fractional coordinates before calling mobilecli", async () => {+ const { device, calls } = createMockMobileDevice("");+ await device.tap(450, 784.5);++ expect(calls.length).toBe(1);+ expect(calls[0]).toEqual(["io", "tap", "450,785", "--device", "test-device"]);+ });++ test("longPress should round fractional coordinates before calling mobilecli", async () => {+ const { device, calls } = createMockMobileDevice("");+ await device.longPress(100.4, 200.6, 500);++ expect(calls.length).toBe(1);+ expect(calls[0]).toEqual(["io", "longpress", "100,201", "--duration", "500", "--device", "test-device"]);+ });++ test("swipeFromCoordinate should round fractional coordinates before calling mobilecli", async () => {+ const { device, calls } = createMockMobileDevice("");+ await device.swipeFromCoordinate(100.5, 300.2, "up", 400);++ expect(calls.length).toBe(1);+ expect(calls[0]).toEqual(["io", "swipe", "101,300,101,-100", "--device", "test-device"]);+ });++ test("tap should keep integer coordinates unchanged", async () => {+ const { device, calls } = createMockMobileDevice("");+ await device.tap(450, 785);++ expect(calls[0]).toEqual(["io", "tap", "450,785", "--device", "test-device"]);+ });+ });+});