@solomonneas/librenms-mcp 0.2.1 → 0.2.2
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 +14 -1
- package/dist/cli.js +532 -56
- package/dist/index.js +525 -51
- package/dist/mcp-bin.js +525 -52
- package/dist/mcp-server.js +526 -52
- package/openclaw.plugin.json +12 -4
- package/package.json +7 -9
package/dist/cli.js
CHANGED
|
@@ -5,37 +5,461 @@ import { realpathSync } from "fs";
|
|
|
5
5
|
import { pathToFileURL } from "url";
|
|
6
6
|
|
|
7
7
|
// src/config.ts
|
|
8
|
-
|
|
8
|
+
import { Effect as Effect6 } from "effect";
|
|
9
|
+
|
|
10
|
+
// node_modules/@lidless-labs/effect-operator-kit/dist/index.js
|
|
11
|
+
import { Data } from "effect";
|
|
12
|
+
import { Effect } from "effect";
|
|
13
|
+
import { Effect as Effect2 } from "effect";
|
|
14
|
+
import { Duration, Effect as Effect3, Schedule } from "effect";
|
|
15
|
+
import { Cause, Effect as Effect4, Exit } from "effect";
|
|
16
|
+
import { Cause as Cause2, Effect as Effect5 } from "effect";
|
|
17
|
+
var ConfigError = class extends Data.TaggedError("ConfigError") {
|
|
18
|
+
};
|
|
19
|
+
var AuthError = class extends Data.TaggedError("AuthError") {
|
|
20
|
+
};
|
|
21
|
+
var ForbiddenError = class extends Data.TaggedError("ForbiddenError") {
|
|
22
|
+
};
|
|
23
|
+
var NotFoundError = class extends Data.TaggedError("NotFoundError") {
|
|
24
|
+
};
|
|
25
|
+
var TimeoutError = class extends Data.TaggedError("TimeoutError") {
|
|
26
|
+
};
|
|
27
|
+
var TransportError = class extends Data.TaggedError("TransportError") {
|
|
28
|
+
};
|
|
29
|
+
var ParseError = class extends Data.TaggedError("ParseError") {
|
|
30
|
+
};
|
|
31
|
+
var UnexpectedStatusError = class extends Data.TaggedError("UnexpectedStatusError") {
|
|
32
|
+
};
|
|
33
|
+
var fromProcessEnv = (env) => ({
|
|
34
|
+
get: (key) => (env ?? process.env)[key]
|
|
35
|
+
});
|
|
36
|
+
var isBlank = (raw) => raw === void 0 || raw.trim() === "";
|
|
37
|
+
var failConfig = (partial) => Effect.fail(new ConfigError(partial));
|
|
38
|
+
var readRaw = (env, key) => env.get(key);
|
|
39
|
+
var TRUE_TOKENS = /* @__PURE__ */ new Set(["true", "1", "yes", "on"]);
|
|
40
|
+
var FALSE_TOKENS = /* @__PURE__ */ new Set(["false", "0", "no", "off"]);
|
|
41
|
+
var parseBooleanEnv = (env, key, fallback) => {
|
|
42
|
+
const raw = readRaw(env, key);
|
|
43
|
+
if (isBlank(raw)) {
|
|
44
|
+
return Effect.succeed(fallback);
|
|
45
|
+
}
|
|
46
|
+
const token = raw.trim().toLowerCase();
|
|
47
|
+
if (TRUE_TOKENS.has(token)) {
|
|
48
|
+
return Effect.succeed(true);
|
|
49
|
+
}
|
|
50
|
+
if (FALSE_TOKENS.has(token)) {
|
|
51
|
+
return Effect.succeed(false);
|
|
52
|
+
}
|
|
53
|
+
return failConfig({
|
|
54
|
+
key,
|
|
55
|
+
value: raw,
|
|
56
|
+
message: `${key} must be a boolean (true/false, 1/0, yes/no, on/off)`
|
|
57
|
+
});
|
|
58
|
+
};
|
|
59
|
+
var parseTimeoutEnv = (env, key, opts) => {
|
|
60
|
+
const raw = readRaw(env, key);
|
|
61
|
+
if (isBlank(raw)) {
|
|
62
|
+
return Effect.succeed(opts.fallbackMs);
|
|
63
|
+
}
|
|
64
|
+
const parsed = Number(raw.trim());
|
|
65
|
+
if (!Number.isFinite(parsed) || parsed <= 0) {
|
|
66
|
+
return failConfig({
|
|
67
|
+
key,
|
|
68
|
+
value: raw,
|
|
69
|
+
message: `${key} must be a positive timeout`
|
|
70
|
+
});
|
|
71
|
+
}
|
|
72
|
+
const unit = opts.unit ?? "s";
|
|
73
|
+
const ms = unit === "ms" ? parsed : parsed * 1e3;
|
|
74
|
+
if (opts.minMs !== void 0 && ms < opts.minMs) {
|
|
75
|
+
return failConfig({
|
|
76
|
+
key,
|
|
77
|
+
value: raw,
|
|
78
|
+
message: `${key} must be at least ${opts.minMs}ms`
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
if (opts.maxMs !== void 0 && ms > opts.maxMs) {
|
|
82
|
+
return failConfig({
|
|
83
|
+
key,
|
|
84
|
+
value: raw,
|
|
85
|
+
message: `${key} must be at most ${opts.maxMs}ms`
|
|
86
|
+
});
|
|
87
|
+
}
|
|
88
|
+
return Effect.succeed(ms);
|
|
89
|
+
};
|
|
90
|
+
var SECRET_PATTERNS = [
|
|
91
|
+
/(authorization:\s*)(basic|bearer)\s+[a-z0-9._~+/=-]+/gi,
|
|
92
|
+
/\b(basic|bearer)\s+[a-z0-9._~+/=-]+/gi,
|
|
93
|
+
/(authorization:\s*)(?:token|digest|hawk|oauth|apikey)\s+\S+/gi,
|
|
94
|
+
/(x-n8n-api-key|x-auth-token|x-emby-token|x-api-key)\s*:\s*\S+/gi,
|
|
95
|
+
/(https?:\/\/)([^@\s/]+)@/gi,
|
|
96
|
+
/([?&](?:access_token|refresh_token|client_secret|id_token|x-api-key|\w+_(?:token|secret)|password|token|secret|api[_-]?key|key)=)[^&\s]+/gi,
|
|
97
|
+
/\beyJ[a-zA-Z0-9_-]+\.[a-zA-Z0-9_-]+\.[a-zA-Z0-9_-]+\b/g
|
|
98
|
+
];
|
|
99
|
+
var MAX_LENGTH = 500;
|
|
100
|
+
var redactString = (message, secrets = []) => {
|
|
101
|
+
let sanitized = message;
|
|
102
|
+
for (const secret of secrets) {
|
|
103
|
+
if (secret) {
|
|
104
|
+
sanitized = sanitized.split(secret).join("[REDACTED]");
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
sanitized = sanitized.replace(SECRET_PATTERNS[0], "$1$2 [REDACTED]").replace(SECRET_PATTERNS[1], "$1 [REDACTED]").replace(SECRET_PATTERNS[2], "$1[REDACTED]").replace(SECRET_PATTERNS[3], "$1: [REDACTED]").replace(SECRET_PATTERNS[4], "$1[REDACTED]@").replace(SECRET_PATTERNS[5], "$1[REDACTED]").replace(SECRET_PATTERNS[6], "[REDACTED]");
|
|
108
|
+
return sanitized.length > MAX_LENGTH ? `${sanitized.slice(0, MAX_LENGTH)}...` : sanitized;
|
|
109
|
+
};
|
|
110
|
+
var defaultRedact = (value) => redactString(value);
|
|
111
|
+
var DEFAULT_SUCCESS_STATUSES = Array.from({ length: 100 }, (_, i) => 200 + i);
|
|
112
|
+
function redactBody(text, redact2) {
|
|
113
|
+
return (redact2 ?? defaultRedact)(text);
|
|
114
|
+
}
|
|
115
|
+
function mergeHeaders(...sources) {
|
|
116
|
+
const merged = new Headers();
|
|
117
|
+
for (const source of sources) {
|
|
118
|
+
if (!source) continue;
|
|
119
|
+
const headers = new Headers(source);
|
|
120
|
+
headers.forEach((value, key) => merged.set(key, value));
|
|
121
|
+
}
|
|
122
|
+
return merged;
|
|
123
|
+
}
|
|
124
|
+
function resolveExpectedStatuses(req) {
|
|
125
|
+
return req.expectedStatuses ?? DEFAULT_SUCCESS_STATUSES;
|
|
126
|
+
}
|
|
127
|
+
function isExpectedStatus(status, expected) {
|
|
128
|
+
return expected.includes(status);
|
|
129
|
+
}
|
|
130
|
+
function resolveBodyEncoding(req) {
|
|
131
|
+
if (req.bodyEncoding !== void 0) return req.bodyEncoding;
|
|
132
|
+
return req.body !== void 0 ? "json" : "none";
|
|
133
|
+
}
|
|
134
|
+
function setContentTypeIfAbsent(headers, contentType) {
|
|
135
|
+
if (!headers.has("content-type")) {
|
|
136
|
+
headers.set("content-type", contentType);
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
function buildRequestBody(req, headers) {
|
|
140
|
+
const encoding = resolveBodyEncoding(req);
|
|
141
|
+
switch (encoding) {
|
|
142
|
+
case "none":
|
|
143
|
+
return void 0;
|
|
144
|
+
case "json":
|
|
145
|
+
setContentTypeIfAbsent(headers, "application/json");
|
|
146
|
+
return JSON.stringify(req.body);
|
|
147
|
+
case "text":
|
|
148
|
+
setContentTypeIfAbsent(headers, "text/plain");
|
|
149
|
+
return String(req.body);
|
|
150
|
+
case "form": {
|
|
151
|
+
setContentTypeIfAbsent(
|
|
152
|
+
headers,
|
|
153
|
+
"application/x-www-form-urlencoded"
|
|
154
|
+
);
|
|
155
|
+
const params = new URLSearchParams();
|
|
156
|
+
if (req.body && typeof req.body === "object") {
|
|
157
|
+
for (const [key, value] of Object.entries(
|
|
158
|
+
req.body
|
|
159
|
+
)) {
|
|
160
|
+
params.set(key, String(value));
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
return params.toString();
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
function mapUnexpectedStatus(req, status, bodyText, expectedStatuses, redact2) {
|
|
168
|
+
const redacted = redactBody(bodyText, redact2);
|
|
169
|
+
const mapperInput = {
|
|
170
|
+
status,
|
|
171
|
+
method: req.method,
|
|
172
|
+
path: req.path,
|
|
173
|
+
bodyText: redacted,
|
|
174
|
+
expectedStatuses
|
|
175
|
+
};
|
|
176
|
+
if (req.statusMapper) {
|
|
177
|
+
const custom = req.statusMapper(mapperInput);
|
|
178
|
+
if (custom !== null) return custom;
|
|
179
|
+
}
|
|
180
|
+
const base = {
|
|
181
|
+
method: req.method,
|
|
182
|
+
path: req.path,
|
|
183
|
+
status,
|
|
184
|
+
body: redacted
|
|
185
|
+
};
|
|
186
|
+
switch (status) {
|
|
187
|
+
case 401:
|
|
188
|
+
return new AuthError(base);
|
|
189
|
+
case 403:
|
|
190
|
+
return new ForbiddenError(base);
|
|
191
|
+
case 404:
|
|
192
|
+
return new NotFoundError(base);
|
|
193
|
+
default:
|
|
194
|
+
return new UnexpectedStatusError({
|
|
195
|
+
method: req.method,
|
|
196
|
+
path: req.path,
|
|
197
|
+
status,
|
|
198
|
+
body: redacted,
|
|
199
|
+
expected: expectedStatuses
|
|
200
|
+
});
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
function parseResponseBody(req, bodyText, redact2) {
|
|
204
|
+
const responseType = req.responseType ?? "json";
|
|
205
|
+
if (responseType === "none") {
|
|
206
|
+
return Effect2.succeed(void 0);
|
|
207
|
+
}
|
|
208
|
+
if (responseType === "text") {
|
|
209
|
+
return Effect2.succeed(bodyText);
|
|
210
|
+
}
|
|
211
|
+
if (bodyText.length === 0) {
|
|
212
|
+
return Effect2.succeed(void 0);
|
|
213
|
+
}
|
|
214
|
+
return Effect2.try({
|
|
215
|
+
try: () => JSON.parse(bodyText),
|
|
216
|
+
catch: (cause) => new ParseError({
|
|
217
|
+
path: req.path,
|
|
218
|
+
body: redactBody(bodyText, redact2),
|
|
219
|
+
message: "Failed to parse JSON response",
|
|
220
|
+
cause
|
|
221
|
+
})
|
|
222
|
+
});
|
|
223
|
+
}
|
|
224
|
+
function fetchWithTimeout(ctx, url, init, timeoutMs, method, path) {
|
|
225
|
+
const fetchImpl = ctx.fetch ?? globalThis.fetch;
|
|
226
|
+
return Effect2.async((resume) => {
|
|
227
|
+
const controller = new AbortController();
|
|
228
|
+
const timeoutId = setTimeout(() => controller.abort(), timeoutMs);
|
|
229
|
+
const finish = (effect) => {
|
|
230
|
+
clearTimeout(timeoutId);
|
|
231
|
+
resume(effect);
|
|
232
|
+
};
|
|
233
|
+
void fetchImpl(url, { ...init, signal: controller.signal }).then((response) => finish(Effect2.succeed(response))).catch((cause) => {
|
|
234
|
+
if (cause instanceof Error && cause.name === "AbortError") {
|
|
235
|
+
finish(
|
|
236
|
+
Effect2.fail(new TimeoutError({ method, path, timeoutMs }))
|
|
237
|
+
);
|
|
238
|
+
return;
|
|
239
|
+
}
|
|
240
|
+
finish(
|
|
241
|
+
Effect2.fail(new TransportError({ method, path, cause }))
|
|
242
|
+
);
|
|
243
|
+
});
|
|
244
|
+
});
|
|
245
|
+
}
|
|
246
|
+
var buildUrl = (baseUrl, path, query) => {
|
|
247
|
+
if (path.includes("\\")) {
|
|
248
|
+
throw new TypeError("buildUrl: path must not contain backslash");
|
|
249
|
+
}
|
|
250
|
+
const url = new URL(path, baseUrl);
|
|
251
|
+
if (url.username || url.password) {
|
|
252
|
+
throw new TypeError("buildUrl: path must not contain URL userinfo");
|
|
253
|
+
}
|
|
254
|
+
if (url.origin !== baseUrl.origin) {
|
|
255
|
+
throw new TypeError(
|
|
256
|
+
`buildUrl: path "${path}" resolves to a different origin (${url.origin}) than baseUrl (${baseUrl.origin})`
|
|
257
|
+
);
|
|
258
|
+
}
|
|
259
|
+
if (query) {
|
|
260
|
+
for (const [key, value] of Object.entries(query)) {
|
|
261
|
+
if (value === null || value === void 0) continue;
|
|
262
|
+
if (Array.isArray(value)) {
|
|
263
|
+
for (const item of value) {
|
|
264
|
+
if (item === null || item === void 0) continue;
|
|
265
|
+
url.searchParams.append(key, String(item));
|
|
266
|
+
}
|
|
267
|
+
} else {
|
|
268
|
+
url.searchParams.set(key, String(value));
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
return url;
|
|
273
|
+
};
|
|
274
|
+
function buildHeaders(ctx, req) {
|
|
275
|
+
const auth = ctx.auth;
|
|
276
|
+
if (!auth) {
|
|
277
|
+
return Effect2.succeed(mergeHeaders(ctx.defaultHeaders, req.headers));
|
|
278
|
+
}
|
|
279
|
+
return Effect2.gen(function* () {
|
|
280
|
+
let headers = mergeHeaders(ctx.defaultHeaders);
|
|
281
|
+
headers = yield* auth.apply(headers);
|
|
282
|
+
return mergeHeaders(headers, req.headers);
|
|
283
|
+
});
|
|
284
|
+
}
|
|
285
|
+
var sendRequest = (ctx, req) => {
|
|
286
|
+
const url = buildUrl(ctx.baseUrl, req.path, req.query);
|
|
287
|
+
const timeoutMs = req.timeoutMs ?? ctx.timeoutMs;
|
|
288
|
+
const expectedStatuses = resolveExpectedStatuses(req);
|
|
289
|
+
return buildHeaders(ctx, req).pipe(
|
|
290
|
+
Effect2.flatMap((headers) => {
|
|
291
|
+
const body = buildRequestBody(req, headers);
|
|
292
|
+
const init = {
|
|
293
|
+
method: req.method,
|
|
294
|
+
headers,
|
|
295
|
+
...body !== void 0 ? { body } : {}
|
|
296
|
+
};
|
|
297
|
+
return fetchWithTimeout(
|
|
298
|
+
ctx,
|
|
299
|
+
url,
|
|
300
|
+
init,
|
|
301
|
+
timeoutMs,
|
|
302
|
+
req.method,
|
|
303
|
+
req.path
|
|
304
|
+
).pipe(
|
|
305
|
+
Effect2.flatMap(
|
|
306
|
+
(response) => Effect2.tryPromise({
|
|
307
|
+
try: () => response.text(),
|
|
308
|
+
catch: (cause) => new TransportError({
|
|
309
|
+
method: req.method,
|
|
310
|
+
path: req.path,
|
|
311
|
+
cause
|
|
312
|
+
})
|
|
313
|
+
}).pipe(
|
|
314
|
+
Effect2.flatMap((bodyText) => {
|
|
315
|
+
if (!isExpectedStatus(response.status, expectedStatuses)) {
|
|
316
|
+
return Effect2.fail(
|
|
317
|
+
mapUnexpectedStatus(
|
|
318
|
+
req,
|
|
319
|
+
response.status,
|
|
320
|
+
bodyText,
|
|
321
|
+
expectedStatuses,
|
|
322
|
+
ctx.redact
|
|
323
|
+
)
|
|
324
|
+
);
|
|
325
|
+
}
|
|
326
|
+
return parseResponseBody(req, bodyText, ctx.redact).pipe(
|
|
327
|
+
Effect2.map((parsedBody) => ({
|
|
328
|
+
status: response.status,
|
|
329
|
+
headers: response.headers,
|
|
330
|
+
bodyText,
|
|
331
|
+
body: parsedBody
|
|
332
|
+
}))
|
|
333
|
+
);
|
|
334
|
+
})
|
|
335
|
+
)
|
|
336
|
+
)
|
|
337
|
+
);
|
|
338
|
+
})
|
|
339
|
+
);
|
|
340
|
+
};
|
|
341
|
+
var defaultShouldRetry = (error) => {
|
|
342
|
+
switch (error._tag) {
|
|
343
|
+
case "TransportError":
|
|
344
|
+
case "TimeoutError":
|
|
345
|
+
return true;
|
|
346
|
+
case "UnexpectedStatusError":
|
|
347
|
+
return error.status >= 500;
|
|
348
|
+
default:
|
|
349
|
+
return false;
|
|
350
|
+
}
|
|
351
|
+
};
|
|
352
|
+
var DEFAULT_POLICY_FIELDS = {
|
|
353
|
+
maxAttempts: 3,
|
|
354
|
+
initialDelayMs: 100,
|
|
355
|
+
maxDelayMs: 5e3,
|
|
356
|
+
factor: 2,
|
|
357
|
+
jitter: true,
|
|
358
|
+
shouldRetry: defaultShouldRetry
|
|
359
|
+
};
|
|
360
|
+
var noRetry = {
|
|
361
|
+
enabled: false,
|
|
362
|
+
...DEFAULT_POLICY_FIELDS,
|
|
363
|
+
shouldRetry: () => false
|
|
364
|
+
};
|
|
365
|
+
var exponentialRetry = (opts) => ({
|
|
366
|
+
enabled: opts.enabled ?? true,
|
|
367
|
+
maxAttempts: opts.maxAttempts ?? DEFAULT_POLICY_FIELDS.maxAttempts,
|
|
368
|
+
initialDelayMs: opts.initialDelayMs ?? DEFAULT_POLICY_FIELDS.initialDelayMs,
|
|
369
|
+
maxDelayMs: opts.maxDelayMs ?? DEFAULT_POLICY_FIELDS.maxDelayMs,
|
|
370
|
+
factor: opts.factor ?? DEFAULT_POLICY_FIELDS.factor,
|
|
371
|
+
jitter: opts.jitter ?? DEFAULT_POLICY_FIELDS.jitter,
|
|
372
|
+
shouldRetry: opts.shouldRetry ?? DEFAULT_POLICY_FIELDS.shouldRetry
|
|
373
|
+
});
|
|
374
|
+
var buildSchedule = (policy) => {
|
|
375
|
+
const base = Schedule.exponential(`${policy.initialDelayMs} millis`, policy.factor);
|
|
376
|
+
const withJitter = policy.jitter ? base.pipe(Schedule.jittered) : base;
|
|
377
|
+
const capped = withJitter.pipe(
|
|
378
|
+
Schedule.modifyDelay((_out, duration) => {
|
|
379
|
+
const ms = Duration.toMillis(duration);
|
|
380
|
+
return ms > policy.maxDelayMs ? `${policy.maxDelayMs} millis` : duration;
|
|
381
|
+
})
|
|
382
|
+
);
|
|
383
|
+
return capped.pipe(Schedule.whileInput((error) => policy.shouldRetry(error)));
|
|
384
|
+
};
|
|
385
|
+
var withRetry = (effect, policy = noRetry) => {
|
|
386
|
+
if (!policy.enabled || policy.maxAttempts <= 1) {
|
|
387
|
+
return effect;
|
|
388
|
+
}
|
|
389
|
+
const retries = Math.max(0, policy.maxAttempts - 1);
|
|
390
|
+
return Effect3.retry(effect, {
|
|
391
|
+
times: retries,
|
|
392
|
+
while: (error) => policy.shouldRetry(error),
|
|
393
|
+
schedule: buildSchedule(policy)
|
|
394
|
+
});
|
|
395
|
+
};
|
|
396
|
+
var ok = (details) => ({
|
|
397
|
+
content: [{ type: "text", text: JSON.stringify(details, null, 2) }],
|
|
398
|
+
details
|
|
399
|
+
});
|
|
400
|
+
|
|
401
|
+
// src/config.ts
|
|
402
|
+
var DEFAULT_REQUEST_TIMEOUT_MS = 3e4;
|
|
403
|
+
var ConfigError2 = class extends Error {
|
|
9
404
|
constructor(message) {
|
|
10
405
|
super(message);
|
|
11
406
|
this.name = "ConfigError";
|
|
12
407
|
}
|
|
13
408
|
};
|
|
14
|
-
function
|
|
15
|
-
|
|
16
|
-
|
|
409
|
+
function runConfigEffect(effect) {
|
|
410
|
+
const result = Effect6.runSync(Effect6.either(effect));
|
|
411
|
+
if (result._tag === "Left") {
|
|
412
|
+
throw new ConfigError2(result.left.message);
|
|
413
|
+
}
|
|
414
|
+
return result.right;
|
|
415
|
+
}
|
|
416
|
+
function parseTlsInsecure(env) {
|
|
417
|
+
const raw = env.get("LIBRENMS_TLS_INSECURE");
|
|
418
|
+
if (!raw) return false;
|
|
419
|
+
if (["true", "1", "yes"].includes(raw.toLowerCase())) {
|
|
420
|
+
return runConfigEffect(parseBooleanEnv(env, "LIBRENMS_TLS_INSECURE", false));
|
|
421
|
+
}
|
|
422
|
+
return false;
|
|
423
|
+
}
|
|
424
|
+
function parseRequestTimeoutMs(env) {
|
|
425
|
+
return runConfigEffect(
|
|
426
|
+
parseTimeoutEnv(env, "LIBRENMS_REQUEST_TIMEOUT_MS", {
|
|
427
|
+
fallbackMs: DEFAULT_REQUEST_TIMEOUT_MS,
|
|
428
|
+
minMs: 1e3,
|
|
429
|
+
unit: "ms"
|
|
430
|
+
})
|
|
431
|
+
);
|
|
17
432
|
}
|
|
18
433
|
function resolveConfig(env) {
|
|
434
|
+
const reader = fromProcessEnv(env);
|
|
19
435
|
const url = env.LIBRENMS_URL;
|
|
20
436
|
const token = env.LIBRENMS_TOKEN;
|
|
21
|
-
if (!url) throw new
|
|
22
|
-
if (!token) throw new
|
|
437
|
+
if (!url) throw new ConfigError2("LIBRENMS_URL is required");
|
|
438
|
+
if (!token) throw new ConfigError2("LIBRENMS_TOKEN is required");
|
|
23
439
|
return {
|
|
24
440
|
url: url.replace(/\/+$/, ""),
|
|
25
441
|
token,
|
|
26
|
-
tlsInsecure:
|
|
442
|
+
tlsInsecure: parseTlsInsecure(reader),
|
|
443
|
+
requestTimeoutMs: parseRequestTimeoutMs(reader)
|
|
27
444
|
};
|
|
28
445
|
}
|
|
29
446
|
|
|
30
447
|
// src/librenms-client.ts
|
|
448
|
+
import { Effect as Effect7 } from "effect";
|
|
31
449
|
import { Agent as UndiciAgent } from "undici";
|
|
450
|
+
|
|
451
|
+
// src/error-message.ts
|
|
452
|
+
function boundaryErrorMessage(error) {
|
|
453
|
+
return error instanceof Error ? error.message : String(error);
|
|
454
|
+
}
|
|
455
|
+
|
|
456
|
+
// src/librenms-client.ts
|
|
32
457
|
var LibreNmsClientError = class extends Error {
|
|
33
458
|
constructor(status, message) {
|
|
34
459
|
super(`LibreNMS ${status}: ${message}`);
|
|
35
460
|
this.status = status;
|
|
36
461
|
this.name = "LibreNmsClientError";
|
|
37
462
|
}
|
|
38
|
-
status;
|
|
39
463
|
};
|
|
40
464
|
var LibreNmsUnreachableError = class extends Error {
|
|
41
465
|
constructor(cause) {
|
|
@@ -47,12 +471,13 @@ var LibreNmsClient = class {
|
|
|
47
471
|
constructor(cfg, opts = {}) {
|
|
48
472
|
this.cfg = cfg;
|
|
49
473
|
this.retryDelayMs = opts.retryDelayMs ?? 1e3;
|
|
474
|
+
this.fetchImpl = opts.fetch ?? fetch;
|
|
50
475
|
if (cfg.tlsInsecure && cfg.url.startsWith("https://")) {
|
|
51
476
|
this.dispatcher = new UndiciAgent({ connect: { rejectUnauthorized: false } });
|
|
52
477
|
}
|
|
53
478
|
}
|
|
54
|
-
cfg;
|
|
55
479
|
retryDelayMs;
|
|
480
|
+
fetchImpl;
|
|
56
481
|
// Node's global fetch (undici) ignores node:https.Agent. To actually skip
|
|
57
482
|
// cert verification for self-signed LibreNMS hosts we pass an undici Agent
|
|
58
483
|
// via the `dispatcher` init option.
|
|
@@ -70,47 +495,95 @@ var LibreNmsClient = class {
|
|
|
70
495
|
return this.request("DELETE", path);
|
|
71
496
|
}
|
|
72
497
|
async request(method, path, body) {
|
|
73
|
-
const
|
|
74
|
-
const
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
}
|
|
80
|
-
let lastErr;
|
|
81
|
-
for (let attempt = 0; attempt < 2; attempt++) {
|
|
82
|
-
try {
|
|
83
|
-
const init = { method, headers, body: bodyStr };
|
|
84
|
-
if (this.dispatcher) init.dispatcher = this.dispatcher;
|
|
85
|
-
const res = await fetch(url, init);
|
|
86
|
-
if (res.status >= 200 && res.status < 300) {
|
|
87
|
-
const text = await res.text();
|
|
88
|
-
if (!text) return void 0;
|
|
89
|
-
return JSON.parse(text);
|
|
90
|
-
}
|
|
91
|
-
if (res.status >= 500) {
|
|
92
|
-
lastErr = new LibreNmsUnreachableError(`HTTP ${res.status}`);
|
|
93
|
-
if (attempt === 0) await sleep(this.retryDelayMs);
|
|
94
|
-
continue;
|
|
95
|
-
}
|
|
96
|
-
const errText = await res.text();
|
|
97
|
-
let msg = errText;
|
|
98
|
-
try {
|
|
99
|
-
msg = JSON.parse(errText).message ?? errText;
|
|
100
|
-
} catch {
|
|
101
|
-
}
|
|
102
|
-
throw new LibreNmsClientError(res.status, msg);
|
|
103
|
-
} catch (e) {
|
|
104
|
-
if (e instanceof LibreNmsClientError) throw e;
|
|
105
|
-
lastErr = new LibreNmsUnreachableError(e.message);
|
|
106
|
-
if (attempt === 0) await sleep(this.retryDelayMs);
|
|
498
|
+
const baseUrl = new URL(`${this.cfg.url}/`);
|
|
499
|
+
const requestPath = `api/v0${path}`;
|
|
500
|
+
const auth = {
|
|
501
|
+
apply: (headers) => {
|
|
502
|
+
headers.set("x-auth-token", this.cfg.token);
|
|
503
|
+
return Effect7.succeed(headers);
|
|
107
504
|
}
|
|
505
|
+
};
|
|
506
|
+
const req = {
|
|
507
|
+
method,
|
|
508
|
+
path: requestPath,
|
|
509
|
+
body,
|
|
510
|
+
bodyEncoding: body !== void 0 ? "json" : "none",
|
|
511
|
+
statusMapper: ({ status, method: mappedMethod, path: mappedPath, bodyText, expectedStatuses }) => new UnexpectedStatusError({
|
|
512
|
+
method: mappedMethod,
|
|
513
|
+
path: mappedPath,
|
|
514
|
+
status,
|
|
515
|
+
body: bodyText,
|
|
516
|
+
expected: expectedStatuses
|
|
517
|
+
})
|
|
518
|
+
};
|
|
519
|
+
const ctx = {
|
|
520
|
+
baseUrl,
|
|
521
|
+
auth,
|
|
522
|
+
timeoutMs: this.cfg.requestTimeoutMs ?? DEFAULT_REQUEST_TIMEOUT_MS,
|
|
523
|
+
fetch: this.fetchWithDispatcher,
|
|
524
|
+
redact: (value) => value
|
|
525
|
+
};
|
|
526
|
+
let result;
|
|
527
|
+
try {
|
|
528
|
+
result = await Effect7.runPromise(
|
|
529
|
+
Effect7.either(
|
|
530
|
+
withRetry(
|
|
531
|
+
sendRequest(ctx, req),
|
|
532
|
+
exponentialRetry({
|
|
533
|
+
maxAttempts: 2,
|
|
534
|
+
initialDelayMs: this.retryDelayMs,
|
|
535
|
+
maxDelayMs: this.retryDelayMs,
|
|
536
|
+
factor: 1,
|
|
537
|
+
jitter: false,
|
|
538
|
+
shouldRetry: shouldRetryLibreNmsRequest
|
|
539
|
+
})
|
|
540
|
+
)
|
|
541
|
+
)
|
|
542
|
+
);
|
|
543
|
+
} catch (error) {
|
|
544
|
+
throw new LibreNmsUnreachableError(boundaryErrorMessage(error));
|
|
545
|
+
}
|
|
546
|
+
if (result._tag === "Right") {
|
|
547
|
+
return result.right.body;
|
|
108
548
|
}
|
|
109
|
-
throw
|
|
549
|
+
throw mapLibreNmsRequestError(result.left);
|
|
110
550
|
}
|
|
551
|
+
fetchWithDispatcher = (input, init = {}) => {
|
|
552
|
+
if (!this.dispatcher) return this.fetchImpl(input, init);
|
|
553
|
+
return this.fetchImpl(input, { ...init, dispatcher: this.dispatcher });
|
|
554
|
+
};
|
|
111
555
|
};
|
|
112
|
-
function
|
|
113
|
-
|
|
556
|
+
function shouldRetryLibreNmsRequest(error) {
|
|
557
|
+
if (error instanceof TransportError) return true;
|
|
558
|
+
if (error instanceof ParseError) return true;
|
|
559
|
+
if (error instanceof UnexpectedStatusError) return error.status >= 500;
|
|
560
|
+
return false;
|
|
561
|
+
}
|
|
562
|
+
function mapLibreNmsRequestError(error) {
|
|
563
|
+
if (error instanceof UnexpectedStatusError) {
|
|
564
|
+
if (error.status >= 500) return new LibreNmsUnreachableError(`HTTP ${error.status}`);
|
|
565
|
+
return new LibreNmsClientError(error.status, responseErrorMessage(error.body));
|
|
566
|
+
}
|
|
567
|
+
if (error instanceof TimeoutError) {
|
|
568
|
+
return new LibreNmsUnreachableError(
|
|
569
|
+
`${error.method} ${error.path} timed out after ${error.timeoutMs}ms`
|
|
570
|
+
);
|
|
571
|
+
}
|
|
572
|
+
if (error instanceof TransportError) {
|
|
573
|
+
const cause = error.cause;
|
|
574
|
+
return new LibreNmsUnreachableError(boundaryErrorMessage(cause));
|
|
575
|
+
}
|
|
576
|
+
if (error instanceof ParseError) {
|
|
577
|
+
return new LibreNmsUnreachableError(boundaryErrorMessage(error.cause ?? error.message));
|
|
578
|
+
}
|
|
579
|
+
return new LibreNmsUnreachableError(boundaryErrorMessage(error));
|
|
580
|
+
}
|
|
581
|
+
function responseErrorMessage(bodyText) {
|
|
582
|
+
try {
|
|
583
|
+
return JSON.parse(bodyText).message ?? bodyText;
|
|
584
|
+
} catch {
|
|
585
|
+
return bodyText;
|
|
586
|
+
}
|
|
114
587
|
}
|
|
115
588
|
|
|
116
589
|
// src/security.ts
|
|
@@ -185,9 +658,8 @@ import { Type } from "@sinclair/typebox";
|
|
|
185
658
|
|
|
186
659
|
// src/tools/_util.ts
|
|
187
660
|
function jsonToolResult(payload) {
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
};
|
|
661
|
+
const r = ok(payload);
|
|
662
|
+
return { content: r.content };
|
|
191
663
|
}
|
|
192
664
|
function safeInt(value, label, opts = {}) {
|
|
193
665
|
const n = typeof value === "number" ? value : typeof value === "string" ? Number(value) : NaN;
|
|
@@ -692,7 +1164,7 @@ function createServer() {
|
|
|
692
1164
|
createLibrenmsUnmuteAlertTool(getClient)
|
|
693
1165
|
];
|
|
694
1166
|
const toolMap = new Map(tools.map((t) => [t.name, t]));
|
|
695
|
-
const server = new Server({ name: "librenms-mcp", version: "0.2.
|
|
1167
|
+
const server = new Server({ name: "librenms-mcp", version: "0.2.2" }, { capabilities: { tools: {} } });
|
|
696
1168
|
server.setRequestHandler(ListToolsRequestSchema, async () => ({
|
|
697
1169
|
tools: tools.map((t) => ({ name: t.name, description: t.description, inputSchema: t.parameters }))
|
|
698
1170
|
}));
|
|
@@ -706,12 +1178,15 @@ function createServer() {
|
|
|
706
1178
|
validateToolArgs(t.parameters, t.name, args);
|
|
707
1179
|
return await t.execute(req.params.name, args);
|
|
708
1180
|
} catch (e) {
|
|
709
|
-
|
|
710
|
-
return { content: [{ type: "text", text: JSON.stringify({ error: msg }) }], isError: true };
|
|
1181
|
+
return mcpErrorResult(e);
|
|
711
1182
|
}
|
|
712
1183
|
});
|
|
713
1184
|
return server;
|
|
714
1185
|
}
|
|
1186
|
+
function mcpErrorResult(error) {
|
|
1187
|
+
const msg = redact(boundaryErrorMessage(error));
|
|
1188
|
+
return { content: [{ type: "text", text: JSON.stringify({ error: msg }) }], isError: true };
|
|
1189
|
+
}
|
|
715
1190
|
async function serve() {
|
|
716
1191
|
const server = createServer();
|
|
717
1192
|
const transport = new StdioServerTransport();
|
|
@@ -730,7 +1205,7 @@ async function serve() {
|
|
|
730
1205
|
}
|
|
731
1206
|
|
|
732
1207
|
// src/cli.ts
|
|
733
|
-
var VERSION = "0.2.
|
|
1208
|
+
var VERSION = "0.2.2";
|
|
734
1209
|
var UsageError = class extends Error {
|
|
735
1210
|
};
|
|
736
1211
|
var HELP = `librenmsctrl - read-only operator CLI for a LibreNMS instance
|
|
@@ -770,7 +1245,8 @@ Command options:
|
|
|
770
1245
|
Environment:
|
|
771
1246
|
LIBRENMS_URL Base URL of the LibreNMS instance (required)
|
|
772
1247
|
LIBRENMS_TOKEN X-Auth-Token API token (required)
|
|
773
|
-
LIBRENMS_TLS_INSECURE Set truthy to skip TLS verification (self-signed hosts)
|
|
1248
|
+
LIBRENMS_TLS_INSECURE Set truthy to skip TLS verification (self-signed hosts)
|
|
1249
|
+
LIBRENMS_REQUEST_TIMEOUT_MS Per-request HTTP timeout in ms (default 30000, min 1000)`;
|
|
774
1250
|
function takeFlag(args, name) {
|
|
775
1251
|
const i = args.indexOf(name);
|
|
776
1252
|
if (i === -1) return false;
|
|
@@ -1141,7 +1617,7 @@ async function run(argv, deps) {
|
|
|
1141
1617
|
deps.err(redact(error.message));
|
|
1142
1618
|
return 2;
|
|
1143
1619
|
}
|
|
1144
|
-
deps.err(redact(
|
|
1620
|
+
deps.err(redact(boundaryErrorMessage(error)));
|
|
1145
1621
|
return 1;
|
|
1146
1622
|
}
|
|
1147
1623
|
return 0;
|
|
@@ -1166,7 +1642,7 @@ if (isEntrypoint) {
|
|
|
1166
1642
|
}).then((code) => {
|
|
1167
1643
|
process.exitCode = code;
|
|
1168
1644
|
}).catch((error) => {
|
|
1169
|
-
process.stderr.write(`${redact(
|
|
1645
|
+
process.stderr.write(`${redact(boundaryErrorMessage(error))}
|
|
1170
1646
|
`);
|
|
1171
1647
|
process.exitCode = 1;
|
|
1172
1648
|
});
|