@nbakka/mcp-appium 1.0.28 → 2.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/lib/server.js DELETED
@@ -1,237 +0,0 @@
1
- #!/usr/bin/env node
2
-
3
- import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
4
- import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
5
- import { z } from "zod";
6
- import axios from "axios";
7
- import { exec } from "child_process";
8
- import { parseStringPromise } from "xml2js";
9
-
10
-
11
- const APPIUM_URL = "http://127.0.0.1:4723"; // Corrected endpoint for Appium 2.x
12
-
13
- const server = new McpServer({ name: "MCP Appium JSONWire", version: "1.0.0" });
14
- const state = { sessionId: null };
15
-
16
- // Helper to extract element ID
17
- function extractElementId(element) {
18
- return element.ELEMENT || element["element-6066-11e4-a52e-4f735466cecf"];
19
- }
20
-
21
- function delay(ms) {
22
- return new Promise(resolve => setTimeout(resolve, ms));
23
- }
24
-
25
- // Start Session tool
26
- server.tool(
27
- "start_session",
28
- "Start Appium session with capabilities",
29
- {
30
- capabilities: z.object({
31
- platformName: z.string(),
32
- udid: z.string(),
33
- automationName: z.string(),
34
- app: z.string().optional(),
35
- }),
36
- },
37
- async ({ capabilities }) => {
38
- try {
39
- // Construct capabilities strictly with prefixes
40
- const alwaysMatch = {
41
- platformName: capabilities.platformName,
42
- "appium:udid": capabilities.udid,
43
- "appium:automationName": capabilities.automationName,
44
- };
45
- if (capabilities.app) {
46
- alwaysMatch["appium:app"] = capabilities.app;
47
- }
48
-
49
- const payload = {
50
- capabilities: {
51
- firstMatch: [{}],
52
- alwaysMatch,
53
- },
54
- };
55
-
56
- const response = await axios.post(`${APPIUM_URL}/session`, payload);
57
- state.sessionId = response.data.value.sessionId;
58
- state.deviceId = capabilities.udid;
59
- await delay(10000);
60
- return { content: [{ type: "text", text: `Session started: ${state.sessionId}` }] };
61
- } catch (e) {
62
- return { content: [{ type: "text", text: `Error starting session: ${e.message}` }] };
63
- }
64
- }
65
- );
66
-
67
-
68
- // Tap tool
69
- server.tool(
70
- "tap",
71
- "Tap element by locator",
72
- {
73
- by: z.enum(["id", "accessibility id", "xpath", "class name", "name"]),
74
- value: z.string(),
75
- },
76
- async ({ by, value }) => {
77
- if (!state.sessionId) return { content: [{ type: "text", text: "No active session" }] };
78
- try {
79
- const findResp = await axios.post(`${APPIUM_URL}/session/${state.sessionId}/element`, { using: by, value });
80
- const elementId = extractElementId(findResp.data.value);
81
- await axios.post(`${APPIUM_URL}/session/${state.sessionId}/element/${elementId}/click`);
82
- return { content: [{ type: "text", text: "Element tapped" }] };
83
- } catch (e) {
84
- return { content: [{ type: "text", text: `Error tapping element: ${e.message}` }] };
85
- }
86
- }
87
- );
88
-
89
- // Swipe tool
90
- server.tool(
91
- "swipe",
92
- "Swipe from start to end coordinates",
93
- {
94
- startX: z.number(),
95
- startY: z.number(),
96
- endX: z.number(),
97
- endY: z.number(),
98
- duration: z.number().optional(),
99
- },
100
- async ({ startX, startY, endX, endY, duration = 800 }) => {
101
- if (!state.sessionId) return { content: [{ type: "text", text: "No active session" }] };
102
- try {
103
- const actions = [
104
- { action: "press", options: { x: startX, y: startY } },
105
- { action: "wait", options: { ms: duration } },
106
- { action: "moveTo", options: { x: endX, y: endY } },
107
- { action: "release", options: {} },
108
- ];
109
- await axios.post(`${APPIUM_URL}/session/${state.sessionId}/touch/perform`, { actions });
110
- return { content: [{ type: "text", text: `Swiped from (${startX},${startY}) to (${endX},${endY})` }] };
111
- } catch (e) {
112
- return { content: [{ type: "text", text: `Error swiping: ${e.message}` }] };
113
- }
114
- }
115
- );
116
-
117
- // Open Deep Link
118
- server.tool(
119
- "open_deep_link_adb",
120
- "Open deep link on device via ADB shell",
121
- {
122
- deepLink: z.string(),
123
- },
124
- async ({ deepLink }) => {
125
- if (!state.deviceId) {
126
- return { content: [{ type: "text", text: "No connected device found" }] };
127
- }
128
-
129
- const command = `adb -s ${state.deviceId} shell am start -a android.intent.action.VIEW -d "${deepLink}"`;
130
-
131
- return new Promise((resolve) => {
132
- exec(command, (error, stdout, stderr) => {
133
- if (error) {
134
- resolve({ content: [{ type: "text", text: `Error executing ADB command: ${error.message}` }] });
135
- return;
136
- }
137
- if (stderr) {
138
- resolve({ content: [{ type: "text", text: `ADB stderr: ${stderr}` }] });
139
- return;
140
- }
141
- resolve({ content: [{ type: "text", text: `Deep link opened via ADB: ${deepLink}` }] });
142
- });
143
- });
144
- }
145
- );
146
-
147
- // Get visible text elements including duplicates
148
- server.tool(
149
- "get_visible_text_elements",
150
- "Get all visible texts from screen XML source including duplicates",
151
- {},
152
- async () => {
153
- if (!state.sessionId) return { content: [{ type: "text", text: "No active session" }] };
154
- try {
155
- const response = await axios.get(`${APPIUM_URL}/session/${state.sessionId}/source`);
156
- const xml = response.data;
157
- console.log(response.data);
158
- const parsed = await parseStringPromise(xml, { explicitArray: false, mergeAttrs: true });
159
-
160
- function collectTextsFromXml(node, texts = []) {
161
- if (!node) return texts;
162
-
163
- // If array, recurse each element
164
- if (Array.isArray(node)) {
165
- node.forEach(child => collectTextsFromXml(child, texts));
166
- return texts;
167
- }
168
-
169
- // If node has 'text' attribute with non-empty value, collect it
170
- if (node.text && node.text.trim() !== "") {
171
- texts.push(node.text.trim());
172
- }
173
-
174
- // Recurse into all children nodes
175
- for (const key in node) {
176
- if (key !== "text" && typeof node[key] === "object") {
177
- collectTextsFromXml(node[key], texts);
178
- }
179
- }
180
- return texts;
181
- }
182
-
183
- const allTexts = collectTextsFromXml(parsed);
184
- return { content: [{ type: "json", json: allTexts }] };
185
- } catch (e) {
186
- return { content: [{ type: "text", text: `Error fetching or parsing source XML: ${e.message}` }] };
187
- }
188
- }
189
- );
190
-
191
- // Click element by text with optional index (default 0)
192
- server.tool(
193
- "click_by_text",
194
- "Click element by visible text with optional index",
195
- {
196
- text: z.string(),
197
- index: z.number().optional().default(0),
198
- },
199
- async ({ text, index }) => {
200
- if (!state.sessionId) return { content: [{ type: "text", text: "No active session" }] };
201
- try {
202
- const xpath = `(//*[normalize-space(@text)='${text}'])[${index + 1}]`;
203
- const findResp = await axios.post(`${APPIUM_URL}/session/${state.sessionId}/element`, { using: "xpath", value: xpath });
204
- const elementId = extractElementId(findResp.data.value);
205
- await axios.post(`${APPIUM_URL}/session/${state.sessionId}/element/${elementId}/click`);
206
- return { content: [{ type: "text", text: `Clicked element with text: "${text}" at index ${index}` }] };
207
- } catch (e) {
208
- return { content: [{ type: "text", text: `Error clicking element: ${e.message}` }] };
209
- }
210
- }
211
- );
212
-
213
-
214
-
215
-
216
-
217
- // Close Session tool
218
- server.tool(
219
- "close_session",
220
- "Close the current Appium session",
221
- {},
222
- async () => {
223
- if (!state.sessionId) return { content: [{ type: "text", text: "No active session" }] };
224
- try {
225
- await axios.delete(`${APPIUM_URL}/session/${state.sessionId}`);
226
- const oldSession = state.sessionId;
227
- state.sessionId = null;
228
- return { content: [{ type: "text", text: `Session ${oldSession} closed` }] };
229
- } catch (e) {
230
- return { content: [{ type: "text", text: `Error closing session: ${e.message}` }] };
231
- }
232
- }
233
- );
234
-
235
- // Connect MCP server to stdio
236
- const transport = new StdioServerTransport();
237
- await server.connect(transport);