native-devtools-mcp 0.10.1

MCP server for computer use & browser automation — screenshot, OCR, click, type, find_text, Chrome/Electron CDP, template matching. macOS, Windows & Android.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
use serde::Deserialize;

#[derive(Deserialize)]
pub struct TakeAxSnapshotParams {
    pub app_name: Option<String>,
}

/// Axis-aligned rectangle in screen points. Used for snapshot bboxes and
/// for the bbox carried on `ax_click` / `ax_set_value` responses.
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct Rect {
    pub x: f64,
    pub y: f64,
    pub w: f64,
    pub h: f64,
}

/// Collect the accessibility tree and format as snapshot text (Windows).
///
/// **Windows-only.** On macOS the server dispatches `take_ax_snapshot`
/// directly against [`crate::macos::ax::collect_ax_tree_indexed`] plus
/// [`crate::tools::ax_session::AxSession::create_snapshot`], so uids carry
/// the generation suffix that `ax_click` and `ax_set_value` require.
/// Exposing a macOS variant here would produce bare `a<N>` uids that those
/// tools reject as `snapshot_expired`.
#[cfg(target_os = "windows")]
pub fn take_ax_snapshot(params: TakeAxSnapshotParams) -> Result<String, String> {
    let nodes = crate::windows::uia::collect_uia_tree(params.app_name.as_deref())?;
    Ok(format_snapshot(&nodes, None))
}

/// A single node in a serialized accessibility tree snapshot.
/// Used by both AX/UIA snapshots (Phase 1) and CDP snapshots (Phase 2).
pub struct AXSnapshotNode {
    pub uid: u32,
    pub role: String,
    pub name: Option<String>,
    pub value: Option<String>,
    pub focused: bool,
    pub disabled: bool,
    pub expanded: Option<bool>,
    pub selected: Option<bool>,
    pub depth: u32,
    /// Screen-point bbox. Populated by the macOS AX collector when the
    /// element exposes `kAXPositionAttribute` and `kAXSizeAttribute`.
    /// Always `None` from Windows UIA and CDP collectors.
    pub bbox: Option<Rect>,
}

/// Format snapshot nodes into the text representation.
///
/// When `generation` is `Some(g)`, each uid renders as `a<N>g<g>` (macOS AX,
/// post-dispatch-branch format). When `None`, each uid renders as bare `a<N>`
/// (preserves exact CDP output byte-for-byte).
///
/// When a node has `bbox: Some`, a trailing `bbox=(x,y,w,h)` attribute is
/// appended. Coordinates are formatted as plain integers via truncating
/// cast — decimal values from AX are not expected in practice (AX returns
/// i32-backed CGPoint/CGSize), and integer output keeps the line stable for
/// human readers and downstream parsers.
pub fn format_snapshot(nodes: &[AXSnapshotNode], generation: Option<u64>) -> String {
    let mut lines = Vec::with_capacity(nodes.len());

    for node in nodes {
        let indent = "  ".repeat(node.depth as usize);

        let uid_tag = match generation {
            Some(g) => format!("uid=a{}g{}", node.uid, g),
            None => format!("uid=a{}", node.uid),
        };
        let mut parts = vec![format!("{} {}", uid_tag, node.role)];

        if let Some(name) = &node.name {
            parts.push(format!("\"{}\"", name));
        }
        if let Some(value) = &node.value {
            parts.push(format!("value=\"{}\"", value));
        }
        if node.focused {
            parts.push("focused".to_string());
        }
        if node.disabled {
            parts.push("disabled".to_string());
        }
        if node.expanded == Some(true) {
            parts.push("expanded".to_string());
        }
        if node.selected == Some(true) {
            parts.push("selected".to_string());
        }
        if let Some(bbox) = &node.bbox {
            parts.push(format!(
                "bbox=({},{},{},{})",
                bbox.x as i64, bbox.y as i64, bbox.w as i64, bbox.h as i64
            ));
        }

        lines.push(format!("{}{}", indent, parts.join(" ")));
    }

    lines.join("\n")
}

