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
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
//! CGEvent-based input simulation for macOS.
//!
//! This module provides system-level input simulation that works with any application,
//! regardless of the UI framework used (AppKit, SwiftUI, egui, etc.).

use core_graphics::event::{
    CGEvent, CGEventFlags, CGEventTapLocation, CGEventType, CGKeyCode, CGMouseButton,
};
use core_graphics::event_source::{CGEventSource, CGEventSourceStateID};
use core_graphics::geometry::CGPoint;
use std::thread;
use std::time::Duration;

/// Mouse button types for click operations.
#[derive(Debug, Clone, Copy, Default)]
pub enum MouseButton {
    #[default]
    Left,
    Right,
    Center,
}

/// Check if the current process has accessibility permissions.
/// CGEvent-based input requires accessibility access to be granted.
pub fn check_accessibility_permission() -> bool {
    use core_foundation::base::TCFType;
    use core_foundation::dictionary::CFDictionary;
    use core_foundation::string::CFString;

    // Link to ApplicationServices framework for AXIsProcessTrustedWithOptions
    #[link(name = "ApplicationServices", kind = "framework")]
    extern "C" {
        fn AXIsProcessTrustedWithOptions(options: core_foundation::base::CFTypeRef) -> bool;
    }

    // kAXTrustedCheckOptionPrompt key
    let key = CFString::new("AXTrustedCheckOptionPrompt");
    let value = core_foundation::boolean::CFBoolean::false_value();

    let options = CFDictionary::from_CFType_pairs(&[(key.as_CFType(), value.as_CFType())]);

    unsafe { AXIsProcessTrustedWithOptions(options.as_concrete_TypeRef() as _) }
}

/// Move the mouse cursor to the specified screen coordinates.
pub fn move_mouse(x: f64, y: f64) -> Result<(), String> {
    let source = CGEventSource::new(CGEventSourceStateID::HIDSystemState)
        .map_err(|_| "Failed to create event source")?;

    let point = CGPoint::new(x, y);

    let event = CGEvent::new_mouse_event(
        source,
        CGEventType::MouseMoved,
        point,
        CGMouseButton::Left, // Button doesn't matter for move
    )
    .map_err(|_| "Failed to create mouse move event")?;

    event.post(CGEventTapLocation::HID);
    Ok(())
}

/// Click at the specified screen coordinates.
pub fn click(x: f64, y: f64, button: MouseButton, click_count: u32) -> Result<(), String> {
    let source = CGEventSource::new(CGEventSourceStateID::HIDSystemState)
        .map_err(|_| "Failed to create event source")?;

    let point = CGPoint::new(x, y);

    let (down_type, up_type, cg_button) = match button {
        MouseButton::Left => (
            CGEventType::LeftMouseDown,
            CGEventType::LeftMouseUp,
            CGMouseButton::Left,
        ),
        MouseButton::Right => (
            CGEventType::RightMouseDown,
            CGEventType::RightMouseUp,
            CGMouseButton::Right,
        ),
        MouseButton::Center => (
            CGEventType::OtherMouseDown,
            CGEventType::OtherMouseUp,
            CGMouseButton::Center,
        ),
    };

    // Perform the click(s)
    for i in 0..click_count {
        let down_event = CGEvent::new_mouse_event(source.clone(), down_type, point, cg_button)
            .map_err(|_| "Failed to create mouse down event")?;

        let up_event = CGEvent::new_mouse_event(source.clone(), up_type, point, cg_button)
            .map_err(|_| "Failed to create mouse up event")?;

        // Set click count (important for double/triple clicks)
        down_event.set_integer_value_field(
            core_graphics::event::EventField::MOUSE_EVENT_CLICK_STATE,
            (i + 1) as i64,
        );
        up_event.set_integer_value_field(
            core_graphics::event::EventField::MOUSE_EVENT_CLICK_STATE,
            (i + 1) as i64,
        );

        down_event.post(CGEventTapLocation::HID);
        // Small delay between down and up for realism
        thread::sleep(Duration::from_millis(10));
        up_event.post(CGEventTapLocation::HID);

        // Delay between clicks for multi-click
        if i < click_count - 1 {
            thread::sleep(Duration::from_millis(50));
        }
    }

    Ok(())
}

