umbriel-mcp 0.2.1

MCP server for the Umbriel Wayland compositor: windows, workspaces, outputs, pointer and keyboard input, screenshots with OCR, and the clipboard.
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
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
mod compositor;
mod config_edit;
mod doctor;
mod input;
mod ipc;
mod keyboard;
mod profile;
mod register;
mod screen;

use base64::prelude::{BASE64_STANDARD, Engine};
use clap::Parser;
use profile::Profile;
use rmcp::RoleServer;
use rmcp::handler::server::tool::ToolCallContext;
use rmcp::handler::server::wrapper::Parameters;
use rmcp::model::{
    CallToolRequestParams, CallToolResponse, CallToolResult, ContentBlock, ErrorData,
};
use rmcp::service::RequestContext;
use rmcp::transport::stdio;
use rmcp::{ServerHandler, ServiceExt, schemars, tool, tool_handler, tool_router};
use serde::Deserialize;

fn failed(error: anyhow::Error) -> ErrorData {
    ErrorData::internal_error(format!("{error:#}"), None)
}

fn as_pretty_json(value: serde_json::Value) -> Result<String, ErrorData> {
    serde_json::to_string_pretty(&value).map_err(|cause| failed(cause.into()))
}

fn acted(action: String) -> Result<String, ErrorData> {
    ipc::dispatch(&action).map_err(failed)?;
    Ok(format!("ok: {action}"))
}

fn workspace_matches(window_workspace: &str, wanted: &str) -> bool {
    let wanted = wanted.trim();
    window_workspace == wanted
        || window_workspace
            .rsplit_once(':')
            .is_some_and(|(_, number_or_name)| number_or_name == wanted)
}

fn refused(reason: String) -> ErrorData {
    ErrorData::invalid_request(reason, None)
}

fn with_optional_target(base: &str, target: &Option<String>) -> String {
    match target {
        Some(value) if !value.is_empty() => format!("{base}:{value}"),
        _ => base.to_string(),
    }
}

#[derive(Debug, Deserialize, schemars::JsonSchema)]
struct WindowFilter {
    /// Workspace to list: a number or name such as "4" (that workspace on every monitor),
    /// or a full id such as "HDMI-A-1:4"
    workspace: Option<String>,
    /// Only list windows on this output, such as "DP-1"
    output: Option<String>,
}

#[derive(Debug, Deserialize, schemars::JsonSchema)]
struct WorkspaceTarget {
    /// Workspace position (1-64) or exact name
    workspace: String,
    /// Restrict the selection to this output, such as "HDMI-A-1"
    output: Option<String>,
}

#[derive(Debug, Deserialize, schemars::JsonSchema)]
struct WindowTarget {
    /// Window id from list_windows; omit to act on the focused window
    window_id: Option<String>,
}

#[derive(Debug, Deserialize, schemars::JsonSchema)]
struct FocusTarget {
    /// Window id from list_windows
    window_id: String,
    /// Also warp the pointer onto the window
    warp_cursor: Option<bool>,
}

#[derive(Debug, Deserialize, schemars::JsonSchema)]
struct ResizeRequest {
    /// Which edge to move: left, right, up or down
    edge: String,
    /// Signed fraction between -0.9 and 0.9
    delta: f32,
}

#[derive(Debug, Deserialize, schemars::JsonSchema)]
struct RawAction {
    /// Any action accepted by `umbriel msg`, such as "overview-toggle"
    action: String,
}

#[derive(Debug, Deserialize, schemars::JsonSchema)]
struct SpawnRequest {
    /// Command line to run
    command: String,
}

#[derive(Debug, Deserialize, schemars::JsonSchema)]
struct PointerPosition {
    x: i32,
    y: i32,
}

#[derive(Debug, Deserialize, schemars::JsonSchema)]
struct ClickRequest {
    /// left, right, middle or side
    button: Option<String>,
    /// Issue a double click
    double: Option<bool>,
}

#[derive(Debug, Deserialize, schemars::JsonSchema)]
struct ScrollRequest {
    /// up or down
    direction: Option<String>,
    /// Number of wheel steps
    amount: Option<i32>,
}

#[derive(Debug, Deserialize, schemars::JsonSchema)]
struct DragRequest {
    from_x: i32,
    from_y: i32,
    to_x: i32,
    to_y: i32,
    /// left, right, middle or side
    button: Option<String>,
}

