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
//! JSON Schema helper functions to suppress non-standard `format` keywords
//! generated by schemars for unsigned integer types.
//!
//! # Why this module exists — READ BEFORE ADDING `uN`/`Option<uN>` FIELDS
//!
//! schemars 1.x emits a non-standard `"format": "uintN"` keyword for every
//! unsigned integer type (`uint8`, `uint16`, `uint32`, `uint64`, `uint`).
//! None of these are part of the JSON Schema spec. Validators (jsonschema,
//! AJV, …) log a warning like
//!
//! ```text
//! unknown format "uint16" ignored in schema at path "#/properties/vid"
//! ```
//!
//! and then silently drop the constraint. This is the source of the
//! `logs/unknown format "uintN" ignored` warnings this crate has shipped
//! multiple times.
//!
//! **Rule:** every `uN` / `Option<uN>` field on a struct that derives
//! [`schemars::JsonSchema`] MUST be annotated with
//! `#[schemars(schema_with = "crate::schema_helpers::uint_schema")]`
//! (or `option_uint_schema` for `Option<uN>`).
//!
//! **Corollary:** if such a field is also omitted from serialized output via
//! `#[serde(skip_serializing_if = "Option::is_none")]`, it MUST additionally
//! carry `#[serde(default)]`: schemars 1.2.2 does not see through
//! `schema_with` to the `Option` type, so without `default` the field lands
//! in the schema's `required` array while serialization still omits it. The
//! default evaluates to `None` (skipped), so no `"default"` key is emitted
//! into the schema. See `src/serial/port_info.rs` (vid/pid/interface) and the
//! `port_info_optional_usb_fields_not_required` regression test.
//!
//! **Guards:** `serial::schema` (per-type schema scan, covers all known public
//! `JsonSchema` types) and `tools::tests::tool_schemas_have_no_nonstandard_uint_formats`
//! (tool `outputSchema` string scan, now covers uint8/uint16/uint32/uint64).
//! Do not delete these tests; extend the type list when adding new
//! `JsonSchema`-deriving structs with unsigned integer fields.

use schemars::{json_schema, Schema, SchemaGenerator};

use crate::limits::{MAX_READ_BYTES, MAX_TIMEOUT_MS, MIN_READ_BYTES};

/// Schema for unsigned integer fields without the non-standard `format` keyword.
/// Emits `{"type": "integer", "minimum": 0}`.
pub fn uint_schema(_gen: &mut SchemaGenerator) -> Schema {
    json_schema!({
        "type": "integer",
        "minimum": 0
    })
}

/// Schema for `Option<usize>`/`Option<u32>`/`Option<u64>` without the
/// non-standard `format` keyword.
/// Emits `{"anyOf": [{"type": "null"}, {"type": "integer", "minimum": 0}]}`.
pub fn option_uint_schema(_gen: &mut SchemaGenerator) -> Schema {
    json_schema!({
        "anyOf": [
            {"type": "null"},
            {"type": "integer", "minimum": 0}
        ]
    })
}

pub fn option_timeout_ms_schema(_gen: &mut SchemaGenerator) -> Schema {
    json_schema!({
        "anyOf": [
            {"type": "null"},
            {"type": "integer", "minimum": 0, "maximum": MAX_TIMEOUT_MS}
        ]
    })
}

/// Schema for `no_new_rx_timeout_ms`: rejects 0 (must be > 0) or null (disabled).
pub fn option_positive_timeout_ms_schema(_gen: &mut SchemaGenerator) -> Schema {
    json_schema!({
        "anyOf": [
            {"type": "null"},
            {"type": "integer", "minimum": 1, "maximum": MAX_TIMEOUT_MS}
        ]
    })
}

pub fn timeout_ms_schema(_gen: &mut SchemaGenerator) -> Schema {
    json_schema!({
        "type": "integer",
        "minimum": 0,
        "maximum": MAX_TIMEOUT_MS
    })
}

pub fn read_max_buffered_bytes_schema(_gen: &mut SchemaGenerator) -> Schema {
    json_schema!({
        "type": "integer",
        "minimum": MIN_READ_BYTES,
        "maximum": MAX_READ_BYTES
    })
}

/// Schema for `Vec<u8>` byte array fields, suppressing the non-standard
/// `format: uint8` on the array items that schemars would otherwise emit.
pub fn byte_array_schema(_gen: &mut SchemaGenerator) -> Schema {
    json_schema!({
        "type": "array",
        "items": {
            "type": "integer",
            "minimum": 0,
            "maximum": 255
        }
    })
}