/// Maps a macOS AXRole string to a short CDP-style role name.
///
/// Known roles are mapped to their CDP equivalents. Unknown roles have the
/// "AX" prefix stripped and the remainder lowercased.
pub fn map_ax_role(ax_role: &str) -> String {
    match ax_role {
        "AXButton" => "button",
        "AXStaticText" => "text",
        "AXTextField" | "AXTextArea" => "textbox",
        "AXCheckBox" => "checkbox",
        "AXWebArea" => "RootWebArea",
        "AXGroup" => "generic",
        "AXLink" => "link",
        "AXImage" => "img",
        "AXList" => "list",
        "AXHeading" => "heading",
        "AXMenuItem" => "menuitem",
        "AXTable" => "table",
        "AXRow" => "row",
        "AXCell" => "cell",
        "AXTabGroup" => "tablist",
        "AXComboBox" | "AXPopUpButton" => "combobox",
        "AXScrollArea" => "scrollbar",
        "AXToolbar" => "toolbar",
        "AXRadioButton" => "radio",
        "AXSlider" => "slider",
        "AXProgressIndicator" => "progressbar",
        unknown => {
            let stripped = unknown.strip_prefix("AX").unwrap_or(unknown);
            return stripped.to_lowercase();
        }
    }
    .to_string()
}

