@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/dist/index.js CHANGED
@@ -146,6 +146,34 @@ function redact(value) {
146
146
  return value;
147
147
  }
148
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
+
149
177
  // src/tools/librenms_status.ts
150
178
  import { Type } from "@sinclair/typebox";
151
179
 
@@ -155,6 +183,18 @@ function jsonToolResult(payload) {
155
183
  content: [{ type: "text", text: JSON.stringify(payload, null, 2) }]
156
184
  };
157
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
+ }
158
198
 
159
199
  // src/tools/librenms_status.ts
160
200
  var Schema = Type.Object({}, { additionalProperties: false });
@@ -199,7 +239,7 @@ function createLibrenmsListDevicesTool(getClient) {
199
239
  parameters: Schema2,
200
240
  execute: async (_id, raw) => {
201
241
  const args = raw ?? {};
202
- const path = args.type ? `/devices?type=${args.type}` : "/devices";
242
+ const path = args.type ? `/devices?type=${encodeURIComponent(args.type)}` : "/devices";
203
243
  const client = getClient();
204
244
  const r = await client.get(path);
205
245
  return jsonToolResult({ devices: r.devices ?? [] });
@@ -332,7 +372,7 @@ function createLibrenmsListAlertsTool(getClient) {
332
372
  parameters: Schema6,
333
373
  execute: async (_id, raw) => {
334
374
  const args = raw ?? {};
335
- const path = args.state !== void 0 ? `/alerts?state=${args.state}` : "/alerts";
375
+ const path = args.state !== void 0 ? `/alerts?state=${encodeURIComponent(args.state)}` : "/alerts";
336
376
  const client = getClient();
337
377
  const r = await client.get(path);
338
378
  return jsonToolResult({ alerts: r.alerts ?? [] });
@@ -356,8 +396,9 @@ function createLibrenmsGetAlertTool(getClient) {
356
396
  parameters: Schema7,
357
397
  execute: async (_id, raw) => {
358
398
  const args = raw;
399
+ const id = safeInt(args.id, "id");
359
400
  const client = getClient();
360
- const r = await client.get(`/alerts/${args.id}`);
401
+ const r = await client.get(`/alerts/${encodeURIComponent(id)}`);
361
402
  return jsonToolResult({ alert: r.alerts?.[0] ?? null });
362
403
  }
363
404
  };
@@ -390,8 +431,10 @@ function createLibrenmsAlertHistoryTool(getClient) {
390
431
  parameters: Schema8,
391
432
  execute: async (_id, raw) => {
392
433
  const args = raw ?? {};
393
- const limit = args.limit ?? 25;
394
- 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)}`;
395
438
  const client = getClient();
396
439
  const r = await client.get(path);
397
440
  return jsonToolResult({
@@ -446,12 +489,13 @@ function createLibrenmsAckAlertTool(getClient) {
446
489
  execute: async (_id, raw) => {
447
490
  assertConfirmedWrite(raw, NAME);
448
491
  const args = raw;
492
+ const id = safeInt(args.id, "id");
449
493
  const client = getClient();
450
494
  const body = {};
451
495
  if (args.note !== void 0) body.note = args.note;
452
496
  if (args.until_clear !== void 0) body.until_clear = args.until_clear;
453
- const r = await client.put(`/alerts/${args.id}`, body);
454
- 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 });
455
499
  }
456
500
  };
457
501
  }
@@ -536,8 +580,9 @@ function createLibrenmsGetPortTool(getClient) {
536
580
  parameters: Schema11,
537
581
  execute: async (_id, raw) => {
538
582
  const args = raw;
583
+ const portId = safeInt(args.port_id, "port_id");
539
584
  const client = getClient();
540
- const r = await client.get(`/ports/${args.port_id}`);
585
+ const r = await client.get(`/ports/${encodeURIComponent(portId)}`);
541
586
  return jsonToolResult({ port: r.port?.[0] ?? null });
542
587
  }
543
588
  };
@@ -570,8 +615,10 @@ function createLibrenmsEventLogTool(getClient) {
570
615
  parameters: Schema12,
571
616
  execute: async (_id, raw) => {
572
617
  const args = raw ?? {};
573
- const limit = args.limit ?? 25;
574
- const path = args.device_id ? `/logs/eventlog/${args.device_id}?limit=${limit}` : `/logs/eventlog?limit=${limit}`;
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)}`;
575
622
  const client = getClient();
576
623
  const r = await client.get(path);
577
624
  return jsonToolResult({
@@ -608,11 +655,12 @@ function createLibrenmsUnmuteAlertTool(getClient) {
608
655
  execute: async (_id, raw) => {
609
656
  assertConfirmedWrite(raw, NAME3);
610
657
  const args = raw;
658
+ const id = safeInt(args.id, "id");
611
659
  const client = getClient();
612
660
  const body = {};
613
661
  if (args.note !== void 0) body.note = args.note;
614
- const r = await client.put(`/alerts/unmute/${args.id}`, body);
615
- return jsonToolResult({ alert_id: args.id, unmuted: true, response: r });
662
+ const r = await client.put(`/alerts/unmute/${encodeURIComponent(id)}`, body);
663
+ return jsonToolResult({ alert_id: id, unmuted: true, response: r });
616
664
  }
617
665
  };
618
666
  }
@@ -624,6 +672,7 @@ function withRedactedErrors(tool) {
624
672
  ...tool,
625
673
  execute: async (id, args) => {
626
674
  try {
675
+ validateToolArgs(tool.parameters, tool.name, args ?? {});
627
676
  return await orig(id, args);
628
677
  } catch (e) {
629
678
  const msg = redact(e.message);