@solomonneas/librenms-mcp 0.1.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/dist/index.js CHANGED
@@ -65,6 +65,9 @@ var LibreNmsClient = class {
65
65
  async put(path, body) {
66
66
  return this.request("PUT", path, body);
67
67
  }
68
+ async delete(path) {
69
+ return this.request("DELETE", path);
70
+ }
68
71
  async request(method, path, body) {
69
72
  const url = this.cfg.url + "/api/v0" + path;
70
73
  const headers = { "x-auth-token": this.cfg.token };
@@ -143,6 +146,34 @@ function redact(value) {
143
146
  return value;
144
147
  }
145
148
 
149
+ // src/validate.ts
150
+ import { TypeCompiler } from "@sinclair/typebox/compiler";
151
+ var ValidationError = class extends Error {
152
+ constructor(message) {
153
+ super(message);
154
+ this.name = "ValidationError";
155
+ }
156
+ };
157
+ var compiledCache = /* @__PURE__ */ new WeakMap();
158
+ function getChecker(schema) {
159
+ let checker = compiledCache.get(schema);
160
+ if (!checker) {
161
+ checker = TypeCompiler.Compile(schema);
162
+ compiledCache.set(schema, checker);
163
+ }
164
+ return checker;
165
+ }
166
+ function validateToolArgs(schema, toolName, args) {
167
+ const checker = getChecker(schema);
168
+ if (checker.Check(args)) return;
169
+ const first = checker.Errors(args).First();
170
+ const where = first?.path ? ` at ${first.path}` : "";
171
+ const detail = first ? `${first.message}${where}` : "schema mismatch";
172
+ throw new ValidationError(
173
+ `Invalid arguments for ${toolName}: ${detail}`
174
+ );
175
+ }
176
+
146
177
  // src/tools/librenms_status.ts
147
178
  import { Type } from "@sinclair/typebox";
148
179
 
@@ -152,6 +183,18 @@ function jsonToolResult(payload) {
152
183
  content: [{ type: "text", text: JSON.stringify(payload, null, 2) }]
153
184
  };
154
185
  }
186
+ function safeInt(value, label, opts = {}) {
187
+ const n = typeof value === "number" ? value : typeof value === "string" ? Number(value) : NaN;
188
+ if (!Number.isInteger(n)) {
189
+ throw new Error(`${label} must be an integer, got: ${String(value)}`);
190
+ }
191
+ const min = opts.min ?? 1;
192
+ if (n < min) throw new Error(`${label} must be >= ${min}, got: ${n}`);
193
+ if (opts.max !== void 0 && n > opts.max) {
194
+ throw new Error(`${label} must be <= ${opts.max}, got: ${n}`);
195
+ }
196
+ return n;
197
+ }
155
198
 
156
199
  // src/tools/librenms_status.ts
157
200
  var Schema = Type.Object({}, { additionalProperties: false });
@@ -196,7 +239,7 @@ function createLibrenmsListDevicesTool(getClient) {
196
239
  parameters: Schema2,
197
240
  execute: async (_id, raw) => {
198
241
  const args = raw ?? {};
199
- const path = args.type ? `/devices?type=${args.type}` : "/devices";
242
+ const path = args.type ? `/devices?type=${encodeURIComponent(args.type)}` : "/devices";
200
243
  const client = getClient();
201
244
  const r = await client.get(path);
202
245
  return jsonToolResult({ devices: r.devices ?? [] });
@@ -329,7 +372,7 @@ function createLibrenmsListAlertsTool(getClient) {
329
372
  parameters: Schema6,
330
373
  execute: async (_id, raw) => {
331
374
  const args = raw ?? {};
332
- const path = args.state !== void 0 ? `/alerts?state=${args.state}` : "/alerts";
375
+ const path = args.state !== void 0 ? `/alerts?state=${encodeURIComponent(args.state)}` : "/alerts";
333
376
  const client = getClient();
334
377
  const r = await client.get(path);
335
378
  return jsonToolResult({ alerts: r.alerts ?? [] });
@@ -353,8 +396,9 @@ function createLibrenmsGetAlertTool(getClient) {
353
396
  parameters: Schema7,
354
397
  execute: async (_id, raw) => {
355
398
  const args = raw;
399
+ const id = safeInt(args.id, "id");
356
400
  const client = getClient();
357
- const r = await client.get(`/alerts/${args.id}`);
401
+ const r = await client.get(`/alerts/${encodeURIComponent(id)}`);
358
402
  return jsonToolResult({ alert: r.alerts?.[0] ?? null });
359
403
  }
360
404
  };
@@ -387,8 +431,10 @@ function createLibrenmsAlertHistoryTool(getClient) {
387
431
  parameters: Schema8,
388
432
  execute: async (_id, raw) => {
389
433
  const args = raw ?? {};
390
- const limit = args.limit ?? 25;
391
- const path = args.device_id ? `/logs/alertlog/${args.device_id}?limit=${limit}` : `/logs/alertlog?limit=${limit}`;
434
+ const limit = safeInt(args.limit ?? 25, "limit");
435
+ const path = args.device_id !== void 0 ? `/logs/alertlog/${encodeURIComponent(
436
+ safeInt(args.device_id, "device_id")
437
+ )}?limit=${encodeURIComponent(limit)}` : `/logs/alertlog?limit=${encodeURIComponent(limit)}`;
392
438
  const client = getClient();
393
439
  const r = await client.get(path);
394
440
  return jsonToolResult({
@@ -443,12 +489,13 @@ function createLibrenmsAckAlertTool(getClient) {
443
489
  execute: async (_id, raw) => {
444
490
  assertConfirmedWrite(raw, NAME);
445
491
  const args = raw;
492
+ const id = safeInt(args.id, "id");
446
493
  const client = getClient();
447
494
  const body = {};
448
495
  if (args.note !== void 0) body.note = args.note;
449
496
  if (args.until_clear !== void 0) body.until_clear = args.until_clear;
450
- const r = await client.put(`/alerts/${args.id}`, body);
451
- return jsonToolResult({ alert_id: args.id, acked: true, response: r });
497
+ const r = await client.put(`/alerts/${encodeURIComponent(id)}`, body);
498
+ return jsonToolResult({ alert_id: id, acked: true, response: r });
452
499
  }
453
500
  };
454
501
  }
@@ -514,6 +561,110 @@ function createLibrenmsSetMaintenanceTool(getClient) {
514
561
  };
515
562
  }
516
563
 
564
+ // src/tools/librenms_get_port.ts
565
+ import { Type as Type11 } from "@sinclair/typebox";
566
+ var Schema11 = Type11.Object(
567
+ {
568
+ port_id: Type11.Integer({
569
+ minimum: 1,
570
+ description: "LibreNMS internal port id (from librenms_list_ports response rows)."
571
+ })
572
+ },
573
+ { additionalProperties: false }
574
+ );
575
+ function createLibrenmsGetPortTool(getClient) {
576
+ return {
577
+ name: "librenms_get_port",
578
+ label: "librenms: get port",
579
+ description: "Single-port detail via GET /api/v0/ports/{port_id}. Returns full column set (admin/oper state, traffic + error counters, ifAlias, etc.).",
580
+ parameters: Schema11,
581
+ execute: async (_id, raw) => {
582
+ const args = raw;
583
+ const portId = safeInt(args.port_id, "port_id");
584
+ const client = getClient();
585
+ const r = await client.get(`/ports/${encodeURIComponent(portId)}`);
586
+ return jsonToolResult({ port: r.port?.[0] ?? null });
587
+ }
588
+ };
589
+ }
590
+
591
+ // src/tools/librenms_event_log.ts
592
+ import { Type as Type12 } from "@sinclair/typebox";
593
+ var Schema12 = Type12.Object(
594
+ {
595
+ device_id: Type12.Optional(
596
+ Type12.Integer({
597
+ minimum: 1,
598
+ description: "Optional device id to scope the event log."
599
+ })
600
+ ),
601
+ limit: Type12.Optional(
602
+ Type12.Integer({
603
+ minimum: 1,
604
+ description: "Max number of log entries. Default 25."
605
+ })
606
+ )
607
+ },
608
+ { additionalProperties: false }
609
+ );
610
+ function createLibrenmsEventLogTool(getClient) {
611
+ return {
612
+ name: "librenms_event_log",
613
+ label: "librenms: event log",
614
+ 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.",
615
+ parameters: Schema12,
616
+ execute: async (_id, raw) => {
617
+ const args = raw ?? {};
618
+ const limit = safeInt(args.limit ?? 25, "limit");
619
+ const path = args.device_id !== void 0 ? `/logs/eventlog/${encodeURIComponent(
620
+ safeInt(args.device_id, "device_id")
621
+ )}?limit=${encodeURIComponent(limit)}` : `/logs/eventlog?limit=${encodeURIComponent(limit)}`;
622
+ const client = getClient();
623
+ const r = await client.get(path);
624
+ return jsonToolResult({
625
+ count: r.logs?.length ?? 0,
626
+ logs: r.logs ?? []
627
+ });
628
+ }
629
+ };
630
+ }
631
+
632
+ // src/tools/librenms_unmute_alert.ts
633
+ import { Type as Type13 } from "@sinclair/typebox";
634
+ var Schema13 = Type13.Object(
635
+ {
636
+ id: Type13.Integer({ minimum: 1, description: "Alert id to unmute." }),
637
+ note: Type13.Optional(
638
+ Type13.String({
639
+ description: "Optional note appended to the alert audit trail (LibreNMS prefixes it with the API user + timestamp)."
640
+ })
641
+ ),
642
+ confirm: Type13.Boolean({
643
+ description: "Must be true to write. Tier-2 safe-write gate."
644
+ })
645
+ },
646
+ { additionalProperties: false }
647
+ );
648
+ var NAME3 = "librenms_unmute_alert";
649
+ function createLibrenmsUnmuteAlertTool(getClient) {
650
+ return {
651
+ name: NAME3,
652
+ label: "librenms: unmute alert",
653
+ description: "Unmute an alert by id via PUT /api/v0/alerts/unmute/{id} (companion to librenms_ack_alert). Tier-2 write; requires confirm:true.",
654
+ parameters: Schema13,
655
+ execute: async (_id, raw) => {
656
+ assertConfirmedWrite(raw, NAME3);
657
+ const args = raw;
658
+ const id = safeInt(args.id, "id");
659
+ const client = getClient();
660
+ const body = {};
661
+ if (args.note !== void 0) body.note = args.note;
662
+ const r = await client.put(`/alerts/unmute/${encodeURIComponent(id)}`, body);
663
+ return jsonToolResult({ alert_id: id, unmuted: true, response: r });
664
+ }
665
+ };
666
+ }
667
+
517
668
  // index.ts
518
669
  function withRedactedErrors(tool) {
519
670
  const orig = tool.execute.bind(tool);
@@ -521,6 +672,7 @@ function withRedactedErrors(tool) {
521
672
  ...tool,
522
673
  execute: async (id, args) => {
523
674
  try {
675
+ validateToolArgs(tool.parameters, tool.name, args ?? {});
524
676
  return await orig(id, args);
525
677
  } catch (e) {
526
678
  const msg = redact(e.message);
@@ -552,6 +704,9 @@ var index_default = definePluginEntry({
552
704
  register(createLibrenmsAlertHistoryTool(getClient));
553
705
  register(createLibrenmsAckAlertTool(getClient));
554
706
  register(createLibrenmsSetMaintenanceTool(getClient));
707
+ register(createLibrenmsGetPortTool(getClient));
708
+ register(createLibrenmsEventLogTool(getClient));
709
+ register(createLibrenmsUnmuteAlertTool(getClient));
555
710
  }
556
711
  });
557
712
  export {