serial-mcp 0.9.3

MCP server for serial port communication. 25 tools (list/open/read/write/transact/capture_boot/get_status/reconfigure/list_profiles/open_profile/save_profile/delete_profile/configure/rollback_profile/get_log/clear_log/export_log/reconnect/compute_checksum/...), resources, prompts, task cancellation, stdio + streamable-HTTP transports.
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
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::Arc;

use rmcp::{model::RequestMetaObject, Json, Peer, RoleServer};
use tokio::time::{Duration, Instant};
use tracing::{debug, info, warn};

use crate::learning;
use crate::profiles::ProfilePersistenceOperation;
use crate::serial::{ConnectionManager, FlushTarget, SerialConnection};
use crate::tools::helpers::{
    clamp_or_err, clamp_timeout_or_err, log_tool_err, lookup_connection, map_budget_err,
    parse_encoding, require_min_or_err, MAX_READ_BYTES, MAX_TIMEOUT_MS, MIN_READ_BYTES,
};
use crate::tools::read_loop::read_from_private_cursor;
use crate::tools::result_builders::{build_read_result, record_read_completion};
use crate::tools::types::{
    CaptureBootArgs, CaptureBootResult, SendBreakArgs, SendBreakResult, SetDtrRtsArgs,
    SetDtrRtsResult, SetFlowControlArgs, SetFlowControlResult,
};

/// Default read-phase timeout for `capture_boot` (omitted/null both resolve
/// here) so the whole operation stays bounded: hold + settle + timeout.
const DEFAULT_CAPTURE_BOOT_TIMEOUT_MS: u64 = 5000;

/// Cancellation-safe release guard for the `capture_boot` reset pulse.
///
/// Armed with the configured RELEASE state BEFORE the assertion (production
/// `set_dtr_rts` sets DTR then RTS, so an RTS failure can leave DTR changed).
/// On drop while armed, spawns a best-effort release using the configured
/// release state THROUGH THE PUBLIC [`SerialConnection::set_dtr_rts`], so the
/// cleanup queues on the per-connection control lock like any other
/// line-control caller (the capture's own pulse guard drops right after this
/// task is spawned — the task is independent, so there is no deadlock).
/// Every explicit path attempts the release and disarms the guard only after
/// success; an armed guard's drop is the ONLY retry path.
struct ResetReleaseGuard {
    connection: Arc<SerialConnection>,
    release_dtr: bool,
    release_rts: bool,
    disarmed: AtomicBool,
}

impl ResetReleaseGuard {
    fn disarm(&self) {
        self.disarmed.store(true, Ordering::Relaxed);
    }
}

impl Drop for ResetReleaseGuard {
    fn drop(&mut self) {
        if self.disarmed.load(Ordering::Relaxed) {
            return;
        }
        let connection = Arc::clone(&self.connection);
        let release_dtr = self.release_dtr;
        let release_rts = self.release_rts;
        if let Ok(handle) = tokio::runtime::Handle::try_current() {
            handle.spawn(async move {
                if let Err(e) = connection.set_dtr_rts(release_dtr, release_rts).await {
                    warn!("capture_boot: best-effort reset-line release failed: {e}");
                }
            });
        }
    }
}

/// Attempt the configured release, disarming the guard on success. On
/// failure the guard stays armed so its drop still attempts cleanup; the
/// caller decides whether to surface the failure as a tool error.
///
/// A closed/disconnected port counts as released: the lines are gone with
/// the port, so no cleanup is needed (this is what makes a close-during-
/// capture return a clean `connection_closed` result instead of an error).
async fn release_reset_lines(
    connection: &SerialConnection,
    dtr: bool,
    rts: bool,
    guard: &mut Option<ResetReleaseGuard>,
) -> Result<(), String> {
    match connection.set_dtr_rts_unlocked(dtr, rts).await {
        Ok(()) => {
            if let Some(g) = guard.as_ref() {
                g.disarm();
            }
            Ok(())
        }
        Err(crate::error::SerialError::ConnectionClosed(_)) => {
            // Port is gone — nothing left to release.
            if let Some(g) = guard.as_ref() {
                g.disarm();
            }
            Ok(())
        }
        Err(e) => Err(log_tool_err(
            "capture_boot",
            "Failed to release reset lines",
            e,
        )),
    }
}

