Skip to main content
Glama
README.md
# lirc-mcp

An [MCP](https://modelcontextprotocol.io) server that lets an AI agent use
infrared devices on a Linux host: receive IR (e.g. learn a remote's buttons),
send it, and record it to files.

It talks to the kernel's LIRC interface (`/dev/lircN`), so it works with any IR
device that has a kernel driver: USB transceivers such as the IR Toy / Irdroid
(`ir_toy`), Windows Media Center receivers (`mceusb`), GPIO IR on a Raspberry
Pi, and more. The only runtime dependency is the MCP Python SDK.

## Safety model

- **Only named devices.** The agent can use only the devices given with
  `--device`; it cannot choose a path.
- **Pinned devices.** A device given as `/dev/lircN` is bound at startup to
  what is there (see [Pinning](#pinning)). Every open is then verified against
  sysfs before the device is used, and refused if it cannot be, so a
  re-plugged or renumbered node cannot redirect access.
- **Receive-only by default.** Sending needs `:send` on the device.
- **Audit log.** `--log FILE` appends a JSON line per tool call (except
  `list_devices`), including the timings sent. Keep it where the agent cannot
  write.
- **Captures.** Files are created only in `--capture-dir`, never following
  symlinks or overwriting. The agent must not be able to write above it.
- **Limits.** Sizes, durations and repeats are capped (see `list_devices`).
- **One at a time.** A second send or carrier measurement on a busy device is
  refused. Plain receiving is not limited, though a measurement may affect what
  it hears. Other programs using the device are not covered.

Revoking access means stopping the server.

## Pinning

| Given as | Found again by |
|---|---|
| `/dev/lircN`, a USB device with a unique identity | vendor, product and serial, whatever its number becomes |
| `/dev/lircN`, identical USB devices (e.g. without serials) | the path, which must keep the same identity and place of attachment |
| `/dev/lircN`, not USB (e.g. GPIO IR) | the path, which must keep the same place of attachment |
| `usb:VID:PID[:SERIAL]` | exactly what you wrote; give the serial to name one unit |

A path that stays a path is pinned for that run only: if the device moves (for
instance to another USB port), restart the server. The server warns on stderr
about such devices, and about a serial that cannot be written in a SPEC.

## Requirements

- Linux with a LIRC device node (`/dev/lircN`) from a kernel IR driver. Check
  with `lirc-mcp --scan`, which also explains why a USB device has none.
- Read/write access to the node, usually by being in its group
  (`ls -l /dev/lirc0`, often `video`) or through a udev rule (see below).
- Python 3.10 or newer.

If your user cannot open the node, a rule such as this gives it to the `video`
group:

```sh
echo 'SUBSYSTEM=="lirc", GROUP="video", MODE="0660"' |
    sudo tee /etc/udev/rules.d/70-lirc.rules
sudo udevadm control --reload
sudo udevadm trigger --subsystem-match=lirc
sudo usermod -aG video "$USER"       # then log in again
```

## Run it

With [uv](https://docs.astral.sh/uv/), no installation step is needed:

```sh
uvx --from git+https://github.com/laphone32/lirc-mcp lirc-mcp --scan
uvx --from git+https://github.com/laphone32/lirc-mcp lirc-mcp --device /dev/lirc0:send
```

Pin a tag or commit (`...lirc-mcp@v0.1.0`) for code you run with device
access. With pip: `pip install git+https://github.com/laphone32/lirc-mcp`,
then run `lirc-mcp`.

```
lirc-mcp --device SPEC [--device SPEC ...] [--capture-dir DIR] [--log FILE]
lirc-mcp --resolve --device SPEC ...   # check devices, print the pinned SPECs
lirc-mcp --scan                        # list USB LIRC devices (or why none)
lirc-mcp --version
```

`SPEC` is `[ALIAS=]TARGET[:send]`, where `TARGET` is `/dev/lircN` or
`usb:VID:PID[:SERIAL]`. Examples: `/dev/lirc0`, `/dev/lirc0:send`,
`tv=usb:04d8:f58b:352:send`. The alias is what the agent sees; it defaults to
the USB product name. Each explicit alias may be used only once.

The parent of `--capture-dir` must already exist; the server refuses to start
if captures could not be saved there. For example, with an alias, captures and
an audit log:

```sh
uvx --from git+https://github.com/laphone32/lirc-mcp lirc-mcp \
    --device tv=/dev/lirc0:send \
    --capture-dir ~/ir-captures \
    --log ~/.local/state/lirc-mcp/audit.jsonl
```

### Claude Code

```sh
claude mcp add ir -- uvx --from git+https://github.com/laphone32/lirc-mcp \
    lirc-mcp --device /dev/lirc0:send
```

### Other MCP clients

```json
{
  "mcpServers": {
    "ir": {
      "command": "uvx",
      "args": ["--from", "git+https://github.com/laphone32/lirc-mcp",
               "lirc-mcp", "--device", "/dev/lirc0:send"]
    }
  }
}
```

### Docker Sandboxes (sbx)

The server must run on the host, where the device is. Register it, then attach
it to one sandbox only:

```sh
sbx mcp add ir --command uvx \
    --args=--from --args=git+https://github.com/laphone32/lirc-mcp \
    --args=lirc-mcp --args=--device --args=/dev/lirc0:send
sbx mcp load ir --sandbox my-sandbox
# later: sbx mcp unload ir --sandbox my-sandbox && sbx mcp rm ir
```

To use `capture`, also pass `--args=--capture-dir --args=DIR`, where `DIR` is in
a folder shared with the sandbox at the same path.

## Tools

All tools return structured results.

| Tool | What it does |
|---|---|
| `list_devices` | The allowed devices: presence, USB identity, kernel features, receive settings, whether sending is allowed; the limits and the capture directory. |
| `ir_receive` | Waits for one IR burst (e.g. a button press) and returns its timings in µs (pulse, space, ..., pulse), the same as `ir-ctl` text, and the carrier if measured (`measure_carrier`). |
| `ir_send` | Transmits a burst given as `timings_us` or as `ir-ctl` text (`+9000 -4500 ...`, or `pulse`/`space`/`carrier` lines); `repeat` sends it again after `repeat_gap_us`, like a held button. One burst per call. Needs `:send`. |
| `capture` | Records bursts for up to 60 s into a new file in `--capture-dir`, one burst per line in `ir-ctl` format; a line can be passed back to `ir_send`. |

`ir_receive`, `capture` and `ir_send` stop promptly when the client cancels the
call.

## Audit log

With `--log FILE` every call except `list_devices` appends a line such as:

```json
{"time": "2026-12-04T10:20:30.123+01:00", "event": "ir_send", "device": "tv", "timings_us": [9000, 4500, 560], "ir_ctl": null, "carrier_hz": null, "duty_cycle": 0, "repeat": 0, "repeat_gap_us": 40000, "ok": true, "result": {"values": 3, "duration_us": 14060, "carrier_hz": 38000, "...": "..."}}
```

The server refuses to start if the log cannot be created.

## Using the LIRC code in your own app

`lirc_mcp.lirc_dev` uses only the standard library: `info()`, `Receiver`
(bursts as µs lists), `send()`, `parse_timings()` and `format_timings()`.

## Development

```sh
uv run --extra dev pytest            # tests, against a fake LIRC device
uv run --extra dev ruff check .
uv run --extra dev ruff format --check .
uv run --extra dev black --check .
curl -fsSL https://google.github.io/styleguide/pylintrc -o .pylintrc
uv run --extra dev pylint src tests  # Google style, as in CI
```

Tests marked `hw` use a real kernel device, e.g. the kernel's loopback device,
which receives what it sends:

```sh
sudo modprobe rc-loopback            # adds a /dev/lircN
LIRC_MCP_TEST_DEVICE=/dev/lircN uv run --extra dev pytest -m hw
sudo modprobe -r rc-loopback
```

## License

MIT