@solomonneas/librenms-mcp 0.2.0 → 0.2.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 +164 -56
- package/dist/cli.js +1180 -0
- package/dist/index.js +61 -12
- package/dist/mcp-bin.js +733 -0
- package/dist/mcp-server.js +149 -78
- package/package.json +51 -13
package/dist/mcp-bin.js
ADDED
|
@@ -0,0 +1,733 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
// mcp-server.ts
|
|
4
|
+
import { Server } from "@modelcontextprotocol/sdk/server/index.js";
|
|
5
|
+
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
|
|
6
|
+
import { CallToolRequestSchema, ListToolsRequestSchema } from "@modelcontextprotocol/sdk/types.js";
|
|
7
|
+
|
|
8
|
+
// src/config.ts
|
|
9
|
+
var ConfigError = class extends Error {
|
|
10
|
+
constructor(message) {
|
|
11
|
+
super(message);
|
|
12
|
+
this.name = "ConfigError";
|
|
13
|
+
}
|
|
14
|
+
};
|
|
15
|
+
function isTruthy(value) {
|
|
16
|
+
if (!value) return false;
|
|
17
|
+
return ["true", "1", "yes"].includes(value.toLowerCase());
|
|
18
|
+
}
|
|
19
|
+
function resolveConfig(env) {
|
|
20
|
+
const url = env.LIBRENMS_URL;
|
|
21
|
+
const token = env.LIBRENMS_TOKEN;
|
|
22
|
+
if (!url) throw new ConfigError("LIBRENMS_URL is required");
|
|
23
|
+
if (!token) throw new ConfigError("LIBRENMS_TOKEN is required");
|
|
24
|
+
return {
|
|
25
|
+
url: url.replace(/\/+$/, ""),
|
|
26
|
+
token,
|
|
27
|
+
tlsInsecure: isTruthy(env.LIBRENMS_TLS_INSECURE)
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
// src/librenms-client.ts
|
|
32
|
+
import { Agent as UndiciAgent } from "undici";
|
|
33
|
+
var LibreNmsClientError = class extends Error {
|
|
34
|
+
constructor(status, message) {
|
|
35
|
+
super(`LibreNMS ${status}: ${message}`);
|
|
36
|
+
this.status = status;
|
|
37
|
+
this.name = "LibreNmsClientError";
|
|
38
|
+
}
|
|
39
|
+
status;
|
|
40
|
+
};
|
|
41
|
+
var LibreNmsUnreachableError = class extends Error {
|
|
42
|
+
constructor(cause) {
|
|
43
|
+
super(`LibreNMS unreachable: ${cause}`);
|
|
44
|
+
this.name = "LibreNmsUnreachableError";
|
|
45
|
+
}
|
|
46
|
+
};
|
|
47
|
+
var LibreNmsClient = class {
|
|
48
|
+
constructor(cfg, opts = {}) {
|
|
49
|
+
this.cfg = cfg;
|
|
50
|
+
this.retryDelayMs = opts.retryDelayMs ?? 1e3;
|
|
51
|
+
if (cfg.tlsInsecure && cfg.url.startsWith("https://")) {
|
|
52
|
+
this.dispatcher = new UndiciAgent({ connect: { rejectUnauthorized: false } });
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
cfg;
|
|
56
|
+
retryDelayMs;
|
|
57
|
+
// Node's global fetch (undici) ignores node:https.Agent. To actually skip
|
|
58
|
+
// cert verification for self-signed LibreNMS hosts we pass an undici Agent
|
|
59
|
+
// via the `dispatcher` init option.
|
|
60
|
+
dispatcher;
|
|
61
|
+
async get(path) {
|
|
62
|
+
return this.request("GET", path);
|
|
63
|
+
}
|
|
64
|
+
async post(path, body) {
|
|
65
|
+
return this.request("POST", path, body);
|
|
66
|
+
}
|
|
67
|
+
async put(path, body) {
|
|
68
|
+
return this.request("PUT", path, body);
|
|
69
|
+
}
|
|
70
|
+
async delete(path) {
|
|
71
|
+
return this.request("DELETE", path);
|
|
72
|
+
}
|
|
73
|
+
async request(method, path, body) {
|
|
74
|
+
const url = this.cfg.url + "/api/v0" + path;
|
|
75
|
+
const headers = { "x-auth-token": this.cfg.token };
|
|
76
|
+
let bodyStr;
|
|
77
|
+
if (body !== void 0) {
|
|
78
|
+
headers["content-type"] = "application/json";
|
|
79
|
+
bodyStr = JSON.stringify(body);
|
|
80
|
+
}
|
|
81
|
+
let lastErr;
|
|
82
|
+
for (let attempt = 0; attempt < 2; attempt++) {
|
|
83
|
+
try {
|
|
84
|
+
const init = { method, headers, body: bodyStr };
|
|
85
|
+
if (this.dispatcher) init.dispatcher = this.dispatcher;
|
|
86
|
+
const res = await fetch(url, init);
|
|
87
|
+
if (res.status >= 200 && res.status < 300) {
|
|
88
|
+
const text = await res.text();
|
|
89
|
+
if (!text) return void 0;
|
|
90
|
+
return JSON.parse(text);
|
|
91
|
+
}
|
|
92
|
+
if (res.status >= 500) {
|
|
93
|
+
lastErr = new LibreNmsUnreachableError(`HTTP ${res.status}`);
|
|
94
|
+
if (attempt === 0) await sleep(this.retryDelayMs);
|
|
95
|
+
continue;
|
|
96
|
+
}
|
|
97
|
+
const errText = await res.text();
|
|
98
|
+
let msg = errText;
|
|
99
|
+
try {
|
|
100
|
+
msg = JSON.parse(errText).message ?? errText;
|
|
101
|
+
} catch {
|
|
102
|
+
}
|
|
103
|
+
throw new LibreNmsClientError(res.status, msg);
|
|
104
|
+
} catch (e) {
|
|
105
|
+
if (e instanceof LibreNmsClientError) throw e;
|
|
106
|
+
lastErr = new LibreNmsUnreachableError(e.message);
|
|
107
|
+
if (attempt === 0) await sleep(this.retryDelayMs);
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
throw lastErr ?? new LibreNmsUnreachableError("unknown");
|
|
111
|
+
}
|
|
112
|
+
};
|
|
113
|
+
function sleep(ms) {
|
|
114
|
+
return new Promise((r) => setTimeout(r, ms));
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
// src/security.ts
|
|
118
|
+
var SECRETS = /* @__PURE__ */ new Set();
|
|
119
|
+
var BASE64_TOKEN_RE = /[A-Za-z0-9+/=]{12,}/g;
|
|
120
|
+
function registerSecret(s) {
|
|
121
|
+
if (s && s.length > 0) SECRETS.add(s);
|
|
122
|
+
}
|
|
123
|
+
function maskString(s) {
|
|
124
|
+
let out = s;
|
|
125
|
+
for (const secret of SECRETS) {
|
|
126
|
+
if (out.includes(secret)) out = out.split(secret).join("REDACTED");
|
|
127
|
+
}
|
|
128
|
+
out = out.replace(BASE64_TOKEN_RE, (token) => {
|
|
129
|
+
try {
|
|
130
|
+
const decoded = Buffer.from(token, "base64").toString("utf8");
|
|
131
|
+
for (const secret of SECRETS) {
|
|
132
|
+
if (decoded.includes(secret)) return "REDACTED";
|
|
133
|
+
}
|
|
134
|
+
} catch {
|
|
135
|
+
}
|
|
136
|
+
return token;
|
|
137
|
+
});
|
|
138
|
+
return out;
|
|
139
|
+
}
|
|
140
|
+
function redact(value) {
|
|
141
|
+
if (typeof value === "string") return maskString(value);
|
|
142
|
+
if (Array.isArray(value)) return value.map(redact);
|
|
143
|
+
if (value && typeof value === "object") {
|
|
144
|
+
const out = {};
|
|
145
|
+
for (const [k, v] of Object.entries(value)) out[k] = redact(v);
|
|
146
|
+
return out;
|
|
147
|
+
}
|
|
148
|
+
return value;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
// src/validate.ts
|
|
152
|
+
import { TypeCompiler } from "@sinclair/typebox/compiler";
|
|
153
|
+
var ValidationError = class extends Error {
|
|
154
|
+
constructor(message) {
|
|
155
|
+
super(message);
|
|
156
|
+
this.name = "ValidationError";
|
|
157
|
+
}
|
|
158
|
+
};
|
|
159
|
+
var compiledCache = /* @__PURE__ */ new WeakMap();
|
|
160
|
+
function getChecker(schema) {
|
|
161
|
+
let checker = compiledCache.get(schema);
|
|
162
|
+
if (!checker) {
|
|
163
|
+
checker = TypeCompiler.Compile(schema);
|
|
164
|
+
compiledCache.set(schema, checker);
|
|
165
|
+
}
|
|
166
|
+
return checker;
|
|
167
|
+
}
|
|
168
|
+
function validateToolArgs(schema, toolName, args) {
|
|
169
|
+
const checker = getChecker(schema);
|
|
170
|
+
if (checker.Check(args)) return;
|
|
171
|
+
const first = checker.Errors(args).First();
|
|
172
|
+
const where = first?.path ? ` at ${first.path}` : "";
|
|
173
|
+
const detail = first ? `${first.message}${where}` : "schema mismatch";
|
|
174
|
+
throw new ValidationError(
|
|
175
|
+
`Invalid arguments for ${toolName}: ${detail}`
|
|
176
|
+
);
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
// src/tools/librenms_status.ts
|
|
180
|
+
import { Type } from "@sinclair/typebox";
|
|
181
|
+
|
|
182
|
+
// src/tools/_util.ts
|
|
183
|
+
function jsonToolResult(payload) {
|
|
184
|
+
return {
|
|
185
|
+
content: [{ type: "text", text: JSON.stringify(payload, null, 2) }]
|
|
186
|
+
};
|
|
187
|
+
}
|
|
188
|
+
function safeInt(value, label, opts = {}) {
|
|
189
|
+
const n = typeof value === "number" ? value : typeof value === "string" ? Number(value) : NaN;
|
|
190
|
+
if (!Number.isInteger(n)) {
|
|
191
|
+
throw new Error(`${label} must be an integer, got: ${String(value)}`);
|
|
192
|
+
}
|
|
193
|
+
const min = opts.min ?? 1;
|
|
194
|
+
if (n < min) throw new Error(`${label} must be >= ${min}, got: ${n}`);
|
|
195
|
+
if (opts.max !== void 0 && n > opts.max) {
|
|
196
|
+
throw new Error(`${label} must be <= ${opts.max}, got: ${n}`);
|
|
197
|
+
}
|
|
198
|
+
return n;
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
// src/tools/librenms_status.ts
|
|
202
|
+
var Schema = Type.Object({}, { additionalProperties: false });
|
|
203
|
+
function createLibrenmsStatusTool(getClient) {
|
|
204
|
+
return {
|
|
205
|
+
name: "librenms_status",
|
|
206
|
+
label: "librenms: status",
|
|
207
|
+
description: "LibreNMS system health (version, totals, last poll) via GET /api/v0/system.",
|
|
208
|
+
parameters: Schema,
|
|
209
|
+
execute: async () => {
|
|
210
|
+
const client = getClient();
|
|
211
|
+
const r = await client.get("/system");
|
|
212
|
+
return jsonToolResult({ system: r.system?.[0] ?? null });
|
|
213
|
+
}
|
|
214
|
+
};
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
// src/tools/librenms_list_devices.ts
|
|
218
|
+
import { Type as Type2 } from "@sinclair/typebox";
|
|
219
|
+
var Schema2 = Type2.Object(
|
|
220
|
+
{
|
|
221
|
+
type: Type2.Optional(
|
|
222
|
+
Type2.Union(
|
|
223
|
+
[
|
|
224
|
+
Type2.Literal("all"),
|
|
225
|
+
Type2.Literal("up"),
|
|
226
|
+
Type2.Literal("down"),
|
|
227
|
+
Type2.Literal("ignored"),
|
|
228
|
+
Type2.Literal("disabled")
|
|
229
|
+
],
|
|
230
|
+
{ description: "Device filter type. Default 'all'." }
|
|
231
|
+
)
|
|
232
|
+
)
|
|
233
|
+
},
|
|
234
|
+
{ additionalProperties: false }
|
|
235
|
+
);
|
|
236
|
+
function createLibrenmsListDevicesTool(getClient) {
|
|
237
|
+
return {
|
|
238
|
+
name: "librenms_list_devices",
|
|
239
|
+
label: "librenms: list devices",
|
|
240
|
+
description: "List devices monitored by LibreNMS via GET /api/v0/devices. Optional type filter (all|up|down|ignored|disabled).",
|
|
241
|
+
parameters: Schema2,
|
|
242
|
+
execute: async (_id, raw) => {
|
|
243
|
+
const args = raw ?? {};
|
|
244
|
+
const path = args.type ? `/devices?type=${encodeURIComponent(args.type)}` : "/devices";
|
|
245
|
+
const client = getClient();
|
|
246
|
+
const r = await client.get(path);
|
|
247
|
+
return jsonToolResult({ devices: r.devices ?? [] });
|
|
248
|
+
}
|
|
249
|
+
};
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
// src/tools/librenms_get_device.ts
|
|
253
|
+
import { Type as Type3 } from "@sinclair/typebox";
|
|
254
|
+
var Schema3 = Type3.Object(
|
|
255
|
+
{
|
|
256
|
+
hostname: Type3.String({
|
|
257
|
+
description: "Device hostname or IP as configured in LibreNMS."
|
|
258
|
+
})
|
|
259
|
+
},
|
|
260
|
+
{ additionalProperties: false }
|
|
261
|
+
);
|
|
262
|
+
function createLibrenmsGetDeviceTool(getClient) {
|
|
263
|
+
return {
|
|
264
|
+
name: "librenms_get_device",
|
|
265
|
+
label: "librenms: get device",
|
|
266
|
+
description: "Fetch a single device by hostname via GET /api/v0/devices/{hostname}.",
|
|
267
|
+
parameters: Schema3,
|
|
268
|
+
execute: async (_id, raw) => {
|
|
269
|
+
const args = raw;
|
|
270
|
+
const client = getClient();
|
|
271
|
+
const r = await client.get(`/devices/${encodeURIComponent(args.hostname)}`);
|
|
272
|
+
return jsonToolResult({ device: r.devices?.[0] ?? null });
|
|
273
|
+
}
|
|
274
|
+
};
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
// src/tools/librenms_list_ports.ts
|
|
278
|
+
import { Type as Type4 } from "@sinclair/typebox";
|
|
279
|
+
var Schema4 = Type4.Object(
|
|
280
|
+
{
|
|
281
|
+
hostname: Type4.String({
|
|
282
|
+
description: "Device hostname or IP as configured in LibreNMS."
|
|
283
|
+
})
|
|
284
|
+
},
|
|
285
|
+
{ additionalProperties: false }
|
|
286
|
+
);
|
|
287
|
+
var COLUMNS = "ifName,ifAdminStatus,ifOperStatus,ifInErrors,ifOutErrors,ifSpeed,ifDescr";
|
|
288
|
+
function createLibrenmsListPortsTool(getClient) {
|
|
289
|
+
return {
|
|
290
|
+
name: "librenms_list_ports",
|
|
291
|
+
label: "librenms: list ports",
|
|
292
|
+
description: "List ports on a device via GET /api/v0/devices/{hostname}/ports with a fixed column set (name, status, errors, speed, descr).",
|
|
293
|
+
parameters: Schema4,
|
|
294
|
+
execute: async (_id, raw) => {
|
|
295
|
+
const args = raw;
|
|
296
|
+
const client = getClient();
|
|
297
|
+
const r = await client.get(`/devices/${encodeURIComponent(args.hostname)}/ports?columns=${COLUMNS}`);
|
|
298
|
+
return jsonToolResult({ ports: r.ports ?? [] });
|
|
299
|
+
}
|
|
300
|
+
};
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
// src/tools/librenms_port_health.ts
|
|
304
|
+
import { Type as Type5 } from "@sinclair/typebox";
|
|
305
|
+
var Schema5 = Type5.Object(
|
|
306
|
+
{
|
|
307
|
+
limit: Type5.Optional(
|
|
308
|
+
Type5.Integer({
|
|
309
|
+
minimum: 1,
|
|
310
|
+
description: "Number of top ports to return. Default 10."
|
|
311
|
+
})
|
|
312
|
+
),
|
|
313
|
+
metric: Type5.Optional(
|
|
314
|
+
Type5.Union(
|
|
315
|
+
[
|
|
316
|
+
Type5.Literal("errors_in"),
|
|
317
|
+
Type5.Literal("errors_out"),
|
|
318
|
+
Type5.Literal("utilization")
|
|
319
|
+
],
|
|
320
|
+
{ description: "Ranking metric. Default 'errors_in'." }
|
|
321
|
+
)
|
|
322
|
+
)
|
|
323
|
+
},
|
|
324
|
+
{ additionalProperties: false }
|
|
325
|
+
);
|
|
326
|
+
var COLUMNS2 = "device_id,ifName,ifInErrors,ifOutErrors,ifSpeed,ifInOctets,ifOutOctets";
|
|
327
|
+
function createLibrenmsPortHealthTool(getClient) {
|
|
328
|
+
return {
|
|
329
|
+
name: "librenms_port_health",
|
|
330
|
+
label: "librenms: port health",
|
|
331
|
+
description: "Rank ports by errors_in (default), errors_out, or utilization via GET /api/v0/ports with client-side sort.",
|
|
332
|
+
parameters: Schema5,
|
|
333
|
+
execute: async (_id, raw) => {
|
|
334
|
+
const args = raw ?? {};
|
|
335
|
+
const limit = args.limit ?? 10;
|
|
336
|
+
const metric = args.metric ?? "errors_in";
|
|
337
|
+
const client = getClient();
|
|
338
|
+
const r = await client.get(`/ports?columns=${COLUMNS2}`);
|
|
339
|
+
const ports = r.ports ?? [];
|
|
340
|
+
const sorted = ports.slice().sort((a, b) => {
|
|
341
|
+
if (metric === "errors_in") {
|
|
342
|
+
return (b.ifInErrors ?? 0) - (a.ifInErrors ?? 0);
|
|
343
|
+
}
|
|
344
|
+
if (metric === "errors_out") {
|
|
345
|
+
return (b.ifOutErrors ?? 0) - (a.ifOutErrors ?? 0);
|
|
346
|
+
}
|
|
347
|
+
const aUtil = a.ifSpeed ? ((a.ifInOctets ?? 0) + (a.ifOutOctets ?? 0)) / a.ifSpeed : 0;
|
|
348
|
+
const bUtil = b.ifSpeed ? ((b.ifInOctets ?? 0) + (b.ifOutOctets ?? 0)) / b.ifSpeed : 0;
|
|
349
|
+
return bUtil - aUtil;
|
|
350
|
+
});
|
|
351
|
+
return jsonToolResult({ metric, limit, top: sorted.slice(0, limit) });
|
|
352
|
+
}
|
|
353
|
+
};
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
// src/tools/librenms_list_alerts.ts
|
|
357
|
+
import { Type as Type6 } from "@sinclair/typebox";
|
|
358
|
+
var Schema6 = Type6.Object(
|
|
359
|
+
{
|
|
360
|
+
state: Type6.Optional(
|
|
361
|
+
Type6.Union(
|
|
362
|
+
[Type6.Literal(0), Type6.Literal(1), Type6.Literal(2)],
|
|
363
|
+
{ description: "Alert state filter: 0=ok, 1=active, 2=ack." }
|
|
364
|
+
)
|
|
365
|
+
)
|
|
366
|
+
},
|
|
367
|
+
{ additionalProperties: false }
|
|
368
|
+
);
|
|
369
|
+
function createLibrenmsListAlertsTool(getClient) {
|
|
370
|
+
return {
|
|
371
|
+
name: "librenms_list_alerts",
|
|
372
|
+
label: "librenms: list alerts",
|
|
373
|
+
description: "List alerts via GET /api/v0/alerts. Optional state filter (0=ok, 1=active, 2=ack).",
|
|
374
|
+
parameters: Schema6,
|
|
375
|
+
execute: async (_id, raw) => {
|
|
376
|
+
const args = raw ?? {};
|
|
377
|
+
const path = args.state !== void 0 ? `/alerts?state=${encodeURIComponent(args.state)}` : "/alerts";
|
|
378
|
+
const client = getClient();
|
|
379
|
+
const r = await client.get(path);
|
|
380
|
+
return jsonToolResult({ alerts: r.alerts ?? [] });
|
|
381
|
+
}
|
|
382
|
+
};
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
// src/tools/librenms_get_alert.ts
|
|
386
|
+
import { Type as Type7 } from "@sinclair/typebox";
|
|
387
|
+
var Schema7 = Type7.Object(
|
|
388
|
+
{
|
|
389
|
+
id: Type7.Integer({ minimum: 1, description: "Alert id." })
|
|
390
|
+
},
|
|
391
|
+
{ additionalProperties: false }
|
|
392
|
+
);
|
|
393
|
+
function createLibrenmsGetAlertTool(getClient) {
|
|
394
|
+
return {
|
|
395
|
+
name: "librenms_get_alert",
|
|
396
|
+
label: "librenms: get alert",
|
|
397
|
+
description: "Fetch a single alert by id via GET /api/v0/alerts/{id}.",
|
|
398
|
+
parameters: Schema7,
|
|
399
|
+
execute: async (_id, raw) => {
|
|
400
|
+
const args = raw;
|
|
401
|
+
const id = safeInt(args.id, "id");
|
|
402
|
+
const client = getClient();
|
|
403
|
+
const r = await client.get(`/alerts/${encodeURIComponent(id)}`);
|
|
404
|
+
return jsonToolResult({ alert: r.alerts?.[0] ?? null });
|
|
405
|
+
}
|
|
406
|
+
};
|
|
407
|
+
}
|
|
408
|
+
|
|
409
|
+
// src/tools/librenms_alert_history.ts
|
|
410
|
+
import { Type as Type8 } from "@sinclair/typebox";
|
|
411
|
+
var Schema8 = Type8.Object(
|
|
412
|
+
{
|
|
413
|
+
device_id: Type8.Optional(
|
|
414
|
+
Type8.Integer({
|
|
415
|
+
minimum: 1,
|
|
416
|
+
description: "Optional device id to scope the alert log."
|
|
417
|
+
})
|
|
418
|
+
),
|
|
419
|
+
limit: Type8.Optional(
|
|
420
|
+
Type8.Integer({
|
|
421
|
+
minimum: 1,
|
|
422
|
+
description: "Max number of log entries. Default 25."
|
|
423
|
+
})
|
|
424
|
+
)
|
|
425
|
+
},
|
|
426
|
+
{ additionalProperties: false }
|
|
427
|
+
);
|
|
428
|
+
function createLibrenmsAlertHistoryTool(getClient) {
|
|
429
|
+
return {
|
|
430
|
+
name: "librenms_alert_history",
|
|
431
|
+
label: "librenms: alert history",
|
|
432
|
+
description: "Recent alert log entries via GET /api/v0/logs/alertlog (optionally scoped to a device_id).",
|
|
433
|
+
parameters: Schema8,
|
|
434
|
+
execute: async (_id, raw) => {
|
|
435
|
+
const args = raw ?? {};
|
|
436
|
+
const limit = safeInt(args.limit ?? 25, "limit");
|
|
437
|
+
const path = args.device_id !== void 0 ? `/logs/alertlog/${encodeURIComponent(
|
|
438
|
+
safeInt(args.device_id, "device_id")
|
|
439
|
+
)}?limit=${encodeURIComponent(limit)}` : `/logs/alertlog?limit=${encodeURIComponent(limit)}`;
|
|
440
|
+
const client = getClient();
|
|
441
|
+
const r = await client.get(path);
|
|
442
|
+
return jsonToolResult({
|
|
443
|
+
count: r.logs?.length ?? 0,
|
|
444
|
+
logs: r.logs ?? []
|
|
445
|
+
});
|
|
446
|
+
}
|
|
447
|
+
};
|
|
448
|
+
}
|
|
449
|
+
|
|
450
|
+
// src/tools/librenms_ack_alert.ts
|
|
451
|
+
import { Type as Type9 } from "@sinclair/typebox";
|
|
452
|
+
|
|
453
|
+
// src/gates.ts
|
|
454
|
+
var WriteGateError = class extends Error {
|
|
455
|
+
constructor(message) {
|
|
456
|
+
super(message);
|
|
457
|
+
this.name = "WriteGateError";
|
|
458
|
+
}
|
|
459
|
+
};
|
|
460
|
+
function assertConfirmedWrite(args, toolName) {
|
|
461
|
+
if (args.confirm !== true) {
|
|
462
|
+
throw new WriteGateError(`${toolName} is a write operation. Pass {"confirm": true} to proceed.`);
|
|
463
|
+
}
|
|
464
|
+
}
|
|
465
|
+
|
|
466
|
+
// src/tools/librenms_ack_alert.ts
|
|
467
|
+
var Schema9 = Type9.Object(
|
|
468
|
+
{
|
|
469
|
+
id: Type9.Integer({ minimum: 1, description: "Alert id to acknowledge." }),
|
|
470
|
+
note: Type9.Optional(
|
|
471
|
+
Type9.String({ description: "Optional acknowledgement note." })
|
|
472
|
+
),
|
|
473
|
+
until_clear: Type9.Optional(
|
|
474
|
+
Type9.Boolean({
|
|
475
|
+
description: "When true, the ack persists until the alert clears (default LibreNMS behavior: ack until next state change)."
|
|
476
|
+
})
|
|
477
|
+
),
|
|
478
|
+
confirm: Type9.Boolean({
|
|
479
|
+
description: "Must be true to write. Tier-2 safe-write gate."
|
|
480
|
+
})
|
|
481
|
+
},
|
|
482
|
+
{ additionalProperties: false }
|
|
483
|
+
);
|
|
484
|
+
var NAME = "librenms_ack_alert";
|
|
485
|
+
function createLibrenmsAckAlertTool(getClient) {
|
|
486
|
+
return {
|
|
487
|
+
name: NAME,
|
|
488
|
+
label: "librenms: ack alert",
|
|
489
|
+
description: "Acknowledge an active alert by id via PUT /api/v0/alerts/{id}. Tier-2 write; requires confirm:true.",
|
|
490
|
+
parameters: Schema9,
|
|
491
|
+
execute: async (_id, raw) => {
|
|
492
|
+
assertConfirmedWrite(raw, NAME);
|
|
493
|
+
const args = raw;
|
|
494
|
+
const id = safeInt(args.id, "id");
|
|
495
|
+
const client = getClient();
|
|
496
|
+
const body = {};
|
|
497
|
+
if (args.note !== void 0) body.note = args.note;
|
|
498
|
+
if (args.until_clear !== void 0) body.until_clear = args.until_clear;
|
|
499
|
+
const r = await client.put(`/alerts/${encodeURIComponent(id)}`, body);
|
|
500
|
+
return jsonToolResult({ alert_id: id, acked: true, response: r });
|
|
501
|
+
}
|
|
502
|
+
};
|
|
503
|
+
}
|
|
504
|
+
|
|
505
|
+
// src/tools/librenms_set_maintenance.ts
|
|
506
|
+
import { Type as Type10 } from "@sinclair/typebox";
|
|
507
|
+
var DURATION_RE = /^\d+:\d{2}$/;
|
|
508
|
+
var START_RE = /^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$/;
|
|
509
|
+
var Schema10 = Type10.Object(
|
|
510
|
+
{
|
|
511
|
+
hostname: Type10.String({
|
|
512
|
+
description: "Device hostname or IP as configured in LibreNMS."
|
|
513
|
+
}),
|
|
514
|
+
duration: Type10.String({
|
|
515
|
+
description: "Maintenance duration. LibreNMS format `H:i`, e.g. `2:00` for 2 hours or `0:30` for 30 minutes."
|
|
516
|
+
}),
|
|
517
|
+
title: Type10.Optional(
|
|
518
|
+
Type10.String({ description: "Maintenance window title." })
|
|
519
|
+
),
|
|
520
|
+
notes: Type10.Optional(Type10.String({ description: "Free-text notes." })),
|
|
521
|
+
start: Type10.Optional(
|
|
522
|
+
Type10.String({
|
|
523
|
+
description: "Start time. LibreNMS format `Y-m-d H:i:00`, e.g. `2026-05-17 14:30:00`. Default: server now."
|
|
524
|
+
})
|
|
525
|
+
),
|
|
526
|
+
confirm: Type10.Boolean({
|
|
527
|
+
description: "Must be true to write. Tier-2 safe-write gate."
|
|
528
|
+
})
|
|
529
|
+
},
|
|
530
|
+
{ additionalProperties: false }
|
|
531
|
+
);
|
|
532
|
+
var NAME2 = "librenms_set_maintenance";
|
|
533
|
+
function createLibrenmsSetMaintenanceTool(getClient) {
|
|
534
|
+
return {
|
|
535
|
+
name: NAME2,
|
|
536
|
+
label: "librenms: set maintenance",
|
|
537
|
+
description: "Put a device into a maintenance window (suppresses alerts) via POST /api/v0/devices/{hostname}/maintenance. Tier-2 write; requires confirm:true.",
|
|
538
|
+
parameters: Schema10,
|
|
539
|
+
execute: async (_id, raw) => {
|
|
540
|
+
assertConfirmedWrite(raw, NAME2);
|
|
541
|
+
const args = raw;
|
|
542
|
+
if (!DURATION_RE.test(args.duration)) {
|
|
543
|
+
throw new Error(
|
|
544
|
+
`duration must be H:i format (e.g. "2:00" or "0:30"), got: ${args.duration}`
|
|
545
|
+
);
|
|
546
|
+
}
|
|
547
|
+
if (args.start && !START_RE.test(args.start)) {
|
|
548
|
+
throw new Error(
|
|
549
|
+
`start must be Y-m-d H:i:00 format (e.g. "2026-05-17 14:30:00"), got: ${args.start}`
|
|
550
|
+
);
|
|
551
|
+
}
|
|
552
|
+
const client = getClient();
|
|
553
|
+
const body = { duration: args.duration };
|
|
554
|
+
if (args.title) body.title = args.title;
|
|
555
|
+
if (args.notes) body.notes = args.notes;
|
|
556
|
+
if (args.start) body.start = args.start;
|
|
557
|
+
const r = await client.post(
|
|
558
|
+
`/devices/${encodeURIComponent(args.hostname)}/maintenance`,
|
|
559
|
+
body
|
|
560
|
+
);
|
|
561
|
+
return jsonToolResult({ hostname: args.hostname, maintenance: r });
|
|
562
|
+
}
|
|
563
|
+
};
|
|
564
|
+
}
|
|
565
|
+
|
|
566
|
+
// src/tools/librenms_get_port.ts
|
|
567
|
+
import { Type as Type11 } from "@sinclair/typebox";
|
|
568
|
+
var Schema11 = Type11.Object(
|
|
569
|
+
{
|
|
570
|
+
port_id: Type11.Integer({
|
|
571
|
+
minimum: 1,
|
|
572
|
+
description: "LibreNMS internal port id (from librenms_list_ports response rows)."
|
|
573
|
+
})
|
|
574
|
+
},
|
|
575
|
+
{ additionalProperties: false }
|
|
576
|
+
);
|
|
577
|
+
function createLibrenmsGetPortTool(getClient) {
|
|
578
|
+
return {
|
|
579
|
+
name: "librenms_get_port",
|
|
580
|
+
label: "librenms: get port",
|
|
581
|
+
description: "Single-port detail via GET /api/v0/ports/{port_id}. Returns full column set (admin/oper state, traffic + error counters, ifAlias, etc.).",
|
|
582
|
+
parameters: Schema11,
|
|
583
|
+
execute: async (_id, raw) => {
|
|
584
|
+
const args = raw;
|
|
585
|
+
const portId = safeInt(args.port_id, "port_id");
|
|
586
|
+
const client = getClient();
|
|
587
|
+
const r = await client.get(`/ports/${encodeURIComponent(portId)}`);
|
|
588
|
+
return jsonToolResult({ port: r.port?.[0] ?? null });
|
|
589
|
+
}
|
|
590
|
+
};
|
|
591
|
+
}
|
|
592
|
+
|
|
593
|
+
// src/tools/librenms_event_log.ts
|
|
594
|
+
import { Type as Type12 } from "@sinclair/typebox";
|
|
595
|
+
var Schema12 = Type12.Object(
|
|
596
|
+
{
|
|
597
|
+
device_id: Type12.Optional(
|
|
598
|
+
Type12.Integer({
|
|
599
|
+
minimum: 1,
|
|
600
|
+
description: "Optional device id to scope the event log."
|
|
601
|
+
})
|
|
602
|
+
),
|
|
603
|
+
limit: Type12.Optional(
|
|
604
|
+
Type12.Integer({
|
|
605
|
+
minimum: 1,
|
|
606
|
+
description: "Max number of log entries. Default 25."
|
|
607
|
+
})
|
|
608
|
+
)
|
|
609
|
+
},
|
|
610
|
+
{ additionalProperties: false }
|
|
611
|
+
);
|
|
612
|
+
function createLibrenmsEventLogTool(getClient) {
|
|
613
|
+
return {
|
|
614
|
+
name: "librenms_event_log",
|
|
615
|
+
label: "librenms: event log",
|
|
616
|
+
description: "Recent device events via GET /api/v0/logs/eventlog (optionally scoped to a device_id). Covers device up/down, syslog ingestion, etc. Distinct from alertlog.",
|
|
617
|
+
parameters: Schema12,
|
|
618
|
+
execute: async (_id, raw) => {
|
|
619
|
+
const args = raw ?? {};
|
|
620
|
+
const limit = safeInt(args.limit ?? 25, "limit");
|
|
621
|
+
const path = args.device_id !== void 0 ? `/logs/eventlog/${encodeURIComponent(
|
|
622
|
+
safeInt(args.device_id, "device_id")
|
|
623
|
+
)}?limit=${encodeURIComponent(limit)}` : `/logs/eventlog?limit=${encodeURIComponent(limit)}`;
|
|
624
|
+
const client = getClient();
|
|
625
|
+
const r = await client.get(path);
|
|
626
|
+
return jsonToolResult({
|
|
627
|
+
count: r.logs?.length ?? 0,
|
|
628
|
+
logs: r.logs ?? []
|
|
629
|
+
});
|
|
630
|
+
}
|
|
631
|
+
};
|
|
632
|
+
}
|
|
633
|
+
|
|
634
|
+
// src/tools/librenms_unmute_alert.ts
|
|
635
|
+
import { Type as Type13 } from "@sinclair/typebox";
|
|
636
|
+
var Schema13 = Type13.Object(
|
|
637
|
+
{
|
|
638
|
+
id: Type13.Integer({ minimum: 1, description: "Alert id to unmute." }),
|
|
639
|
+
note: Type13.Optional(
|
|
640
|
+
Type13.String({
|
|
641
|
+
description: "Optional note appended to the alert audit trail (LibreNMS prefixes it with the API user + timestamp)."
|
|
642
|
+
})
|
|
643
|
+
),
|
|
644
|
+
confirm: Type13.Boolean({
|
|
645
|
+
description: "Must be true to write. Tier-2 safe-write gate."
|
|
646
|
+
})
|
|
647
|
+
},
|
|
648
|
+
{ additionalProperties: false }
|
|
649
|
+
);
|
|
650
|
+
var NAME3 = "librenms_unmute_alert";
|
|
651
|
+
function createLibrenmsUnmuteAlertTool(getClient) {
|
|
652
|
+
return {
|
|
653
|
+
name: NAME3,
|
|
654
|
+
label: "librenms: unmute alert",
|
|
655
|
+
description: "Unmute an alert by id via PUT /api/v0/alerts/unmute/{id} (companion to librenms_ack_alert). Tier-2 write; requires confirm:true.",
|
|
656
|
+
parameters: Schema13,
|
|
657
|
+
execute: async (_id, raw) => {
|
|
658
|
+
assertConfirmedWrite(raw, NAME3);
|
|
659
|
+
const args = raw;
|
|
660
|
+
const id = safeInt(args.id, "id");
|
|
661
|
+
const client = getClient();
|
|
662
|
+
const body = {};
|
|
663
|
+
if (args.note !== void 0) body.note = args.note;
|
|
664
|
+
const r = await client.put(`/alerts/unmute/${encodeURIComponent(id)}`, body);
|
|
665
|
+
return jsonToolResult({ alert_id: id, unmuted: true, response: r });
|
|
666
|
+
}
|
|
667
|
+
};
|
|
668
|
+
}
|
|
669
|
+
|
|
670
|
+
// mcp-server.ts
|
|
671
|
+
function createServer() {
|
|
672
|
+
const cfg = resolveConfig(process.env);
|
|
673
|
+
registerSecret(cfg.token);
|
|
674
|
+
const getClient = () => new LibreNmsClient(cfg);
|
|
675
|
+
const tools = [
|
|
676
|
+
createLibrenmsStatusTool(getClient),
|
|
677
|
+
createLibrenmsListDevicesTool(getClient),
|
|
678
|
+
createLibrenmsGetDeviceTool(getClient),
|
|
679
|
+
createLibrenmsListPortsTool(getClient),
|
|
680
|
+
createLibrenmsPortHealthTool(getClient),
|
|
681
|
+
createLibrenmsListAlertsTool(getClient),
|
|
682
|
+
createLibrenmsGetAlertTool(getClient),
|
|
683
|
+
createLibrenmsAlertHistoryTool(getClient),
|
|
684
|
+
createLibrenmsAckAlertTool(getClient),
|
|
685
|
+
createLibrenmsSetMaintenanceTool(getClient),
|
|
686
|
+
createLibrenmsGetPortTool(getClient),
|
|
687
|
+
createLibrenmsEventLogTool(getClient),
|
|
688
|
+
createLibrenmsUnmuteAlertTool(getClient)
|
|
689
|
+
];
|
|
690
|
+
const toolMap = new Map(tools.map((t) => [t.name, t]));
|
|
691
|
+
const server = new Server({ name: "librenms-mcp", version: "0.2.0" }, { capabilities: { tools: {} } });
|
|
692
|
+
server.setRequestHandler(ListToolsRequestSchema, async () => ({
|
|
693
|
+
tools: tools.map((t) => ({ name: t.name, description: t.description, inputSchema: t.parameters }))
|
|
694
|
+
}));
|
|
695
|
+
server.setRequestHandler(CallToolRequestSchema, async (req) => {
|
|
696
|
+
const t = toolMap.get(req.params.name);
|
|
697
|
+
if (!t) {
|
|
698
|
+
return { content: [{ type: "text", text: JSON.stringify({ error: `unknown tool: ${req.params.name}` }) }], isError: true };
|
|
699
|
+
}
|
|
700
|
+
try {
|
|
701
|
+
const args = req.params.arguments ?? {};
|
|
702
|
+
validateToolArgs(t.parameters, t.name, args);
|
|
703
|
+
return await t.execute(req.params.name, args);
|
|
704
|
+
} catch (e) {
|
|
705
|
+
const msg = redact(e.message);
|
|
706
|
+
return { content: [{ type: "text", text: JSON.stringify({ error: msg }) }], isError: true };
|
|
707
|
+
}
|
|
708
|
+
});
|
|
709
|
+
return server;
|
|
710
|
+
}
|
|
711
|
+
async function serve() {
|
|
712
|
+
const server = createServer();
|
|
713
|
+
const transport = new StdioServerTransport();
|
|
714
|
+
const __send = transport.send.bind(transport);
|
|
715
|
+
transport.send = (message) => {
|
|
716
|
+
const tools = message?.result?.tools;
|
|
717
|
+
if (Array.isArray(tools)) {
|
|
718
|
+
for (const t of tools) {
|
|
719
|
+
if (t?.inputSchema) delete t.inputSchema.$schema;
|
|
720
|
+
if (t?.outputSchema) delete t.outputSchema.$schema;
|
|
721
|
+
}
|
|
722
|
+
}
|
|
723
|
+
return __send(message);
|
|
724
|
+
};
|
|
725
|
+
await server.connect(transport);
|
|
726
|
+
}
|
|
727
|
+
|
|
728
|
+
// mcp-bin.ts
|
|
729
|
+
serve().catch((error) => {
|
|
730
|
+
const msg = error instanceof Error ? error.message : String(error);
|
|
731
|
+
console.error(`librenms-mcp fatal: ${msg}`);
|
|
732
|
+
process.exit(1);
|
|
733
|
+
});
|