/// Atomic boot capture: purge OS input, mark the RX live edge under the pump
/// gate, optionally pulse DTR/RTS, then capture only post-mark bytes through
/// the existing read pipeline with a PRIVATE cursor.
///
/// The shared `read` cursor is never moved and ring history stays readable.
/// The result stays in memory (`max_buffered_bytes` bounds it); there is no
/// file output. Line release is guaranteed on every path via
/// `ResetReleaseGuard`.
#[allow(clippy::too_many_arguments)]
pub async fn capture_boot(
    connections: &Arc<ConnectionManager>,
    rx_sessions: &Arc<crate::rx_session::RxSessionManager>,
    budget: &Arc<dyn crate::buffer_budget::BufferBudget>,
    meta: RequestMetaObject,
    ct: tokio_util::sync::CancellationToken,
    peer: Peer<RoleServer>,
    args: CaptureBootArgs,
) -> Result<Json<CaptureBootResult>, String> {
    debug!(
        "capture_boot {} (reset {:?}, settle {:?}, timeout {:?})",
        args.connection_id, args.reset, args.settle_ms, args.timeout_ms
    );

    // ── 1. Validate everything BEFORE any line transition ─────────────────
    let encoding = parse_encoding(&args.encoding)?;
    let connection = lookup_connection(connections, &args.connection_id).await?;

    // The connection's max_buffered_bytes default bounds the in-memory result.
    let max_buffered_bytes = require_min_or_err(
        "capture_boot.max_buffered_bytes",
        connection.max_buffered_bytes_default(),
        MIN_READ_BYTES,
    )?;
    let max_buffered_bytes = clamp_or_err(
        "capture_boot.max_buffered_bytes",
        max_buffered_bytes,
        MAX_READ_BYTES,
    )?;

    // Omitted/null timeout resolves to the bounded default (5000ms); serde
    // cannot distinguish an omitted field from an explicit null on
    // Option<u64>, so both take the default and the operation stays bounded.
    let timeout_ms = Some(args.timeout_ms.unwrap_or(DEFAULT_CAPTURE_BOOT_TIMEOUT_MS));
    if let Some(ms) = args.timeout_ms {
        clamp_timeout_or_err("capture_boot.timeout_ms", ms, MAX_TIMEOUT_MS)?;
    }
    if let Some(ms) = args.no_new_rx_timeout_ms {
        if ms == 0 {
            return Err("capture_boot.no_new_rx_timeout_ms must be > 0".into());
        }
        clamp_timeout_or_err("capture_boot.no_new_rx_timeout_ms", ms, MAX_TIMEOUT_MS)?;
    }
    if let Some(ms) = args.settle_ms {
        clamp_timeout_or_err("capture_boot.settle_ms", ms, MAX_TIMEOUT_MS)?;
    }
    if let Some(reset) = &args.reset {
        if reset.hold_ms == 0 {
            return Err("capture_boot.reset.hold_ms must be >= 1".into());
        }
        clamp_timeout_or_err("capture_boot.reset.hold_ms", reset.hold_ms, MAX_TIMEOUT_MS)?;
    }

    let matcher = match args.r#match {
        Some(ref m) => Some(
            crate::match_config::validate_match_request(m)
                .map_err(|e| format!("capture_boot.match: {e}"))?,
        ),
        None => None,
    };

    // Framing/parser/protocol precedence (same 4 layers as read).
    let rx_framing = crate::precedence::resolve_field(
        args.rx_framing.clone(),
        args.protocol,
        crate::framing::preset_rx_framing,
        connection.rx_framing_default(),
        connection.protocol_default(),
    );
    let rx_parser = crate::precedence::resolve_field(
        args.rx_parser.clone(),
        args.protocol,
        crate::framing::preset_rx_parser,
        connection.rx_parser_default(),
        connection.protocol_default(),
    );

    // Construction validation BEFORE any reset: an invalid framing config
    // must never touch lines or pulse the device.
    if let Some(ref cfg) = rx_framing {
        crate::framing::FrameDecoder::new(cfg, rx_parser.as_ref())
            .map_err(|e| format!("capture_boot.rx_framing: {e}"))?;
    }

    // ── 2. Reserve output budget and get the RX session ───────────────────
    let _reservation = budget
        .try_reserve(max_buffered_bytes)
        .map_err(|e| map_budget_err("capture_boot.max_buffered_bytes", e))?;

    let progress_token = meta.get_progress_token();

    // Same ring size the connection was opened with (matches `open`).
    let session = rx_sessions
        .get_or_create(Arc::clone(&connection), connection.rx_buffer_size())
        .await
        .map_err(|e| format!("capture_boot: {e}"))?;

    let has_reset = args.reset.is_some();

    // ── 3. Serialize line control when a reset is configured. The lock is
    //        scoped to the ASSERT/HOLD/RELEASE pulse only —
    //        it is dropped below, before the settle and read phases, so
    //        other line-control callers are not blocked for the whole
    //        capture duration. ─────────────────────────────────────────────
    let _pulse_control_guard = if has_reset {
        Some(connection.control_lock().lock().await)
    } else {
        None
    };

    // ── 4. Pump gate: wait for any in-flight pump read+append; block the
    //        pump until mark/reset setup completes ────────────────────────
    let pump_guard = session.pump_gate_guard().await;

    // ── 5. Purge unread OS input (bytes that predate capture but have not
    //        yet entered the ring). Failure is a tool error BEFORE any line
    //        assertion. ───────────────────────────────────────────────────
    connection
        .flush_buffers(FlushTarget::Input)
        .await
        .map_err(|e| {
            log_tool_err(
                "capture_boot",
                &format!("Failed to purge OS input on {}", args.connection_id),
                e,
            )
        })?;
    let os_input_flushed = true;

    // ── 6. Atomic mark at the live edge (pump is quiesced) ────────────────
    let mark_offset = session.ring().end_offset();
    let pre_mark_bytes = mark_offset;

    // ── 7. Arm the release guard and assert reset lines while still
    //        holding the pump gate ────────────────────────────────────────
    let mut release_guard = args.reset.as_ref().map(|reset| ResetReleaseGuard {
        connection: Arc::clone(&connection),
        release_dtr: reset.release_dtr,
        release_rts: reset.release_rts,
        disarmed: AtomicBool::new(false),
    });
    if let Some(reset) = &args.reset {
        connection
            .set_dtr_rts_unlocked(reset.assert_dtr, reset.assert_rts)
            .await
            .map_err(|e| {
                log_tool_err(
                    "capture_boot",
                    &format!("Failed to assert reset lines on {}", args.connection_id),
                    e,
                )
            })?;
    }

    // ── 8. Release the pump gate immediately so the pump captures bytes
    //        during hold and line release ─────────────────────────────────
    drop(pump_guard);

    // ── 9. Hold the reset state; ALWAYS release lines afterwards ─────────
    if let Some(reset) = &args.reset {
        let hold = tokio::time::sleep(Duration::from_millis(reset.hold_ms));
        tokio::pin!(hold);
        let cancelled = ct.cancelled();
        tokio::pin!(cancelled);
        tokio::select! {
            _ = &mut cancelled => {
                // Cancellation during hold: release lines first, then route
                // the already-cancelled token through the read path so the
                // result is a structured `cancelled` outcome, not an
                // ad-hoc error.
                if let Err(e) = release_reset_lines(&connection, reset.release_dtr, reset.release_rts, &mut release_guard).await {
                    warn!("capture_boot: release during cancellation failed (cleanup armed): {e}");
                }
            }
            _ = &mut hold => {
                release_reset_lines(&connection, reset.release_dtr, reset.release_rts, &mut release_guard).await?;
            }
        }
    }

    // ── 10. The control lock covered only the pulse; release it before the
    //        settle and read phases. The release guard stays alive: on the
    //        abrupt paths (assert/release failure, cancellation with a
    //        failed release) it remains armed and its drop — which runs
    //        AFTER this guard in the unwind order — queues the cleanup
    //        through the public `set_dtr_rts` (control-lock serialized).
    drop(_pulse_control_guard);

    // ── 11. Optional settle delay while the pump keeps appending ─────────
    if let Some(ms) = args.settle_ms {
        if ms > 0 {
            let settle = tokio::time::sleep(Duration::from_millis(ms));
            tokio::pin!(settle);
            let cancelled = ct.cancelled();
            tokio::pin!(cancelled);
            tokio::select! {
                _ = &mut cancelled => {}
                _ = &mut settle => {}
            }
        }
    }

    // ── 12. Consume from the mark with a PRIVATE cursor ──────────────────
    let (outcome, _final_private_cursor) = read_from_private_cursor(
        &session,
        mark_offset,
        max_buffered_bytes,
        timeout_ms,
        &ct,
        progress_token,
        Some(&peer),
        matcher,
        args.no_new_rx_timeout_ms,
        Some(Arc::clone(&connection)),
        rx_framing,
        rx_parser,
    )
    .await?;

    // NOTE: no unconditional disarm here. The release guard is disarmed ONLY
    // by a successful (or ConnectionClosed) `release_reset_lines`; if the
    // cancellation path had to swallow a release failure, the guard remains
    // armed so its drop retries the configured release state. That retry is
    // the only guarantee for cleanup on that path.

    // ── 13. Build the nested ReadResult and record read logs like `read` ─
    let result = build_read_result(
        outcome,
        args.connection_id.clone(),
        connection.name().map(str::to_string),
        encoding,
        timeout_ms,
        args.no_new_rx_timeout_ms,
    )?;
    record_read_completion(&connection, &result.0, args.r#match.as_ref());

    info!(
        "capture_boot {} mark={} pre_mark={} stop_reason={}",
        args.connection_id, mark_offset, pre_mark_bytes, result.0.stop_reason
    );

    Ok(Json(CaptureBootResult {
        connection_id: args.connection_id,
        name: connection.name().map(str::to_string),
        reset: args.reset,
        mark_offset,
        pre_mark_bytes,
        os_input_flushed,
        read: result.0,
    }))
}