/// Maps a Windows UIA ControlType ID to a short CDP-style role name.
///
/// Unknown IDs are formatted as `unknown_<id>`.
#[cfg(target_os = "windows")]
pub fn map_uia_control_type(control_type_id: i32) -> String {
    match control_type_id {
        50000 => "button",
        50001 => "calendar",
        50002 => "checkbox",
        50003 => "combobox",
        50004 => "textbox",
        50005 => "link",
        50006 => "img",
        50007 => "listitem",
        50008 => "list",
        50009 => "menu",
        50010 => "menubar",
        50011 => "menuitem",
        50012 => "progressbar",
        50013 => "radio",
        50014 => "scrollbar",
        50015 => "slider",
        50016 => "spinner",
        50017 => "statusbar",
        50018 => "tab",
        50019 => "tablist",
        50020 => "text",
        50021 => "toolbar",
        50022 => "tooltip",
        50023 => "tree",
        50024 => "treeitem",
        50025 => "custom",
        50026 => "generic",
        50027 => "thumb",
        50028 => "datagrid",
        50029 => "dataitem",
        50030 => "document",
        50031 => "splitbutton",
        50032 => "window",
        50033 => "pane",
        50034 => "header",
        50035 => "headeritem",
        50036 => "table",
        50037 => "titlebar",
        50038 => "separator",
        50039 => "semanticzoom",
        50040 => "appbar",
        _ => return format!("unknown_{}", control_type_id),
    }
    .to_string()
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_format_snapshot_basic_preserves_cdp_output_with_none_generation() {
        let nodes = vec![
            AXSnapshotNode {
                uid: 1,
                role: "RootWebArea".to_string(),
                name: Some("Page Title".to_string()),
                value: None,
                focused: false,
                disabled: false,
                expanded: None,
                selected: None,
                depth: 0,
                bbox: None,
            },
            AXSnapshotNode {
                uid: 2,
                role: "button".to_string(),
                name: Some("Submit".to_string()),
                value: None,
                focused: false,
                disabled: false,
                expanded: None,
                selected: None,
                depth: 1,
                bbox: None,
            },
            AXSnapshotNode {
                uid: 3,
                role: "textbox".to_string(),
                name: None,
                value: Some("hello".to_string()),
                focused: true,
                disabled: false,
                expanded: None,
                selected: None,
                depth: 1,
                bbox: None,
            },
        ];

        let result = format_snapshot(&nodes, None);
        assert_eq!(
            result,
            "uid=a1 RootWebArea \"Page Title\"\n  uid=a2 button \"Submit\"\n  uid=a3 textbox value=\"hello\" focused"
        );
    }

    #[test]
    fn test_format_snapshot_with_attributes_none_generation() {
        let nodes = vec![AXSnapshotNode {
            uid: 1,
            role: "checkbox".to_string(),
            name: Some("Remember me".to_string()),
            value: None,
            focused: false,
            disabled: true,
            expanded: Some(false),
            selected: Some(true),
            depth: 0,
            bbox: None,
        }];

        let result = format_snapshot(&nodes, None);
        assert_eq!(result, "uid=a1 checkbox \"Remember me\" disabled selected");
    }

    #[test]
    fn test_format_snapshot_empty_name_omitted_none_generation() {
        let nodes = vec![AXSnapshotNode {
            uid: 1,
            role: "generic".to_string(),
            name: None,
            value: None,
            focused: false,
            disabled: false,
            expanded: None,
            selected: None,
            depth: 0,
            bbox: None,
        }];

        let result = format_snapshot(&nodes, None);
        assert_eq!(result, "uid=a1 generic");
    }

    #[test]
    fn test_format_snapshot_emits_generation_tag_when_some() {
        let nodes = vec![AXSnapshotNode {
            uid: 42,
            role: "button".to_string(),
            name: Some("5".to_string()),
            value: None,
            focused: false,
            disabled: false,
            expanded: None,
            selected: None,
            depth: 0,
            bbox: None,
        }];
        let result = format_snapshot(&nodes, Some(3));
        assert_eq!(result, "uid=a42g3 button \"5\"");
    }

    #[test]
    fn test_format_snapshot_emits_bbox_when_some() {
        let nodes = vec![AXSnapshotNode {
            uid: 1,
            role: "button".to_string(),
            name: Some("5".to_string()),
            value: None,
            focused: false,
            disabled: false,
            expanded: None,
            selected: None,
            depth: 0,
            bbox: Some(Rect {
                x: 412.0,
                y: 285.0,
                w: 64.0,
                h: 32.0,
            }),
        }];
        let result = format_snapshot(&nodes, Some(3));
        assert_eq!(result, "uid=a1g3 button \"5\" bbox=(412,285,64,32)");
    }

    #[test]
    fn test_format_snapshot_omits_bbox_when_none_even_with_generation() {
        let nodes = vec![AXSnapshotNode {
            uid: 1,
            role: "generic".to_string(),
            name: None,
            value: None,
            focused: false,
            disabled: false,
            expanded: None,
            selected: None,
            depth: 0,
            bbox: None,
        }];
        let result = format_snapshot(&nodes, Some(3));
        assert_eq!(result, "uid=a1g3 generic");
    }

    #[test]
    fn test_rect_renders_integer_coords_without_trailing_decimals() {
        // Load-bearing: CDP output and existing tests assume plain integers.
        let rect = Rect {
            x: 0.0,
            y: 0.0,
            w: 1440.0,
            h: 900.0,
        };
        assert_eq!(
            format!(
                "bbox=({},{},{},{})",
                rect.x as i64, rect.y as i64, rect.w as i64, rect.h as i64
            ),
            "bbox=(0,0,1440,900)"
        );
    }

    #[test]
    fn test_map_macos_role() {
        assert_eq!(map_ax_role("AXButton"), "button");
        assert_eq!(map_ax_role("AXStaticText"), "text");
        assert_eq!(map_ax_role("AXTextField"), "textbox");
        assert_eq!(map_ax_role("AXTextArea"), "textbox");
        assert_eq!(map_ax_role("AXCheckBox"), "checkbox");
        assert_eq!(map_ax_role("AXWebArea"), "RootWebArea");
        assert_eq!(map_ax_role("AXGroup"), "generic");
        assert_eq!(map_ax_role("AXLink"), "link");
        assert_eq!(map_ax_role("AXImage"), "img");
        assert_eq!(map_ax_role("AXList"), "list");
        assert_eq!(map_ax_role("AXHeading"), "heading");
        assert_eq!(map_ax_role("AXMenuItem"), "menuitem");
        assert_eq!(map_ax_role("AXTable"), "table");
        assert_eq!(map_ax_role("AXRow"), "row");
        assert_eq!(map_ax_role("AXCell"), "cell");
        assert_eq!(map_ax_role("AXTabGroup"), "tablist");
        assert_eq!(map_ax_role("AXComboBox"), "combobox");
        assert_eq!(map_ax_role("AXPopUpButton"), "combobox");
        assert_eq!(map_ax_role("AXScrollArea"), "scrollbar");
        assert_eq!(map_ax_role("AXToolbar"), "toolbar");
        assert_eq!(map_ax_role("AXRadioButton"), "radio");
        assert_eq!(map_ax_role("AXSlider"), "slider");
        assert_eq!(map_ax_role("AXProgressIndicator"), "progressbar");
    }

    #[test]
    fn test_map_macos_role_unknown_passthrough() {
        assert_eq!(map_ax_role("AXSplitGroup"), "splitgroup");
        assert_eq!(map_ax_role("AXOutline"), "outline");
    }

    #[cfg(target_os = "windows")]
    #[test]
    fn test_map_windows_control_type() {
        assert_eq!(map_uia_control_type(50000), "button");
        assert_eq!(map_uia_control_type(50001), "calendar");
        assert_eq!(map_uia_control_type(50002), "checkbox");
        assert_eq!(map_uia_control_type(50003), "combobox");
        assert_eq!(map_uia_control_type(50004), "textbox");
        assert_eq!(map_uia_control_type(50005), "link");
        assert_eq!(map_uia_control_type(50006), "img");
        assert_eq!(map_uia_control_type(50007), "listitem");
        assert_eq!(map_uia_control_type(50008), "list");
        assert_eq!(map_uia_control_type(50009), "menu");
        assert_eq!(map_uia_control_type(50010), "menubar");
        assert_eq!(map_uia_control_type(50011), "menuitem");
        assert_eq!(map_uia_control_type(50012), "progressbar");
        assert_eq!(map_uia_control_type(50013), "radio");
        assert_eq!(map_uia_control_type(50014), "scrollbar");
        assert_eq!(map_uia_control_type(50015), "slider");
        assert_eq!(map_uia_control_type(50016), "spinner");
        assert_eq!(map_uia_control_type(50017), "statusbar");
        assert_eq!(map_uia_control_type(50018), "tab");
        assert_eq!(map_uia_control_type(50019), "tablist");
        assert_eq!(map_uia_control_type(50020), "text");
        assert_eq!(map_uia_control_type(50021), "toolbar");
        assert_eq!(map_uia_control_type(50022), "tooltip");
        assert_eq!(map_uia_control_type(50023), "tree");
        assert_eq!(map_uia_control_type(50024), "treeitem");
        assert_eq!(map_uia_control_type(50025), "custom");
        assert_eq!(map_uia_control_type(50026), "generic");
        assert_eq!(map_uia_control_type(50027), "thumb");
        assert_eq!(map_uia_control_type(50028), "datagrid");
        assert_eq!(map_uia_control_type(50029), "dataitem");
        assert_eq!(map_uia_control_type(50030), "document");
        assert_eq!(map_uia_control_type(50031), "splitbutton");
        assert_eq!(map_uia_control_type(50032), "window");
        assert_eq!(map_uia_control_type(50033), "pane");
        assert_eq!(map_uia_control_type(50034), "header");
        assert_eq!(map_uia_control_type(50035), "headeritem");
        assert_eq!(map_uia_control_type(50036), "table");
        assert_eq!(map_uia_control_type(50037), "titlebar");
        assert_eq!(map_uia_control_type(50038), "separator");
        assert_eq!(map_uia_control_type(50039), "semanticzoom");
        assert_eq!(map_uia_control_type(50040), "appbar");
    }

    #[cfg(target_os = "windows")]
    #[test]
    fn test_map_windows_control_type_unknown() {
        assert_eq!(map_uia_control_type(99999), "unknown_99999");
    }
}