#[derive(Debug, Deserialize, schemars::JsonSchema)]
struct TypeRequest {
    /// Literal text to type into the focused surface
    text: String,
    /// Delay between keystrokes in milliseconds
    delay_ms: Option<u32>,
}

#[derive(Debug, Deserialize, schemars::JsonSchema)]
struct KeyRequest {
    /// Chord such as "ctrl+c", "alt+Tab" or "Return"
    keys: String,
}

#[derive(Debug, Deserialize, schemars::JsonSchema)]
struct CaptureRequest {
    /// Capture only this output, such as "HDMI-A-1"
    output: Option<String>,
    /// Capture only this window id, even if it is on a workspace that is not shown
    window_id: Option<String>,
    /// Downscale so the image is at most this wide
    max_width: Option<u32>,
}

#[derive(Debug, Deserialize, schemars::JsonSchema)]
struct TextSearchRequest {
    /// Text to look for on screen, matched case-insensitively
    target: String,
    /// Restrict the search to this output
    output: Option<String>,
}

#[derive(Debug, Deserialize, schemars::JsonSchema)]
struct ActionFilter {
    /// Only list actions whose name, description or group contains this text, such as "workspace"
    filter: Option<String>,
}

#[derive(Debug, Deserialize, schemars::JsonSchema)]
struct OptionQuery {
    /// Words that must all appear in the option path, such as "gap" or "blur enabled"; omit for a
    /// per-section summary
    query: Option<String>,
}

#[derive(Debug, Deserialize, schemars::JsonSchema)]
struct ConfigSection {
    /// Only return this section and its subsections, such as "keybinds", "layout" or "window_rule"
    section: Option<String>,
}

#[derive(Debug, Deserialize, schemars::JsonSchema)]
struct KeybindRequest {
    /// Key chord as Umbriel writes it, such as "Mod+F" or "Mod+Shift+Return"
    chord: String,
    /// Umbriel action, as listed by list_actions, such as "window-toggle-fullscreen" or "spawn:kitty"
    action: String,
    /// Repeat the action while the keys are held; omit to keep Umbriel's default
    repeat: Option<bool>,
}

#[derive(Debug, Deserialize, schemars::JsonSchema)]
struct KeybindChord {
    /// Key chord to unbind, such as "Mod+F"
    chord: String,
}

#[derive(Debug, Deserialize, schemars::JsonSchema)]
struct ClipboardWriteRequest {
    text: String,
}

#[derive(Debug, Deserialize, schemars::JsonSchema)]
struct LayoutRequest {
    /// scrolling, dwindle, master or floating
    mode: String,
}

#[derive(Clone)]
struct UmbrielServer {
    profile: Profile,
}