/// Drag from one point to another.
pub fn drag(
    start_x: f64,
    start_y: f64,
    end_x: f64,
    end_y: f64,
    button: MouseButton,
) -> Result<(), String> {
    let source = CGEventSource::new(CGEventSourceStateID::HIDSystemState)
        .map_err(|_| "Failed to create event source")?;

    let start_point = CGPoint::new(start_x, start_y);
    let end_point = CGPoint::new(end_x, end_y);

    let (down_type, drag_type, up_type, cg_button) = match button {
        MouseButton::Left => (
            CGEventType::LeftMouseDown,
            CGEventType::LeftMouseDragged,
            CGEventType::LeftMouseUp,
            CGMouseButton::Left,
        ),
        MouseButton::Right => (
            CGEventType::RightMouseDown,
            CGEventType::RightMouseDragged,
            CGEventType::RightMouseUp,
            CGMouseButton::Right,
        ),
        MouseButton::Center => (
            CGEventType::OtherMouseDown,
            CGEventType::OtherMouseDragged,
            CGEventType::OtherMouseUp,
            CGMouseButton::Center,
        ),
    };

    // Mouse down at start
    let down_event = CGEvent::new_mouse_event(source.clone(), down_type, start_point, cg_button)
        .map_err(|_| "Failed to create mouse down event")?;
    down_event.post(CGEventTapLocation::HID);
    thread::sleep(Duration::from_millis(10));

    // Drag to end (interpolate for smoother movement)
    let steps = 10;
    for i in 1..=steps {
        let t = i as f64 / steps as f64;
        let current_x = start_x + (end_x - start_x) * t;
        let current_y = start_y + (end_y - start_y) * t;
        let current_point = CGPoint::new(current_x, current_y);

        let drag_event =
            CGEvent::new_mouse_event(source.clone(), drag_type, current_point, cg_button)
                .map_err(|_| "Failed to create drag event")?;
        drag_event.post(CGEventTapLocation::HID);
        thread::sleep(Duration::from_millis(10));
    }

    // Mouse up at end
    let up_event = CGEvent::new_mouse_event(source.clone(), up_type, end_point, cg_button)
        .map_err(|_| "Failed to create mouse up event")?;
    up_event.post(CGEventTapLocation::HID);

    Ok(())
}

/// Scroll at the specified position.
pub fn scroll(x: f64, y: f64, delta_x: i32, delta_y: i32) -> Result<(), String> {
    // First move mouse to position
    move_mouse(x, y)?;
    thread::sleep(Duration::from_millis(10));

    // Use CGEventCreateScrollWheelEvent via FFI since core-graphics crate doesn't expose it
    #[link(name = "CoreGraphics", kind = "framework")]
    extern "C" {
        fn CGEventCreateScrollWheelEvent(
            source: *const std::ffi::c_void,
            units: u32,
            wheel_count: u32,
            wheel1: i32,
            wheel2: i32,
        ) -> *mut std::ffi::c_void;
        fn CGEventPost(tap: u32, event: *mut std::ffi::c_void);
        fn CFRelease(cf: *mut std::ffi::c_void);
    }

    unsafe {
        // units: 0 = pixel, 1 = line
        let event = CGEventCreateScrollWheelEvent(
            std::ptr::null(),
            0, // kCGScrollEventUnitPixel
            2, // wheel_count
            delta_y,
            delta_x,
        );

        if event.is_null() {
            return Err("Failed to create scroll event".to_string());
        }

        CGEventPost(0, event); // kCGHIDEventTap = 0
        CFRelease(event);
    }

    Ok(())
}

