tmux-mcp 0.1.0-alpha.16

Model Context Protocol server exposing tmux through libtmux (alpha)
Documentation
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
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
use std::ffi::{OsStr, OsString};
use std::sync::atomic::{AtomicU64, Ordering};

use libtmux::{Command, CommandChain, Error, NewSessionOptions};
use rmcp::handler::server::wrapper::{Json, Parameters};
use rmcp::{tool, tool_router};

use crate::{
    ChannelArgs, ChannelSignal, CreateSessionArgs, Killed, Layout, PaneArgs, PaneChanged, PaneView,
    PasteTextArgs, Pasted, ResizePaneArgs, SelectLayoutArgs, SelectPaneArgs, SelectWindowArgs,
    SendKeysArgs, Sent, SessionArgs, SessionView, Size, TmuxTools, WindowArgs, Windows,
};

use super::error::{EffectBoundary, ToolError, bad_input, tmux_error, vanished};
use super::lossy;
use super::pane_input::{MissingSource, PaneInputReach, active_run_error};

/// Numbers temporary paste buffers so concurrent calls cannot share one.
static PASTE_BUFFER_COUNTER: AtomicU64 = AtomicU64::new(0);

/// Keys that pass an active run's reservation when sent alone.
///
/// Compared exactly: any other spelling is ordinary input and waits for the
/// run. The run frame survives both, so the command's completion is still
/// proved and the reservation released.
const INTERRUPT_KEYS: [&str; 2] = ["C-c", "C-\\"];

fn literal_input(pane: &str, text: String) -> Command {
    Command::new("send-keys")
        .arg("-t")
        .arg(pane)
        .arg("-l")
        .arg("--")
        .sensitive_arg(OsString::from(text))
}

fn named_input(pane: &str, keys: Vec<String>, enter: bool) -> Command {
    let mut command = Command::new("send-keys").arg("-t").arg(pane).arg("--");
    for key in keys {
        command = command.arg(key);
    }
    if enter {
        command = command.arg("Enter");
    }
    command
}

fn input_dispatch(
    pane: &str,
    text: Option<String>,
    keys: Vec<String>,
    enter: bool,
) -> Option<CommandChain> {
    let mut commands = Vec::with_capacity(2);
    if let Some(text) = text {
        commands.push(literal_input(pane, text));
    }
    if !keys.is_empty() || enter {
        commands.push(named_input(pane, keys, enter));
    }
    let mut commands = commands.into_iter();
    let first = commands.next()?;
    Some(commands.fold(CommandChain::new(first), CommandChain::then))
}

async fn delete_private_paste_buffer(server: &libtmux::Server, name: &str) -> Result<(), Error> {
    if server.buffer(name).await?.is_none() {
        return Ok(());
    }
    server.delete_buffer(name).await
}

fn cleanup_after_refusal(primary: ToolError, cleanup: Result<(), Error>) -> ToolError {
    match cleanup {
        Ok(()) => primary,
        Err(cleanup) => {
            let message = primary.into_error_data().message;
            let mut boundary = EffectBoundary::new("paste_text");
            boundary.mark();
            boundary.local(format!(
                "{message}; temporary paste buffer cleanup failed: {cleanup}"
            ))
        }
    }
}

impl TmuxTools {
    /// Refuse to destroy a window that currently contains the caller pane.
    pub(super) async fn protect_window_caller(
        &self,
        window: &libtmux::Window,
    ) -> Result<(), ToolError> {
        let Some(own) = self.protected_pane().await? else {
            return Ok(());
        };
        let panes = window.panes().await.map_err(|e| tmux_error(&e))?;
        if panes.iter().any(|pane| pane.id().to_string() == own) {
            return Err(Self::self_harm("window", own));
        }
        Ok(())
    }

    /// Refuse to destroy a session that currently contains the caller pane.
    async fn protect_session_caller(&self, session: &libtmux::Session) -> Result<(), ToolError> {
        let Some(own) = self.protected_pane().await? else {
            return Ok(());
        };
        let panes = session.panes().await.map_err(|e| tmux_error(&e))?;
        if panes.iter().any(|pane| pane.id().to_string() == own) {
            return Err(Self::self_harm("session", own));
        }
        Ok(())
    }