#[tool_router]
impl UmbrielServer {
    #[tool(
        description = "List the windows open on this desktop with id, app_id, title, geometry, monitor and workspace. Use it to answer what is open or what is on workspace 4; pass workspace \"4\" for that workspace on every monitor."
    )]
    fn list_windows(
        &self,
        Parameters(filter): Parameters<WindowFilter>,
    ) -> Result<String, ErrorData> {
        let all = ipc::windows().map_err(failed)?;
        let kept: Vec<_> = all
            .into_iter()
            .filter(|entry| {
                let field = |key: &str| entry.get(key).and_then(|v| v.as_str()).unwrap_or("");
                filter
                    .workspace
                    .as_ref()
                    .is_none_or(|wanted| workspace_matches(field("workspace"), wanted))
                    && filter
                        .output
                        .as_ref()
                        .is_none_or(|wanted| field("output") == wanted)
            })
            .collect();
        as_pretty_json(serde_json::Value::Array(kept))
    }

    #[tool(
        description = "List this desktop's workspaces with monitor, index, layout, window count and focus. Each monitor has its own numbered workspaces."
    )]
    fn list_workspaces(&self) -> Result<String, ErrorData> {
        as_pretty_json(ipc::query("workspaces").map_err(failed)?)
    }

    #[tool(
        description = "List this desktop's monitors (outputs) with position, modes, scale and the workspace each one shows."
    )]
    fn list_outputs(&self) -> Result<String, ErrorData> {
        as_pretty_json(ipc::query("outputs").map_err(failed)?)
    }

    #[tool(description = "Get the window that currently has focus on this desktop.")]
    fn get_active_window(&self) -> Result<String, ErrorData> {
        as_pretty_json(ipc::query("focused-window").map_err(failed)?)
    }

    #[tool(description = "List layer-shell surfaces such as bars, panels and wallpapers.")]
    fn list_layers(&self) -> Result<String, ErrorData> {
        as_pretty_json(ipc::query("layers").map_err(failed)?)
    }

    #[tool(
        description = "Switch to a workspace by position or name, optionally scoped to an output."
    )]
    fn switch_workspace(
        &self,
        Parameters(target): Parameters<WorkspaceTarget>,
    ) -> Result<String, ErrorData> {
        let selector = match &target.output {
            Some(output) => format!("{}/{}", target.workspace, output),
            None => target.workspace.clone(),
        };
        acted(format!("workspace-switch:{selector}"))
    }

    #[tool(description = "Focus a window by id, optionally warping the pointer onto it.")]
    fn focus_window(
        &self,
        Parameters(target): Parameters<FocusTarget>,
    ) -> Result<String, ErrorData> {
        let verb = if target.warp_cursor.unwrap_or(false) {
            "window-focus-warp"
        } else {
            "window-focus"
        };
        acted(format!("{verb}:{}", target.window_id))
    }

    #[tool(description = "Ask a window to close; acts on the focused window when no id is given.")]
    fn close_window(
        &self,
        Parameters(target): Parameters<WindowTarget>,
    ) -> Result<String, ErrorData> {
        acted(with_optional_target("window-close", &target.window_id))
    }

    #[tool(
        description = "Kill the process owning a window; acts on the focused window when no id is given."
    )]
    fn kill_window(
        &self,
        Parameters(target): Parameters<WindowTarget>,
    ) -> Result<String, ErrorData> {
        acted(with_optional_target("window-kill", &target.window_id))
    }

    #[tool(description = "Move the focused window to a workspace, optionally scoped to an output.")]
    fn move_window_to_workspace(
        &self,
        Parameters(target): Parameters<WorkspaceTarget>,
    ) -> Result<String, ErrorData> {
        let selector = match &target.output {
            Some(output) => format!("{}/{}", target.workspace, output),
            None => target.workspace.clone(),
        };
        acted(format!("window-move-to-workspace:{selector}"))
    }

    #[tool(description = "Toggle fullscreen for the focused window.")]
    fn toggle_fullscreen(&self) -> Result<String, ErrorData> {
        acted("window-toggle-fullscreen".to_string())
    }

    #[tool(description = "Float or tile a window; acts on the focused window when no id is given.")]
    fn toggle_floating(
        &self,
        Parameters(target): Parameters<WindowTarget>,
    ) -> Result<String, ErrorData> {
        acted(with_optional_target(
            "window-toggle-floating",
            &target.window_id,
        ))
    }

    #[tool(
        description = "Resize the focused window by moving one edge by a signed fraction (-0.9 to 0.9)."
    )]
    fn resize_window(
        &self,
        Parameters(request): Parameters<ResizeRequest>,
    ) -> Result<String, ErrorData> {
        let verb = match request.edge.as_str() {
            "left" => "window-modify-width-left",
            "right" => "window-modify-width-right",
            "up" => "window-modify-height-up",
            "down" => "window-modify-height-down",
            other => {
                return Err(ErrorData::invalid_params(
                    format!("unknown edge {other}; expected left, right, up or down"),
                    None,
                ));
            }
        };
        acted(format!("{verb}:{}", request.delta))
    }

    #[tool(
        description = "Set the active workspace layout: scrolling, dwindle, master or floating."
    )]
    fn set_workspace_layout(
        &self,
        Parameters(request): Parameters<LayoutRequest>,
    ) -> Result<String, ErrorData> {
        acted(format!("workspace-set-layout:{}", request.mode))
    }

    #[tool(description = "Toggle the workspace overview.")]
    fn toggle_overview(&self) -> Result<String, ErrorData> {
        acted("overview-toggle".to_string())
    }

    #[tool(description = "Launch a command through the compositor with an activation token.")]
    fn launch_app(
        &self,
        Parameters(request): Parameters<SpawnRequest>,
    ) -> Result<String, ErrorData> {
        acted(format!("spawn:{}", request.command))
    }

    #[tool(description = "Run any raw umbriel action, for anything the other tools do not cover.")]
    fn run_action(&self, Parameters(request): Parameters<RawAction>) -> Result<String, ErrorData> {
        self.profile
            .permit_action("run_action", &request.action)
            .map_err(refused)?;
        acted(request.action)
    }

    #[tool(description = "Move the pointer to absolute screen coordinates.")]
    fn mouse_move(
        &self,
        Parameters(position): Parameters<PointerPosition>,
    ) -> Result<String, ErrorData> {
        input::move_pointer(position.x, position.y).map_err(failed)?;
        Ok(format!("pointer at {},{}", position.x, position.y))
    }

    #[tool(description = "Click a mouse button at the current pointer position.")]
    fn mouse_click(
        &self,
        Parameters(request): Parameters<ClickRequest>,
    ) -> Result<String, ErrorData> {
        let button = request.button.unwrap_or_else(|| "left".to_string());
        input::click(&button, request.double.unwrap_or(false)).map_err(failed)?;
        Ok(format!("clicked {button}"))
    }

    #[tool(description = "Scroll the mouse wheel at the current pointer position.")]
    fn mouse_scroll(
        &self,
        Parameters(request): Parameters<ScrollRequest>,
    ) -> Result<String, ErrorData> {
        let direction = request.direction.unwrap_or_else(|| "down".to_string());
        let amount = request.amount.unwrap_or(3);
        input::scroll(&direction, amount).map_err(failed)?;
        Ok(format!("scrolled {direction} by {amount}"))
    }

    #[tool(description = "Press at one point, drag to another, and release.")]
    fn mouse_drag(
        &self,
        Parameters(request): Parameters<DragRequest>,
    ) -> Result<String, ErrorData> {
        let button = request.button.unwrap_or_else(|| "left".to_string());
        input::drag(
            (request.from_x, request.from_y),
            (request.to_x, request.to_y),
            &button,
        )
        .map_err(failed)?;
        Ok(format!(
            "dragged {button} from {},{} to {},{}",
            request.from_x, request.from_y, request.to_x, request.to_y
        ))
    }

    #[tool(description = "Type literal text into the focused surface.")]
    fn type_text(&self, Parameters(request): Parameters<TypeRequest>) -> Result<String, ErrorData> {
        input::type_text(&request.text, request.delay_ms.unwrap_or(0)).map_err(failed)?;
        Ok(format!("typed {} characters", request.text.chars().count()))
    }

    #[tool(description = "Press a key chord such as ctrl+c, alt+Tab or Return.")]
    fn key_press(&self, Parameters(request): Parameters<KeyRequest>) -> Result<String, ErrorData> {
        input::press_keys(&request.keys).map_err(failed)?;
        Ok(format!("pressed {}", request.keys))
    }

    #[tool(
        description = "Capture the screen, one output, or one window as a downscaled image. A window_id captures that window even on a hidden workspace, without switching to it."
    )]
    fn screenshot(
        &self,
        Parameters(request): Parameters<CaptureRequest>,
    ) -> Result<CallToolResult, ErrorData> {
        let shot = screen::capture(
            request.output.as_deref(),
            request.window_id.as_deref(),
            request.max_width.unwrap_or(screen::DEFAULT_MAX_WIDTH),
        )
        .map_err(failed)?;
        Ok(CallToolResult::success(vec![
            ContentBlock::image(BASE64_STANDARD.encode(&shot.jpeg), "image/jpeg"),
            ContentBlock::text(shot.coordinate_note()),
        ]))
    }

    #[tool(
        description = "Capture the screen, one output, or one window and return the text found in it by OCR. A window_id reads that window even on a hidden workspace, without switching to it."
    )]
    fn screenshot_with_ocr(
        &self,
        Parameters(request): Parameters<CaptureRequest>,
    ) -> Result<CallToolResult, ErrorData> {
        let shot = screen::capture(
            request.output.as_deref(),
            request.window_id.as_deref(),
            request.max_width.unwrap_or(screen::DEFAULT_MAX_WIDTH),
        )
        .map_err(failed)?;
        let recognised = screen::read_text(&shot.native_png).map_err(failed)?;
        Ok(CallToolResult::success(vec![
            ContentBlock::image(BASE64_STANDARD.encode(&shot.jpeg), "image/jpeg"),
            ContentBlock::text(format!(
                "{}\n\nOCR text:\n{recognised}",
                shot.coordinate_note()
            )),
        ]))
    }

    #[tool(
        description = "Find text on screen and report absolute screen coordinates for each match."
    )]
    fn find_text_on_screen(
        &self,
        Parameters(request): Parameters<TextSearchRequest>,
    ) -> Result<String, ErrorData> {
        let shot = screen::capture(request.output.as_deref(), None, screen::DEFAULT_MAX_WIDTH)
            .map_err(failed)?;
        let found = screen::find_text(&shot.native_png, &request.target).map_err(failed)?;
        if found.is_empty() {
            return Ok(format!("no match for {:?} on screen", request.target));
        }
        let lines: Vec<String> = found
            .iter()
            .map(|hit| {
                format!(
                    "{:?} confidence {:.0} at screen ({}, {})",
                    hit.text,
                    hit.confidence,
                    i64::from(hit.center_x) + shot.origin_x,
                    i64::from(hit.center_y) + shot.origin_y
                )
            })
            .collect();
        Ok(lines.join("\n"))
    }

    #[tool(description = "Find text on screen and click its first match.")]
    fn click_text(
        &self,
        Parameters(request): Parameters<TextSearchRequest>,
    ) -> Result<String, ErrorData> {
        let shot = screen::capture(request.output.as_deref(), None, screen::DEFAULT_MAX_WIDTH)
            .map_err(failed)?;
        let found = screen::find_text(&shot.native_png, &request.target).map_err(failed)?;
        let best = found.first().ok_or_else(|| {
            ErrorData::internal_error(format!("no match for {:?}", request.target), None)
        })?;

        let x = i64::from(best.center_x) + shot.origin_x;
        let y = i64::from(best.center_y) + shot.origin_y;
        input::move_pointer(x as i32, y as i32).map_err(failed)?;
        input::click("left", false).map_err(failed)?;
        Ok(format!("clicked {:?} at ({x}, {y})", best.text))
    }

    #[tool(
        description = "Describe the Umbriel compositor itself: installed version, keyboard layouts, active keybind submap, color management and tearing state."
    )]
    fn umbriel_info(&self) -> Result<String, ErrorData> {
        as_pretty_json(compositor::info())
    }

    #[tool(
        description = "List the actions Umbriel can perform (what run_action and keybinds accept), with descriptions. Filter by text such as \"workspace\" or \"focus\"."
    )]
    fn list_actions(
        &self,
        Parameters(request): Parameters<ActionFilter>,
    ) -> Result<String, ErrorData> {
        compositor::list_actions(request.filter.as_deref()).map_err(failed)
    }

    #[tool(
        description = "Search Umbriel's configuration options by path, such as \"gap\" or \"blur\", returning exact key names, types, defaults, ranges and allowed values. Use it before suggesting config changes."
    )]
    fn config_options(
        &self,
        Parameters(request): Parameters<OptionQuery>,
    ) -> Result<String, ErrorData> {
        as_pretty_json(compositor::config_options(request.query.as_deref()).map_err(failed)?)
    }

    #[tool(
        description = "Read the user's Umbriel config, or one section such as \"keybinds\", together with whether it validates. Use it for questions like what a key does or how a setting is configured."
    )]
    fn read_config(
        &self,
        Parameters(request): Parameters<ConfigSection>,
    ) -> Result<String, ErrorData> {
        compositor::read_config(request.section.as_deref()).map_err(failed)
    }

    #[tool(
        description = "Bind a key chord to an Umbriel action in the user's config, replacing any existing binding for it. The edited config is checked with `umbriel config validate` before saving, the previous config is backed up, and Umbriel applies it immediately. Use list_actions for valid actions."
    )]
    fn set_keybind(
        &self,
        Parameters(request): Parameters<KeybindRequest>,
    ) -> Result<String, ErrorData> {
        self.profile
            .permit_action("set_keybind", &request.action)
            .map_err(refused)?;
        config_edit::set_keybind(&request.chord, &request.action, request.repeat).map_err(failed)
    }

    #[tool(
        description = "Remove a key chord's binding from the user's Umbriel config. The previous config is backed up and Umbriel applies the change immediately."
    )]
    fn remove_keybind(
        &self,
        Parameters(request): Parameters<KeybindChord>,
    ) -> Result<String, ErrorData> {
        config_edit::remove_keybind(&request.chord).map_err(failed)
    }

    #[tool(description = "Read the current clipboard contents.")]
    fn clipboard_read(&self) -> Result<String, ErrorData> {
        input::clipboard_read().map_err(failed)
    }

    #[tool(description = "Replace the clipboard contents.")]
    fn clipboard_write(
        &self,
        Parameters(request): Parameters<ClipboardWriteRequest>,
    ) -> Result<String, ErrorData> {
        input::clipboard_write(&request.text).map_err(failed)?;
        Ok(format!(
            "copied {} characters",
            request.text.chars().count()
        ))
    }
}