/// Map a key name to a CGKeyCode.
fn key_name_to_code(key: &str) -> Option<CGKeyCode> {
    // Virtual key codes from Events.h
    Some(match key.to_lowercase().as_str() {
        // Letters
        "a" => 0x00,
        "s" => 0x01,
        "d" => 0x02,
        "f" => 0x03,
        "h" => 0x04,
        "g" => 0x05,
        "z" => 0x06,
        "x" => 0x07,
        "c" => 0x08,
        "v" => 0x09,
        "b" => 0x0B,
        "q" => 0x0C,
        "w" => 0x0D,
        "e" => 0x0E,
        "r" => 0x0F,
        "y" => 0x10,
        "t" => 0x11,
        "1" | "!" => 0x12,
        "2" | "@" => 0x13,
        "3" | "#" => 0x14,
        "4" | "$" => 0x15,
        "6" | "^" => 0x16,
        "5" | "%" => 0x17,
        "=" | "+" => 0x18,
        "9" | "(" => 0x19,
        "7" | "&" => 0x1A,
        "-" | "_" => 0x1B,
        "8" | "*" => 0x1C,
        "0" | ")" => 0x1D,
        "]" | "}" => 0x1E,
        "o" => 0x1F,
        "u" => 0x20,
        "[" | "{" => 0x21,
        "i" => 0x22,
        "p" => 0x23,
        "l" => 0x25,
        "j" => 0x26,
        "'" | "\"" => 0x27,
        "k" => 0x28,
        ";" | ":" => 0x29,
        "\\" | "|" => 0x2A,
        "," | "<" => 0x2B,
        "/" | "?" => 0x2C,
        "n" => 0x2D,
        "m" => 0x2E,
        "." | ">" => 0x2F,
        "`" | "~" => 0x32,

        // Special keys
        "return" | "enter" => 0x24,
        "tab" => 0x30,
        "space" | " " => 0x31,
        "delete" | "backspace" => 0x33,
        "escape" | "esc" => 0x35,
        "command" | "cmd" => 0x37,
        "shift" => 0x38,
        "capslock" => 0x39,
        "option" | "alt" => 0x3A,
        "control" | "ctrl" => 0x3B,
        "rightshift" => 0x3C,
        "rightoption" | "rightalt" => 0x3D,
        "rightcontrol" | "rightctrl" => 0x3E,
        "fn" | "function" => 0x3F,

        // Function keys
        "f1" => 0x7A,
        "f2" => 0x78,
        "f3" => 0x63,
        "f4" => 0x76,
        "f5" => 0x60,
        "f6" => 0x61,
        "f7" => 0x62,
        "f8" => 0x64,
        "f9" => 0x65,
        "f10" => 0x6D,
        "f11" => 0x67,
        "f12" => 0x6F,
        "f13" => 0x69,
        "f14" => 0x6B,
        "f15" => 0x71,
        "f16" => 0x6A,
        "f17" => 0x40,
        "f18" => 0x4F,
        "f19" => 0x50,
        "f20" => 0x5A,

        // Navigation
        "home" => 0x73,
        "end" => 0x77,
        "pageup" => 0x74,
        "pagedown" => 0x79,
        "left" | "leftarrow" => 0x7B,
        "right" | "rightarrow" => 0x7C,
        "down" | "downarrow" => 0x7D,
        "up" | "uparrow" => 0x7E,
        "forwarddelete" => 0x75,
        "help" => 0x72,

        _ => return None,
    })
}

/// Press a key with optional modifiers.
pub fn press_key(key: &str, modifiers: &[String]) -> Result<(), String> {
    let source = CGEventSource::new(CGEventSourceStateID::HIDSystemState)
        .map_err(|_| "Failed to create event source")?;

    let keycode = key_name_to_code(key).ok_or_else(|| format!("Unknown key: {}", key))?;

    // Build modifier flags
    let mut flags = CGEventFlags::empty();
    for modifier in modifiers {
        match modifier.to_lowercase().as_str() {
            "shift" => flags |= CGEventFlags::CGEventFlagShift,
            "control" | "ctrl" => flags |= CGEventFlags::CGEventFlagControl,
            "option" | "alt" => flags |= CGEventFlags::CGEventFlagAlternate,
            "command" | "cmd" => flags |= CGEventFlags::CGEventFlagCommand,
            _ => return Err(format!("Unknown modifier: {}", modifier)),
        }
    }

    // Key down
    let down_event = CGEvent::new_keyboard_event(source.clone(), keycode, true)
        .map_err(|_| "Failed to create key down event")?;
    down_event.set_flags(flags);
    down_event.post(CGEventTapLocation::HID);

    thread::sleep(Duration::from_millis(10));

    // Key up
    let up_event = CGEvent::new_keyboard_event(source, keycode, false)
        .map_err(|_| "Failed to create key up event")?;
    up_event.set_flags(flags);
    up_event.post(CGEventTapLocation::HID);

    Ok(())
}

