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: reject a swipe that gives only one of x and y (#477)

Mobile Kevin Bot!17f2eae321ce
diff --git a/src/server.ts b/src/server.ts
index e919c2a..659a1e9 100644
--- a/src/server.ts
+++ b/src/server.ts
@@ -836,12 +836,16 @@ export const createMcpServer = (): McpServer => {
{
device: z.string().describe("The device identifier to use. Use mobile_list_available_devices to find which devices are available to you."),
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"),
+ x: z.coerce.number().min(0).optional().describe("The x coordinate to start the swipe from, in pixels. Give both x and y, or neither to start from the center of screen"),
+ y: z.coerce.number().min(0).optional().describe("The y coordinate to start the swipe from, in pixels. Give both x and y, or neither to start from the center of screen"),
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 }) => {
+ if ((x === undefined) !== (y === undefined)) {
+ throw new ActionableError("Provide both x and y to start the swipe from a point, or neither to start from the center of the screen");
+ }
+
const robot = getRobotFromDevice(device);
if (x !== undefined && y !== undefined) {
diff --git a/test/server-swipe.test.ts b/test/server-swipe.test.ts
new file mode 100644
index 0000000..1440666
--- /dev/null
+++ b/test/server-swipe.test.ts
@@ -0,0 +1,46 @@
+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-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();
+ const result: any = await client.callTool({ name: "mobile_swipe_on_screen", arguments: { device: MISSING_DEVICE, direction: "up", ...args } });
+ return result.content[0].text;
+};
+
+const reachedTheDeviceLookup = (text: string): boolean => text.includes(`Device "${MISSING_DEVICE}" not found`);
+
+test("rejects a swipe that gives x without y", async () => {
+ const text = await swipe({ x: 100 });
+
+ expect(text).toContain("Provide both x and y");
+ expect(reachedTheDeviceLookup(text)).toBe(false);
+});
+
+test("rejects a swipe that gives y without x", async () => {
+ const text = await swipe({ y: 620 });
+
+ expect(text).toContain("Provide both x and y");
+ expect(reachedTheDeviceLookup(text)).toBe(false);
+});
+
+test("accepts a swipe that gives both x and y", async () => {
+ expect(reachedTheDeviceLookup(await swipe({ x: 100, y: 620 }))).toBe(true);
+});
+
+test("accepts a swipe that gives neither x nor y", async () => {
+ expect(reachedTheDeviceLookup(await swipe({}))).toBe(true);
+});