mobile-debug-mcp 0.21.5 → 0.22.0

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.
Files changed (84) hide show
  1. package/AGENTS.md +74 -0
  2. package/README.md +24 -5
  3. package/dist/interact/index.js +220 -13
  4. package/dist/observe/ios.js +10 -3
  5. package/dist/server-core.js +707 -0
  6. package/dist/server.js +6 -693
  7. package/dist/utils/resolve-device.js +15 -3
  8. package/docs/CHANGELOG.md +6 -1
  9. package/docs/tools/interact.md +69 -30
  10. package/package.json +3 -3
  11. package/skills/README.md +35 -0
  12. package/skills/test-authoring/SKILL.md +57 -0
  13. package/skills/test-authoring/references/repo-test-layout.md +47 -0
  14. package/skills/test-authoring/references/test-authoring-workflow.md +73 -0
  15. package/skills/test-authoring/references/test-quality-checklist.md +39 -0
  16. package/src/interact/index.ts +250 -13
  17. package/src/observe/ios.ts +12 -3
  18. package/src/server-core.ts +762 -0
  19. package/src/server.ts +8 -754
  20. package/src/types.ts +10 -1
  21. package/src/utils/resolve-device.ts +19 -3
  22. package/test/device/automated/observe/capture_screenshot.android.smoke.ts +30 -0
  23. package/test/device/automated/observe/capture_screenshot.ios.smoke.ts +30 -0
  24. package/test/{observe/device → device/automated/observe}/get_logs.android.smoke.ts +1 -1
  25. package/test/{observe/device → device/automated/observe}/get_logs.ios.smoke.ts +1 -1
  26. package/test/device/automated/observe/get_ui_tree.android.smoke.ts +31 -0
  27. package/test/device/automated/observe/get_ui_tree.ios.smoke.ts +31 -0
  28. package/test/device/index.ts +52 -0
  29. package/test/{interact/device/smoke-test.ts → device/manual/interact/app_lifecycle.manual.ts} +5 -5
  30. package/test/{manage/device/run-build-install-ios.ts → device/manual/manage/build_install_ios.manual.ts} +1 -1
  31. package/test/{manage/device → device/manual/manage}/install.integration.ts +6 -6
  32. package/test/{manage/device/run-install-android.ts → device/manual/manage/install_android.manual.ts} +1 -1
  33. package/test/{manage/device/run-install-ios.ts → device/manual/manage/install_ios.manual.ts} +1 -1
  34. package/test/device/manual/observe/capture_screenshot.manual.ts +29 -0
  35. package/test/{helpers/run-get-logs.ts → device/manual/observe/get_logs.manual.ts} +1 -1
  36. package/test/device/manual/observe/get_ui_tree.manual.ts +29 -0
  37. package/test/{observe/device/logstream-real.ts → device/manual/observe/logstream.manual.ts} +1 -1
  38. package/test/{observe/device/run-screen-fingerprint.ts → device/manual/observe/screen_fingerprint.manual.ts} +1 -1
  39. package/test/{observe/device/run-scroll-test-android.ts → device/manual/observe/scroll_to_element_android.manual.ts} +1 -1
  40. package/test/{observe/device/test-ui-tree.ts → device/manual/observe/ui_tree.manual.ts} +6 -6
  41. package/test/unit/index.ts +47 -27
  42. package/test/unit/interact/handler_shapes.test.ts +55 -0
  43. package/test/unit/interact/tap_element.test.ts +170 -0
  44. package/test/unit/interact/wait_for_screen_change.test.ts +34 -0
  45. package/test/{interact/unit → unit/interact}/wait_for_ui_contract.test.ts +11 -10
  46. package/test/unit/interact/wait_for_ui_selector_matching.test.ts +76 -0
  47. package/test/unit/manage/handler_shapes.test.ts +43 -0
  48. package/test/{observe/unit → unit/observe}/capture_debug_snapshot.test.ts +5 -1
  49. package/test/{observe/unit → unit/observe}/find_element.test.ts +12 -6
  50. package/test/unit/observe/get_screen_fingerprint.test.ts +71 -0
  51. package/test/unit/observe/ios-getlogs.test.ts +53 -0
  52. package/test/unit/observe/scroll_to_element.test.ts +127 -0
  53. package/test/unit/server/contract.test.ts +45 -0
  54. package/test/unit/server/response_shapes.test.ts +93 -0
  55. package/test/unit/system/adb_version.test.ts +35 -0
  56. package/test/unit/system/get_system_status.test.ts +20 -0
  57. package/test/unit/system/system_status.test.ts +141 -0
  58. package/test/{utils → unit/utils}/detect_java.test.ts +1 -1
  59. package/test/unit/utils/exec.test.ts +51 -0
  60. package/test/unit/utils/resolve_device.test.ts +63 -0
  61. package/tsconfig.json +2 -2
  62. package/test/interact/device/run-real-test.ts +0 -3
  63. package/test/interact/unit/wait_for_screen_change.test.ts +0 -32
  64. package/test/interact/unit/wait_for_ui.test.ts +0 -76
  65. package/test/interact/unit/wait_for_ui_new.test.ts +0 -57
  66. package/test/observe/device/wait_for_element_real.ts +0 -3
  67. package/test/observe/unit/get_screen_fingerprint.test.ts +0 -69
  68. package/test/observe/unit/ios-getlogs.test.ts +0 -67
  69. package/test/observe/unit/scroll_to_element.test.ts +0 -129
  70. package/test/observe/unit/wait_for_element_mock.ts +0 -2
  71. package/test/observe/unit/wait_for_ui_edge_cases.test.ts +0 -41
  72. package/test/observe/unit/wait_for_ui_stability.test.ts +0 -30
  73. package/test/system/adb_version.test.ts +0 -25
  74. package/test/system/get_system_status.test.ts +0 -52
  75. package/test/system/system_status.test.ts +0 -109
  76. /package/test/{manage/unit → unit/manage}/build.test.ts +0 -0
  77. /package/test/{manage/unit → unit/manage}/build_and_install.test.ts +0 -0
  78. /package/test/{manage/unit → unit/manage}/detection.test.ts +0 -0
  79. /package/test/{manage/unit → unit/manage}/diagnostics.test.ts +0 -0
  80. /package/test/{manage/unit → unit/manage}/install.test.ts +0 -0
  81. /package/test/{manage/unit → unit/manage}/mcp_disable_autodetect.test.ts +0 -0
  82. /package/test/{observe/unit → unit/observe}/get_logs.test.ts +0 -0
  83. /package/test/{observe/unit → unit/observe}/logparse.test.ts +0 -0
  84. /package/test/{observe/unit → unit/observe}/logstream.test.ts +0 -0