pub async fn set_dtr_rts(
    connections: &Arc<ConnectionManager>,
    args: SetDtrRtsArgs,
) -> Result<Json<SetDtrRtsResult>, String> {
    debug!(
        "set_dtr_rts {} dtr={} rts={}",
        args.connection_id, args.dtr, args.rts
    );

    let connection = lookup_connection(connections, &args.connection_id).await?;
    connection
        .set_dtr_rts(args.dtr, args.rts)
        .await
        .map_err(|e| {
            log_tool_err(
                "set_dtr_rts",
                &format!("Failed to set control lines on {}", args.connection_id),
                e,
            )
        })?;

    info!(
        "Control lines on {} set to dtr={} rts={}",
        args.connection_id, args.dtr, args.rts
    );

    Ok(Json(SetDtrRtsResult {
        connection_id: args.connection_id,
        name: connection.name().map(str::to_string),
        dtr: args.dtr,
        rts: args.rts,
    }))
}

pub async fn set_flow_control(
    connections: &Arc<ConnectionManager>,
    store: &Arc<crate::profile_store::ProfileStore>,
    args: SetFlowControlArgs,
) -> Result<Json<SetFlowControlResult>, String> {
    debug!(
        "set_flow_control {} flow_control={}",
        args.connection_id, args.flow_control
    );

    let flow_control = args.flow_control.parse::<crate::serial::FlowControl>()?;
    let connection = lookup_connection(connections, &args.connection_id).await?;

    // Hold the learning lock across the hardware mutation, the effective
    // snapshot, and the CAS persistence attempt.
    let _learning_guard = connection.learning_lock().lock().await;

    connection
        .set_flow_control(flow_control)
        .await
        .map_err(|e| {
            log_tool_err(
                "set_flow_control",
                &format!("Failed to set flow control on {}", args.connection_id),
                e,
            )
        })?;

    // Write-through learning: hardware mutation succeeded; persist flow
    // control through the bound profile (if any). Failure keeps the result
    // successful with `state="failed"`.
    let (profile, persistence) =
        learning::learn(store, &connection, ProfilePersistenceOperation::Learned).await;

    Ok(Json(SetFlowControlResult {
        connection_id: args.connection_id,
        name: connection.name().map(str::to_string),
        flow_control,
        profile,
        profile_persistence: Some(persistence),
    }))
}