#[tool_handler(
    name = "umbriel-mcp",
    instructions = "Inspect and control this desktop's windows, workspaces, monitors, input, screen and clipboard (Umbriel Wayland compositor).\n\nThis desktop runs Umbriel, not Hyprland, Sway, i3 or X11, so hyprctl, swaymsg, i3-msg, wmctrl and xdotool do not work here; answer questions about open windows, workspaces and monitors with these tools. Each monitor has its own numbered workspaces: list_windows with workspace \"4\" shows workspace 4 on every monitor. Window and workspace state come straight from Umbriel's IPC socket; use run_action for any compositor action without a dedicated tool. For questions about Umbriel itself, use umbriel_info (version and state), list_actions (what it can do), config_options (valid config keys) and read_config (the user's keybinds and settings) instead of guessing. Change keybinds with set_keybind and remove_keybind rather than editing the config file directly."
)]
impl ServerHandler for UmbrielServer {
    async fn call_tool(
        &self,
        request: CallToolRequestParams,
        context: RequestContext<RoleServer>,
    ) -> Result<CallToolResponse, ErrorData> {
        self.profile.permit_tool(&request.name).map_err(refused)?;
        let tool_call = ToolCallContext::new(self, request, context);
        Self::tool_router().call(tool_call).await
    }
}

