zigbee2mqtt-mcp 0.8.0 → 0.8.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +2 -2
- package/dist/client.js +56 -3
- package/dist/devices.js +2 -0
- package/dist/http.js +7 -10
- package/dist/start.js +2 -2
- package/dist/tools.js +2 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -85,7 +85,7 @@ Add to your MCP configuration:
|
|
|
85
85
|
| Variable | Default | Purpose |
|
|
86
86
|
| --- | --- | --- |
|
|
87
87
|
| `Z2M_MQTT_URL` | *required* | Broker URL. `mqtt://`, `mqtts://`, `ws://`, `wss://` |
|
|
88
|
-
| `Z2M_MQTT_USERNAME` / `Z2M_MQTT_PASSWORD` | – | Broker credentials |
|
|
88
|
+
| `Z2M_MQTT_USERNAME` / `Z2M_MQTT_PASSWORD` | – | Broker credentials. A refused login is not retried on its own; the next tool call tries once |
|
|
89
89
|
| `Z2M_BASE_TOPIC` | `zigbee2mqtt` | Must match `mqtt.base_topic` |
|
|
90
90
|
| `Z2M_MQTT_CLIENT_ID` | `zigbee2mqtt-mcp-<pid>` | Override if your broker requires a fixed client ID |
|
|
91
91
|
| `Z2M_WRITE_MODE` | `safe` | `off`, `safe` or `full` |
|
|
@@ -182,7 +182,7 @@ Opt in to the destructive tier only when you want it:
|
|
|
182
182
|
| `z2m_bridge_info` | Version, coordinator, channel, PAN ID, permit_join, restart_required, runtime stats |
|
|
183
183
|
| `z2m_list_devices` | Filter and sort devices by type, availability, link quality, battery, pending update |
|
|
184
184
|
| `z2m_get_device` | Exposes, settable options, endpoints, bindings, configured reportings, current state |
|
|
185
|
-
| `z2m_health_report` | Whole-estate audit in one call |
|
|
185
|
+
| `z2m_health_report` | Whole-estate audit in one call. `totals.affected_devices` counts devices with an issue; `totals.issue_count` counts findings, so one device can add several |
|
|
186
186
|
| `z2m_network_map` | Mesh topology with parent, depth, link quality, orphan detection |
|
|
187
187
|
| `z2m_list_groups` | Groups, members and scenes |
|
|
188
188
|
| `z2m_get_logs` | Bridge logs and lifecycle events, buffered or watched live |
|
package/dist/client.js
CHANGED
|
@@ -18,6 +18,24 @@ export function waiterKey(subTopic, transaction) {
|
|
|
18
18
|
export class NoBridgeDataError extends Error {
|
|
19
19
|
name = "NoBridgeDataError";
|
|
20
20
|
}
|
|
21
|
+
/**
|
|
22
|
+
* The broker refused the login. Trying again with the same credentials cannot
|
|
23
|
+
* succeed and only adds failed logins on the broker, so nothing retries it on
|
|
24
|
+
* its own: the next tool call makes one new attempt.
|
|
25
|
+
*/
|
|
26
|
+
export class BrokerAuthError extends Error {
|
|
27
|
+
name = "BrokerAuthError";
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* CONNACK codes that refuse the login: 4 and 5 in MQTT 3.1.1 (bad username or
|
|
31
|
+
* password, not authorized), 0x86 and 0x87 in MQTT 5.
|
|
32
|
+
*/
|
|
33
|
+
const AUTH_REFUSED = new Set([4, 5, 0x86, 0x87]);
|
|
34
|
+
/** True for mqtt.js's error when the broker refused the login. */
|
|
35
|
+
export function isAuthRefusal(error) {
|
|
36
|
+
const code = error?.code;
|
|
37
|
+
return typeof code === "number" && AUTH_REFUSED.has(code);
|
|
38
|
+
}
|
|
21
39
|
const LOG_BUFFER = 1000;
|
|
22
40
|
const EVENT_BUFFER = 200;
|
|
23
41
|
/**
|
|
@@ -54,6 +72,8 @@ export class Z2MClient {
|
|
|
54
72
|
closed = false;
|
|
55
73
|
/** Fails the connection being set up, if any. */
|
|
56
74
|
abortConnect = null;
|
|
75
|
+
/** The login refusal that ended the latest connection, for callers waiting on it. */
|
|
76
|
+
refusal = null;
|
|
57
77
|
bridge = new Map();
|
|
58
78
|
deviceState = new Map();
|
|
59
79
|
availability = new Map();
|
|
@@ -131,13 +151,21 @@ export class Z2MClient {
|
|
|
131
151
|
client.end(true);
|
|
132
152
|
if (this.client === client)
|
|
133
153
|
this.client = null;
|
|
154
|
+
this.refusal = error instanceof BrokerAuthError ? error : null;
|
|
134
155
|
reject(error);
|
|
135
156
|
};
|
|
136
157
|
this.abortConnect = fail;
|
|
137
158
|
const timer = setTimeout(() => fail(new Error(`Timed out connecting to ${this.safeUrl} after ${config.connectTimeoutMs}ms`)), config.connectTimeoutMs);
|
|
138
159
|
client.on("error", (error) => {
|
|
139
|
-
|
|
140
|
-
|
|
160
|
+
const reason = scrubSecrets(error.message, config);
|
|
161
|
+
const refused = isAuthRefusal(error);
|
|
162
|
+
if (settled && refused) {
|
|
163
|
+
this.giveUp(client, reason);
|
|
164
|
+
return;
|
|
165
|
+
}
|
|
166
|
+
this.log.error("mqtt error:", reason);
|
|
167
|
+
const message = `MQTT connection to ${this.safeUrl} failed: ${reason}`;
|
|
168
|
+
fail(refused ? new BrokerAuthError(message) : new Error(message));
|
|
141
169
|
});
|
|
142
170
|
client.on("message", (topic, payload) => this.handleMessage(topic, payload));
|
|
143
171
|
client.on("close", () => this.log.debug("mqtt connection closed"));
|
|
@@ -160,6 +188,7 @@ export class Z2MClient {
|
|
|
160
188
|
settled = true;
|
|
161
189
|
clearTimeout(timer);
|
|
162
190
|
this.abortConnect = null;
|
|
191
|
+
this.refusal = null;
|
|
163
192
|
resolve();
|
|
164
193
|
})
|
|
165
194
|
.catch(fail);
|
|
@@ -167,14 +196,38 @@ export class Z2MClient {
|
|
|
167
196
|
});
|
|
168
197
|
});
|
|
169
198
|
}
|
|
199
|
+
/**
|
|
200
|
+
* A working connection dropped and mqtt.js's reconnect was refused the
|
|
201
|
+
* login. The client is dropped rather than left to retry, so the broker sees
|
|
202
|
+
* one failed login, and the next tool call dials once with a fresh client.
|
|
203
|
+
* That also recovers once the login is fixed on the broker side. The cached
|
|
204
|
+
* bridge topics are dropped so that the new connection waits for fresh ones.
|
|
205
|
+
*/
|
|
206
|
+
giveUp(client, reason) {
|
|
207
|
+
if (this.client !== client)
|
|
208
|
+
return;
|
|
209
|
+
this.client = null;
|
|
210
|
+
this.bridge.delete("devices");
|
|
211
|
+
this.bridge.delete("info");
|
|
212
|
+
this.refusal = new BrokerAuthError(`MQTT connection to ${this.safeUrl} failed: ${reason}`);
|
|
213
|
+
this.log.error(`mqtt error: ${reason}. Not logging in again until the next tool call.`);
|
|
214
|
+
client.end(true);
|
|
215
|
+
}
|
|
170
216
|
/** Lets mqtt.js finish its own reconnect instead of opening a rival client. */
|
|
171
217
|
async awaitReconnect() {
|
|
172
218
|
const deadline = Date.now() + this.config.connectTimeoutMs;
|
|
173
219
|
while (Date.now() < deadline) {
|
|
220
|
+
// After a refusal another caller may already be dialing again: share
|
|
221
|
+
// that attempt, bridge topics included, rather than poll its client.
|
|
222
|
+
if (this.connectPromise)
|
|
223
|
+
return this.connectPromise;
|
|
174
224
|
if (this.client?.connected)
|
|
175
225
|
return;
|
|
176
|
-
if (!this.client)
|
|
226
|
+
if (!this.client) {
|
|
227
|
+
if (this.refusal)
|
|
228
|
+
throw this.refusal;
|
|
177
229
|
break;
|
|
230
|
+
}
|
|
178
231
|
await sleep(100);
|
|
179
232
|
}
|
|
180
233
|
if (!this.client?.connected) {
|
package/dist/devices.js
CHANGED
|
@@ -182,7 +182,9 @@ export function buildHealthReport(devices, info, bridgeState, bridgeHealth, capa
|
|
|
182
182
|
devices: devices.length,
|
|
183
183
|
routers: devices.filter((d) => d.type === "Router").length,
|
|
184
184
|
end_devices: devices.filter((d) => d.type === "EndDevice").length,
|
|
185
|
+
// One per finding, so a device on three lists counts three times.
|
|
185
186
|
issue_count: Object.values(nonEmpty).reduce((sum, list) => sum + list.length, 0),
|
|
187
|
+
affected_devices: new Set(Object.values(nonEmpty).flatMap((list) => list.map((issue) => issue.ieee_address))).size,
|
|
186
188
|
},
|
|
187
189
|
capabilities,
|
|
188
190
|
issues: nonEmpty,
|
package/dist/http.js
CHANGED
|
@@ -3,6 +3,7 @@ import { requireBearerAuth } from "@modelcontextprotocol/sdk/server/auth/middlew
|
|
|
3
3
|
import { InvalidTokenError } from "@modelcontextprotocol/sdk/server/auth/errors.js";
|
|
4
4
|
import { createMcpExpressApp } from "@modelcontextprotocol/sdk/server/express.js";
|
|
5
5
|
import { StreamableHTTPServerTransport } from "@modelcontextprotocol/sdk/server/streamableHttp.js";
|
|
6
|
+
import express from "express";
|
|
6
7
|
import { DEFAULT_HTTP_PATH } from "./config.js";
|
|
7
8
|
export const MCP_PATH = DEFAULT_HTTP_PATH;
|
|
8
9
|
const LOOPBACK = new Set(["127.0.0.1", "localhost", "::1"]);
|
|
@@ -49,16 +50,12 @@ export function pathMatcher(expected) {
|
|
|
49
50
|
return timingSafeEqual(digest(trimmed), want);
|
|
50
51
|
};
|
|
51
52
|
}
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
export async function startHttp(config, createServer, log) {
|
|
59
|
-
// With no explicit list the SDK only checks the Host header on loopback
|
|
60
|
-
// binds, which is where DNS rebinding applies.
|
|
61
|
-
const app = createMcpExpressApp({ host: config.httpHost, allowedHosts: config.httpAllowedHosts });
|
|
53
|
+
export async function startHttp(config, createServer, log, { anyHost = false } = {}) {
|
|
54
|
+
const app = anyHost && !config.httpAllowedHosts && !isLoopback(config.httpHost)
|
|
55
|
+
? express().use(express.json())
|
|
56
|
+
: // With no explicit list the SDK only checks the Host header on
|
|
57
|
+
// loopback binds, which is where DNS rebinding applies.
|
|
58
|
+
createMcpExpressApp({ host: config.httpHost, allowedHosts: config.httpAllowedHosts });
|
|
62
59
|
const isMcpPath = pathMatcher(config.httpPath);
|
|
63
60
|
// Ahead of authentication, so no other path reveals whether a token is set.
|
|
64
61
|
app.use((req, res, next) => {
|
package/dist/start.js
CHANGED
|
@@ -27,7 +27,7 @@ export const version = packageVersion();
|
|
|
27
27
|
* signal handlers that shut it down. Shared by the command line entry point
|
|
28
28
|
* and the Home Assistant app.
|
|
29
29
|
*/
|
|
30
|
-
export async function start({ serverName } = {}) {
|
|
30
|
+
export async function start({ serverName, anyHost } = {}) {
|
|
31
31
|
let config;
|
|
32
32
|
let configError;
|
|
33
33
|
try {
|
|
@@ -56,7 +56,7 @@ export async function start({ serverName } = {}) {
|
|
|
56
56
|
process.on("SIGINT", shutdown);
|
|
57
57
|
process.on("SIGTERM", shutdown);
|
|
58
58
|
if (config.transport === "http") {
|
|
59
|
-
http = await startHttp(config, () => createServer(ctx, tools, log, version, serverName), log);
|
|
59
|
+
http = await startHttp(config, () => createServer(ctx, tools, log, version, serverName), log, { anyHost });
|
|
60
60
|
log.info(`ready on http://${config.httpHost}:${http.port}${config.httpPath}`);
|
|
61
61
|
return { config, log, client, http, shutdownSignal: stopping.signal };
|
|
62
62
|
}
|
package/dist/tools.js
CHANGED
|
@@ -362,6 +362,8 @@ const readTools = [
|
|
|
362
362
|
"Read-only and generates no Zigbee traffic, so prefer it over z2m_check_updates and z2m_network_map " +
|
|
363
363
|
"for routine checks. Use this to answer 'is my Zigbee network healthy?'. " +
|
|
364
364
|
"Returns {bridge, totals, capabilities, issues, hints}; an empty issues object means nothing is wrong. " +
|
|
365
|
+
"totals.affected_devices counts distinct devices with at least one issue; totals.issue_count counts " +
|
|
366
|
+
"findings, so a device that is offline, stale and low on battery adds three. " +
|
|
365
367
|
"Note that collect_seconds makes the call wait for that many seconds before answering.",
|
|
366
368
|
inputSchema: {
|
|
367
369
|
type: "object",
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "zigbee2mqtt-mcp",
|
|
3
|
-
"version": "0.8.
|
|
3
|
+
"version": "0.8.1",
|
|
4
4
|
"mcpName": "io.github.alexpfau/zigbee2mqtt-mcp",
|
|
5
5
|
"description": "Model Context Protocol server for administering a Zigbee2MQTT estate: health reporting, network diagnostics, OTA updates, pairing and device management over the MQTT bridge API.",
|
|
6
6
|
"keywords": [
|