pub async fn send_break(
    connections: &Arc<ConnectionManager>,
    meta: RequestMetaObject,
    ct: tokio_util::sync::CancellationToken,
    peer: Peer<RoleServer>,
    args: SendBreakArgs,
) -> Result<Json<SendBreakResult>, String> {
    debug!(
        "send_break {} duration={}ms",
        args.connection_id, args.duration_ms
    );

    clamp_timeout_or_err("send_break.duration_ms", args.duration_ms, MAX_TIMEOUT_MS)?;
    let connection = lookup_connection(connections, &args.connection_id).await?;

    struct BreakResetGuard {
        connection: Arc<SerialConnection>,
        disarmed: AtomicBool,
    }

    impl BreakResetGuard {
        fn disarm(&self) {
            self.disarmed.store(true, Ordering::Relaxed);
        }
    }

    impl Drop for BreakResetGuard {
        fn drop(&mut self) {
            if self.disarmed.load(Ordering::Relaxed) {
                return;
            }
            let connection = Arc::clone(&self.connection);
            if let Ok(handle) = tokio::runtime::Handle::try_current() {
                handle.spawn(async move {
                    let _ = connection.set_break_state(false).await;
                });
            }
        }
    }

    connection
        .set_break_state(true)
        .await
        .map_err(|e| log_tool_err("send_break", "Failed to assert BREAK", e))?;
    let guard = BreakResetGuard {
        connection: Arc::clone(&connection),
        disarmed: AtomicBool::new(false),
    };

    let progress_token = meta.get_progress_token();
    if let Some(token) = progress_token.clone() {
        let _ = peer
            .notify_progress(
                rmcp::model::ProgressNotificationParam::new(token, 0.0)
                    .with_total(args.duration_ms as f64)
                    .with_message("break asserted"),
            )
            .await;
    }

    let start = Instant::now();
    let deadline = start + Duration::from_millis(args.duration_ms);
    let mut progress_ticker = tokio::time::interval(Duration::from_millis(250));
    progress_ticker.set_missed_tick_behavior(tokio::time::MissedTickBehavior::Skip);
    let mut progress_emitted = false;

    loop {
        tokio::select! {
            _ = ct.cancelled() => return Err("Cancelled".into()),
            _ = tokio::time::sleep_until(deadline) => break,
            _ = progress_ticker.tick() => {
                let elapsed = start.elapsed().as_millis() as u64;
                if let Some(token) = progress_token.clone() {
                    // Skip emitting progress at t=0 (redundant with initial message)
                    if progress_emitted || elapsed > 0 {
                        progress_emitted = true;
                            let _ = peer
                                .notify_progress(
                                    rmcp::model::ProgressNotificationParam::new(
                                        token,
                                        elapsed as f64,
                                    )
                                    .with_total(args.duration_ms as f64)
                                    .with_message("holding break"),
                                )
                                .await;
                    }
                }
            }
        }
    }

    connection
        .set_break_state(false)
        .await
        .map_err(|e| log_tool_err("send_break", "Failed to release BREAK", e))?;
    guard.disarm();

    let actual_duration_ms = start.elapsed().as_millis() as u64;

    info!(
        "Sent break on {} for {}ms (actual {}ms)",
        args.connection_id, args.duration_ms, actual_duration_ms
    );

    if let Some(token) = progress_token {
        let _ = peer
            .notify_progress(
                rmcp::model::ProgressNotificationParam::new(token, args.duration_ms as f64)
                    .with_total(args.duration_ms as f64)
                    .with_message("break released"),
            )
            .await;
    }

    Ok(Json(SendBreakResult {
        connection_id: args.connection_id,
        name: connection.name().map(str::to_string),
        duration_ms: args.duration_ms,
        actual_duration_ms,
    }))
}