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: accept a url with an upper-case scheme in mobile_open_url (#478)
Mobile Kevin Bot!a7333dbddbf4
diff --git a/src/server.ts b/src/server.tsindex f8b9cb3..b54e508 100644--- a/src/server.ts+++ b/src/server.ts@@ -15,7 +15,7 @@ import { getJpegDimensions } from "./jpeg";import { describeCoordinateMapping } from "./coordinate-mapping";import { Mobilecli } from "./mobilecli";import { MobileDevice } from "./mobile-device";-import { validateOutputPath, validateFileExtension } from "./utils";+import { validateOutputPath, validateFileExtension, withLowercaseScheme } from "./utils";import { formatElements } from "./format-elements";type ScreenshotContent = { type: "text", text: string } | { type: "image", data: string, mimeType: string };@@ -810,12 +810,13 @@ export const createMcpServer = (): McpServer => {{ readOnlyHint: false, destructiveHint: false, openWorldHint: true },async ({ device, url }) => {const allowUnsafeUrls = process.env.MOBILEMCP_ALLOW_UNSAFE_URLS === "1";- if (!allowUnsafeUrls && !url.startsWith("http://") && !url.startsWith("https://")) {+ const normalizedUrl = withLowercaseScheme(url);+ if (!allowUnsafeUrls && !normalizedUrl.startsWith("http://") && !normalizedUrl.startsWith("https://")) {throw new ActionableError("Only http:// and https:// URLs are allowed. Set MOBILEMCP_ALLOW_UNSAFE_URLS=1 to allow other URL schemes.");}const robot = getRobotFromDevice(device);- await robot.openUrl(url);+ await robot.openUrl(normalizedUrl);return `Opened URL: ${url}`;});diff --git a/src/utils.ts b/src/utils.tsindex dcc430d..63493a6 100644--- a/src/utils.ts+++ b/src/utils.ts@@ -18,6 +18,15 @@ export function validateLocale(locale: string): void {}}+// a url scheme per RFC 3986 section 3.1: a letter, then letters, digits, "+", "-" or "."+const URL_SCHEME = /^[a-zA-Z][a-zA-Z0-9+.-]*:/;++// url schemes are case-insensitive, but android matches an intent's scheme+// case-sensitively, so HTTPS://example.com would find no browser+export function withLowercaseScheme(url: string): string {+ return url.replace(URL_SCHEME, scheme => scheme.toLowerCase());+}+function resolveRoot(root: string): string {const resolved = path.resolve(root);diff --git a/test/server-open-url.test.ts b/test/server-open-url.test.tsnew file mode 100644index 0000000..ad7cb46--- /dev/null+++ b/test/server-open-url.test.ts@@ -0,0 +1,42 @@+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 url that passes the scheme check fails on the device lookup+const MISSING_DEVICE = "no-such-device";++const SCHEME_REJECTION = "Only http:// and https:// URLs are allowed";++const createConnectedClient = async (): Promise<Client> => {+ process.env.MOBILEMCP_DISABLE_TELEMETRY = "1";+ delete process.env.MOBILEMCP_ALLOW_UNSAFE_URLS;+ const server = createMcpServer();+ const client = new Client({ name: "open-url-test", version: "1.0.0" });+ const [clientTransport, serverTransport] = InMemoryTransport.createLinkedPair();+ await server.connect(serverTransport);+ await client.connect(clientTransport);+ return client;+};++const openUrl = async (url: string): Promise<string> => {+ const client = await createConnectedClient();+ const result: any = await client.callTool({ name: "mobile_open_url", arguments: { device: MISSING_DEVICE, url } });+ return result.content[0].text;+};++const schemeWasAccepted = (text: string): boolean => !text.includes(SCHEME_REJECTION) && text.includes(`Device "${MISSING_DEVICE}" not found`);++const schemeWasRejected = (text: string): boolean => text.includes(SCHEME_REJECTION);++// url schemes are case-insensitive (RFC 3986 section 3.1)+for (const url of ["https://example.com", "http://example.com", "HTTPS://EXAMPLE.COM", "Http://example.com/Path"]) {+ test(`accepts ${url}`, async () => {+ expect(schemeWasAccepted(await openUrl(url))).toBe(true);+ });+}++for (const url of ["javascript:alert(1)", "JAVASCRIPT:alert(1)", "file:///etc/hosts", "httpx://example.com", "example.com", "-h"]) {+ test(`rejects ${url}`, async () => {+ expect(schemeWasRejected(await openUrl(url))).toBe(true);+ });+}diff --git a/test/utils.ts b/test/utils.tsindex 12365c9..3d2a279 100644--- a/test/utils.ts+++ b/test/utils.ts@@ -6,10 +6,35 @@ import {validateLocale,validateFileExtension,validateOutputPath,+ withLowercaseScheme,} from "../src/utils";test.describe("utils", () => {+ // android resolves an intent's scheme case-sensitively, so HTTPS://example.com+ // finds no browser, although url schemes are case-insensitive+ test.describe("withLowercaseScheme", () => {+ test("should lowercase an upper-case scheme", () => {+ expect(withLowercaseScheme("HTTPS://EXAMPLE.COM")).toBe("https://EXAMPLE.COM");+ });++ test("should lowercase a mixed-case scheme and keep the case of the path", () => {+ expect(withLowercaseScheme("Http://example.com/Path?Q=A")).toBe("http://example.com/Path?Q=A");+ });++ test("should lowercase a custom scheme", () => {+ expect(withLowercaseScheme("MyApp://Screen/Two")).toBe("myapp://Screen/Two");+ });++ test("should leave a lower-case url unchanged", () => {+ expect(withLowercaseScheme("https://example.com/A")).toBe("https://example.com/A");+ });++ test("should leave a string without a scheme unchanged", () => {+ expect(withLowercaseScheme("Example.com/Path")).toBe("Example.com/Path");+ });+ });+test.describe("validatePackageName", () => {test("should accept a standard android package name", () => {expect(() => validatePackageName("com.example.app")).not.toThrow();