@@ -0,0 +1,707 @@
1
+ import { Server } from '@modelcontextprotocol/sdk/server/index.js';
2
+ import { ListToolsRequestSchema, CallToolRequestSchema } from '@modelcontextprotocol/sdk/types.js';
3
+ import { ToolsManage } from './manage/index.js';
4
+ import { ToolsInteract } from './interact/index.js';
5
+ import { ToolsObserve } from './observe/index.js';
6
+ import { AndroidManage } from './manage/index.js';
7
+ import { iOSManage } from './manage/index.js';
8
+ import { getSystemStatus } from './system/index.js';
9
+ export const serverInfo = {
10
+ name: 'mobile-debug-mcp',
11
+ version: '0.7.0'
12
+ };
13
+ export function wrapResponse(data) {
14
+ return {
15
+ content: [{
16
+ type: 'text',
17
+ text: JSON.stringify(data, null, 2)
18
+ }]
19
+ };
20
+ }
21
+ export const toolDefinitions = [
22
+ {
23
+ name: 'start_app',
24
+ description: 'Launch a mobile app on Android or iOS simulator',
25
+ inputSchema: {
26
+ type: 'object',
27
+ properties: {
28
+ platform: {
29
+ type: 'string',
30
+ enum: ['android', 'ios']
31
+ },
32
+ appId: {
33
+ type: 'string',
34
+ description: 'Android package name or iOS bundle id'
35
+ },
36
+ deviceId: {
37
+ type: 'string',
38
+ description: 'Device UDID (iOS) or Serial (Android). Defaults to booted/connected.'
39
+ }
40
+ },
41
+ required: ['platform', 'appId']
42
+ }
43
+ },
44
+ {
45
+ name: 'terminate_app',
46
+ description: 'Terminate a mobile app on Android or iOS simulator',
47
+ inputSchema: {
48
+ type: 'object',
49
+ properties: {
50
+ platform: {
51
+ type: 'string',
52
+ enum: ['android', 'ios']
53
+ },
54
+ appId: {
55
+ type: 'string',
56
+ description: 'Android package name or iOS bundle id'
57
+ },
58
+ deviceId: {
59
+ type: 'string',
60
+ description: 'Device UDID (iOS) or Serial (Android). Defaults to booted/connected.'
61
+ }
62
+ },
63
+ required: ['platform', 'appId']
64
+ }
65
+ },
66
+ {
67
+ name: 'restart_app',
68
+ description: 'Restart a mobile app on Android or iOS simulator',
69
+ inputSchema: {
70
+ type: 'object',
71
+ properties: {
72
+ platform: {
73
+ type: 'string',
74
+ enum: ['android', 'ios']
75
+ },
76
+ appId: {
77
+ type: 'string',
78
+ description: 'Android package name or iOS bundle id'
79
+ },
80
+ deviceId: {
81
+ type: 'string',
82
+ description: 'Device UDID (iOS) or Serial (Android). Defaults to booted/connected.'
83
+ }
84
+ },
85
+ required: ['platform', 'appId']
86
+ }
87
+ },
88
+ {
89
+ name: 'reset_app_data',
90
+ description: 'Reset app data (clear storage) for a mobile app on Android or iOS simulator',
91
+ inputSchema: {
92
+ type: 'object',
93
+ properties: {
94
+ platform: {
95
+ type: 'string',
96
+ enum: ['android', 'ios']
97
+ },
98
+ appId: {
99
+ type: 'string',
100
+ description: 'Android package name or iOS bundle id'
101
+ },
102
+ deviceId: {
103
+ type: 'string',
104
+ description: 'Device UDID (iOS) or Serial (Android). Defaults to booted/connected.'
105
+ }
106
+ },
107
+ required: ['platform', 'appId']
108
+ }
109
+ },
110
+ {
111
+ name: 'install_app',
112
+ description: 'Install an app on Android or iOS. Accepts a built binary (apk/.ipa/.app) or a project directory to build then install. platform and projectType are required.',
113
+ inputSchema: {
114
+ type: 'object',
115
+ properties: {
116
+ platform: { type: 'string', enum: ['android', 'ios'], description: 'Platform to install to (required).' },
117
+ projectType: { type: 'string', enum: ['native', 'kmp', 'react-native', 'flutter'], description: 'Project type to guide build/install tool selection (required).' },
118
+ appPath: { type: 'string', description: 'Path to APK, .app, .ipa, or project directory' },
119
+ deviceId: { type: 'string', description: 'Device UDID (iOS) or Serial (Android). Defaults to booted/connected.' }
120
+ },
121
+ required: ['platform', 'projectType', 'appPath']
122
+ }
123
+ },
124
+ {
125
+ name: 'build_app',
126
+ description: 'Build a project for Android or iOS and return the built artifact path. Does not install. platform and projectType are required.',
127
+ inputSchema: {
128
+ type: 'object',
129
+ properties: {
130
+ platform: { type: 'string', enum: ['android', 'ios'], description: 'Platform to build for (required).' },
131
+ projectType: { type: 'string', enum: ['native', 'kmp', 'react-native', 'flutter'], description: 'Project type to guide build tool selection (required).' },
132
+ projectPath: { type: 'string', description: 'Path to project directory (contains gradlew or xcodeproj/xcworkspace)' },
133
+ variant: { type: 'string', description: 'Optional build variant (e.g., Debug/Release)' }
134
+ },
135
+ required: ['platform', 'projectType', 'projectPath']
136
+ }
137
+ },
138
+ {
139
+ name: 'get_logs',
140
+ description: 'Get recent logs from Android or iOS simulator. Returns device metadata and structured logs suitable for AI consumption.',
141
+ inputSchema: {
142
+ type: 'object',
143
+ properties: {
144
+ platform: {
145
+ type: 'string',
146
+ enum: ['android', 'ios']
147
+ },
148
+ appId: {
149
+ type: 'string',
150
+ description: 'Filter by Android package name or iOS bundle id'
151
+ },
152
+ deviceId: {
153
+ type: 'string',
154
+ description: 'Device UDID (iOS) or Serial (Android). Defaults to booted/connected.'
155
+ },
156
+ pid: { type: 'number', description: 'Filter by process id' },
157
+ tag: { type: 'string', description: 'Filter by tag (Android) or subsystem/category (iOS)' },
158
+ level: { type: 'string', description: 'Log level filter (VERBOSE, DEBUG, INFO, WARN, ERROR)' },
159
+ contains: { type: 'string', description: 'Substring to match in log message' },
160
+ since_seconds: { type: 'number', description: 'Only return logs from the last N seconds' },
161
+ limit: { type: 'number', description: 'Override default number of returned lines' },
162
+ lines: {
163
+ type: 'number',
164
+ description: 'Legacy - number of log lines (android only)'
165
+ }
166
+ },
167
+ required: ['platform']
168
+ }
169
+ },
170
+ {
171
+ name: 'list_devices',
172
+ description: 'List connected devices and their metadata (android + ios).',
173
+ inputSchema: {
174
+ type: 'object',
175
+ properties: {
176
+ platform: { type: 'string', enum: ['android', 'ios'] }
177
+ }
178
+ }
179
+ },
180
+ {
181
+ name: 'get_system_status',
182
+ description: 'Quick healthcheck of local mobile debugging environment (adb, devices, logs, env, iOS).',
183
+ inputSchema: { type: 'object', properties: {} }
184
+ },
185
+ {
186
+ name: 'capture_screenshot',
187
+ description: 'Capture a screenshot from an Android device or iOS simulator. Returns device metadata and the screenshot image.',
188
+ inputSchema: {
189
+ type: 'object',
190
+ properties: {
191
+ platform: {
192
+ type: 'string',
193
+ enum: ['android', 'ios']
194
+ },
195
+ deviceId: {
196
+ type: 'string',
197
+ description: 'Device UDID (iOS) or Serial (Android). Defaults to booted/connected.'
198
+ }
199
+ },
200
+ required: ['platform']
201
+ }
202
+ },
203
+ {
204
+ name: 'capture_debug_snapshot',
205
+ description: 'Capture a complete debug snapshot (screenshot, ui tree, activity, fingerprint, logs). Returns structured JSON.',
206
+ inputSchema: {
207
+ type: 'object',
208
+ properties: {
209
+ reason: { type: 'string', description: 'Optional reason for snapshot' },
210
+ includeLogs: { type: 'boolean', description: 'Whether to include logs', default: true },
211
+ logLines: { type: 'number', description: 'Maximum number of log lines to include', default: 200 },
212
+ platform: { type: 'string', enum: ['android', 'ios'], description: 'Optional platform override' },
213
+ appId: { type: 'string', description: 'Optional appId to scope logs (package/bundle id)' },
214
+ deviceId: { type: 'string', description: 'Optional device serial/udid' },
215
+ sessionId: { type: 'string', description: 'Optional log stream session id to prefer' }
216
+ }
217
+ }
218
+ },
219
+ {
220
+ name: 'start_log_stream',
221
+ description: 'Start streaming logs for a target application on Android or iOS. For Android this uses adb logcat --pid=<pid>; for iOS it streams `xcrun simctl spawn <device> log stream` with a predicate.',
222
+ inputSchema: {
223
+ type: 'object',
224
+ properties: {
225
+ platform: { type: 'string', enum: ['android', 'ios'], default: 'android' },
226
+ packageName: { type: 'string', description: 'Android package name or iOS bundle id' },
227
+ level: { type: 'string', enum: ['error', 'warn', 'info', 'debug'], default: 'error' },
228
+ deviceId: { type: 'string', description: 'Device Serial (Android) or UDID (iOS). Defaults to connected/booted device.' },
229
+ sessionId: { type: 'string', description: 'Session identifier for the log stream' }
230
+ },
231
+ required: ['packageName']
232
+ }
233
+ },
234
+ {
235
+ name: 'read_log_stream',
236
+ description: 'Read accumulated log stream entries for the active session.',
237
+ inputSchema: {
238
+ type: 'object',
239
+ properties: {
240
+ sessionId: { type: 'string' }
241
+ }
242
+ }
243
+ },
244
+ {
245
+ name: 'stop_log_stream',
246
+ description: 'Stop an active log stream for the session.',
247
+ inputSchema: {
248
+ type: 'object',
249
+ properties: {
250
+ sessionId: { type: 'string' }
251
+ }
252
+ }
253
+ },
254
+ {
255
+ name: 'get_ui_tree',
256
+ description: 'Get the current UI hierarchy from an Android device or iOS simulator. Returns a structured JSON representation of the screen content.',
257
+ inputSchema: {
258
+ type: 'object',
259
+ properties: {
260
+ platform: {
261
+ type: 'string',
262
+ enum: ['android', 'ios'],
263
+ description: 'Platform to get UI tree for'
264
+ },
265
+ deviceId: {
266
+ type: 'string',
267
+ description: 'Device Serial (Android) or UDID (iOS). Defaults to connected/booted device.'
268
+ }
269
+ },
270
+ required: ['platform']
271
+ }
272
+ },
273
+ {
274
+ name: 'get_current_screen',
275
+ description: 'Get the currently visible activity on an Android device. Returns package and activity name.',
276
+ inputSchema: {
277
+ type: 'object',
278
+ properties: {
279
+ deviceId: {
280
+ type: 'string',
281
+ description: 'Device Serial (Android). Defaults to connected/booted device.'
282
+ }
283
+ }
284
+ }
285
+ },
286
+ {
287
+ name: 'get_screen_fingerprint',
288
+ description: 'Generate a stable fingerprint representing the current visible screen (activity + visible UI elements).',
289
+ inputSchema: {
290
+ type: 'object',
291
+ properties: {
292
+ platform: { type: 'string', enum: ['android', 'ios'], description: 'Optional platform override (android|ios)' },
293
+ deviceId: { type: 'string', description: 'Optional device id/udid to target' }
294
+ }
295
+ }
296
+ },
297
+ {
298
+ name: 'wait_for_screen_change',
299
+ description: 'Wait until the current screen fingerprint differs from a provided previousFingerprint. Useful to wait for navigation/animation completion.',
300
+ inputSchema: {
301
+ type: 'object',
302
+ properties: {
303
+ platform: { type: 'string', enum: ['android', 'ios'], description: 'Optional platform override (android|ios)' },
304
+ previousFingerprint: { type: 'string', description: 'The fingerprint to compare against (required)' },
305
+ timeoutMs: { type: 'number', description: 'Timeout in ms to wait for change (default 5000)', default: 5000 },
306
+ pollIntervalMs: { type: 'number', description: 'Polling interval in ms (default 300)', default: 300 },
307
+ deviceId: { type: 'string', description: 'Optional device id/udid to target' }
308
+ },
309
+ required: ['previousFingerprint']
310
+ }
311
+ },
312
+ {
313
+ name: 'wait_for_ui',
314
+ description: 'Deterministic UI wait primitive. Waits for selector condition with retries and backoff.',
315
+ inputSchema: {
316
+ type: 'object',
317
+ properties: {
318
+ selector: {
319
+ type: 'object',
320
+ properties: {
321
+ text: { type: 'string' },
322
+ resource_id: { type: 'string' },
323
+ accessibility_id: { type: 'string' },
324
+ contains: { type: 'boolean', description: 'When true, perform substring matching', default: false }
325
+ }
326
+ },
327
+ condition: { type: 'string', enum: ['exists', 'not_exists', 'visible', 'clickable'], default: 'exists' },
328
+ timeout_ms: { type: 'number', default: 60000 },
329
+ poll_interval_ms: { type: 'number', default: 300 },
330
+ match: { type: 'object', properties: { index: { type: 'number' } } },
331
+ retry: { type: 'object', properties: { max_attempts: { type: 'number', default: 1 }, backoff_ms: { type: 'number', default: 0 } } },
332
+ platform: { type: 'string', enum: ['android', 'ios'], description: 'Optional platform override' },
333
+ deviceId: { type: 'string', description: 'Optional device serial/udid' }
334
+ }
335
+ }
336
+ },
337
+ {
338
+ name: 'find_element',
339
+ description: 'Find a UI element by semantic query (text, content-desc, resource-id, class). Returns best match.',
340
+ inputSchema: {
341
+ type: 'object',
342
+ properties: {
343
+ query: { type: 'string', description: 'Search query (text or label)' },
344
+ exact: { type: 'boolean', description: 'Require exact match (true/false)', default: false },
345
+ timeoutMs: { type: 'number', description: 'Timeout in ms to keep searching', default: 3000 },
346
+ platform: { type: 'string', enum: ['android', 'ios'], description: 'Optional platform override' },
347
+ deviceId: { type: 'string', description: 'Optional device serial/udid' }
348
+ },
349
+ required: ['query']
350
+ }
351
+ },
352
+ {
353
+ name: 'tap',
354
+ description: 'Simulate a finger tap on the device screen at specific coordinates.',
355
+ inputSchema: {
356
+ type: 'object',
357
+ properties: {
358
+ platform: {
359
+ type: 'string',
360
+ enum: ['android', 'ios'],
361
+ description: 'Platform to tap on'
362
+ },
363
+ x: {
364
+ type: 'number',
365
+ description: 'X coordinate'
366
+ },
367
+ y: {
368
+ type: 'number',
369
+ description: 'Y coordinate'
370
+ },
371
+ deviceId: {
372
+ type: 'string',
373
+ description: 'Device Serial/UDID. Defaults to connected/booted device.'
374
+ }
375
+ },
376
+ required: ['x', 'y']
377
+ }
378
+ },
379
+ {
380
+ name: 'tap_element',
381
+ description: 'Tap a previously resolved UI element using its elementId.',
382
+ inputSchema: {
383
+ type: 'object',
384
+ properties: {
385
+ elementId: {
386
+ type: 'string',
387
+ description: 'A unique element identifier returned by wait_for_ui'
388
+ }
389
+ },
390
+ required: ['elementId']
391
+ }
392
+ },
393
+ {
394
+ name: 'swipe',
395
+ description: 'Simulate a swipe gesture on an Android device.',
396
+ inputSchema: {
397
+ type: 'object',
398
+ properties: {
399
+ platform: {
400
+ type: 'string',
401
+ enum: ['android', 'ios'],
402
+ description: 'Platform to swipe on (android or ios)'
403
+ },
404
+ x1: { type: 'number', description: 'Start X coordinate' },
405
+ y1: { type: 'number', description: 'Start Y coordinate' },
406
+ x2: { type: 'number', description: 'End X coordinate' },
407
+ y2: { type: 'number', description: 'End Y coordinate' },
408
+ duration: { type: 'number', description: 'Duration in ms' },
409
+ deviceId: {
410
+ type: 'string',
411
+ description: 'Device Serial/UDID. Defaults to connected/booted device.'
412
+ }
413
+ },
414
+ required: ['x1', 'y1', 'x2', 'y2', 'duration']
415
+ }
416
+ },
417
+ {
418
+ name: 'scroll_to_element',
419
+ description: 'Scroll the current screen until a target UI element becomes visible, then return its details.',
420
+ inputSchema: {
421
+ type: 'object',
422
+ properties: {
423
+ platform: { type: 'string', enum: ['android', 'ios'], description: 'Platform to operate on (required)' },
424
+ selector: {
425
+ type: 'object',
426
+ properties: {
427
+ text: { type: 'string' },
428
+ resourceId: { type: 'string' },
429
+ contentDesc: { type: 'string' },
430
+ className: { type: 'string' }
431
+ }
432
+ },
433
+ direction: { type: 'string', enum: ['down', 'up'], default: 'down' },
434
+ maxScrolls: { type: 'number', default: 10 },
435
+ scrollAmount: { type: 'number', default: 0.7 },
436
+ deviceId: { type: 'string', description: 'Device UDID (iOS) or Serial (Android). Defaults to booted/connected.' }
437
+ },
438
+ required: ['platform', 'selector']
439
+ }
440
+ },
441
+ {
442
+ name: 'type_text',
443
+ description: 'Type text into the currently focused input field on an Android device.',
444
+ inputSchema: {
445
+ type: 'object',
446
+ properties: {
447
+ platform: {
448
+ type: 'string',
449
+ enum: ['android'],
450
+ description: 'Platform to type on (currently only android supported)'
451
+ },
452
+ text: {
453
+ type: 'string',
454
+ description: 'The text to type'
455
+ },
456
+ deviceId: {
457
+ type: 'string',
458
+ description: 'Device Serial/UDID. Defaults to connected/booted device.'
459
+ }
460
+ },
461
+ required: ['text']
462
+ }
463
+ },
464
+ {
465
+ name: 'press_back',
466
+ description: 'Simulate pressing the Android Back button.',
467
+ inputSchema: {
468
+ type: 'object',
469
+ properties: {
470
+ platform: {
471
+ type: 'string',
472
+ enum: ['android'],
473
+ description: 'Platform (currently only android supported)'
474
+ },
475
+ deviceId: {
476
+ type: 'string',
477
+ description: 'Device Serial/UDID. Defaults to connected/booted device.'
478
+ }
479
+ }
480
+ }
481
+ }
482
+ ];
483
+ async function handleStartApp(args) {
484
+ const { platform, appId, deviceId } = args;
485
+ const res = await (platform === 'android' ? new AndroidManage().startApp(appId, deviceId) : new iOSManage().startApp(appId, deviceId));
486
+ const response = {
487
+ device: res.device,
488
+ appStarted: res.appStarted,
489
+ launchTimeMs: res.launchTimeMs
490
+ };
491
+ return wrapResponse(response);
492
+ }
493
+ async function handleTerminateApp(args) {
494
+ const { platform, appId, deviceId } = args;
495
+ const res = await (platform === 'android' ? new AndroidManage().terminateApp(appId, deviceId) : new iOSManage().terminateApp(appId, deviceId));
496
+ const response = { device: res.device, appTerminated: res.appTerminated };
497
+ return wrapResponse(response);
498
+ }
499
+ async function handleRestartApp(args) {
500
+ const { platform, appId, deviceId } = args;
501
+ const res = await (platform === 'android' ? new AndroidManage().restartApp(appId, deviceId) : new iOSManage().restartApp(appId, deviceId));
502
+ const response = { device: res.device, appRestarted: res.appRestarted, launchTimeMs: res.launchTimeMs };
503
+ return wrapResponse(response);
504
+ }
505
+ async function handleResetAppData(args) {
506
+ const { platform, appId, deviceId } = args;
507
+ const res = await (platform === 'android' ? new AndroidManage().resetAppData(appId, deviceId) : new iOSManage().resetAppData(appId, deviceId));
508
+ const response = { device: res.device, dataCleared: res.dataCleared };
509
+ return wrapResponse(response);
510
+ }
511
+ async function handleInstallApp(args) {
512
+ const { platform, projectType, appPath, deviceId } = args;
513
+ const res = await ToolsManage.installAppHandler({ platform, appPath, deviceId, projectType });
514
+ const response = {
515
+ device: res.device,
516
+ installed: res.installed,
517
+ output: res.output,
518
+ error: res.error
519
+ };
520
+ return wrapResponse(response);
521
+ }
522
+ async function handleBuildApp(args) {
523
+ const { platform, projectType, projectPath, variant } = args;
524
+ const res = await ToolsManage.buildAppHandler({ platform, projectPath, variant, projectType });
525
+ return wrapResponse(res);
526
+ }
527
+ async function handleBuildAndInstall(args) {
528
+ const { platform, projectType, projectPath, deviceId, timeout } = args;
529
+ const res = await ToolsManage.buildAndInstallHandler({ platform, projectPath, deviceId, timeout, projectType });
530
+ return {
531
+ content: [
532
+ { type: 'text', text: res.ndjson },
533
+ { type: 'text', text: JSON.stringify(res.result, null, 2) }
534
+ ]
535
+ };
536
+ }
537
+ async function handleGetLogs(args) {
538
+ const { platform, appId, deviceId, pid, tag, level, contains, since_seconds, limit, lines } = args;
539
+ const res = await ToolsObserve.getLogsHandler({ platform, appId, deviceId, pid, tag, level, contains, since_seconds, limit, lines });
540
+ const filtered = !!(pid || tag || level || contains || since_seconds || appId);
541
+ return {
542
+ content: [
543
+ { type: 'text', text: JSON.stringify({ device: res.device, result: { count: res.logCount, filtered, crashLines: (res.crashLines || []), source: res.source, meta: res.meta || {} } }, null, 2) },
544
+ { type: 'text', text: JSON.stringify({ logs: res.logs }, null, 2) }
545
+ ]
546
+ };
547
+ }
548
+ async function handleListDevices(args) {
549
+ const { platform, appId } = args;
550
+ const res = await ToolsManage.listDevicesHandler({ platform, appId });
551
+ return wrapResponse(res);
552
+ }
553
+ async function handleGetSystemStatus() {
554
+ const result = await getSystemStatus();
555
+ return wrapResponse(result);
556
+ }
557
+ async function handleCaptureScreenshot(args) {
558
+ const { platform, deviceId } = args;
559
+ const res = await ToolsObserve.captureScreenshotHandler({ platform, deviceId });
560
+ const mime = res.screenshot_mime || 'image/png';
561
+ const content = [
562
+ { type: 'text', text: JSON.stringify({ device: res.device, result: { resolution: res.resolution, mimeType: mime } }, null, 2) },
563
+ { type: 'image', data: res.screenshot, mimeType: mime }
564
+ ];
565
+ if (res.screenshot_fallback) {
566
+ content.push({ type: 'text', text: JSON.stringify({ note: 'JPEG fallback included for compatibility', mimeType: res.screenshot_fallback_mime || 'image/jpeg' }) });
567
+ content.push({ type: 'image', data: res.screenshot_fallback, mimeType: res.screenshot_fallback_mime || 'image/jpeg' });
568
+ }
569
+ return { content };
570
+ }
571
+ async function handleCaptureDebugSnapshot(args) {
572
+ const { reason, includeLogs, logLines, platform, appId, deviceId, sessionId } = args;
573
+ const res = await ToolsObserve.captureDebugSnapshotHandler({ reason, includeLogs, logLines, platform, appId, deviceId, sessionId });
574
+ return wrapResponse(res);
575
+ }
576
+ async function handleGetUITree(args) {
577
+ const { platform, deviceId } = args;
578
+ const res = await ToolsObserve.getUITreeHandler({ platform, deviceId });
579
+ return wrapResponse(res);
580
+ }
581
+ async function handleGetCurrentScreen(args) {
582
+ const { deviceId } = args;
583
+ const res = await ToolsObserve.getCurrentScreenHandler({ deviceId });
584
+ return wrapResponse(res);
585
+ }
586
+ async function handleGetScreenFingerprint(args) {
587
+ const { platform, deviceId } = args;
588
+ const res = await ToolsObserve.getScreenFingerprintHandler({ platform, deviceId });
589
+ return wrapResponse(res);
590
+ }
591
+ async function handleWaitForScreenChange(args) {
592
+ const { platform, previousFingerprint, timeoutMs, pollIntervalMs, deviceId } = args;
593
+ const res = await ToolsInteract.waitForScreenChangeHandler({ platform, previousFingerprint, timeoutMs, pollIntervalMs, deviceId });
594
+ return wrapResponse(res);
595
+ }
596
+ async function handleWaitForUI(args) {
597
+ const { selector, condition = 'exists', timeout_ms = 60000, poll_interval_ms = 300, match, retry, platform, deviceId } = args;
598
+ const res = await ToolsInteract.waitForUIHandler({ selector, condition, timeout_ms, poll_interval_ms, match, retry, platform, deviceId });
599
+ return wrapResponse(res);
600
+ }
601
+ async function handleFindElement(args) {
602
+ const { query, exact = false, timeoutMs = 3000, platform, deviceId } = args;
603
+ const res = await ToolsInteract.findElementHandler({ query, exact, timeoutMs, platform, deviceId });
604
+ return wrapResponse(res);
605
+ }
606
+ async function handleTap(args) {
607
+ const { platform, x, y, deviceId } = args;
608
+ const res = await ToolsInteract.tapHandler({ platform, x, y, deviceId });
609
+ return wrapResponse(res);
610
+ }
611
+ async function handleTapElement(args) {
612
+ const { elementId } = args;
613
+ const res = await ToolsInteract.tapElementHandler({ elementId });
614
+ return wrapResponse(res);
615
+ }
616
+ async function handleSwipe(args) {
617
+ const { platform = 'android', x1, y1, x2, y2, duration, deviceId } = args;
618
+ const res = await ToolsInteract.swipeHandler({ platform, x1, y1, x2, y2, duration, deviceId });
619
+ return wrapResponse(res);
620
+ }
621
+ async function handleScrollToElement(args) {
622
+ const { platform, selector, direction, maxScrolls, scrollAmount, deviceId } = args;
623
+ const res = await ToolsInteract.scrollToElementHandler({ platform, selector, direction, maxScrolls, scrollAmount, deviceId });
624
+ return wrapResponse(res);
625
+ }
626
+ async function handleTypeText(args) {
627
+ const { text, deviceId } = args;
628
+ const res = await ToolsInteract.typeTextHandler({ text, deviceId });
629
+ return wrapResponse(res);
630
+ }
631
+ async function handlePressBack(args) {
632
+ const { deviceId } = args;
633
+ const res = await ToolsInteract.pressBackHandler({ deviceId });
634
+ return wrapResponse(res);
635
+ }
636
+ async function handleStartLogStream(args) {
637
+ const { platform, packageName, level, sessionId, deviceId } = args;
638
+ const res = await ToolsObserve.startLogStreamHandler({ platform, packageName, level, sessionId, deviceId });
639
+ return wrapResponse(res);
640
+ }
641
+ async function handleReadLogStream(args) {
642
+ const { platform, sessionId, limit, since } = args;
643
+ const res = await ToolsObserve.readLogStreamHandler({ platform, sessionId, limit, since });
644
+ return wrapResponse(res);
645
+ }
646
+ async function handleStopLogStream(args) {
647
+ const { platform, sessionId } = args;
648
+ const res = await ToolsObserve.stopLogStreamHandler({ platform, sessionId });
649
+ return wrapResponse(res);
650
+ }
651
+ const toolHandlers = {
652
+ start_app: handleStartApp,
653
+ terminate_app: handleTerminateApp,
654
+ restart_app: handleRestartApp,
655
+ reset_app_data: handleResetAppData,
656
+ install_app: handleInstallApp,
657
+ build_app: handleBuildApp,
658
+ build_and_install: handleBuildAndInstall,
659
+ get_logs: handleGetLogs,
660
+ list_devices: handleListDevices,
661
+ get_system_status: handleGetSystemStatus,
662
+ capture_screenshot: handleCaptureScreenshot,
663
+ capture_debug_snapshot: handleCaptureDebugSnapshot,
664
+ get_ui_tree: handleGetUITree,
665
+ get_current_screen: handleGetCurrentScreen,
666
+ get_screen_fingerprint: handleGetScreenFingerprint,
667
+ wait_for_screen_change: handleWaitForScreenChange,
668
+ wait_for_ui: handleWaitForUI,
669
+ find_element: handleFindElement,
670
+ tap: handleTap,
671
+ tap_element: handleTapElement,
672
+ swipe: handleSwipe,
673
+ scroll_to_element: handleScrollToElement,
674
+ type_text: handleTypeText,
675
+ press_back: handlePressBack,
676
+ start_log_stream: handleStartLogStream,
677
+ read_log_stream: handleReadLogStream,
678
+ stop_log_stream: handleStopLogStream
679
+ };
680
+ export async function handleToolCall(name, args = {}) {
681
+ const handler = toolHandlers[name];
682
+ if (!handler)
683
+ throw new Error(`Unknown tool: ${name}`);
684
+ try {
685
+ return await handler(args);
686
+ }
687
+ catch (error) {
688
+ return {
689
+ content: [{ type: 'text', text: `Error executing tool ${name}: ${error instanceof Error ? error.message : String(error)}` }]
690
+ };
691
+ }
692
+ }
693
+ export function createServer() {
694
+ const server = new Server(serverInfo, {
695
+ capabilities: {
696
+ tools: {}
697
+ }
698
+ });
699
+ server.setRequestHandler(ListToolsRequestSchema, async () => ({
700
+ tools: toolDefinitions
701
+ }));
702
+ server.setRequestHandler(CallToolRequestSchema, async (request) => {
703
+ const { name, arguments: args } = request.params;
704
+ return handleToolCall(name, args);
705
+ });
706
+ return server;
707
+ }