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