Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
4beaa377c4 | ||
|
|
b31fb35464 | ||
|
|
108aa400d3 |
@@ -17,7 +17,8 @@
|
||||
## API
|
||||
|
||||
1. **Live base URL** — `http://bot.dev.kkhost.pl` (Traefik → docker-compose stack on this host; container port 3000 via external network dev_net, no published ports).
|
||||
2. **Endpoints** — all under the `/api` global prefix: `GET /api/status` (`{ connected, myNodeNum, host, port }`), `POST /api/send` (body `{ "text": "...", "to"?: <node number | node name | "!b" | "broadcast"> }` — numbers pass through as-is (integer 1..0xffffffff), names resolve like `GET /api/nodes/:id`, omitted/""/"!b"/"broadcast" → broadcast; unknown or ambiguous targets are a 400. Returns `{ id }`), `GET /api/channels` (the device's Meshtastic channels as `{ index, name, uplinkEnabled, downlinkEnabled }[]`; psk/keys redacted), `GET /api/messages` (message history newest first: rx text with `from`, tx sends with `to` + `status` pending→delivered/failed via routing ACKs — broadcasts stay pending because firmware strips want_ack; last 100, in-memory, reset on reconnect), `GET /api/nodes` (known nodes sorted by num as `{ num, longName?, shortName?, hwModel?, batteryLevel?, lastHeard: number|null, position?: { lat, lon, alt? } }[]`, from the initial sync plus live POSITION_APP updates; reset on reconnect) and `GET /api/nodes/:id` (lookup by exact node number/numeric string, then case-insensitive exact long/short name, else unique substring; 404 when ambiguous or missing). Scalar API docs are served at `/` (OpenAPI JSON at `/docs-json`). Keep this list in sync with the controller. All `POST /api/send` traffic targets channel index 2 (`testowy`) on purpose — never the primary channel; override via `MESH_SEND_CHANNEL` (default `2`).
|
||||
2. **Endpoints** — all under the `/api` global prefix: `GET /api/status` (`{ connected, myNodeNum, host, port }`), `POST /api/send` (body `{ "text": "...", "to"?: <node number | node name | "!b" | "broadcast"> }` — numbers pass through as-is (integer 1..0xffffffff), names resolve like `GET /api/nodes/:id`, omitted/""/"!b"/"broadcast" → broadcast; unknown or ambiguous targets are a 400. Returns `{ id }`), `GET /api/channels` (the device's Meshtastic channels as `{ index, name, uplinkEnabled, downlinkEnabled }[]`; psk/keys redacted), `GET /api/device` (device info captured during the initial sync: `{ myNodeNum, firmwareVersion?, metadata?, localConfig: {<section>: ...}, moduleConfig: {<module>: ...} }` — DeviceMetadata as-is plus per-section Config/ModuleConfig messages; security publicKey/privateKey/adminKey and network wifiPsk redacted to `"REDACTED"`; in-memory, reset on reconnect), `GET /api/messages` (message history newest first: rx text with `from`, tx sends with `to` + `status` pending→delivered/failed via routing ACKs — broadcasts stay pending because firmware strips want_ack; last 100, in-memory, reset on reconnect), `GET /api/nodes` (known nodes sorted by num as `{ num, longName?, shortName?, hwModel?, batteryLevel?, lastHeard: number|null, position?: { lat, lon, alt? }, snr?, hopsAway?, viaMqtt? }[]`, from the initial sync plus live POSITION_APP updates; reset on reconnect) and `GET /api/nodes/:id` (lookup by exact node number/numeric string, then case-insensitive exact long/short name, else unique substring; 404 when ambiguous or missing). Scalar API docs are served at `/` (OpenAPI JSON at `/docs-json`). Keep this list in sync with the controller. All `POST /api/send` traffic targets channel index 3 (`test`) on purpose — never the primary channel; override via `MESH_SEND_CHANNEL` (default `3`).
|
||||
3. **MCP endpoint** — `POST /mcp` (excluded from the `/api` prefix in main.ts, like Scalar at `/`): an in-process MCP server over the official SDK's Streamable HTTP transport in stateless mode (`sessionIdGenerator: undefined`, fresh `McpServer` + transport per request). Tools map 1:1 to MeshService methods: `mesh_status`, `send_message(text, to?)` (same resolution as `POST /api/send`; errors surface as tool errors with the same messages), `list_channels`, `list_nodes`, `get_node(id)` (tool error when missing/ambiguous), `get_messages`, `device_info` (same redaction as `GET /api/device`). Results are JSON in a text content block; sends ride channel index 3 like everything else. Note: the SDK's root entry is broken upstream (#2273) — import via subpaths (`@modelcontextprotocol/sdk/server/mcp.js` etc.).
|
||||
|
||||
## Other
|
||||
|
||||
|
||||
@@ -1,5 +1,25 @@
|
||||
# Changelog
|
||||
|
||||
## v0.7.0 — 2026-09-24 20:21
|
||||
|
||||
- New `GET /api/device` endpoint (and matching MCP tool `device_info`, now seven tools): device info captured during the initial sync — this device's node number, firmware version and metadata (`meshtastic.DeviceMetadata` as-is; it carries no key material), plus per-section config and module-config messages keyed by section name. In-memory only, reset on every bridge reconnect like channels/nodes.
|
||||
- `MeshService.deviceInfo()` captures the new FromRadio cases during sync: `metadata` (DeviceMetadata), `config` (one `meshtastic.Config` per section — oneof `payloadVariant` cases device/position/power/network/display/lora/bluetooth/security/sessionkey/deviceUi) and `moduleConfig` (one ModuleConfig per module). Verified against the installed `@meshtastic/protobufs` 2.8.0: FromRadio carries `meshtastic.Config`, not `LocalConfig` (that type lives in a separate localonly namespace and is not part of FromRadio).
|
||||
- **Mandatory redaction (trust boundary):** key material never leaves the process — security section `publicKey`/`privateKey`/`adminKey` and network section `wifiPsk` are replaced with `"REDACTED"` in all output.
|
||||
- Node summaries (`GET /api/nodes`, `GET /api/nodes/:id`, MCP `list_nodes`/`get_node`) gain optional fields from the NodeInfo: `snr` (dB), `hopsAway`, `viaMqtt` — present only when the device reported them. Note: `meshtastic.NodeInfo` in 2.8.0 has no firmware version field, so per-node firmwareVersion is not captured (device-level firmware version comes from DeviceMetadata via `/api/device`).
|
||||
- Swagger: `GET /api/device` documented with the redaction note and an example; nodes endpoint example/description extended for the new optional fields. README + AGENTS.md endpoint lists updated.
|
||||
- Tests: fake-bridge gains `metadataFrame`, `configSectionFrame`, `moduleConfigSectionFrame`; new device-info specs (metadata → firmwareVersion, config/module-config sections captured with security key material absent from the JSON output), NodeInfo snr/hopsAway/viaMqtt in summaries; MCP spec updated to seven tools incl. a `device_info` call check.
|
||||
|
||||
## v0.6.0 — 2026-09-24 18:28
|
||||
|
||||
- New MCP (Model Context Protocol) server endpoint at `POST /mcp` (outside the `/api` prefix, like Scalar at `/`), served in-process via the official `@modelcontextprotocol/sdk` Streamable HTTP transport in stateless mode (`sessionIdGenerator: undefined`; a fresh `McpServer` + transport per request). Six tools map 1:1 to existing `MeshService` methods with no new logic: `mesh_status`, `send_message(text, to?)` (same target resolution as `POST /api/send` — omitted/""/"!b"/"broadcast" → broadcast, numbers pass through, names via the node DB; resolution errors surface as tool errors with the same messages), `list_channels`, `list_nodes`, `get_node(id)` (tool error when missing/ambiguous), and `get_messages`. Tool results are JSON in a text content block.
|
||||
- New dependency: `@modelcontextprotocol/sdk` (^1.30.1; zod comes with it). Note: the SDK's root entry is broken in all published 1.x tarballs (upstream issue #2273 — the exports map points to a dist index that was never shipped), so imports use the documented subpaths (`@modelcontextprotocol/sdk/server/mcp.js`, `.../server/streamableHttp.js`, `.../inMemory.js`, `.../client/index.js`).
|
||||
- Test helpers shared by both specs extracted from `mesh.service.spec.ts` into `src/mesh/fake-bridge.ts` (fake bridge, waitFor, nodeInfoFrame, KAMIL fixtures); new `mcp.controller.spec.ts` drives the MCP server over an in-memory transport pair — all six tools listed with descriptions/schemas, and mesh_status / send_message (broadcast + name resolution + error messages) / get_node / list_channels / list_nodes / get_messages return correct shapes.
|
||||
- README gains an MCP section; AGENTS.md endpoint list updated for `/mcp`.
|
||||
|
||||
## v0.5.0 — 2026-09-24 17:37
|
||||
|
||||
- Publishing channel moved from `testowy` (index 2) to the new `test` channel (index 3); `MESH_SEND_CHANNEL` default is now `3`. All docs/comments referencing the old pinning updated accordingly.
|
||||
|
||||
## v0.4.0 — 2026-09-24 14:39
|
||||
|
||||
- `POST /api/send` now accepts node names and explicit broadcast sentinels in `to`: omitted/"" or `"!b"`/`"broadcast"` (case-insensitive, trimmed) → broadcast address `0xffffffff`; numbers pass through as-is (must be an integer in 1..0xffffffff); numeric strings are parsed the same way; anything else is looked up by name via the existing node DB lookup (exact long/short name, then unique substring). Unknown or ambiguous targets are a 400 with `no node matching '<to>'`. New service method `MeshService.resolveDest(to?)` does the resolution — `sendText()` keeps its resolved-number signature.
|
||||
|
||||
@@ -28,8 +28,9 @@ All API endpoints live under the `/api` prefix:
|
||||
- `GET /api/status` — `{ connected, myNodeNum, host, port }`
|
||||
- `POST /api/send` — body `{ "text": "...", "to"?: <node number | node name | "!b" | "broadcast"> }`, returns `{ id }`. Numbers pass through as-is (integer in 1..0xffffffff); names resolve like `GET /api/nodes/:id` (exact long/short name, then unique substring) — unknown or ambiguous targets are a 400
|
||||
- `GET /api/channels` — the device's Meshtastic channels as `{ index, name, uplinkEnabled, downlinkEnabled }[]` (psk/keys are redacted; empty until the initial sync completes)
|
||||
- `GET /api/device` — device info captured during the initial sync: `{ myNodeNum, firmwareVersion?, metadata?, localConfig: {<section>: ...}, moduleConfig: {<module>: ...} }`. Key material is never exposed (security publicKey/privateKey/adminKey and network wifiPsk are replaced with `"REDACTED"`); in-memory only, reset on every bridge reconnect
|
||||
- `GET /api/messages` — message history, newest first: inbound text (`dir:'rx'`, with `from`) and our sends (`dir:'tx'`, with `to` + `status`). tx records start `pending` and move to `delivered`/`failed` (with human-readable `ackError`) when the routing ACK/NACK arrives; broadcasts stay `pending` because firmware strips want_ack before TX. In-memory only, last 100, reset on every bridge reconnect
|
||||
- `GET /api/nodes` — known mesh nodes as `{ num, longName?, shortName?, hwModel?, batteryLevel?, lastHeard: number|null, position?: { lat, lon, alt? } }[]`, sorted by node number. Collected during the initial sync plus live position updates; names/hwModel/battery appear only when reported, `lastHeard` is epoch seconds (null = never heard), and `position` only when both coordinates are known
|
||||
- `GET /api/nodes` — known mesh nodes as `{ num, longName?, shortName?, hwModel?, batteryLevel?, lastHeard: number|null, position?: { lat, lon, alt? }, snr?, hopsAway?, viaMqtt? }[]`, sorted by node number. Collected during the initial sync plus live position updates; names/hwModel/battery appear only when reported, `lastHeard` is epoch seconds (null = never heard), `position` only when both coordinates are known, and snr/hopsAway/viaMqtt only when carried by the NodeInfo
|
||||
- `GET /api/nodes/:id` — one node looked up by exact node number (numbers or numeric strings), then case-insensitive exact long/short name, else the unique case-insensitive substring match; 404 when nothing or more than one node matches
|
||||
|
||||
Interactive API docs (Scalar) are served at `/`, with the OpenAPI JSON at
|
||||
@@ -40,6 +41,7 @@ curl http://localhost:3000/api/status
|
||||
curl -X POST http://localhost:3000/api/send -H 'content-type: application/json' -d '{"text":"hi"}'
|
||||
curl -X POST http://localhost:3000/api/send -H 'content-type: application/json' -d '{"to":123456789,"text":"hello"}'
|
||||
curl http://localhost:3000/api/channels
|
||||
curl http://localhost:3000/api/device
|
||||
curl http://localhost:3000/api/messages
|
||||
curl http://localhost:3000/api/nodes
|
||||
curl http://localhost:3000/api/nodes/kamil
|
||||
@@ -50,15 +52,43 @@ broadcast address `0xffffffff`; anything else is a node number or name.
|
||||
DMs are unicast and wantAck-tracked via `GET /api/messages`; broadcasts stay
|
||||
pending because firmware strips want_ack before TX.
|
||||
|
||||
All sends always target channel index 2 (`testowy`) — never the primary
|
||||
All sends always target channel index 3 (`test`) — never the primary
|
||||
channel. Override with `MESH_SEND_CHANNEL`.
|
||||
|
||||
## MCP server
|
||||
|
||||
The bot also exposes an MCP (Model Context Protocol) endpoint at
|
||||
`POST /mcp` (outside the `/api` prefix), served in-process over the SDK's
|
||||
Streamable HTTP transport in stateless mode. It maps 1:1 to the REST
|
||||
endpoints — no new logic, same channel pinning:
|
||||
|
||||
- `mesh_status` — bridge connection state (`{ connected, myNodeNum, host, port }`)
|
||||
- `send_message(text, to?)` — send a text message; `to` resolves like `POST /api/send` (omitted/""/"!b"/"broadcast" → broadcast, number or numeric string → node number, else name lookup); returns `{ id }`; unknown/ambiguous targets are tool errors
|
||||
- `list_channels` — device channels (keys redacted)
|
||||
- `list_nodes` — known nodes as lean summaries
|
||||
- `get_node(id)` — one node by number or name; tool error when missing/ambiguous
|
||||
- `get_messages` — message history, newest first (last 100, tx records carry delivery status)
|
||||
- `device_info` — device info from the initial sync: node number, firmware version, metadata and per-section config/module-config (key material redacted as `"REDACTED"`)
|
||||
|
||||
Point any MCP client at it (Streamable HTTP URL):
|
||||
|
||||
```json
|
||||
{ "mcpServers": { "mesh-bot": { "url": "http://bot.dev.kkhost.pl/mcp" } } }
|
||||
```
|
||||
|
||||
```sh
|
||||
curl -X POST http://localhost:3000/mcp \
|
||||
-H 'content-type: application/json' \
|
||||
-H 'accept: application/json, text/event-stream' \
|
||||
-d '{"jsonrpc":"2.0","id":1,"method":"tools/list"}'
|
||||
```
|
||||
|
||||
## Env vars
|
||||
|
||||
- `MESH_HOST` (default `192.168.1.4`) — bridge host
|
||||
- `MESH_PORT` (default `4403`) — bridge port
|
||||
- `MESH_RECONNECT_MS` (default `5000`) — reconnect interval in ms
|
||||
- `MESH_SEND_CHANNEL` (default `2`) — channel index all sends target (`testowy`; never the primary channel)
|
||||
- `MESH_SEND_CHANNEL` (default `3`) — channel index all sends target (`test`; never the primary channel)
|
||||
- `PORT` (default `3000`) — HTTP listen port
|
||||
|
||||
Note: the serial bridge accepts only ONE client at a time — if another app is
|
||||
|
||||
Generated
+211
-8
@@ -1,16 +1,17 @@
|
||||
{
|
||||
"name": "mesh-bot",
|
||||
"version": "0.0.1",
|
||||
"version": "0.5.0",
|
||||
"lockfileVersion": 3,
|
||||
"requires": true,
|
||||
"packages": {
|
||||
"": {
|
||||
"name": "mesh-bot",
|
||||
"version": "0.0.1",
|
||||
"version": "0.5.0",
|
||||
"license": "UNLICENSED",
|
||||
"dependencies": {
|
||||
"@bufbuild/protobuf": "^2.15.0",
|
||||
"@meshtastic/protobufs": "^2.8.0",
|
||||
"@modelcontextprotocol/sdk": "^1.30.1",
|
||||
"@nestjs/common": "^12.0.1",
|
||||
"@nestjs/core": "^12.0.1",
|
||||
"@nestjs/platform-express": "^12.0.1",
|
||||
@@ -221,6 +222,18 @@
|
||||
"node": ">=0.1.90"
|
||||
}
|
||||
},
|
||||
"node_modules/@hono/node-server": {
|
||||
"version": "2.1.1",
|
||||
"resolved": "https://registry.npmjs.org/@hono/node-server/-/node-server-2.1.1.tgz",
|
||||
"integrity": "sha512-ELuehkj5VCBdgEw9zs+ivkKwyzzUCSQuE96YmiPvn1ECBoZCczbFXJLeEGMTYjphP6gydh4pHMqEYPVMYUVgQg==",
|
||||
"license": "MIT",
|
||||
"engines": {
|
||||
"node": ">=20"
|
||||
},
|
||||
"peerDependencies": {
|
||||
"hono": "^4"
|
||||
}
|
||||
},
|
||||
"node_modules/@inquirer/ansi": {
|
||||
"version": "2.0.8",
|
||||
"resolved": "https://registry.npmjs.org/@inquirer/ansi/-/ansi-2.0.8.tgz",
|
||||
@@ -617,6 +630,46 @@
|
||||
"integrity": "sha512-p68VexhnH7ojf3U27RdryUDqnKJgKaWUbCL96vJR7N0mqDuhKG5OIECk3sCPPdynxjk+yoVhRkScqtv5KQjzsw==",
|
||||
"license": "MIT"
|
||||
},
|
||||
"node_modules/@modelcontextprotocol/sdk": {
|
||||
"version": "1.30.1",
|
||||
"resolved": "https://registry.npmjs.org/@modelcontextprotocol/sdk/-/sdk-1.30.1.tgz",
|
||||
"integrity": "sha512-H2HxLvC3HDNybePJaLdSrU1hhUK5iQw+WvV1b01myFyI7sdVGe1u/IPTE5D9fGCiJDVtgMV/lmFkQXLmQyIFYA==",
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"@hono/node-server": "^1.19.9 || ^2.0.5",
|
||||
"ajv": "^8.17.1",
|
||||
"ajv-formats": "^3.0.1",
|
||||
"content-type": "^1.0.5",
|
||||
"cors": "^2.8.5",
|
||||
"cross-spawn": "^7.0.5",
|
||||
"eventsource": "^3.0.2",
|
||||
"eventsource-parser": "^3.0.0",
|
||||
"express": "^5.2.1",
|
||||
"express-rate-limit": "^8.2.1",
|
||||
"hono": "^4.11.4",
|
||||
"jose": "^6.1.3",
|
||||
"json-schema-typed": "^8.0.2",
|
||||
"pkce-challenge": "^5.0.0",
|
||||
"raw-body": "^3.0.0",
|
||||
"zod": "^3.25 || ^4.0",
|
||||
"zod-to-json-schema": "^3.25.1"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">=18"
|
||||
},
|
||||
"peerDependencies": {
|
||||
"@cfworker/json-schema": "^4.1.1",
|
||||
"zod": "^3.25 || ^4.0"
|
||||
},
|
||||
"peerDependenciesMeta": {
|
||||
"@cfworker/json-schema": {
|
||||
"optional": true
|
||||
},
|
||||
"zod": {
|
||||
"optional": false
|
||||
}
|
||||
}
|
||||
},
|
||||
"node_modules/@nestjs/cli": {
|
||||
"version": "12.0.5",
|
||||
"resolved": "https://registry.npmjs.org/@nestjs/cli/-/cli-12.0.5.tgz",
|
||||
@@ -2179,7 +2232,6 @@
|
||||
"version": "8.20.0",
|
||||
"resolved": "https://registry.npmjs.org/ajv/-/ajv-8.20.0.tgz",
|
||||
"integrity": "sha512-Thbli+OlOj+iMPYFBVBfJ3OmCAnaSyNn4M1vz9T6Gka5Jt9ba/HIR56joy65tY6kx/FCF5VXNB819Y7/GUrBGA==",
|
||||
"dev": true,
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"fast-deep-equal": "^3.1.3",
|
||||
@@ -2196,7 +2248,6 @@
|
||||
"version": "3.0.1",
|
||||
"resolved": "https://registry.npmjs.org/ajv-formats/-/ajv-formats-3.0.1.tgz",
|
||||
"integrity": "sha512-8iUql50EUR+uUcdRQ3HDqa6EVyo3docL8g5WJ3FNcWmu62IbkGUue/pEyLBW8VGKKucTPgqeks4fIU1DA4yowQ==",
|
||||
"dev": true,
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"ajv": "^8.0.0"
|
||||
@@ -2764,6 +2815,20 @@
|
||||
"url": "https://opencollective.com/express"
|
||||
}
|
||||
},
|
||||
"node_modules/cross-spawn": {
|
||||
"version": "7.0.6",
|
||||
"resolved": "https://registry.npmjs.org/cross-spawn/-/cross-spawn-7.0.6.tgz",
|
||||
"integrity": "sha512-uV2QOWP2nWzsy2aMp8aRibhi9dlzF5Hgh5SHaB9OiTGEyDTiJJyx0uy51QXdyWbtAHNua4XJzUKca3OzKUd3vA==",
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"path-key": "^3.1.0",
|
||||
"shebang-command": "^2.0.0",
|
||||
"which": "^2.0.1"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">= 8"
|
||||
}
|
||||
},
|
||||
"node_modules/debug": {
|
||||
"version": "4.4.3",
|
||||
"resolved": "https://registry.npmjs.org/debug/-/debug-4.4.3.tgz",
|
||||
@@ -2991,6 +3056,27 @@
|
||||
"node": ">= 0.6"
|
||||
}
|
||||
},
|
||||
"node_modules/eventsource": {
|
||||
"version": "3.0.7",
|
||||
"resolved": "https://registry.npmjs.org/eventsource/-/eventsource-3.0.7.tgz",
|
||||
"integrity": "sha512-CRT1WTyuQoD771GW56XEZFQ/ZoSfWid1alKGDYMmkt2yl8UXrVR4pspqWNEcqKvVIzg6PAltWjxcSSPrboA4iA==",
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"eventsource-parser": "^3.0.1"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">=18.0.0"
|
||||
}
|
||||
},
|
||||
"node_modules/eventsource-parser": {
|
||||
"version": "3.1.1",
|
||||
"resolved": "https://registry.npmjs.org/eventsource-parser/-/eventsource-parser-3.1.1.tgz",
|
||||
"integrity": "sha512-EKN1vKAMcZ8MlYMpaNuxN6R9yakzH6uajHcHVTqWJzvu5pWw9DyhbP35HH8MVBQ+dZjAfDxk+A8NiR9KWaXiyQ==",
|
||||
"license": "MIT",
|
||||
"engines": {
|
||||
"node": ">=18.0.0"
|
||||
}
|
||||
},
|
||||
"node_modules/expect-type": {
|
||||
"version": "1.4.0",
|
||||
"resolved": "https://registry.npmjs.org/expect-type/-/expect-type-1.4.0.tgz",
|
||||
@@ -3044,6 +3130,25 @@
|
||||
"url": "https://opencollective.com/express"
|
||||
}
|
||||
},
|
||||
"node_modules/express-rate-limit": {
|
||||
"version": "8.7.0",
|
||||
"resolved": "https://registry.npmjs.org/express-rate-limit/-/express-rate-limit-8.7.0.tgz",
|
||||
"integrity": "sha512-hOwV7WOxXfjRpAM1DSJWZDXx3GhplwD8IfwuwvogD8i1Qnkgosw/H45s4ZnFAUHDAhPjlY9hLBvJhKmGMyY26g==",
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"debug": "^4.4.3",
|
||||
"ip-address": "^10.2.0"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">= 16"
|
||||
},
|
||||
"funding": {
|
||||
"url": "https://github.com/sponsors/express-rate-limit"
|
||||
},
|
||||
"peerDependencies": {
|
||||
"express": ">= 4.11"
|
||||
}
|
||||
},
|
||||
"node_modules/external-editor": {
|
||||
"version": "3.1.0",
|
||||
"resolved": "https://registry.npmjs.org/external-editor/-/external-editor-3.1.0.tgz",
|
||||
@@ -3083,7 +3188,6 @@
|
||||
"version": "3.1.3",
|
||||
"resolved": "https://registry.npmjs.org/fast-deep-equal/-/fast-deep-equal-3.1.3.tgz",
|
||||
"integrity": "sha512-f3qQ9oQy9j2AhBe/H9VC91wLmKBCCU/gDOnKNAYG5hswO7BLKj09Hc5HYNz9cGI++xlpDCIgDaitVs03ATR84Q==",
|
||||
"dev": true,
|
||||
"license": "MIT"
|
||||
},
|
||||
"node_modules/fast-safe-stringify": {
|
||||
@@ -3113,7 +3217,6 @@
|
||||
"version": "3.1.8",
|
||||
"resolved": "https://registry.npmjs.org/fast-uri/-/fast-uri-3.1.8.tgz",
|
||||
"integrity": "sha512-GZMtZUTNRpOVIECoXwLNZS5xUGE+mVNbTB8h/7Rwh2TFWcBQiPzTgyZi05BF9UMZKkLJv8XBRJTlU7zg8+ZfMg==",
|
||||
"dev": true,
|
||||
"funding": [
|
||||
{
|
||||
"type": "github",
|
||||
@@ -3428,6 +3531,15 @@
|
||||
"node": ">= 0.4"
|
||||
}
|
||||
},
|
||||
"node_modules/hono": {
|
||||
"version": "4.13.9",
|
||||
"resolved": "https://registry.npmjs.org/hono/-/hono-4.13.9.tgz",
|
||||
"integrity": "sha512-7dMkQmZoC4E6F7AtaQSPhlWAdnBti+j7rreMZl8QB4jFiEhP9TWbGWUMi8WYzBCgmgulxuvLQupKqo+Co6Omyg==",
|
||||
"license": "MIT",
|
||||
"engines": {
|
||||
"node": ">=16.9.0"
|
||||
}
|
||||
},
|
||||
"node_modules/html-escaper": {
|
||||
"version": "2.0.2",
|
||||
"resolved": "https://registry.npmjs.org/html-escaper/-/html-escaper-2.0.2.tgz",
|
||||
@@ -3618,6 +3730,15 @@
|
||||
"url": "https://github.com/sponsors/sindresorhus"
|
||||
}
|
||||
},
|
||||
"node_modules/ip-address": {
|
||||
"version": "10.7.2",
|
||||
"resolved": "https://registry.npmjs.org/ip-address/-/ip-address-10.7.2.tgz",
|
||||
"integrity": "sha512-7H/2gFSIitxc0hG3nOI1glS8QLo/EHBFFLk8vEUjXY/xu0AdL8jZ9U1IzO2PUm0d2D/ofQcAifb0g6OBkt8U7w==",
|
||||
"license": "MIT",
|
||||
"engines": {
|
||||
"node": ">= 12"
|
||||
}
|
||||
},
|
||||
"node_modules/ipaddr.js": {
|
||||
"version": "1.9.1",
|
||||
"resolved": "https://registry.npmjs.org/ipaddr.js/-/ipaddr.js-1.9.1.tgz",
|
||||
@@ -3669,6 +3790,12 @@
|
||||
"url": "https://github.com/sponsors/sindresorhus"
|
||||
}
|
||||
},
|
||||
"node_modules/isexe": {
|
||||
"version": "2.0.0",
|
||||
"resolved": "https://registry.npmjs.org/isexe/-/isexe-2.0.0.tgz",
|
||||
"integrity": "sha512-RHxMLp9lnKHGHRng9QFhRCMbYAcVpn69smSGcq3f36xjgVVWThj4qqLbTLlq7Ssj8B+fIQ1EuCEGI2lKsyQeIw==",
|
||||
"license": "ISC"
|
||||
},
|
||||
"node_modules/istanbul-lib-coverage": {
|
||||
"version": "3.2.2",
|
||||
"resolved": "https://registry.npmjs.org/istanbul-lib-coverage/-/istanbul-lib-coverage-3.2.2.tgz",
|
||||
@@ -3717,6 +3844,15 @@
|
||||
"node": ">=6"
|
||||
}
|
||||
},
|
||||
"node_modules/jose": {
|
||||
"version": "6.2.12",
|
||||
"resolved": "https://registry.npmjs.org/jose/-/jose-6.2.12.tgz",
|
||||
"integrity": "sha512-9NiFmJEex0sy2Dk58j2UGBSHgUs2ypF9eZSu4L6vjOX3Dp96Sw1F3uL+H+D1sx02jZZdzUT0HgvCy59CuvXcWw==",
|
||||
"license": "MIT",
|
||||
"funding": {
|
||||
"url": "https://github.com/sponsors/panva"
|
||||
}
|
||||
},
|
||||
"node_modules/js-tokens": {
|
||||
"version": "10.0.0",
|
||||
"resolved": "https://registry.npmjs.org/js-tokens/-/js-tokens-10.0.0.tgz",
|
||||
@@ -3750,9 +3886,14 @@
|
||||
"version": "1.0.0",
|
||||
"resolved": "https://registry.npmjs.org/json-schema-traverse/-/json-schema-traverse-1.0.0.tgz",
|
||||
"integrity": "sha512-NM8/P9n3XjXhIZn1lLhkFaACTOURQXjWhV4BA/RnOv8xvgqtqpAX9IO4mRQxSx1Rlo4tqzeqb0sOlruaOy3dug==",
|
||||
"dev": true,
|
||||
"license": "MIT"
|
||||
},
|
||||
"node_modules/json-schema-typed": {
|
||||
"version": "8.0.2",
|
||||
"resolved": "https://registry.npmjs.org/json-schema-typed/-/json-schema-typed-8.0.2.tgz",
|
||||
"integrity": "sha512-fQhoXdcvc3V28x7C7BMs4P5+kNlgUURe2jmUT1T//oBRMDrqy1QPelJimwZGo7Hg9VPV3EQV5Bnq4hbFy2vetA==",
|
||||
"license": "BSD-2-Clause"
|
||||
},
|
||||
"node_modules/json5": {
|
||||
"version": "2.2.3",
|
||||
"resolved": "https://registry.npmjs.org/json5/-/json5-2.2.3.tgz",
|
||||
@@ -4658,6 +4799,15 @@
|
||||
"node": ">= 0.8"
|
||||
}
|
||||
},
|
||||
"node_modules/path-key": {
|
||||
"version": "3.1.1",
|
||||
"resolved": "https://registry.npmjs.org/path-key/-/path-key-3.1.1.tgz",
|
||||
"integrity": "sha512-ojmeN0qd+y0jszEtoY48r0Peq5dwMEkIlCOu6Q5f41lfkswXuKtYrhgoTpLnyIcHm24Uhqx+5Tqm2InSwLhE6Q==",
|
||||
"license": "MIT",
|
||||
"engines": {
|
||||
"node": ">=8"
|
||||
}
|
||||
},
|
||||
"node_modules/path-to-regexp": {
|
||||
"version": "8.4.2",
|
||||
"resolved": "https://registry.npmjs.org/path-to-regexp/-/path-to-regexp-8.4.2.tgz",
|
||||
@@ -4695,6 +4845,15 @@
|
||||
"url": "https://github.com/sponsors/jonschlinkert"
|
||||
}
|
||||
},
|
||||
"node_modules/pkce-challenge": {
|
||||
"version": "5.0.1",
|
||||
"resolved": "https://registry.npmjs.org/pkce-challenge/-/pkce-challenge-5.0.1.tgz",
|
||||
"integrity": "sha512-wQ0b/W4Fr01qtpHlqSqspcj3EhBvimsdh0KlHhH8HRZnMsEa0ea2fTULOXOS9ccQr3om+GcGRk4e+isrZWV8qQ==",
|
||||
"license": "MIT",
|
||||
"engines": {
|
||||
"node": ">=16.20.0"
|
||||
}
|
||||
},
|
||||
"node_modules/pluralize": {
|
||||
"version": "8.0.0",
|
||||
"resolved": "https://registry.npmjs.org/pluralize/-/pluralize-8.0.0.tgz",
|
||||
@@ -4850,7 +5009,6 @@
|
||||
"version": "2.0.2",
|
||||
"resolved": "https://registry.npmjs.org/require-from-string/-/require-from-string-2.0.2.tgz",
|
||||
"integrity": "sha512-Xf0nWe6RseziFMu+Ap9biiUbmplq6S9/p+7w7YXP/JBHhrUDDUhwa+vANyubuqfZWTveU//DYVGsDG7RKL/vEw==",
|
||||
"dev": true,
|
||||
"license": "MIT",
|
||||
"engines": {
|
||||
"node": ">=0.10.0"
|
||||
@@ -5037,6 +5195,27 @@
|
||||
"integrity": "sha512-E5LDX7Wrp85Kil5bhZv46j8jOeboKq5JMmYM3gVGdGH8xFpPWXUMsNrlODCrkoxMEeNi/XZIwuRvY4XNwYMJpw==",
|
||||
"license": "ISC"
|
||||
},
|
||||
"node_modules/shebang-command": {
|
||||
"version": "2.0.0",
|
||||
"resolved": "https://registry.npmjs.org/shebang-command/-/shebang-command-2.0.0.tgz",
|
||||
"integrity": "sha512-kHxr2zZpYtdmrN1qDjrrX/Z1rR1kG8Dx+gkpK1G4eXmvXswmcE1hTWBWYUzlraYw1/yZp6YuDY77YtvbN0dmDA==",
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"shebang-regex": "^3.0.0"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">=8"
|
||||
}
|
||||
},
|
||||
"node_modules/shebang-regex": {
|
||||
"version": "3.0.0",
|
||||
"resolved": "https://registry.npmjs.org/shebang-regex/-/shebang-regex-3.0.0.tgz",
|
||||
"integrity": "sha512-7++dFhtcx3353uBaq8DDR4NuxBetBzC7ZQOhmTQInHEd6bSrXdiEyzCvG07Z44UYdLShWUyXt5M/yhz8ekcb1A==",
|
||||
"license": "MIT",
|
||||
"engines": {
|
||||
"node": ">=8"
|
||||
}
|
||||
},
|
||||
"node_modules/side-channel": {
|
||||
"version": "1.1.1",
|
||||
"resolved": "https://registry.npmjs.org/side-channel/-/side-channel-1.1.1.tgz",
|
||||
@@ -5866,6 +6045,21 @@
|
||||
"defaults": "^1.0.3"
|
||||
}
|
||||
},
|
||||
"node_modules/which": {
|
||||
"version": "2.0.2",
|
||||
"resolved": "https://registry.npmjs.org/which/-/which-2.0.2.tgz",
|
||||
"integrity": "sha512-BLI3Tl1TW3Pvl70l3yq3Y64i+awpwXqsGBYWkkqMtnbXgrMD+yj7rhW0kuEDxzJaYXGjEW5ogapKNMEKNMjibA==",
|
||||
"license": "ISC",
|
||||
"dependencies": {
|
||||
"isexe": "^2.0.0"
|
||||
},
|
||||
"bin": {
|
||||
"node-which": "bin/node-which"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">= 8"
|
||||
}
|
||||
},
|
||||
"node_modules/why-is-node-running": {
|
||||
"version": "2.3.0",
|
||||
"resolved": "https://registry.npmjs.org/why-is-node-running/-/why-is-node-running-2.3.0.tgz",
|
||||
@@ -5925,6 +6119,15 @@
|
||||
"funding": {
|
||||
"url": "https://github.com/sponsors/colinhacks"
|
||||
}
|
||||
},
|
||||
"node_modules/zod-to-json-schema": {
|
||||
"version": "3.25.2",
|
||||
"resolved": "https://registry.npmjs.org/zod-to-json-schema/-/zod-to-json-schema-3.25.2.tgz",
|
||||
"integrity": "sha512-O/PgfnpT1xKSDeQYSCfRI5Gy3hPf91mKVDuYLUHZJMiDFptvP41MSnWofm8dnCm0256ZNfZIM7DSzuSMAFnjHA==",
|
||||
"license": "ISC",
|
||||
"peerDependencies": {
|
||||
"zod": "^3.25.28 || ^4"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+2
-1
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "mesh-bot",
|
||||
"version": "0.4.0",
|
||||
"version": "0.7.0",
|
||||
"description": "",
|
||||
"author": "",
|
||||
"private": true,
|
||||
@@ -23,6 +23,7 @@
|
||||
"dependencies": {
|
||||
"@bufbuild/protobuf": "^2.15.0",
|
||||
"@meshtastic/protobufs": "^2.8.0",
|
||||
"@modelcontextprotocol/sdk": "^1.30.1",
|
||||
"@nestjs/common": "^12.0.1",
|
||||
"@nestjs/core": "^12.0.1",
|
||||
"@nestjs/platform-express": "^12.0.1",
|
||||
|
||||
+2
-1
@@ -9,7 +9,8 @@ import { AppModule } from './app.module.js';
|
||||
async function bootstrap() {
|
||||
const app = await NestFactory.create<NestExpressApplication>(AppModule);
|
||||
app.enableShutdownHooks();
|
||||
app.setGlobalPrefix('api');
|
||||
// 'mcp' is excluded so the MCP endpoint lives at /mcp, not /api/mcp (like Scalar at /)
|
||||
app.setGlobalPrefix('api', { exclude: ['mcp'] });
|
||||
|
||||
// Scalar API docs at / (OpenAPI JSON at /docs-json), outside the api prefix.
|
||||
const docConfig = new DocumentBuilder()
|
||||
|
||||
@@ -0,0 +1,138 @@
|
||||
import { create, toBinary } from '@bufbuild/protobuf';
|
||||
import * as P from '@meshtastic/protobufs';
|
||||
import * as net from 'node:net';
|
||||
import type { AddressInfo } from 'node:net';
|
||||
import { frame } from './codec.js';
|
||||
|
||||
/** In-process TCP stand-in for the meshtastic-serial-bridge; hands out accepted sockets. */
|
||||
export function startBridge(
|
||||
onSocket?: (s: net.Socket) => void,
|
||||
): Promise<{ port: number; sockets: net.Socket[]; close: () => void }> {
|
||||
const sockets: net.Socket[] = [];
|
||||
const server = net.createServer((socket) => {
|
||||
onSocket?.(socket); // attached at accept time, before any data can arrive
|
||||
sockets.push(socket);
|
||||
});
|
||||
return new Promise((resolve) => {
|
||||
server.listen(0, '127.0.0.1', () => {
|
||||
resolve({
|
||||
port: (server.address() as AddressInfo).port,
|
||||
sockets,
|
||||
close: () => server.close(),
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
export async function waitFor(
|
||||
cond: () => boolean | Promise<boolean>,
|
||||
timeoutMs = 5000,
|
||||
): Promise<void> {
|
||||
const t0 = Date.now();
|
||||
while (!(await cond())) {
|
||||
if (Date.now() - t0 > timeoutMs) throw new Error('timed out waiting');
|
||||
await new Promise((r) => setTimeout(r, 10));
|
||||
}
|
||||
}
|
||||
|
||||
/** Plain-object NodeInfo fields (init shape, not the runtime message type) accepted by nodeInfoFrame. */
|
||||
export interface NodeInfoFields {
|
||||
user?: {
|
||||
id?: string;
|
||||
longName?: string;
|
||||
shortName?: string;
|
||||
hwModel?: number;
|
||||
};
|
||||
position?: { latitudeI?: number; longitudeI?: number; altitude?: number };
|
||||
lastHeard?: number;
|
||||
deviceMetrics?: { batteryLevel?: number };
|
||||
snr?: number;
|
||||
viaMqtt?: boolean;
|
||||
hopsAway?: number;
|
||||
}
|
||||
|
||||
// init shape for create(): all fields optional (protobuf-es' MessageInit is not exported)
|
||||
type Init<T> = {
|
||||
[P in keyof T as P extends '$unknown' | '$typeName' ? never : P]?: T[P];
|
||||
};
|
||||
|
||||
/** DeviceMetadata frame as streamed once during the initial sync. */
|
||||
export function metadataFrame(md: Init<P.Mesh.DeviceMetadata>): Uint8Array {
|
||||
const fr = create(P.Mesh.FromRadioSchema, {
|
||||
payloadVariant: { case: 'metadata', value: md },
|
||||
});
|
||||
return frame(toBinary(P.Mesh.FromRadioSchema, fr));
|
||||
}
|
||||
|
||||
// Config/ModuleConfig section frames as streamed one-per-section during the initial sync.
|
||||
// (meshtastic.Config / meshtastic.ModuleConfig live in their own root namespaces, not under Mesh)
|
||||
export function configSectionFrame(
|
||||
sectionCase: P.Config.Config['payloadVariant']['case'],
|
||||
value: unknown,
|
||||
): Uint8Array {
|
||||
const cfg = create(P.Config.ConfigSchema, {
|
||||
payloadVariant: {
|
||||
case: sectionCase,
|
||||
value,
|
||||
} as P.Config.Config['payloadVariant'],
|
||||
});
|
||||
return frame(
|
||||
toBinary(
|
||||
P.Mesh.FromRadioSchema,
|
||||
create(P.Mesh.FromRadioSchema, {
|
||||
payloadVariant: { case: 'config', value: cfg },
|
||||
}),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
export function moduleConfigSectionFrame(
|
||||
sectionCase: P.ModuleConfig.ModuleConfig['payloadVariant']['case'],
|
||||
value: unknown,
|
||||
): Uint8Array {
|
||||
const mc = create(P.ModuleConfig.ModuleConfigSchema, {
|
||||
payloadVariant: {
|
||||
case: sectionCase,
|
||||
value,
|
||||
} as P.ModuleConfig.ModuleConfig['payloadVariant'],
|
||||
});
|
||||
return frame(
|
||||
toBinary(
|
||||
P.Mesh.FromRadioSchema,
|
||||
create(P.Mesh.FromRadioSchema, {
|
||||
payloadVariant: { case: 'moduleConfig', value: mc },
|
||||
}),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
export function nodeInfoFrame(
|
||||
num: number,
|
||||
fields?: NodeInfoFields,
|
||||
): Uint8Array {
|
||||
const ni = create(P.Mesh.NodeInfoSchema, { num, ...fields });
|
||||
return frame(
|
||||
toBinary(
|
||||
P.Mesh.FromRadioSchema,
|
||||
create(P.Mesh.FromRadioSchema, {
|
||||
payloadVariant: { case: 'nodeInfo', value: ni },
|
||||
}),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
export const KAMIL_NUM = 123456789;
|
||||
|
||||
export function kamilNodeInfo(): NodeInfoFields {
|
||||
return {
|
||||
user: {
|
||||
id: '!kamil',
|
||||
longName: 'Kamil SP8MK/P',
|
||||
shortName: 'KK',
|
||||
hwModel: P.Mesh.HardwareModel.TBEAM,
|
||||
},
|
||||
position: { latitudeI: 521049637, longitudeI: 209833152, altitude: 123 },
|
||||
lastHeard: 1758000000,
|
||||
deviceMetrics: { batteryLevel: 87 },
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,273 @@
|
||||
import { Client } from '@modelcontextprotocol/sdk/client/index.js';
|
||||
import { InMemoryTransport } from '@modelcontextprotocol/sdk/inMemory.js';
|
||||
import type * as net from 'node:net';
|
||||
import { describe, expect, it } from 'vitest';
|
||||
import { BROADCAST_ADDR } from './codec.js';
|
||||
import {
|
||||
KAMIL_NUM,
|
||||
kamilNodeInfo,
|
||||
nodeInfoFrame,
|
||||
startBridge,
|
||||
waitFor,
|
||||
} from './fake-bridge.js';
|
||||
import { createMeshMcpServer } from './mcp.controller.js';
|
||||
import { MeshService } from './mesh.service.js';
|
||||
|
||||
// callTool returns CallToolResult | a legacy compatibility shape; we only ever get the former
|
||||
type ToolOutcome = { content: { type: string; text?: string }[] };
|
||||
|
||||
function toolText(r: Awaited<ReturnType<Client['callTool']>>): string {
|
||||
const c = (r as ToolOutcome).content[0];
|
||||
if (!c || c.type !== 'text') throw new Error('expected a text content block');
|
||||
return c.text ?? '';
|
||||
}
|
||||
|
||||
async function connectedService(): Promise<{
|
||||
svc: MeshService;
|
||||
sockets: net.Socket[];
|
||||
close: () => void;
|
||||
}> {
|
||||
const bridge = await startBridge();
|
||||
process.env.MESH_HOST = '127.0.0.1';
|
||||
process.env.MESH_PORT = String(bridge.port);
|
||||
const svc = new MeshService();
|
||||
svc.onModuleInit();
|
||||
await waitFor(() => bridge.sockets.length === 1 && svc.status().connected);
|
||||
return {
|
||||
svc,
|
||||
sockets: bridge.sockets,
|
||||
close: () => {
|
||||
svc.onModuleDestroy();
|
||||
bridge.close();
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
// Drive the MCP server over an in-memory transport pair (no HTTP involved)
|
||||
async function connectMcp(svc: MeshService): Promise<{
|
||||
client: Client;
|
||||
close: () => Promise<void>;
|
||||
}> {
|
||||
const [clientTransport, serverTransport] =
|
||||
InMemoryTransport.createLinkedPair();
|
||||
const server = createMeshMcpServer(svc);
|
||||
await server.connect(serverTransport);
|
||||
const client = new Client({ name: 'mesh-bot-test', version: '0.0.0' });
|
||||
await client.connect(clientTransport);
|
||||
return {
|
||||
client,
|
||||
close: async () => {
|
||||
await client.close();
|
||||
await server.close();
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
describe('MCP server tools', () => {
|
||||
it('lists all seven tools with descriptions and input schemas', async () => {
|
||||
const env = await connectedService();
|
||||
try {
|
||||
const mcp = await connectMcp(env.svc);
|
||||
try {
|
||||
const { tools } = await mcp.client.listTools();
|
||||
expect(tools.map((t) => t.name).sort()).toEqual([
|
||||
'device_info',
|
||||
'get_messages',
|
||||
'get_node',
|
||||
'list_channels',
|
||||
'list_nodes',
|
||||
'mesh_status',
|
||||
'send_message',
|
||||
]);
|
||||
for (const t of tools) expect(t.description?.length).toBeGreaterThan(0);
|
||||
|
||||
const send = tools.find((t) => t.name === 'send_message')!;
|
||||
expect(send.inputSchema.required).toEqual(['text']);
|
||||
expect(Object.keys(send.inputSchema.properties ?? {})).toEqual([
|
||||
'text',
|
||||
'to',
|
||||
]);
|
||||
} finally {
|
||||
await mcp.close();
|
||||
}
|
||||
} finally {
|
||||
env.close();
|
||||
}
|
||||
});
|
||||
|
||||
it('mesh_status returns the bridge connection state', async () => {
|
||||
const env = await connectedService();
|
||||
try {
|
||||
const mcp = await connectMcp(env.svc);
|
||||
try {
|
||||
const r = await mcp.client.callTool({
|
||||
name: 'mesh_status',
|
||||
arguments: {},
|
||||
});
|
||||
expect(r.isError).not.toBe(true);
|
||||
expect(JSON.parse(toolText(r))).toEqual({
|
||||
connected: true,
|
||||
myNodeNum: null,
|
||||
host: '127.0.0.1',
|
||||
port: Number(process.env.MESH_PORT),
|
||||
});
|
||||
} finally {
|
||||
await mcp.close();
|
||||
}
|
||||
} finally {
|
||||
env.close();
|
||||
}
|
||||
});
|
||||
|
||||
it('send_message broadcasts by default and resolves names to node numbers', async () => {
|
||||
const env = await connectedService();
|
||||
try {
|
||||
env.sockets[0].write(nodeInfoFrame(KAMIL_NUM, kamilNodeInfo()));
|
||||
await waitFor(async () => (await env.svc.nodes()).length === 1);
|
||||
|
||||
const mcp = await connectMcp(env.svc);
|
||||
try {
|
||||
const r1 = await mcp.client.callTool({
|
||||
name: 'send_message',
|
||||
arguments: { text: 'hello' },
|
||||
});
|
||||
expect(r1.isError).not.toBe(true);
|
||||
const bcast = JSON.parse(toolText(r1)) as { id: number };
|
||||
expect(bcast.id).toBeGreaterThan(0);
|
||||
expect(env.svc.messages()[0]).toMatchObject({
|
||||
dir: 'tx',
|
||||
to: BROADCAST_ADDR,
|
||||
});
|
||||
|
||||
// name lookup via the node DB (unique substring)
|
||||
const r2 = await mcp.client.callTool({
|
||||
name: 'send_message',
|
||||
arguments: { text: 'dm', to: 'kamil' },
|
||||
});
|
||||
expect(r2.isError).not.toBe(true);
|
||||
expect(env.svc.messages()[0]).toMatchObject({
|
||||
dir: 'tx',
|
||||
to: KAMIL_NUM,
|
||||
});
|
||||
} finally {
|
||||
await mcp.close();
|
||||
}
|
||||
} finally {
|
||||
env.close();
|
||||
}
|
||||
});
|
||||
|
||||
it('send_message surfaces resolution errors as tool errors with the REST messages', async () => {
|
||||
const env = await connectedService();
|
||||
try {
|
||||
const mcp = await connectMcp(env.svc);
|
||||
try {
|
||||
const r1 = await mcp.client.callTool({
|
||||
name: 'send_message',
|
||||
arguments: { text: 'x', to: 'zzz-nope' },
|
||||
});
|
||||
expect(r1.isError).toBe(true);
|
||||
expect(toolText(r1)).toContain("no node matching 'zzz-nope'");
|
||||
|
||||
const r2 = await mcp.client.callTool({
|
||||
name: 'send_message',
|
||||
arguments: { text: 'x', to: '0' },
|
||||
});
|
||||
expect(r2.isError).toBe(true);
|
||||
expect(toolText(r2)).toContain('invalid node number');
|
||||
} finally {
|
||||
await mcp.close();
|
||||
}
|
||||
} finally {
|
||||
env.close();
|
||||
}
|
||||
});
|
||||
|
||||
it('get_node resolves by name and errors when missing', async () => {
|
||||
const env = await connectedService();
|
||||
try {
|
||||
env.sockets[0].write(nodeInfoFrame(KAMIL_NUM, kamilNodeInfo()));
|
||||
await waitFor(async () => (await env.svc.nodes()).length === 1);
|
||||
|
||||
const mcp = await connectMcp(env.svc);
|
||||
try {
|
||||
const r1 = await mcp.client.callTool({
|
||||
name: 'get_node',
|
||||
arguments: { id: 'kamil' },
|
||||
});
|
||||
expect(r1.isError).not.toBe(true);
|
||||
expect(JSON.parse(toolText(r1))).toMatchObject({
|
||||
num: KAMIL_NUM,
|
||||
longName: 'Kamil SP8MK/P',
|
||||
shortName: 'KK',
|
||||
});
|
||||
|
||||
const r2 = await mcp.client.callTool({
|
||||
name: 'get_node',
|
||||
arguments: { id: 'zzz' },
|
||||
});
|
||||
expect(r2.isError).toBe(true);
|
||||
expect(toolText(r2)).toContain("no node matching 'zzz'");
|
||||
} finally {
|
||||
await mcp.close();
|
||||
}
|
||||
} finally {
|
||||
env.close();
|
||||
}
|
||||
});
|
||||
|
||||
it('list_channels, list_nodes, get_messages and device_info return the service data', async () => {
|
||||
const env = await connectedService();
|
||||
try {
|
||||
env.sockets[0].write(nodeInfoFrame(KAMIL_NUM, kamilNodeInfo()));
|
||||
await waitFor(async () => (await env.svc.nodes()).length === 1);
|
||||
|
||||
const mcp = await connectMcp(env.svc);
|
||||
try {
|
||||
expect(
|
||||
JSON.parse(
|
||||
toolText(
|
||||
await mcp.client.callTool({
|
||||
name: 'list_channels',
|
||||
arguments: {},
|
||||
}),
|
||||
),
|
||||
),
|
||||
).toEqual([]); // no channel frames fed yet
|
||||
|
||||
// no metadata/config frames fed yet → empty sections, null node num
|
||||
expect(
|
||||
JSON.parse(
|
||||
toolText(
|
||||
await mcp.client.callTool({ name: 'device_info', arguments: {} }),
|
||||
),
|
||||
),
|
||||
).toEqual({ myNodeNum: null, localConfig: {}, moduleConfig: {} });
|
||||
|
||||
const nodes = JSON.parse(
|
||||
toolText(
|
||||
await mcp.client.callTool({ name: 'list_nodes', arguments: {} }),
|
||||
),
|
||||
) as { num: number; longName?: string }[];
|
||||
expect(nodes).toHaveLength(1);
|
||||
expect(nodes[0].num).toBe(KAMIL_NUM);
|
||||
expect(nodes[0].longName).toBe('Kamil SP8MK/P');
|
||||
|
||||
await mcp.client.callTool({
|
||||
name: 'send_message',
|
||||
arguments: { text: 'hi' },
|
||||
});
|
||||
const msgs = JSON.parse(
|
||||
toolText(
|
||||
await mcp.client.callTool({ name: 'get_messages', arguments: {} }),
|
||||
),
|
||||
) as { dir: string; to?: number }[];
|
||||
expect(msgs[0]).toMatchObject({ dir: 'tx', to: BROADCAST_ADDR }); // newest first
|
||||
} finally {
|
||||
await mcp.close();
|
||||
}
|
||||
} finally {
|
||||
env.close();
|
||||
}
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,124 @@
|
||||
import { Controller, Post, Req, Res } from '@nestjs/common';
|
||||
import type { Request } from 'express';
|
||||
import type { ServerResponse } from 'node:http';
|
||||
import { ApiExcludeEndpoint } from '@nestjs/swagger';
|
||||
import { z } from 'zod';
|
||||
// The SDK's root entry is broken in all published 1.x tarballs (upstream #2273: the exports map points to a dist index that was never shipped) — use the documented subpath imports instead.
|
||||
import { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js';
|
||||
import { StreamableHTTPServerTransport } from '@modelcontextprotocol/sdk/server/streamableHttp.js';
|
||||
import * as pkg from '../../package.json';
|
||||
import { MeshService } from './mesh.service.js';
|
||||
|
||||
const CHANNEL_NOTE =
|
||||
' All sends ride the pinned "test" channel (index 3) — never the primary channel.';
|
||||
|
||||
function jsonResult(value: unknown): {
|
||||
content: { type: 'text'; text: string }[];
|
||||
} {
|
||||
return { content: [{ type: 'text', text: JSON.stringify(value) }] };
|
||||
}
|
||||
|
||||
/** Build an MCP server exposing the MeshService as tools (1:1 with the REST endpoints). */
|
||||
export function createMeshMcpServer(mesh: MeshService): McpServer {
|
||||
const server = new McpServer({ name: 'mesh-bot', version: pkg.version });
|
||||
|
||||
server.registerTool(
|
||||
'mesh_status',
|
||||
{
|
||||
description: `Bridge connection state: whether the bot is connected to the meshtastic bridge, this device's node number, and the bridge host/port.${CHANNEL_NOTE}`,
|
||||
},
|
||||
async () => jsonResult(mesh.status()),
|
||||
);
|
||||
|
||||
server.registerTool(
|
||||
'send_message',
|
||||
{
|
||||
description: `Send a text message over the mesh; returns { id } — the Meshtastic packet id. to: omitted/"" or "!b"/"broadcast" (case-insensitive) → broadcast; number or numeric string → that node number (integer in 1..0xffffffff); anything else is looked up by name (exact long/short name, then unique substring). Unknown or ambiguous targets are an error.${CHANNEL_NOTE}`,
|
||||
inputSchema: { text: z.string(), to: z.string().optional() },
|
||||
},
|
||||
async ({ text, to }) =>
|
||||
jsonResult({ id: await mesh.sendText(await mesh.resolveDest(to), text) }),
|
||||
);
|
||||
|
||||
server.registerTool(
|
||||
'list_channels',
|
||||
{
|
||||
description: `The device's Meshtastic channels as { index, name, uplinkEnabled, downlinkEnabled }[] sorted by index (psk/keys redacted); empty until the initial sync completes.${CHANNEL_NOTE}`,
|
||||
},
|
||||
async () => jsonResult(mesh.channels()),
|
||||
);
|
||||
|
||||
server.registerTool(
|
||||
'list_nodes',
|
||||
{
|
||||
description: `Known mesh nodes as lean summaries sorted by node number; names/hwModel/batteryLevel appear only when the device reported them, lastHeard is epoch seconds (null = never heard), position only when both coordinates are known.${CHANNEL_NOTE}`,
|
||||
},
|
||||
async () => jsonResult(await mesh.nodes()),
|
||||
);
|
||||
|
||||
server.registerTool(
|
||||
'get_node',
|
||||
{
|
||||
description: `Look up one node by exact node number (number or numeric string), then case-insensitive exact long/short name, else the unique case-insensitive substring match. Errors when nothing or more than one node matches.${CHANNEL_NOTE}`,
|
||||
inputSchema: { id: z.string() },
|
||||
},
|
||||
async ({ id }) => {
|
||||
const n = await mesh.node(id);
|
||||
if (!n) throw new Error(`no node matching '${id}'`);
|
||||
return jsonResult(n);
|
||||
},
|
||||
);
|
||||
|
||||
server.registerTool(
|
||||
'get_messages',
|
||||
{
|
||||
description: `Message history, newest first (last 100): inbound text (dir "rx", with from) and our sends (dir "tx", with to + status pending→delivered/failed; broadcasts stay pending because firmware strips want_ack). In-memory only, reset on every bridge reconnect.${CHANNEL_NOTE}`,
|
||||
},
|
||||
async () => jsonResult(mesh.messages()),
|
||||
);
|
||||
|
||||
server.registerTool(
|
||||
'device_info',
|
||||
{
|
||||
description: `Device info captured during the initial sync: this device's node number, firmware version and metadata, plus per-section config/module-config (security publicKey/privateKey/adminKey and network wifiPsk redacted as "REDACTED"). In-memory only, reset on every bridge reconnect.${CHANNEL_NOTE}`,
|
||||
},
|
||||
async () => jsonResult(mesh.deviceInfo()),
|
||||
);
|
||||
|
||||
return server;
|
||||
}
|
||||
|
||||
@Controller()
|
||||
export class McpController {
|
||||
constructor(private readonly mesh: MeshService) {}
|
||||
|
||||
// MCP Streamable HTTP endpoint (stateless mode): a fresh server + transport per request, outside the /api prefix.
|
||||
@Post('mcp')
|
||||
@ApiExcludeEndpoint()
|
||||
async mcp(@Req() req: Request, @Res() res: ServerResponse): Promise<void> {
|
||||
const server = createMeshMcpServer(this.mesh);
|
||||
try {
|
||||
const transport = new StreamableHTTPServerTransport({
|
||||
sessionIdGenerator: undefined,
|
||||
});
|
||||
await server.connect(transport);
|
||||
await transport.handleRequest(req, res, req.body);
|
||||
res.on('close', () => {
|
||||
void transport.close();
|
||||
void server.close();
|
||||
});
|
||||
} catch (error) {
|
||||
if (!res.headersSent) {
|
||||
res.statusCode = 500;
|
||||
res.setHeader('content-type', 'application/json');
|
||||
res.end(
|
||||
JSON.stringify({
|
||||
jsonrpc: '2.0',
|
||||
error: { code: -32603, message: String(error) },
|
||||
id: null,
|
||||
}),
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -13,6 +13,7 @@ import {
|
||||
ApiResponse,
|
||||
ApiTags,
|
||||
} from '@nestjs/swagger';
|
||||
import type { DeviceInfo } from './mesh.service.js';
|
||||
import { MeshService } from './mesh.service.js';
|
||||
|
||||
export class SendTextDto {
|
||||
@@ -57,7 +58,7 @@ export class MeshController {
|
||||
@ApiOperation({
|
||||
summary: 'Send a text message over the mesh',
|
||||
description:
|
||||
'Queues a TEXT_MESSAGE_APP packet to the resolved target (node number, node name, or broadcast). DMs are unicast and wantAck-tracked via GET /api/messages; broadcasts stay pending because firmware strips want_ack. All sends ride the pinned test channel (index 2, "testowy" — never the primary channel) regardless of DM or broadcast, since modern firmware PKI handles unicast encryption/channel anyway. Returns the Meshtastic packet id.',
|
||||
'Queues a TEXT_MESSAGE_APP packet to the resolved target (node number, node name, or broadcast). DMs are unicast and wantAck-tracked via GET /api/messages; broadcasts stay pending because firmware strips want_ack. All sends ride the pinned test channel (index 3, "test" — never the primary channel) regardless of DM or broadcast, since modern firmware PKI handles unicast encryption/channel anyway. Returns the Meshtastic packet id.',
|
||||
})
|
||||
@ApiResponse({
|
||||
status: 200,
|
||||
@@ -93,8 +94,8 @@ export class MeshController {
|
||||
example: [
|
||||
{ index: 0, name: 'Default', uplinkEnabled: true, downlinkEnabled: true },
|
||||
{
|
||||
index: 2,
|
||||
name: 'testowy',
|
||||
index: 3,
|
||||
name: 'test',
|
||||
uplinkEnabled: false,
|
||||
downlinkEnabled: false,
|
||||
},
|
||||
@@ -104,6 +105,51 @@ export class MeshController {
|
||||
return this.mesh.channels();
|
||||
}
|
||||
|
||||
@Get('device')
|
||||
@ApiOperation({
|
||||
summary: 'Device info (metadata + config, keys redacted)',
|
||||
description:
|
||||
'This device\'s node number, firmware version and metadata, plus per-section config and module-config messages captured during the initial sync; empty until it completes. Key material is never exposed: security publicKey/privateKey/adminKey and network wifiPsk are replaced with "REDACTED". In-memory only, reset on every bridge reconnect.',
|
||||
})
|
||||
@ApiResponse({
|
||||
status: 200,
|
||||
description: 'Device info',
|
||||
example: {
|
||||
myNodeNum: 123456789,
|
||||
firmwareVersion: '2.7.0.1',
|
||||
metadata: {
|
||||
firmwareVersion: '2.7.0.1',
|
||||
deviceStateVersion: 1,
|
||||
canShutdown: true,
|
||||
hasWifi: false,
|
||||
hasBluetooth: true,
|
||||
hasEthernet: false,
|
||||
role: 3,
|
||||
positionFlags: 65,
|
||||
hwModel: 42,
|
||||
hasRemoteHardware: false,
|
||||
hasPKC: true,
|
||||
excludedModules: 0,
|
||||
},
|
||||
localConfig: {
|
||||
device: { role: 3, buttonGpio: 13 },
|
||||
security: {
|
||||
publicKey: 'REDACTED',
|
||||
privateKey: 'REDACTED',
|
||||
adminKey: 'REDACTED',
|
||||
isManaged: true,
|
||||
},
|
||||
network: { wifiEnabled: false, wifiSsid: '', wifiPsk: 'REDACTED' },
|
||||
},
|
||||
moduleConfig: {
|
||||
telemetry: { environmentUpdateInterval: 900 },
|
||||
},
|
||||
},
|
||||
})
|
||||
device(): DeviceInfo {
|
||||
return this.mesh.deviceInfo();
|
||||
}
|
||||
|
||||
@Get('messages')
|
||||
@ApiOperation({
|
||||
summary: 'Message history (last 100)',
|
||||
@@ -118,8 +164,8 @@ export class MeshController {
|
||||
id: 456789,
|
||||
dir: 'tx',
|
||||
to: 42,
|
||||
text: 'hello testowy',
|
||||
channel: 2,
|
||||
text: 'hello test',
|
||||
channel: 3,
|
||||
at: 1758000123456,
|
||||
status: 'delivered',
|
||||
},
|
||||
@@ -141,7 +187,7 @@ export class MeshController {
|
||||
@ApiOperation({
|
||||
summary: 'Known mesh nodes (lean summaries)',
|
||||
description:
|
||||
'Node database collected during the initial sync plus live POSITION_APP updates; reset on every bridge reconnect. Names, hwModel and batteryLevel appear only when the device reported them.',
|
||||
'Node database collected during the initial sync plus live POSITION_APP updates; reset on every bridge reconnect. Names, hwModel and batteryLevel appear only when the device reported them; snr/hopsAway/viaMqtt only when carried by the NodeInfo.',
|
||||
})
|
||||
@ApiResponse({
|
||||
status: 200,
|
||||
@@ -155,6 +201,9 @@ export class MeshController {
|
||||
batteryLevel: 87,
|
||||
lastHeard: 1758000000,
|
||||
position: { lat: 52.1049637, lon: 20.9833152, alt: 123 },
|
||||
snr: -2.5,
|
||||
hopsAway: 1,
|
||||
viaMqtt: true,
|
||||
},
|
||||
],
|
||||
})
|
||||
|
||||
@@ -1,9 +1,10 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
import { McpController } from './mcp.controller.js';
|
||||
import { MeshController } from './mesh.controller.js';
|
||||
import { MeshService } from './mesh.service.js';
|
||||
|
||||
@Module({
|
||||
controllers: [MeshController],
|
||||
controllers: [MeshController, McpController],
|
||||
providers: [MeshService],
|
||||
})
|
||||
export class MeshModule {}
|
||||
|
||||
+159
-72
@@ -1,62 +1,23 @@
|
||||
import { create, fromBinary, toBinary } from '@bufbuild/protobuf';
|
||||
import * as P from '@meshtastic/protobufs';
|
||||
import * as net from 'node:net';
|
||||
import type { AddressInfo } from 'node:net';
|
||||
import { describe, expect, it } from 'vitest';
|
||||
import { BROADCAST_ADDR, extractFrames, frame } from './codec.js';
|
||||
import {
|
||||
KAMIL_NUM,
|
||||
configSectionFrame,
|
||||
kamilNodeInfo,
|
||||
metadataFrame,
|
||||
moduleConfigSectionFrame,
|
||||
nodeInfoFrame,
|
||||
startBridge,
|
||||
waitFor,
|
||||
} from './fake-bridge.js';
|
||||
import { MeshService } from './mesh.service.js';
|
||||
|
||||
function startBridge(onSocket?: (s: net.Socket) => void): Promise<{
|
||||
port: number;
|
||||
sockets: net.Socket[];
|
||||
close: () => void;
|
||||
}> {
|
||||
const sockets: net.Socket[] = [];
|
||||
const server = net.createServer((socket) => {
|
||||
onSocket?.(socket); // attached at accept time, before any data can arrive
|
||||
sockets.push(socket);
|
||||
});
|
||||
return new Promise((resolve) => {
|
||||
server.listen(0, '127.0.0.1', () => {
|
||||
resolve({
|
||||
port: (server.address() as AddressInfo).port,
|
||||
sockets,
|
||||
close: () => server.close(),
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
async function waitFor(
|
||||
cond: () => boolean | Promise<boolean>,
|
||||
timeoutMs = 5000,
|
||||
): Promise<void> {
|
||||
const t0 = Date.now();
|
||||
while (!(await cond())) {
|
||||
if (Date.now() - t0 > timeoutMs) throw new Error('timed out waiting');
|
||||
await new Promise((r) => setTimeout(r, 10));
|
||||
}
|
||||
}
|
||||
|
||||
function nodeInfoFrame(
|
||||
num: number,
|
||||
fields?: Partial<P.Mesh.NodeInfo>,
|
||||
): Uint8Array {
|
||||
const ni = create(P.Mesh.NodeInfoSchema, { num, ...fields });
|
||||
return frame(
|
||||
toBinary(
|
||||
P.Mesh.FromRadioSchema,
|
||||
create(P.Mesh.FromRadioSchema, {
|
||||
payloadVariant: { case: 'nodeInfo', value: ni },
|
||||
}),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
// live position update: POSITION_APP packet with a serialized Position in Data.payload
|
||||
function positionPacketFrame(
|
||||
from: number,
|
||||
pos: Partial<P.Mesh.Position>,
|
||||
pos: { latitudeI?: number; longitudeI?: number },
|
||||
): Uint8Array {
|
||||
const data = create(P.Mesh.DataSchema, {
|
||||
portnum: P.Portnums.PortNum.POSITION_APP,
|
||||
@@ -80,7 +41,6 @@ function positionPacketFrame(
|
||||
);
|
||||
}
|
||||
|
||||
const KAMIL_NUM = 123456789;
|
||||
// inbound text message: TEXT_MESSAGE_APP packet with UTF-8 in Data.payload
|
||||
function textPacketFrame(
|
||||
from: number,
|
||||
@@ -138,19 +98,6 @@ function routingFrame(
|
||||
),
|
||||
);
|
||||
}
|
||||
function kamilNodeInfo(): Partial<P.Mesh.NodeInfo> {
|
||||
return {
|
||||
user: {
|
||||
id: '!kamil',
|
||||
longName: 'Kamil SP8MK/P',
|
||||
shortName: 'KK',
|
||||
hwModel: P.Mesh.HardwareModel.TBEAM,
|
||||
},
|
||||
position: { latitudeI: 521049637, longitudeI: 209833152, altitude: 123 },
|
||||
lastHeard: 1758000000,
|
||||
deviceMetrics: { batteryLevel: 87 },
|
||||
};
|
||||
}
|
||||
|
||||
function channelFrame(
|
||||
index: number,
|
||||
@@ -295,11 +242,11 @@ describe('MeshService connect', () => {
|
||||
});
|
||||
|
||||
describe('MeshService send', () => {
|
||||
it('sends on the test channel (index 2) by default, never primary', async () => {
|
||||
it('sends on the test channel (index 3) by default, never primary', async () => {
|
||||
const bridge = await startBridge();
|
||||
process.env.MESH_HOST = '127.0.0.1';
|
||||
process.env.MESH_PORT = String(bridge.port);
|
||||
delete process.env.MESH_SEND_CHANNEL; // default path: "testowy" (index 2)
|
||||
delete process.env.MESH_SEND_CHANNEL; // default path: "test" (index 3)
|
||||
const svc = new MeshService();
|
||||
try {
|
||||
svc.onModuleInit();
|
||||
@@ -309,7 +256,7 @@ describe('MeshService send', () => {
|
||||
|
||||
const out: Uint8Array[] = [];
|
||||
bridge.sockets[0].on('data', (c) => out.push(c));
|
||||
const id = await svc.sendText(42, 'hello testowy');
|
||||
const id = await svc.sendText(42, 'hello test');
|
||||
expect(id).toBeGreaterThan(0);
|
||||
|
||||
const readOutbound = (): P.Mesh.MeshPacket | undefined => {
|
||||
@@ -331,7 +278,7 @@ describe('MeshService send', () => {
|
||||
|
||||
await waitFor(() => readOutbound() !== undefined);
|
||||
const pkt = readOutbound()!;
|
||||
expect(pkt.channel).toBe(2); // "testowy" — never the primary channel (0)
|
||||
expect(pkt.channel).toBe(3); // "test" — never the primary channel (0)
|
||||
expect(pkt.to).toBe(42);
|
||||
const decoded =
|
||||
pkt.payloadVariant?.case === 'decoded'
|
||||
@@ -339,7 +286,7 @@ describe('MeshService send', () => {
|
||||
: undefined;
|
||||
expect(
|
||||
new TextDecoder().decode(decoded?.payload ?? new Uint8Array()),
|
||||
).toBe('hello testowy');
|
||||
).toBe('hello test');
|
||||
} finally {
|
||||
delete process.env.MESH_SEND_CHANNEL;
|
||||
svc.onModuleDestroy();
|
||||
@@ -537,13 +484,13 @@ describe('MeshService message history', () => {
|
||||
() => bridge.sockets.length === 1 && svc.status().connected,
|
||||
);
|
||||
|
||||
const id = await svc.sendText(42, 'hello testowy');
|
||||
const id = await svc.sendText(42, 'hello test');
|
||||
expect(svc.messages()[0]).toMatchObject({
|
||||
id,
|
||||
dir: 'tx',
|
||||
to: 42,
|
||||
text: 'hello testowy',
|
||||
channel: 2,
|
||||
text: 'hello test',
|
||||
channel: 3,
|
||||
status: 'pending',
|
||||
});
|
||||
|
||||
@@ -670,3 +617,143 @@ describe('MeshService resolveDest', () => {
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
describe('MeshService device info', () => {
|
||||
it('captures DeviceMetadata and exposes firmwareVersion', async () => {
|
||||
const bridge = await startBridge();
|
||||
process.env.MESH_HOST = '127.0.0.1';
|
||||
process.env.MESH_PORT = String(bridge.port);
|
||||
const svc = new MeshService();
|
||||
try {
|
||||
svc.onModuleInit();
|
||||
await waitFor(
|
||||
() => bridge.sockets.length === 1 && svc.status().connected,
|
||||
);
|
||||
|
||||
// before the sync: only the node number (null until myInfo arrives)
|
||||
expect(svc.deviceInfo()).toEqual({
|
||||
myNodeNum: null,
|
||||
localConfig: {},
|
||||
moduleConfig: {},
|
||||
});
|
||||
|
||||
bridge.sockets[0].write(
|
||||
metadataFrame({ firmwareVersion: '2.7.0.1', hasWifi: false, role: 3 }),
|
||||
);
|
||||
|
||||
await waitFor(() => svc.deviceInfo().firmwareVersion === '2.7.0.1');
|
||||
const info = svc.deviceInfo();
|
||||
expect(info.firmwareVersion).toBe('2.7.0.1');
|
||||
expect(info.metadata?.firmwareVersion).toBe('2.7.0.1'); // metadata stored as-is
|
||||
} finally {
|
||||
svc.onModuleDestroy();
|
||||
bridge.close();
|
||||
}
|
||||
});
|
||||
|
||||
it('captures config/module-config sections and redacts key material', async () => {
|
||||
const bridge = await startBridge();
|
||||
process.env.MESH_HOST = '127.0.0.1';
|
||||
process.env.MESH_PORT = String(bridge.port);
|
||||
const svc = new MeshService();
|
||||
try {
|
||||
svc.onModuleInit();
|
||||
await waitFor(
|
||||
() => bridge.sockets.length === 1 && svc.status().connected,
|
||||
);
|
||||
|
||||
const secret = 'super-secret-wifi-psk';
|
||||
bridge.sockets[0].write(
|
||||
configSectionFrame('security', {
|
||||
publicKey: new Uint8Array([1, 2, 3]),
|
||||
privateKey: new Uint8Array([4, 5, 6]),
|
||||
adminKey: [new Uint8Array([7])],
|
||||
isManaged: true,
|
||||
}),
|
||||
);
|
||||
bridge.sockets[0].write(
|
||||
configSectionFrame('network', {
|
||||
wifiEnabled: false,
|
||||
wifiSsid: 'home',
|
||||
wifiPsk: secret,
|
||||
}),
|
||||
);
|
||||
bridge.sockets[0].write(configSectionFrame('device', { role: 3 }));
|
||||
bridge.sockets[0].write(
|
||||
moduleConfigSectionFrame('telemetry', {
|
||||
environmentUpdateInterval: 900,
|
||||
powerMeasurementEnabled: true,
|
||||
}),
|
||||
);
|
||||
|
||||
await waitFor(
|
||||
() =>
|
||||
Object.keys(svc.deviceInfo().localConfig).length === 3 &&
|
||||
Object.keys(svc.deviceInfo().moduleConfig).length === 1,
|
||||
);
|
||||
const info = svc.deviceInfo();
|
||||
expect(Object.keys(info.localConfig).sort()).toEqual([
|
||||
'device',
|
||||
'network',
|
||||
'security',
|
||||
]);
|
||||
expect(info.moduleConfig.telemetry).toMatchObject({
|
||||
environmentUpdateInterval: 900,
|
||||
powerMeasurementEnabled: true,
|
||||
});
|
||||
|
||||
// key material is replaced with a marker — never exposed
|
||||
expect(info.localConfig.security).toMatchObject({
|
||||
publicKey: 'REDACTED',
|
||||
privateKey: 'REDACTED',
|
||||
adminKey: 'REDACTED',
|
||||
isManaged: true, // non-secret fields pass through
|
||||
});
|
||||
expect(info.localConfig.network?.wifiPsk).toBe('REDACTED');
|
||||
const json = JSON.stringify(info);
|
||||
expect(json).not.toContain(secret);
|
||||
} finally {
|
||||
svc.onModuleDestroy();
|
||||
bridge.close();
|
||||
}
|
||||
});
|
||||
|
||||
it('summarizes NodeInfo snr/hopsAway/viaMqtt when present', async () => {
|
||||
const bridge = await startBridge();
|
||||
process.env.MESH_HOST = '127.0.0.1';
|
||||
process.env.MESH_PORT = String(bridge.port);
|
||||
const svc = new MeshService();
|
||||
try {
|
||||
svc.onModuleInit();
|
||||
await waitFor(
|
||||
() => bridge.sockets.length === 1 && svc.status().connected,
|
||||
);
|
||||
|
||||
bridge.sockets[0].write(nodeInfoFrame(KAMIL_NUM, kamilNodeInfo()));
|
||||
bridge.sockets[0].write(
|
||||
nodeInfoFrame(42, {
|
||||
user: { id: '!ot', longName: 'Other Node' },
|
||||
snr: -2.5,
|
||||
hopsAway: 1,
|
||||
viaMqtt: true,
|
||||
}),
|
||||
);
|
||||
|
||||
await waitFor(async () => (await svc.nodes()).length === 2);
|
||||
const nodes = await svc.nodes();
|
||||
expect(nodes.find((n) => n.num === 42)).toEqual({
|
||||
num: 42,
|
||||
longName: 'Other Node',
|
||||
lastHeard: null,
|
||||
snr: -2.5,
|
||||
hopsAway: 1,
|
||||
viaMqtt: true,
|
||||
});
|
||||
// absent in the proto → omitted from the summary
|
||||
expect(nodes.find((n) => n.num === KAMIL_NUM)).not.toHaveProperty('snr');
|
||||
} finally {
|
||||
svc.onModuleDestroy();
|
||||
bridge.close();
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
@@ -59,6 +59,35 @@ export interface NodeSummary {
|
||||
/** epoch seconds of the last heard packet; null when never heard */
|
||||
lastHeard: number | null;
|
||||
position?: { lat: number; lon: number; alt?: number };
|
||||
/** signal-to-noise ratio of the last received packet, dB (NodeInfo.snr) */
|
||||
snr?: number;
|
||||
/** hops from this node to the one that reported it (optional in proto) */
|
||||
hopsAway?: number;
|
||||
/** true when the node info arrived via MQTT rather than LoRa */
|
||||
viaMqtt?: boolean;
|
||||
}
|
||||
|
||||
/** Shape of deviceInfo() output: sync-captured device data, key material redacted. */
|
||||
export interface DeviceInfo {
|
||||
myNodeNum: number | null;
|
||||
firmwareVersion?: string;
|
||||
metadata?: Proto.Mesh.DeviceMetadata;
|
||||
localConfig: Record<string, Record<string, unknown>>;
|
||||
moduleConfig: Record<string, Record<string, unknown>>;
|
||||
}
|
||||
|
||||
// key material must never leave the process — replaced with a marker so callers can see the field exists
|
||||
const REDACTED = 'REDACTED';
|
||||
/** Config sections that carry secrets: security (PKI keys) and network (wifi psk). */
|
||||
function redactSection(
|
||||
name: string,
|
||||
value: Record<string, unknown>,
|
||||
): Record<string, unknown> {
|
||||
if (name !== 'security' && name !== 'network') return value;
|
||||
const out = { ...value };
|
||||
for (const key of ['publicKey', 'privateKey', 'adminKey', 'wifiPsk'])
|
||||
if (out[key] !== undefined) out[key] = REDACTED;
|
||||
return out;
|
||||
}
|
||||
|
||||
@Injectable()
|
||||
@@ -71,9 +100,9 @@ export class MeshService implements OnModuleInit, OnModuleDestroy {
|
||||
process.env.MESH_RECONNECT_MS ?? '5000',
|
||||
10,
|
||||
);
|
||||
// sends are pinned to the test channel ("testowy", index 2) on purpose — never the primary channel
|
||||
// sends are pinned to the test channel ("test", index 3) on purpose — never the primary channel
|
||||
private readonly sendChannel = parseInt(
|
||||
process.env.MESH_SEND_CHANNEL ?? '2',
|
||||
process.env.MESH_SEND_CHANNEL ?? '3',
|
||||
10,
|
||||
);
|
||||
|
||||
@@ -88,6 +117,10 @@ export class MeshService implements OnModuleInit, OnModuleDestroy {
|
||||
>();
|
||||
// node database: NodeInfo frames from the initial sync + live POSITION_APP updates
|
||||
private nodeMap = new Map<number, NodeEntry>();
|
||||
// device metadata + per-section config/module-config streamed during the initial sync
|
||||
private metadata: Proto.Mesh.DeviceMetadata | null = null;
|
||||
private configSections = new Map<string, Record<string, unknown>>();
|
||||
private moduleConfigSections = new Map<string, Record<string, unknown>>();
|
||||
// message history (newest kept, capped at HISTORY_LIMIT) + pending tx records awaiting their routing ACK/NACK
|
||||
private history: MessageRecord[] = [];
|
||||
private pendingTx = new Map<number, MessageRecord>();
|
||||
@@ -132,9 +165,33 @@ export class MeshService implements OnModuleInit, OnModuleDestroy {
|
||||
}));
|
||||
}
|
||||
|
||||
/**
|
||||
* Device info captured during the initial sync: this device's node number, firmware version and
|
||||
* metadata (DeviceMetadata as-is — it carries no key material), plus per-section config and
|
||||
* module-config messages keyed by section name. In-memory only; empty until the sync completes,
|
||||
* reset on every bridge reconnect. Key material is redacted: security publicKey/privateKey/adminKey
|
||||
* and network wifiPsk are replaced with "REDACTED" — never exposed over HTTP.
|
||||
*/
|
||||
deviceInfo(): DeviceInfo {
|
||||
const out: DeviceInfo = {
|
||||
myNodeNum: this.myNodeNum,
|
||||
localConfig: {},
|
||||
moduleConfig: {},
|
||||
};
|
||||
if (this.metadata) {
|
||||
out.firmwareVersion = this.metadata.firmwareVersion;
|
||||
out.metadata = this.metadata;
|
||||
}
|
||||
for (const [name, value] of this.configSections)
|
||||
out.localConfig[name] = redactSection(name, value);
|
||||
for (const [name, value] of this.moduleConfigSections)
|
||||
out.moduleConfig[name] = value;
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Lean summaries of all known nodes, sorted by node number.
|
||||
* Names/hwModel/batteryLevel appear only when the device reported them;
|
||||
* Names/hwModel/batteryLevel/snr/hopsAway/viaMqtt appear only when the device reported them;
|
||||
* `lastHeard` is epoch seconds (null = never heard); `position` only when both coordinates are known.
|
||||
*/
|
||||
async nodes(): Promise<NodeSummary[]> {
|
||||
@@ -247,9 +304,13 @@ export class MeshService implements OnModuleInit, OnModuleDestroy {
|
||||
// omit UNSET (enum value 0) rather than report "UNSET"
|
||||
const hwName = n.user?.hwModel ? names.get(n.user.hwModel) : undefined;
|
||||
if (hwName && hwName !== 'UNSET') s.hwModel = hwName;
|
||||
// optional proto field: undefined when the device never reported metrics
|
||||
// optional proto fields: undefined when the device never reported them
|
||||
const battery = n.deviceMetrics?.batteryLevel;
|
||||
if (battery !== undefined) s.batteryLevel = battery;
|
||||
// non-optional proto scalars decode with their defaults (snr=0, viaMqtt=false), so a default value means "not reported"
|
||||
if (n.snr !== undefined && n.snr !== 0) s.snr = n.snr;
|
||||
if (n.hopsAway !== undefined) s.hopsAway = n.hopsAway; // optional in proto → truly absent when not reported
|
||||
if (n.viaMqtt === true) s.viaMqtt = true;
|
||||
const p = n.position;
|
||||
if (p?.latitudeI !== undefined && p.longitudeI !== undefined) {
|
||||
// alt included only when the device sent it — protobuf-es leaves absent optionals undefined, so an explicit 0 (sea level) is kept
|
||||
@@ -265,9 +326,12 @@ export class MeshService implements OnModuleInit, OnModuleDestroy {
|
||||
this.socket = socket;
|
||||
socket.on('connect', () => {
|
||||
this.connected = true;
|
||||
// fresh sync on every (re)connect — drop stale channels/nodes/history from the old session
|
||||
// fresh sync on every (re)connect — drop stale channels/nodes/device info/history from the old session
|
||||
this.channelMap = new Map();
|
||||
this.nodeMap = new Map();
|
||||
this.metadata = null;
|
||||
this.configSections.clear();
|
||||
this.moduleConfigSections.clear();
|
||||
this.history = [];
|
||||
this.pendingTx.clear();
|
||||
this.logger.log(`bridge connected ${this.host}:${this.port}`);
|
||||
@@ -328,6 +392,21 @@ export class MeshService implements OnModuleInit, OnModuleDestroy {
|
||||
// device streams the whole node DB during initial sync
|
||||
this.nodeMap.set(v.value.num, v.value);
|
||||
break;
|
||||
case 'metadata':
|
||||
// DeviceMetadata streamed once during initial sync (no key material in it)
|
||||
this.metadata = v.value;
|
||||
break;
|
||||
case 'config': {
|
||||
// device streams one Config per section during initial sync
|
||||
const sec = v.value.payloadVariant;
|
||||
if (sec?.case) this.configSections.set(sec.case, sec.value);
|
||||
break;
|
||||
}
|
||||
case 'moduleConfig': {
|
||||
const mod = v.value.payloadVariant;
|
||||
if (mod?.case) this.moduleConfigSections.set(mod.case, mod.value);
|
||||
break;
|
||||
}
|
||||
case 'configCompleteId':
|
||||
this.logger.log(
|
||||
`bridge ready (config complete), ${this.channelMap.size} channels, ${this.nodeMap.size} nodes`,
|
||||
|
||||
Reference in New Issue
Block a user