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
Initial import
gmegidish7bd6e26483a8
diff --git a/.gitignore b/.gitignorenew file mode 100644index 0000000..491fc35--- /dev/null+++ b/.gitignore@@ -0,0 +1,2 @@+node_modules+libdiff --git a/package.json b/package.jsonnew file mode 100644index 0000000..335220f--- /dev/null+++ b/package.json@@ -0,0 +1,48 @@+{+ "name": "@mobile-next/appium-mcp",+ "version": "0.0.6",+ "description": "Appium MCP",+ "repository": {+ "type": "git",+ "url": "git+https://github.com/mobile-next/appium-mcp.git"+ },+ "engines": {+ "node": ">=18"+ },+ "license": "Apache-2.0",+ "scripts": {+ "build": "tsc",+ "lint": "eslint .",+ "watch": "tsc --watch",+ "clean": "rm -rf lib"+ },+ "exports": {+ "./package.json": "./package.json",+ ".": {+ "types": "./index.d.ts",+ "default": "./index.js"+ }+ },+ "dependencies": {+ "@modelcontextprotocol/sdk": "^1.6.1",+ "commander": "^13.1.0",+ "webdriverio": "^9.12.2",+ "zod-to-json-schema": "^3.24.4"+ },+ "devDependencies": {+ "@eslint/eslintrc": "^3.2.0",+ "@eslint/js": "^9.19.0",+ "@stylistic/eslint-plugin": "^3.0.1",+ "@types/node": "^22.13.10",+ "@typescript-eslint/eslint-plugin": "^8.26.1",+ "@typescript-eslint/parser": "^8.26.1",+ "@typescript-eslint/utils": "^8.26.1",+ "eslint": "^9.19.0",+ "eslint-plugin-import": "^2.31.0",+ "eslint-plugin-notice": "^1.0.0",+ "typescript": "^5.8.2"+ },+ "bin": {+ "mcp-server-appium": "cli.js"+ }+}diff --git a/src/index.ts b/src/index.tsnew file mode 100644index 0000000..18e732d--- /dev/null+++ b/src/index.ts@@ -0,0 +1,275 @@+import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";+import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";+import { z } from "zod";++import { appendFileSync, writeFileSync } from 'fs';+import { execSync } from 'child_process';+import { remote } from 'webdriverio';++const capabilities = {+ platformName: 'Android',+ 'appium:automationName': 'UiAutomator2',+ 'appium:deviceName': 'Android',+};++const wdOpts = {+ hostname: process.env.APPIUM_HOST || 'localhost',+ port: parseInt(process.env.APPIUM_PORT || "4723", 10) || 4723,+ // logLevel: 'info',+ capabilities,+};++let driver: WebdriverIO.Browser;++const server = new McpServer({+ name: "appium-mcp",+ version: "1.0.0",+ capabilities: {+ resources: {},+ tools: {},+ },+});++const log = (text: string) => {+ appendFileSync('/tmp/log.txt', `${text}\n`);+};++server.tool(+ "list-apps-on-device",+ "List all apps on device",+ {},+ async ({}) => {+ log(`gilm: listing apps on device`);+ const result = execSync(`adb shell pm list packages`)+ .toString()+ .split("\n")+ .filter(line => line.startsWith("package:"))+ .map(line => line.substring("package:".length));+ log(`gilm result: ${result.toString()}`);++ return {+ content: [{ type: 'text', text: `These apps are installed on the device: ${result.join(",")}` }]+ };+ }+);++server.tool(+ "launch-app",+ "Launch an app on mobile device",+ {+ packageName: z.string().describe("The package name of the app to launch"),+ },+ async ({ packageName }) => {+ log(`gilm launching packageName: "${packageName}"`);+ try {+ // FIXME: use appium or monkey for this+ const result1 = execSync(`adb shell cmd package resolve-activity ${packageName}`)+ .toString()+ .split("\n")+ .map(line => line.trim())+ .filter(line => line.startsWith("name="))+ .map(line => line.substring("name=".length));++ if (result1.length !== 1) {+ return {+ content: [{ type: 'text', text: `Error launching app: ${packageName}, got too many entries with 'name': ${result1.join(",")}` }],+ isError: true+ };+ }++ log(`gilm: starting activity: ${packageName}, ${result1[0]}`);+ await driver.startActivity(packageName, result1[0]);++ return {+ content: [{ type: 'text', text: `Successfully launched app ${packageName}` }]+ };+ } catch (error: any) {+ return {+ content: [{ type: 'text', text: `Error launching settings app: ${error.message}` }],+ isError: true+ };+ }+ }+);++server.tool(+ "click-element",+ "Click on an element on device",+ {+ text: z.string().describe("Text of the element to click"),+ },+ async ({ text }) => {+ log(`gilm: clicking: ${text}`);+ try {++ // const screenshot = await driver.takeScreenshot();+ // writeFileSync('/tmp/screenshot.png', screenshot);+ const element = await driver.$(`//*[contains(@text, "${text}")]`);+ log(`gilm: found element: ${JSON.stringify(element)}`);++ if (!element) {+ log("gilm: element not found");++ return {+ content: [{ type: 'text', text: `Element with text "${text}" not found` }],+ isError: true+ };+ }++ await element.click();++ return {+ content: [{ type: 'text', text: `Successfully clicked on element with text "${text}"` }]+ };+ } catch (error: any) {+ return {+ content: [{ type: 'text', text: `Element with text "${text}" not found` }],+ isError: true+ };+ }+ }+);+++server.tool(+ "press-back-button",+ "Press the back button on device",+ {},+ async ({}) => {+ log(`gilm: clicking: back`);+ try {+ await driver.back();+ return {+ content: [{ type: 'text', text: `Successfully pressed the back button` }]+ };+ } catch (error: any) {+ return {+ content: [{ type: 'text', text: `Failed to press the back button` }],+ isError: true+ };+ }+ }+);++server.tool(+ "open-url",+ "Open a URL in browser on device",+ {+ url: z.string().describe("The URL to open"),+ },+ async ({ url }) => {+ log(`gilm: opening URL: ${url}`);+ try {+ await driver.url(url);+ return {+ content: [{ type: 'text', text: `Successfully opened URL: ${url}` }]+ };+ } catch (error: any) {+ return {+ content: [{ type: 'text', text: `Failed to open URL: ${url}` }],+ isError: true,+ };+ }+ }+);++server.tool(+ "swipe-down-on-screen",+ "Swipe down on the screen",+ {},+ async ({}) => {+ log(`gilm: swiping down on screen`);+ try {+ await driver.performActions([{+ type: 'pointer',+ id: 'finger1',+ parameters: { pointerType: 'touch' },+ actions: [+ { type: 'pointerMove', duration: 0, x: 200, y: 800 }, // start point+ { type: 'pointerDown', button: 0 },+ { type: 'pause', duration: 100 },+ { type: 'pointerMove', duration: 500, x: 200, y: 300 }, // swipe up+ { type: 'pointerUp', button: 0 }+ ]+ }]);++ await driver.releaseActions();++ return {+ content: [{ type: 'text', text: `Successfully swiped down on screen` }]+ };+ } catch (error: any) {+ return {+ content: [{ type: 'text', text: `Failed to swipe down on screen` }],+ isError: true,+ }+ }+ }+);++server.tool(+ "swipe-down-until-element-is-visible",+ "Swipe down on the screen until an element is visible",+ {+ text: z.string().describe("The text of the element to swipe down until"),+ },+ async ({ text }) => {+ log(`gilm: swiping down on screen until element is visible: ${text}`);++ try {+ await driver.swipe({+ direction: 'down',+ duration: 1500,+ percent: 0.95, // Using 95% of the screen to avoid triggering OS features+ scrollableElement: await driver.$(`//*[contains(@text, "${text}")]`),+ });++ return {+ content: [{ type: 'text', text: `Successfully swiped down on screen until element is visible: ${text}` }]+ }++ } catch (error: any) {+ log(`gilm: error: ${JSON.stringify(error.stack)}`);+ return {+ content: [{ type: 'text', text: `Failed to swipe down on screen` }],+ isError: true,+ }+ }+ }+);++server.tool(+ 'type-text',+ 'Type text into an element',+ {+ text: z.string().describe("The text to type"),+ },+ async ({ text }) => {+ log(`gilm: typing text: ${text}`);++ try {+ await driver.keys(text);+ return {+ content: [{ type: 'text', text: `Successfully typed text: ${text}` }]+ }+ } catch (error: any) {+ return {+ content: [{ type: 'text', text: `Failed to type text: ${text}` }],+ isError: true,+ }+ }+ }+);++async function main() {+ driver = await remote(wdOpts);++ const transport = new StdioServerTransport();+ await server.connect(transport);+ console.error("Appium MCP Server running on stdio");+}++main().catch((error) => {+ console.error("Fatal error in main():", error);+ process.exit(1);+});\ No newline at end of filediff --git a/tsconfig.json b/tsconfig.jsonnew file mode 100644index 0000000..7bc09d3--- /dev/null+++ b/tsconfig.json@@ -0,0 +1,14 @@+{+ "compilerOptions": {+ "target": "ESNext",+ "skipLibCheck": true,+ "esModuleInterop": true,+ "moduleResolution": "node",+ "strict": true,+ "module": "CommonJS",+ "outDir": "./lib"+ },+ "include": [+ "src",+ ],+}