    pub(crate) async fn send_keys_one(
        &self,
        SendKeysArgs {
            pane,
            text,
            keys,
            enter,
        }: SendKeysArgs,
    ) -> Result<Json<Sent>, ToolError> {
        let keys = keys.unwrap_or_default();
        if text.is_none() && keys.is_empty() && !enter {
            return Err(bad_input("send_keys needs text, keys, or enter".to_owned()));
        }
        let interrupt = text.is_none()
            && !enter
            && keys
                .iter()
                .all(|key| INTERRUPT_KEYS.contains(&key.as_str()));

        let reach = PaneInputReach::Synchronized;
        let missing = MissingSource::CallerInput;
        let (initial, reservation, plan) = if interrupt {
            let initial = self.preflight_interrupt(&pane, reach, missing).await?;
            let plan = self.preflight_interrupt(&pane, reach, missing).await?;
            (initial, None, plan)
        } else {
            let initial = self.preflight_pane_input(&pane, reach, missing).await?;
            let reservation = initial
                .reserve()
                .ok_or_else(|| active_run_error(initial.target.id().as_ref()))?;
            let plan = self
                .preflight_reserved_pane_input(&pane, reach, missing, &reservation)
                .await?;
            (initial, Some(reservation), plan)
        };
        if !initial.same_authority(&plan)
            || reservation
                .as_ref()
                .is_some_and(|reservation| !plan.owns(reservation))
        {
            return Err(bad_input(format!(
                "pane {pane} changed its configured input authority before send dispatch"
            )));
        }

        // Computed before dispatch: a line this call submits is recorded as
        // a fresh echo immediately, because the terminal's echo can reach a
        // waiting `wait_for_text` client before this call returns. The
        // pane's line itself is not published until dispatch is confirmed
        // below, so a concurrent reader never sees it reported empty while
        // tmux is still typing it for real.
        let mut tracked_keys = keys.clone();
        if enter {
            tracked_keys.push("Enter".to_owned());
        }
        let echo_update = self.echoes.apply(
            plan.generation,
            &plan.endpoint,
            &plan.configured,
            text.as_deref(),
            &tracked_keys,
        );

        let dispatch = input_dispatch(plan.target.id().as_ref(), text, keys, enter)
            .ok_or_else(|| bad_input("send_keys needs text, keys, or enter".to_owned()))?;
        let mut boundary = EffectBoundary::new("send_keys");
        if dispatch.command_count() > 1 {
            boundary.mark();
        }
        let result = match self.server.chain(dispatch).await {
            Ok(result) => result,
            Err(error) => {
                self.echoes.abandon(echo_update);
                return Err(boundary.error(error));
            }
        };
        if let Some(error) = result.refusal_for("send-keys") {
            self.echoes.abandon(echo_update);
            return Err(boundary.error(error));
        }
        self.echoes.commit(echo_update);

        Ok(Json(Sent {
            pane: plan.target.id().to_string(),
            panes: plan.configured,
        }))
    }
}

