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
//! Shared RX stop metadata for all serial data tools.
//!
//! Every tool that consumes from the RX session (`read`, `transact`,
//! `capture_boot`) produces the same stop metadata vocabulary so clients can
//! reason about why an operation ended and whether the result was truncated.

use std::fmt;

use schemars::JsonSchema;
use serde::{Deserialize, Serialize};

/// Canonical reason an RX operation stopped.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
#[serde(rename_all = "snake_case")]
pub enum RxStopReason {
    /// Operation completed its data collection window (first-byte settle for
    /// `read`, or pattern found / max_bytes reached for `read(match=...)`).
    DataComplete,
    /// The requested timeout elapsed before the operation could finish normally.
    Timeout,
    /// The configured match pattern was found in the accumulated buffer.
    MatchFound,
    /// The max_bytes limit was reached before the operation could finish otherwise.
    MaxBufferedBytes,
    /// No new data was received within the configured silence timeout.
    NoNewRxTimeout,
    /// The underlying serial connection was closed.
    ConnectionClosed,
    /// The MCP request was cancelled by the client.
    Cancelled,
    /// The read collected the configured maximum number of frames
    /// (`max_frames` in framing configuration).
    MaxFrames,
    /// A runtime frame decode error occurred (e.g. SLIP malformed escape
    /// sequence).
    FramingError,
    /// The read drained all buffered bytes immediately (cat semantics).
    Drained,
}

impl fmt::Display for RxStopReason {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let s = serde_json::to_value(self)
            .map_err(|_| fmt::Error)?
            .as_str()
            .ok_or(fmt::Error)?
            .to_string();
        write!(f, "{s}")
    }
}

/// Structured metadata attached to every RX operation result.
#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)]
pub struct RxStopMetadata {
    /// Why the operation stopped.
    pub stop_reason: RxStopReason,
    /// `true` when `bytes_returned < bytes_observed` because the operation
    /// capped the returned data (e.g. max_bytes limit exceeded observed data).
    pub truncated: bool,
    /// Total bytes the operation observed from the RX stream before stopping.
    #[schemars(schema_with = "crate::schema_helpers::uint_schema")]
    pub bytes_observed: usize,
    /// Bytes actually returned in the result `data` field.
    #[schemars(schema_with = "crate::schema_helpers::uint_schema")]
    pub bytes_returned: usize,
}

impl RxStopMetadata {
    pub fn data_complete(bytes_observed: usize, bytes_returned: usize) -> Self {
        Self {
            stop_reason: RxStopReason::DataComplete,
            truncated: bytes_returned < bytes_observed,
            bytes_observed,
            bytes_returned,
        }
    }

    pub fn timeout(bytes_observed: usize) -> Self {
        Self {
            stop_reason: RxStopReason::Timeout,
            truncated: false,
            bytes_observed,
            bytes_returned: bytes_observed,
        }
    }

    pub fn match_found(bytes_observed: usize, bytes_returned: usize) -> Self {
        Self {
            stop_reason: RxStopReason::MatchFound,
            truncated: bytes_returned < bytes_observed,
            bytes_observed,
            bytes_returned,
        }
    }

    pub fn max_buffered_bytes(bytes_observed: usize, bytes_returned: usize) -> Self {
        Self {
            stop_reason: RxStopReason::MaxBufferedBytes,
            truncated: bytes_returned < bytes_observed,
            bytes_observed,
            bytes_returned,
        }
    }

    pub fn connection_closed(bytes_observed: usize, bytes_returned: usize) -> Self {
        Self {
            stop_reason: RxStopReason::ConnectionClosed,
            truncated: bytes_returned < bytes_observed,
            bytes_observed,
            bytes_returned,
        }
    }

    pub fn cancelled() -> Self {
        Self {
            stop_reason: RxStopReason::Cancelled,
            truncated: false,
            bytes_observed: 0,
            bytes_returned: 0,
        }
    }

    pub fn no_new_rx_timeout(bytes_observed: usize) -> Self {
        Self {
            stop_reason: RxStopReason::NoNewRxTimeout,
            truncated: false,
            bytes_observed,
            bytes_returned: bytes_observed,
        }
    }

    pub fn max_frames(bytes_observed: usize, bytes_returned: usize) -> Self {
        Self {
            stop_reason: RxStopReason::MaxFrames,
            truncated: false,
            bytes_observed,
            bytes_returned,
        }
    }

    pub fn framing_error(bytes_observed: usize) -> Self {
        Self {
            stop_reason: RxStopReason::FramingError,
            truncated: false,
            bytes_observed,
            bytes_returned: 0,
        }
    }

    pub fn drained(_end_offset: u64, _bytes_observed: usize, bytes_returned: usize) -> Self {
        Self {
            stop_reason: RxStopReason::Drained,
            truncated: false,
            bytes_observed: bytes_returned,
            bytes_returned,
        }
    }

    pub fn new(
        reason: RxStopReason,
        bytes_observed: usize,
        bytes_returned: usize,
        truncated: bool,
    ) -> Self {
        Self {
            stop_reason: reason,
            truncated,
            bytes_observed,
            bytes_returned,
        }
    }

    pub fn with_bytes(mut self, observed: usize, returned: usize) -> Self {
        self.bytes_observed = observed;
        self.bytes_returned = returned;
        self.truncated = returned < observed;
        self
    }
}