#[derive(Parser)]
#[command(version, about = "MCP server for the Umbriel Wayland compositor")]
struct Cli {
    #[command(subcommand)]
    command: Option<Command>,

    /// How much authority the server grants: read-only observes only, no-exec adds input and
    /// window control but cannot start programs, full allows everything
    #[arg(long, value_enum, default_value_t = Profile::Full)]
    profile: Profile,
}

#[derive(clap::Subcommand)]
enum Command {
    /// Check that Umbriel, its input protocols, screen capture, and OCR are ready
    Doctor,
    /// Register this server with an AI coding harness, or print the config to paste
    Install {
        harness: register::Harness,
        /// Restrict the registered server to this profile (default: no restriction)
        #[arg(long, value_enum)]
        profile: Option<Profile>,
    },
}

#[tokio::main]
async fn main() -> anyhow::Result<std::process::ExitCode> {
    let cli = Cli::parse();
    match cli.command {
        Some(Command::Doctor) => return Ok(doctor::run()),
        Some(Command::Install { harness, profile }) => return Ok(register::run(harness, profile)),
        None => {}
    }
    let service = UmbrielServer {
        profile: cli.profile,
    }
    .serve(stdio())
    .await?;
    service.waiting().await?;
    Ok(std::process::ExitCode::SUCCESS)
}

#[cfg(test)]
mod tests {
    use super::UmbrielServer;
    use crate::profile::TOOL_ACCESS;
    use crate::register::SERVER_SUMMARY;
    use rmcp::ServerHandler;

    #[test]
    fn the_registration_summary_is_the_first_line_of_the_server_instructions() {
        let instructions = UmbrielServer {
            profile: crate::profile::Profile::Full,
        }
        .get_info()
        .instructions
        .expect("server instructions");

        assert_eq!(instructions.lines().next(), Some(SERVER_SUMMARY));
    }

    #[test]
    fn every_advertised_tool_is_explicitly_classified_and_nothing_stale_remains() {
        let mut advertised: Vec<String> = UmbrielServer::tool_router()
            .list_all()
            .into_iter()
            .map(|tool| tool.name.to_string())
            .collect();
        let mut classified: Vec<String> = TOOL_ACCESS
            .iter()
            .map(|(name, _)| (*name).to_owned())
            .collect();
        advertised.sort();
        classified.sort();

        assert_eq!(
            advertised, classified,
            "TOOL_ACCESS must list exactly the advertised tools"
        );
    }
}