/// Type a string of text by simulating key presses.
pub fn type_text(text: &str) -> Result<(), String> {
    let source = CGEventSource::new(CGEventSourceStateID::HIDSystemState)
        .map_err(|_| "Failed to create event source")?;

    for c in text.chars() {
        // Check if shift is needed
        let needs_shift = c.is_uppercase()
            || matches!(
                c,
                '!' | '@'
                    | '#'
                    | '$'
                    | '%'
                    | '^'
                    | '&'
                    | '*'
                    | '('
                    | ')'
                    | '_'
                    | '+'
                    | '{'
                    | '}'
                    | '|'
                    | ':'
                    | '"'
                    | '<'
                    | '>'
                    | '?'
                    | '~'
            );

        let key_char = c.to_lowercase().next().unwrap_or(c);
        let key_str = key_char.to_string();

        if let Some(keycode) = key_name_to_code(&key_str) {
            let mut flags = CGEventFlags::empty();
            if needs_shift {
                flags |= CGEventFlags::CGEventFlagShift;
            }

            // Key down
            let down_event = CGEvent::new_keyboard_event(source.clone(), keycode, true)
                .map_err(|_| "Failed to create key down event")?;
            down_event.set_flags(flags);
            down_event.post(CGEventTapLocation::HID);

            thread::sleep(Duration::from_millis(5));

            // Key up
            let up_event = CGEvent::new_keyboard_event(source.clone(), keycode, false)
                .map_err(|_| "Failed to create key up event")?;
            up_event.set_flags(flags);
            up_event.post(CGEventTapLocation::HID);

            thread::sleep(Duration::from_millis(5));
        } else {
            // For characters we don't have a direct mapping for,
            // use CGEvent's ability to set Unicode string
            let down_event = CGEvent::new_keyboard_event(source.clone(), 0, true)
                .map_err(|_| "Failed to create key event")?;

            // Set the Unicode string for this character
            down_event.set_string(&c.to_string());
            down_event.post(CGEventTapLocation::HID);

            thread::sleep(Duration::from_millis(5));

            let up_event = CGEvent::new_keyboard_event(source.clone(), 0, false)
                .map_err(|_| "Failed to create key up event")?;
            up_event.post(CGEventTapLocation::HID);

            thread::sleep(Duration::from_millis(5));
        }
    }

    Ok(())
}

/// Get the current cursor position in screen coordinates.
///
/// Creates a dummy CGEvent and reads its location, which reflects
/// the current mouse position.
pub fn get_cursor_position() -> Result<(f64, f64), String> {
    let source = CGEventSource::new(CGEventSourceStateID::HIDSystemState)
        .map_err(|_| "Failed to create event source")?;

    let event = CGEvent::new(source).map_err(|_| "Failed to create CGEvent")?;

    let point = event.location();
    Ok((point.x, point.y))
}

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

    #[test]
    fn test_key_mapping() {
        assert!(key_name_to_code("a").is_some());
        assert!(key_name_to_code("return").is_some());
        assert!(key_name_to_code("f1").is_some());
        assert!(key_name_to_code("nonexistent").is_none());
    }

    #[test]
    fn test_get_cursor_position_returns_coordinates() {
        // Just verify it returns successfully; coordinates can be negative
        // on multi-monitor setups where a display is above/left of primary.
        let (_x, _y) = get_cursor_position().expect("should get cursor position");
    }
}