#[tool_router(router = control_router, vis = "pub(super)")]
impl TmuxTools {
    /// Kill one window.
    #[tool(
        description = "Kill a window, closing it in every session that links it.",
        title = "Kill Window",
        meta = crate::capability_meta!(Teardown, None, [Delete], [TmuxMetadata], true, true, {
            "window" => [TmuxLookup]
        }; idempotent)
    )]
    pub async fn kill_window(
        &self,
        Parameters(WindowArgs { window }): Parameters<WindowArgs>,
    ) -> Result<Json<Killed>, ToolError> {
        let window = self.find_window(&window).await?;
        let id = window.id().to_string();
        self.protect_window_caller(&window).await?;
        window.kill().await.map_err(|e| tmux_error(&e))?;

        Ok(Json(Killed { id }))
    }

    /// Kill one pane.
    #[tool(
        description = "Kill a pane. Killing a window's last pane closes the window.",
        title = "Kill Pane",
        meta = crate::capability_meta!(Teardown, None, [Delete], [TmuxMetadata], true, true, {
            "pane" => [TmuxLookup]
        }; idempotent)
    )]
    pub async fn kill_pane(
        &self,
        Parameters(PaneArgs { pane }): Parameters<PaneArgs>,
    ) -> Result<Json<Killed>, ToolError> {
        let pane = self.find_pane(&pane).await?;
        let id = pane.id().to_string();
        if self.protected_pane().await? == Some(id.as_str()) {
            return Err(Self::self_harm("pane", &id));
        }
        pane.kill().await.map_err(|e| tmux_error(&e))?;

        Ok(Json(Killed { id }))
    }

    /// Create a detached session.
    #[tool(
        description = "Create a new detached tmux session.",
        title = "Create Session",
        meta = crate::capability_meta!(
            Execute, ConfiguredProcess,
            effects = [Change],
            outputs = [TmuxMetadata],
            secrets = true,
            untrusted = true,
            sinks = {
                "name" => [TmuxState, TmuxFormat],
                "start_directory" => [TmuxState, TmuxFormat]
            },
            literalized = ["name", "start_directory"],
            nested = [],
            self_bounded = false,
            always_load = false,
        )
    )]
    pub async fn create_session(
        &self,
        Parameters(CreateSessionArgs {
            name,
            start_directory,
        }): Parameters<CreateSessionArgs>,
    ) -> Result<Json<SessionView>, ToolError> {
        let mut options = NewSessionOptions::new(name);
        if let Some(directory) = start_directory {
            options = options.start_directory(directory);
        }

        let session = self
            .server
            .new_session(options)
            .await
            .map_err(|e| tmux_error(&e))?;
        let foreign_attached = self.foreign_attached_sessions().await;

        Ok(Json(SessionView {
            id: session.id().to_string(),
            name: lossy(session.name()),
            windows: session.window_count(),
            attached: foreign_attached.as_ref().map_or_else(
                || session.is_attached(),
                |set| set.contains(&session.id().to_string()),
            ),
        }))
    }

    /// Kill a session and everything in it.
    #[tool(
        description = "Kill a tmux session and everything in it.",
        title = "Kill Session",
        meta = crate::capability_meta!(Teardown, None, [Delete], [TmuxMetadata], true, true, {
            "session" => [TmuxLookup]
        }; idempotent)
    )]
    pub async fn kill_session(
        &self,
        Parameters(SessionArgs { session }): Parameters<SessionArgs>,
    ) -> Result<Json<Killed>, ToolError> {
        let target = self.find_session(&session).await?;
        let id = target.id().to_string();
        self.protect_session_caller(&target).await?;
        target.kill().await.map_err(|e| tmux_error(&e))?;

        Ok(Json(Killed { id }))
    }

    /// Move one edge of a pane.
    #[tool(
        description = "Move one edge of a pane by a number of rows or columns.",
        title = "Resize Pane",
        meta = crate::capability_meta!(Manage, None, [Change], [TmuxMetadata], true, true, {
            "pane" => [TmuxLookup],
            "direction" => [TmuxState],
            "cells" => [TmuxState]
        })
    )]
    pub async fn resize_pane(
        &self,
        Parameters(ResizePaneArgs {
            pane,
            direction,
            cells,
        }): Parameters<ResizePaneArgs>,
    ) -> Result<Json<Size>, ToolError> {
        let direction = crate::schema::resize_direction(&direction).ok_or_else(|| {
            bad_input(format!(
                "direction must be {}, not {direction}",
                crate::schema::words_or(&crate::schema::resize_direction_words())
            ))
        })?;

        let mut pane = self.find_pane(&pane).await?;
        pane.resize_by(direction, cells)
            .await
            .map_err(|e| tmux_error(&e))?;

        Ok(Json(Size {
            pane: pane.id().to_string(),
            width: pane.width(),
            height: pane.height(),
        }))
    }

    /// Type text into a pane, or press keys in it.
    #[tool(
        description = "Type text into a pane, press named keys in it, or both. `text` is sent \
                       literally, so C-c in it types those three characters. Use `keys` for \
                       anything without a character of its own -- C-c to interrupt a running \
                       command, Escape, Up, C-d -- which are tmux key names and are \
                       interpreted. Text, keys, and optional Enter keep that order in one tmux \
                       dispatch. Before input, the configured synchronized-pane cohort is \
                       observed; a dead, \
                       input-disabled, mode-owned, terminal-attended, or inherited-caller member \
                       refuses the whole call, and so does an active run_shell_command, except \
                       for keys C-c or C-\\ sent alone, which interrupt it. Returned pane IDs \
                       describe configured membership, not confirmed delivery. The \
                       observation can race with tmux processing the input. A submitted line is \
                       remembered briefly so wait_for_text can discount its own echo; an \
                       unrecognized key stops that for the pane's current line rather than mask \
                       it inaccurately.",
        title = "Send Keys To Pane",
        meta = crate::capability_meta!(Execute, PaneInput, [Change], [TmuxMetadata], true, true, {
            "pane" => [TmuxLookup],
            "text" => [PaneInput],
            "keys" => [PaneInput],
            "enter" => [PaneInput]
        })
    )]
    pub async fn send_keys(
        &self,
        Parameters(args): Parameters<SendKeysArgs>,
    ) -> Result<Json<Sent>, ToolError> {
        self.send_keys_one(args).await
    }

    /// Move focus to a pane, or to the one beside it.
    #[tool(
        description = "Select a pane, making it its window's active pane. Give a direction to \
                       move relative to it instead: up, down, left, and right follow the \
                       layout, last returns to the previously active pane, and next and \
                       previous step through the window in order.",
        title = "Select Pane",
        meta = crate::capability_meta!(Manage, None, [Change], [TmuxMetadata], true, true, {
            "pane" => [TmuxLookup],
            "direction" => [TmuxState]
        })
    )]
    pub async fn select_pane(
        &self,
        Parameters(SelectPaneArgs { pane, direction }): Parameters<SelectPaneArgs>,
    ) -> Result<Json<PaneView>, ToolError> {
        let target = self.find_pane(&pane).await?;

        // `next` and `previous` are resolved here rather than with a tmux
        // target, because tmux's `{next}` is relative to the active pane and
        // this tool is relative to the pane the caller named.
        let selected = match direction.as_deref() {
            Some("next" | "previous") => {
                let panes = self
                    .find_window(target.window_id().as_ref())
                    .await?
                    .panes()
                    .await
                    .map_err(|e| tmux_error(&e))?;
                let at = panes
                    .iter()
                    .position(|candidate| candidate.id() == target.id())
                    .ok_or_else(|| vanished("the pane vanished from its own window"))?;
                let step = if matches!(direction.as_deref(), Some("next")) {
                    at + 1
                } else {
                    at + panes.len() - 1
                };
                let mut chosen = panes
                    .get(step % panes.len())
                    .cloned()
                    .ok_or_else(|| vanished("the window has no panes"))?;
                chosen.select().await.map_err(|e| tmux_error(&e))?;
                chosen
            }
            other => {
                let mut boundary = EffectBoundary::new("select_pane");
                let flag = match other {
                    None | Some("next" | "previous") => None,
                    Some("up") => Some("-U"),
                    Some("down") => Some("-D"),
                    Some("left") => Some("-L"),
                    Some("right") => Some("-R"),
                    Some("last") => Some("-l"),
                    Some(unknown) => {
                        return Err(bad_input(format!(
                            "direction must be up, down, left, right, last, next, or \
                                 previous, not {unknown}"
                        )));
                    }
                };
                let mut command = Command::new("select-pane")
                    .arg("-t")
                    .arg(target.id().to_string());
                if let Some(flag) = flag {
                    command = command.arg(flag);
                }
                let result = boundary.tmux(self.server.cmd(command).await)?;
                if let Some(error) = result.refusal_for("select-pane") {
                    return Err(boundary.error(error));
                }
                boundary.mark();

                // Which pane that landed on is tmux's answer, not ours.
                let window = boundary
                    .tmux(self.server.window_by_id(target.window_id()).await)?
                    .ok_or_else(|| boundary.local("the pane's window is gone"))?;
                boundary
                    .tmux(window.active_pane().await)?
                    .ok_or_else(|| boundary.local("the window reported no active pane"))?
            }
        };

        let socket = self.socket();
        Ok(Json(self.pane_view(&selected, socket)))
    }

    /// Move focus to a window, or to the one beside it.
    #[tool(
        description = "Select a window, making it its session's active window. Give a \
                       direction to move relative to it instead: next and previous step \
                       through the session in index order, and last returns to the \
                       previously active window.",
        title = "Select Window",
        meta = crate::capability_meta!(Manage, None, [Change], [TmuxMetadata], true, true, {
            "window" => [TmuxLookup],
            "direction" => [TmuxState]
        })
    )]
    pub async fn select_window(
        &self,
        Parameters(SelectWindowArgs { window, direction }): Parameters<SelectWindowArgs>,
    ) -> Result<Json<Windows>, ToolError> {
        let mut target = self.find_window(&window).await?;
        let mut boundary = EffectBoundary::new("select_window");

        match direction.as_deref() {
            None => {
                boundary.tmux(target.select().await)?;
                boundary.mark();
            }
            Some(step) => {
                let flag = match step {
                    "next" => "-n",
                    "previous" => "-p",
                    "last" => "-l",
                    unknown => {
                        return Err(bad_input(format!(
                            "direction must be next, previous, or last, not {unknown}"
                        )));
                    }
                };
                // tmux resolves all three against the session, not against
                // `-t`: `cmd-select-window.c` calls `session_next`,
                // `session_previous` or `session_last` on the target's session
                // and never looks at the window. Selecting the named window
                // first is what makes a step relative to it.
                //
                // `last` is excluded from that. It means the session's
                // previously active window, so selecting the named one first
                // would rewrite the very pointer being asked about.
                if step != "last" {
                    boundary.tmux(target.select().await)?;
                    boundary.mark();
                }
                let result = boundary.tmux(
                    self.server
                        .cmd(
                            Command::new("select-window")
                                .arg(flag)
                                .arg("-t")
                                .arg(target.session_id().to_string()),
                        )
                        .await,
                )?;
                if let Some(error) = result.refusal_for("select-window") {
                    return Err(boundary.error(error));
                }
                boundary.mark();
            }
        }

        // Which window that landed on is tmux's answer, not ours.
        let session = boundary
            .tmux(self.server.session_by_id(target.session_id()).await)?
            .ok_or_else(|| boundary.local("the window's session is gone"))?;
        let active = boundary
            .tmux(session.active_window().await)?
            .ok_or_else(|| boundary.local("the session reported no active window"))?;

        Ok(Json(Self::render_windows(&[active])))
    }

    /// Arrange a window's panes.
    #[tool(
        description = "Rearrange a window's panes using a saved tmux layout or a named \
                       layout and its unique abbreviation. Names follow the running \
                       daemon's version; mirrored main layouts require tmux 3.5. \
                       Invalid syntax is refused before window lookup. Return the \
                       saved layout tmux actually applied.",
        title = "Arrange Window Panes",
        meta = crate::capability_meta!(Manage, None, [Change], [TmuxMetadata], true, true, {
            "window" => [TmuxLookup],
            "layout" => [TmuxState]
        }; idempotent)
    )]
    pub async fn select_layout(
        &self,
        Parameters(SelectLayoutArgs { window, layout }): Parameters<SelectLayoutArgs>,
    ) -> Result<Json<Layout>, ToolError> {
        self.server
            .validate_layouts([(OsStr::new(&layout), 1)])
            .await
            .map_err(|e| tmux_error(&e))?;
        let mut target = self.find_window(&window).await?;
        target
            .select_layout(layout)
            .await
            .map_err(|e| tmux_error(&e))?;

        Ok(Json(Layout {
            window: target.id().to_string(),
            layout: lossy(target.layout()),
        }))
    }

    /// Empty a pane's scrollback.
    #[tool(
        name = "clear_pane_scrollback",
        description = "Discard a pane's scrollback, so the next capture_pane returns only \
                       what happens next. Use this before running something whose output you \
                       want to read cleanly: it is far cheaper than reading past the old \
                       output every time. The visible screen is left alone.",
        title = "Clear Pane History",
        meta = crate::capability_meta!(Teardown, None, [Delete], [TmuxMetadata], true, true, {
            "pane" => [TmuxLookup]
        }; idempotent)
    )]
    pub async fn clear_pane(
        &self,
        Parameters(PaneArgs { pane }): Parameters<PaneArgs>,
    ) -> Result<Json<PaneChanged>, ToolError> {
        let target = self.find_pane(&pane).await?;
        target.clear_history().await.map_err(|e| tmux_error(&e))?;

        Ok(Json(PaneChanged {
            pane: target.id().to_string(),
        }))
    }

    /// Deliver text to a pane without typing it.
    #[tool(
        description = "Put text into a pane through a tmux paste buffer instead of typing it \
                       key by key. Use this for anything long or awkward: send_keys types the \
                       text, so a shell reading it can react to each character, and a \
                       bracketed-paste aware program treats a paste as one block. Optional \
                       Enter is appended to that same block. Empty text without Enter is a \
                       guarded buffer-free no-op. Paste targets only the named pane, even when \
                       synchronized input is enabled. A dead, input-disabled, mode-owned, \
                       terminal-attended, or inherited-caller target is refused before setup \
                       and again immediately before paste. The private buffer is deleted after \
                       setup, refusal, and paste outcomes; observations can still race with tmux. \
                       A submitted paste is remembered briefly so wait_for_text can discount its \
                       own echo.",
        title = "Paste Text Into Pane",
        meta = crate::capability_meta!(Execute, PaneInput, [Change], [TmuxMetadata], true, true, {
            "pane" => [TmuxLookup],
            "text" => [PaneInput],
            "enter" => [PaneInput]
        })
    )]
    pub async fn paste_text(
        &self,
        Parameters(PasteTextArgs { pane, text, enter }): Parameters<PasteTextArgs>,
    ) -> Result<Json<Pasted>, ToolError> {
        let initial = self
            .preflight_pane_input(
                &pane,
                PaneInputReach::TargetOnly,
                MissingSource::CallerInput,
            )
            .await?;
        let bytes = text.len();
        if text.is_empty() && !enter {
            return Ok(Json(Pasted {
                pane: initial.target.id().to_string(),
                bytes,
            }));
        }
        let echoed_text = text.clone();
        let mut payload = text;
        if enter {
            payload.push('\n');
        }
        let buffer = format!(
            "tmux-mcp-{}-{}",
            std::process::id(),
            PASTE_BUFFER_COUNTER.fetch_add(1, Ordering::Relaxed)
        );

        if let Err(error) = self
            .server
            .set_buffer(Some(&buffer), OsString::from(payload))
            .await
        {
            let primary = tmux_error(&error);
            return Err(cleanup_after_refusal(
                primary,
                delete_private_paste_buffer(&self.server, &buffer).await,
            ));
        }
        let Some(reservation) = initial.reserve() else {
            return Err(cleanup_after_refusal(
                active_run_error(initial.target.id().as_ref()),
                delete_private_paste_buffer(&self.server, &buffer).await,
            ));
        };
        let plan = match self
            .preflight_reserved_pane_input(
                &pane,
                PaneInputReach::TargetOnly,
                MissingSource::PasteTransition,
                &reservation,
            )
            .await
        {
            Ok(plan) if initial.same_authority(&plan) && plan.owns(&reservation) => plan,
            Ok(_) => {
                return Err(cleanup_after_refusal(
                    bad_input(format!(
                        "pane {pane} changed its configured input authority before paste dispatch"
                    )),
                    delete_private_paste_buffer(&self.server, &buffer).await,
                ));
            }
            Err(primary) => {
                return Err(cleanup_after_refusal(
                    primary,
                    delete_private_paste_buffer(&self.server, &buffer).await,
                ));
            }
        };

        // Recorded before dispatch, the same as send_keys: a paste's echo
        // can reach a waiting `wait_for_text` client before this call even
        // returns.
        let target_id = plan.target.id().to_string();
        let mut tracked_keys = Vec::new();
        if enter {
            tracked_keys.push("Enter".to_owned());
        }
        let echo_text = (!echoed_text.is_empty()).then_some(echoed_text.as_str());
        let echo_update = self.echoes.apply(
            plan.generation,
            &plan.endpoint,
            std::slice::from_ref(&target_id),
            echo_text,
            &tracked_keys,
        );

        let pasted = plan.target.paste_buffer(Some(&buffer)).await;
        if pasted.is_ok() {
            self.echoes.commit(echo_update);
        } else {
            self.echoes.abandon(echo_update);
        }
        let deleted = delete_private_paste_buffer(&self.server, &buffer).await;
        paste_outcome(pasted, deleted).map_err(|error| tmux_error(&error))?;

        Ok(Json(Pasted {
            pane: plan.target.id().to_string(),
            bytes,
        }))
    }

    /// Signal a `wait-for` channel.
    #[tool(
        description = "Signal a tmux wait-for channel, releasing every current waiter. With \
                       no waiter, one signal is latched; signalling the same channel again \
                       clears that latch.",
        title = "Signal Channel",
        meta = crate::capability_meta!(Manage, None, [Change], [TmuxMetadata], true, true, {
            "channel" => [TmuxState],
            "seconds" => [None]
        })
    )]
    pub async fn signal_channel(
        &self,
        Parameters(ChannelArgs { channel, .. }): Parameters<ChannelArgs>,
    ) -> Result<Json<ChannelSignal>, ToolError> {
        self.server
            .signal_channel(&channel)
            .await
            .map_err(|e| tmux_error(&e))?;

        Ok(Json(ChannelSignal { channel }))
    }
}

