NervesMCP.DeviceProbe (NervesMCP v0.1.0)

Copy Markdown View Source

Detects what is on the other end of the device connection and decides which MCP tools should be exposed.

When no MCP activity has happened for idle_threshold ms, a small probe is sent to the device. The probe evaluates the Nerves firmware UUID and classifies the connection into one of:

  • :nerves - Elixir evaluated and a firmware UUID came back (confirmed Nerves)
  • :elixir - Elixir evaluated but no UUID (Elixir runs, not a Nerves device)
  • :shell - bytes came back but no valid Elixir result (some other serial)
  • :down - nothing came back at all
  • :unknown - not probed yet / could not determine

The probe is resilient: a device that returns some output but cannot run Elixir degrades to :shell rather than reporting failure, so the server can still offer the shell tools.

NervesMCP.Server.server/0 reads mode/1 on every request and lists tools accordingly. On a mode change we broadcast notifications/tools/list_changed so connected clients refetch the tool list immediately.

Summary

Functions

Returns a specification to start this module under a supervisor.

Current detected mode. Fast, non-blocking; falls back to :unknown.

Run a probe synchronously in the caller and update the cached mode.

Full status map for the device_status tool.

Record MCP activity so the idle timer does not probe during active use.

Types

mode()

@type mode() :: :nerves | :elixir | :shell | :down | :unknown

status()

@type status() :: %{
  mode: mode(),
  detail: String.t(),
  idle_ms: integer() | nil,
  last_probe_ms_ago: integer() | nil
}

Functions

child_spec(init_arg)

Returns a specification to start this module under a supervisor.

See Supervisor.

mode(id)

@spec mode(term()) :: mode()

Current detected mode. Fast, non-blocking; falls back to :unknown.

refresh(id, timeout \\ 4000)

@spec refresh(term(), timeout()) :: {mode() | :busy, String.t()}

Run a probe synchronously in the caller and update the cached mode.

start_link(opts)

@spec start_link(keyword()) :: GenServer.on_start()

status(id)

@spec status(term()) :: status()

Full status map for the device_status tool.

touch(id)

@spec touch(term()) :: :ok

Record MCP activity so the idle timer does not probe during active use.