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