fn paste_outcome(pasted: Result<(), Error>, deleted: Result<(), Error>) -> Result<(), Error> {
    match deleted {
        Ok(()) => pasted,
        Err(cleanup) => {
            let error = match pasted {
                Ok(()) => cleanup,
                Err(paste) => paste,
            };
            Err(error.after_effect("paste_text"))
        }
    }
}

#[cfg(test)]
mod tests {
    use std::time::Duration;

    use libtmux::test::TestServer;
    use libtmux::{Command, Error, ErrorKind, Server, ServerGoneKind};
    use rmcp::handler::server::wrapper::Parameters;
    use rmcp::model::ErrorCode;

    use crate::{SendKeysArgs, TmuxTools};

    use super::paste_outcome;

    fn cleanup_error() -> Error {
        Error::ServerGone {
            command: "delete-buffer",
            kind: ServerGoneKind::NotRunning,
        }
    }

    #[test]
    fn paste_cleanup_decides_whether_replay_is_safe() {
        assert!(paste_outcome(Ok(()), Ok(())).is_ok());

        let cleanup = paste_outcome(Ok(()), Err(cleanup_error()))
            .expect_err("a leaked buffer follows a completed paste");
        assert_eq!(cleanup.kind(), ErrorKind::PartialEffect);

        let paste = paste_outcome(Err(Error::RuntimeNested), Ok(()))
            .expect_err("successful cleanup restores the paste error");
        assert_eq!(paste.kind(), ErrorKind::InvalidInput);

        let both = paste_outcome(Err(Error::RuntimeNested), Err(cleanup_error()))
            .expect_err("failed cleanup leaves the setup effect behind");
        assert!(
            matches!(
                both,
                Error::AfterEffect { source, .. }
                    if source.kind() == ErrorKind::InvalidInput
            ),
            "the paste failure remains the source",
        );
    }

