From cf064d46a275a752f46e47a74b49980a06a2f568 Mon Sep 17 00:00:00 2001 From: Ryan Malloy Date: Wed, 4 Feb 2026 13:39:16 -0700 Subject: [PATCH] Add in-memory logging and traffic capture - Add server-wide log buffer with configurable level (DEBUG/INFO/WARNING/ERROR) - Add per-port traffic capture for TX/RX data - Auto-enable traffic logging for spy:// URLs - Add configure_logging and enable_traffic_log tools - Add serial://log and serial://{port}/log resources - Update docs for new logging features --- docs/src/content/docs/reference/resources.mdx | 68 ++++ .../content/docs/reference/tools-common.mdx | 74 +++- .../content/docs/reference/url-handlers.mdx | 22 +- src/mcserial/server.py | 333 +++++++++++++++++- 4 files changed, 478 insertions(+), 19 deletions(-) diff --git a/docs/src/content/docs/reference/resources.mdx b/docs/src/content/docs/reference/resources.mdx index eb37de2..79b411d 100644 --- a/docs/src/content/docs/reference/resources.mdx +++ b/docs/src/content/docs/reference/resources.mdx @@ -130,6 +130,74 @@ Like the data resource, reading raw data consumes the bytes from the serial buff --- +## `serial://log` + +Read the server-wide log buffer. Returns recent log entries as formatted text, useful for debugging server behavior and diagnosing issues. + +**URI:** `serial://log` + +**Example output:** +``` +# mcserial Server Log (47 entries) + +[2024-02-03T18:45:12] INFO server: Opened /dev/ttyUSB0 at 115200 baud +[2024-02-03T18:45:13] DEBUG /dev/ttyUSB0: TX 8 bytes +[2024-02-03T18:45:14] WARNING zmodem: Sanitized filename: "../etc/passwd" -> "etc_passwd" +[2024-02-03T18:45:15] ERROR server: Failed to open /dev/ttyUSB1: Permission denied +``` + +Log entries include: +- **Timestamp** in ISO-8601 format +- **Level**: DEBUG, INFO, WARNING, or ERROR +- **Source**: "server" for general events, or port name for port-specific events +- **Message**: Human-readable description of the event + +Use `configure_logging()` to adjust the minimum log level and buffer size. The default level is INFO, which captures port open/close events and errors. + + + +--- + +## `serial://{port}/log` + +Read the per-port traffic log (if enabled). Returns a history of TX/RX data in hex + ASCII format. + +**URI pattern:** `serial://{port}/log` + +**Example URIs:** +``` +serial:///dev/ttyUSB0/log +serial://spy:///dev/ttyUSB0/log +``` + +**Example output:** +``` +# Traffic Log: /dev/ttyUSB0 (23 entries) + +[18:45:12.123] TX (8 bytes): 01 03 00 00 00 01 84 0a |........| +[18:45:12.189] RX (7 bytes): 01 03 02 00 64 b8 44 |....d.D| +[18:45:13.001] TX (8 bytes): 01 03 00 01 00 01 d5 ca |........| +``` + +Each entry shows: +- **Timestamp** with millisecond precision +- **Direction**: TX (transmitted) or RX (received) +- **Byte count** +- **Hex dump**: First 16 bytes in hexadecimal +- **ASCII preview**: Printable characters (non-printable shown as `.`) + + + + + +--- + ## URI Encoding The `{port}` segment in resource URIs uses the device path directly. For paths containing special characters (like forward slashes in Linux device paths), the MCP client handles URL encoding: diff --git a/docs/src/content/docs/reference/tools-common.mdx b/docs/src/content/docs/reference/tools-common.mdx index c5ceebe..fc34266 100644 --- a/docs/src/content/docs/reference/tools-common.mdx +++ b/docs/src/content/docs/reference/tools-common.mdx @@ -7,7 +7,7 @@ sidebar: import { Aside } from '@astrojs/starlight/components'; -These 18 tools work in both RS-232 and RS-485 modes. They cover port discovery, connection management, reading and writing data, configuration, flow control, and diagnostics. +These 20 tools work in both RS-232 and RS-485 modes. They cover port discovery, connection management, reading and writing data, configuration, flow control, diagnostics, and logging.