mcp210x-uart/README.md

130 lines
5.1 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# cp210x-mcp
MCP server for customizing Silicon Labs CP210x USB-UART bridge devices. Read and write USB descriptor strings, power configuration, and device lock state — all through natural language in Claude Code.
Built on [FastMCP](https://gofastmcp.com/) and Silicon Labs' `libcp210xmanufacturing` native library via Python ctypes.
## Why
CP210x chips (CP2102, CP2102N, CP2104, etc.) store USB descriptor strings in one-time-programmable EPROM. Silicon Labs provides a GUI tool and a C library for customization, but both are clunky to use — especially when managing multiple identical devices that need unique identifiers.
This MCP server lets you customize devices conversationally:
```
> What CP210x devices are connected?
> Change the product string of device 0 to "RYLR998 0033001104645C0B00001130"
> Set up a udev rule so it always appears at /dev/rylr998-1130
```
## Features
- **List and inspect** connected CP210x devices (part number, VID/PID, strings, power, lock state)
- **Write USB descriptors** — product string, manufacturer, serial number
- **Configure power** — max current draw, self-powered vs bus-powered
- **Generate udev rules** — stable `/dev/` symlinks based on product string
- **Reset device** — trigger USB re-enumeration after changes
- **Lock device** — permanently freeze configuration (with strict confirmation)
- **Elicitation support** — write operations request confirmation when the MCP client supports it; graceful fallback otherwise. Lock is the exception: it *requires* explicit confirmation and will refuse if elicitation is unavailable.
## Requirements
- Linux x86_64
- `libcp210xmanufacturing.so` — Silicon Labs CP210x manufacturing library
- Python 3.10+
- [uv](https://docs.astral.sh/uv/) (recommended)
## Installation
### 1. Install the native library
**Arch Linux (AUR package included):**
```bash
cd aur/cp210xmanufacturing
makepkg -si
```
**From source:**
```bash
# Extract AN721SW toolkit (included in docs/)
cd AN721SW/Linux/LibrarySourcePackages/cp210xmanufacturing
make LIB_ARCH=64
sudo make install
sudo ldconfig
```
The library installs to `/usr/lib/libcp210xmanufacturing.so` and uses libusb to communicate with devices. You'll need udev rules for non-root access — the AUR package installs these automatically, or copy `aur/cp210xmanufacturing/SiliconLabs.rules` to `/usr/lib/udev/rules.d/`.
### 2. Install the MCP server
```bash
uv tool install .
```
### 3. Add to Claude Code
```bash
claude mcp add cp210x -- uvx cp210x-mcp
```
Or for development (runs from source):
```bash
claude mcp add cp210x-local -- uv run --directory /path/to/cp2102-uart cp210x-mcp
```
## Tools
| Tool | Description |
|------|-------------|
| `list_devices` | List connected CP210x devices with description and serial |
| `get_device_info` | Full device details — part number, VID/PID, strings, power, lock state |
| `set_product_string` | Write USB product string (max 126 chars) |
| `set_manufacturer_string` | Write USB manufacturer string (max 45 chars) |
| `set_serial_number` | Write USB serial number (max 63 chars) |
| `set_max_power` | Set max USB power draw in mA (0500, rounded to nearest 2) |
| `set_self_powered` | Toggle self-powered vs bus-powered reporting |
| `reset_device` | USB disconnect/reconnect to apply changes |
| `lock_device` | **Permanently** freeze device configuration |
| `setup_udev_rule` | Generate and install a udev rule for a stable `/dev/` symlink |
## Architecture
```
Claude Code ──stdio──▶ FastMCP server (server.py)
Python ctypes (bindings.py)
libcp210xmanufacturing.so
libusb ──▶ CP210x device
```
The native library uses **libusb** for device access, which is separate from the kernel's `cp210x` serial driver. Both can coexist — you can read/write UART data over `/dev/ttyUSB0` while simultaneously customizing USB descriptors through this server.
### Safety model
CP210x EPROM writes are one-time-programmable with limited write cycles. The server uses a tiered confirmation model:
- **Reads** — no confirmation needed
- **Writes** (strings, power) — requests confirmation via MCP elicitation; falls back to proceeding if the client doesn't support it
- **Lock** — *always* requires elicitation; hard-refuses without it
## Complementary tools
This server handles **device customization** (USB descriptors, power config). For **serial communication** (sending/receiving data over UART), use [mcserial](https://github.com/ryanmalloy/mcserial).
## Reference
- [CP2102 Datasheet](https://www.silabs.com/documents/public/data-sheets/CP2102-9.pdf)
- [AN721: Device Customization Guide](https://www.silabs.com/documents/public/example-code/AN721SW.zip) — source for `libcp210xmanufacturing`
- [AN197: Serial Communication Guide](https://www.silabs.com/documents/public/application-notes/an197-serial-communications-guide-cp210x.pdf)
## License
MIT