    #[tokio::test]
    async fn a_later_send_keys_failure_reports_the_first_effect() {
        let guard = TestServer::builder().start().await.expect("tmux starts");
        let session = guard
            .server()
            .new_session("send-boundary")
            .await
            .expect("a session starts");
        session
            .set_hook(
                "after-send-keys",
                "if-shell -F '#{?hook_flag_l,0,1}' \
                 'wait-for -S retry-send-held; wait-for retry-send-release'",
            )
            .await
            .expect("the Enter reply is held");
        let pane = session
            .panes()
            .await
            .expect("panes are listed")
            .into_iter()
            .next()
            .expect("the session has a pane");
        let bounded = Server::builder()
            .socket_path(guard.socket_path())
            .config_file(guard.server().config_file().expect("the fixture config"))
            .tmux_executable(guard.server().tmux_executable())
            .default_timeout(Duration::from_secs(2))
            .build()
            .expect("a bounded handle");
        let tools = TmuxTools::builder(bounded.clone()).caller(None).build();

        let result = tools
            .send_keys(Parameters(SendKeysArgs {
                pane: pane.id().to_string(),
                text: Some(String::from("printf retry-boundary")),
                keys: None,
                enter: true,
            }))
            .await;
        let held = guard
            .server()
            .wait_for_channel("retry-send-held", Duration::from_secs(2))
            .await
            .expect("the hook channel is readable");
        guard
            .server()
            .cmd(Command::new("wait-for").arg("-S").arg("retry-send-release"))
            .await
            .expect("the hook is released");
        drop(tools);
        bounded.shutdown().await.expect("the bounded handle stops");
        guard.shutdown().await.expect("tmux fixture shuts down");

        assert_eq!(held, libtmux::ChannelWait::Signalled);
        let Err(error) = result else {
            panic!("Enter reached tmux but its held reply did not fail");
        };
        let error = error.into_error_data();
        assert_eq!(error.code, ErrorCode::INTERNAL_ERROR);
        let detail = error.data.expect("the error carries detail");
        assert_eq!(detail["kind"], "partial_effect", "{detail}");
        assert_eq!(detail["retryable"], false, "{detail}");
        assert_eq!(detail["stale"], false, "{detail}");
    }
}