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(swipe): reject a non-positive swipe distance (#499)

Gil Megidishce098e168036
diff --git a/src/server.ts b/src/server.ts
index 9db3237..d0e1c61 100644
--- a/src/server.ts
+++ b/src/server.ts
@@ -835,7 +835,7 @@ export const createMcpServer = (): McpServer => {
direction: z.enum(["up", "down", "left", "right"]).describe("The direction to swipe"),
x: z.coerce.number().min(0).optional().describe("The x coordinate to start the swipe from, in pixels. If not provided, uses center of screen"),
y: z.coerce.number().min(0).optional().describe("The y coordinate to start the swipe from, in pixels. If not provided, uses center of screen"),
- distance: z.coerce.number().optional().describe("The distance to swipe in pixels. Defaults to 400 pixels for iOS or 30% of screen dimension for Android"),
+ distance: z.coerce.number().min(1).optional().describe("The distance to swipe in pixels. Defaults to 400 pixels for iOS or 30% of screen dimension for Android"),
},
{ readOnlyHint: false, destructiveHint: false, openWorldHint: true },
async ({ device, direction, x, y, distance }) => {
diff --git a/test/server-swipe-arguments.test.ts b/test/server-swipe-arguments.test.ts
new file mode 100644
index 0000000..288ca8d
--- /dev/null
+++ b/test/server-swipe-arguments.test.ts
@@ -0,0 +1,58 @@
+import { expect, test } from "@playwright/test";
+import { Client, InMemoryTransport } from "@modelcontextprotocol/client";
+import { createMcpServer } from "../src/server";
+
+// no device is attached under this id, so a call that passes argument checks fails on the device lookup
+const MISSING_DEVICE = "no-such-device";
+
+const createConnectedClient = async (): Promise<Client> => {
+ process.env.MOBILEMCP_DISABLE_TELEMETRY = "1";
+ const server = createMcpServer();
+ const client = new Client({ name: "swipe-arguments-test", version: "1.0.0" });
+ const [clientTransport, serverTransport] = InMemoryTransport.createLinkedPair();
+ await server.connect(serverTransport);
+ await client.connect(clientTransport);
+ return client;
+};
+
+const swipe = async (args: Record<string, unknown>): Promise<string> => {
+ const client = await createConnectedClient();
+ try {
+ const result: any = await client.callTool({
+ name: "mobile_swipe_on_screen",
+ arguments: { device: MISSING_DEVICE, direction: "up", x: 100, y: 1000, ...args },
+ });
+ return result.content[0].text;
+ } catch (error: any) {
+ // a schema violation is reported as a protocol error rather than a tool result
+ return String(error);
+ } finally {
+ await client.close();
+ }
+};
+
+const reachedTheDeviceLookup = (text: string): boolean => text.includes(`Device "${MISSING_DEVICE}" not found`);
+
+test("rejects a negative swipe distance instead of swiping the opposite way", async () => {
+ const text = await swipe({ distance: -400 });
+ expect(text).not.toContain("Swiped up");
+ expect(reachedTheDeviceLookup(text)).toBe(false);
+});
+
+test("rejects a zero swipe distance, which no robot treats the same way", async () => {
+ const text = await swipe({ distance: 0 });
+ expect(text).not.toContain("Swiped up");
+ expect(reachedTheDeviceLookup(text)).toBe(false);
+});
+
+test("still accepts a positive distance and an omitted distance", async () => {
+ expect(reachedTheDeviceLookup(await swipe({ distance: 400 }))).toBe(true);
+ expect(reachedTheDeviceLookup(await swipe({ distance: 1 }))).toBe(true);
+ expect(reachedTheDeviceLookup(await swipe({}))).toBe(true);
+});
+
+test("rejects a negative distance given as a string, which the schema coerces", async () => {
+ const text = await swipe({ distance: "-400" });
+ expect(text).not.toContain("Swiped up");
+ expect(reachedTheDeviceLookup(text)).toBe(false);
+});