@teamnetwork-nz/sonicwall-api-mcp-server 0.6.0 → 0.8.0
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 +4 -0
- package/dist/http-server.js +119 -0
- package/dist/index.js +185 -55
- package/package.json +51 -50
package/README.md
CHANGED
|
@@ -1,5 +1,9 @@
|
|
|
1
1
|
# SonicWall NSA MCP Server
|
|
2
2
|
|
|
3
|
+
## Streamable HTTP
|
|
4
|
+
|
|
5
|
+
Set `SONICWALL_TRANSPORT=http` to serve stateless MCP at `/mcp`. It binds to `127.0.0.1` by default; remote binding requires `SONICWALL_HTTP_BEARER_TOKEN` and `SONICWALL_HTTP_ALLOWED_HOSTS`. Optional controls include `SONICWALL_HTTP_PORT`, `SONICWALL_HTTP_ALLOWED_ORIGINS`, and `SONICWALL_HTTP_MAX_BODY_BYTES`; `/health` is available for health checks.
|
|
6
|
+
|
|
3
7
|
A [Model Context Protocol](https://modelcontextprotocol.io/) server that provides full read/write access to SonicWall NSA series appliances via the SonicOS REST API.
|
|
4
8
|
|
|
5
9
|
## Features
|
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
"use strict";
|
|
3
|
+
var __create = Object.create;
|
|
4
|
+
var __defProp = Object.defineProperty;
|
|
5
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
6
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
7
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
8
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
9
|
+
var __export = (target, all) => {
|
|
10
|
+
for (var name in all)
|
|
11
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
12
|
+
};
|
|
13
|
+
var __copyProps = (to, from, except, desc) => {
|
|
14
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
15
|
+
for (let key of __getOwnPropNames(from))
|
|
16
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
17
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
18
|
+
}
|
|
19
|
+
return to;
|
|
20
|
+
};
|
|
21
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
22
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
23
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
24
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
25
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
26
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
27
|
+
mod
|
|
28
|
+
));
|
|
29
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
30
|
+
|
|
31
|
+
// src/http-server.ts
|
|
32
|
+
var http_server_exports = {};
|
|
33
|
+
__export(http_server_exports, {
|
|
34
|
+
createHttpServer: () => createHttpServer,
|
|
35
|
+
validateHttpOptions: () => validateHttpOptions
|
|
36
|
+
});
|
|
37
|
+
module.exports = __toCommonJS(http_server_exports);
|
|
38
|
+
var import_node_crypto = require("crypto");
|
|
39
|
+
var import_node_http = __toESM(require("http"));
|
|
40
|
+
var import_streamableHttp = require("@modelcontextprotocol/sdk/server/streamableHttp.js");
|
|
41
|
+
var loopback = /* @__PURE__ */ new Set(["127.0.0.1", "::1", "localhost"]);
|
|
42
|
+
var send = (res, status, body) => {
|
|
43
|
+
res.writeHead(status, { "Content-Type": "application/json" }).end(JSON.stringify(body));
|
|
44
|
+
};
|
|
45
|
+
var error = (res, status, code, message) => send(res, status, { jsonrpc: "2.0", error: { code, message }, id: null });
|
|
46
|
+
var matches = (value, expected) => {
|
|
47
|
+
if (!value?.startsWith("Bearer ")) return false;
|
|
48
|
+
const supplied = Buffer.from(value.slice(7));
|
|
49
|
+
const wanted = Buffer.from(expected);
|
|
50
|
+
return supplied.length === wanted.length && (0, import_node_crypto.timingSafeEqual)(supplied, wanted);
|
|
51
|
+
};
|
|
52
|
+
var readBody = (req, limit) => new Promise((resolve, reject) => {
|
|
53
|
+
const chunks = [];
|
|
54
|
+
let size = 0;
|
|
55
|
+
let oversized = false;
|
|
56
|
+
req.on("data", (chunk) => {
|
|
57
|
+
size += chunk.length;
|
|
58
|
+
oversized ||= size > limit;
|
|
59
|
+
if (!oversized) chunks.push(chunk);
|
|
60
|
+
});
|
|
61
|
+
req.on("end", () => {
|
|
62
|
+
if (oversized) return reject(Object.assign(new Error("Request body too large"), { statusCode: 413 }));
|
|
63
|
+
try {
|
|
64
|
+
resolve(JSON.parse(Buffer.concat(chunks).toString("utf8")));
|
|
65
|
+
} catch {
|
|
66
|
+
reject(Object.assign(new Error("Invalid JSON body"), { statusCode: 400 }));
|
|
67
|
+
}
|
|
68
|
+
});
|
|
69
|
+
req.on("error", reject);
|
|
70
|
+
});
|
|
71
|
+
function validateHttpOptions(o) {
|
|
72
|
+
if (!Number.isInteger(o.port) || o.port < 1 || o.port > 65535) throw new Error(`Invalid HTTP port: ${o.port}`);
|
|
73
|
+
if (o.maxBodyBytes !== void 0 && (!Number.isInteger(o.maxBodyBytes) || o.maxBodyBytes < 1)) throw new Error(`Invalid HTTP body limit: ${o.maxBodyBytes}`);
|
|
74
|
+
if (!loopback.has(o.host) && !o.bearerToken) throw new Error("A bearer token is required when binding beyond loopback");
|
|
75
|
+
if (!loopback.has(o.host) && !o.allowedHosts?.length) throw new Error("HTTP_ALLOWED_HOSTS is required when binding beyond loopback");
|
|
76
|
+
}
|
|
77
|
+
function createHttpServer(factory, o) {
|
|
78
|
+
validateHttpOptions(o);
|
|
79
|
+
const hosts = new Set(o.allowedHosts?.map((v) => v.toLowerCase()));
|
|
80
|
+
const origins = new Set(o.allowedOrigins?.map((v) => v.toLowerCase()));
|
|
81
|
+
return import_node_http.default.createServer(async (req, res) => {
|
|
82
|
+
const path = new URL(req.url ?? "/", "http://localhost").pathname;
|
|
83
|
+
if (path === "/health") return send(res, 200, { status: "ok", server: o.serverName });
|
|
84
|
+
if (path !== "/mcp") return void res.writeHead(404).end();
|
|
85
|
+
if (hosts.size && !hosts.has((req.headers.host ?? "").toLowerCase())) return error(res, 403, -32e3, "Host not allowed");
|
|
86
|
+
if (req.headers.origin && !origins.has(req.headers.origin.toLowerCase())) return error(res, 403, -32e3, "Origin not allowed");
|
|
87
|
+
if (o.bearerToken && !matches(req.headers.authorization, o.bearerToken)) {
|
|
88
|
+
res.setHeader("WWW-Authenticate", "Bearer");
|
|
89
|
+
return error(res, 401, -32001, "Unauthorized");
|
|
90
|
+
}
|
|
91
|
+
if (req.method !== "POST") {
|
|
92
|
+
res.setHeader("Allow", "POST");
|
|
93
|
+
return error(res, 405, -32e3, "Method not allowed");
|
|
94
|
+
}
|
|
95
|
+
const server = factory();
|
|
96
|
+
const transport = new import_streamableHttp.StreamableHTTPServerTransport({ sessionIdGenerator: void 0 });
|
|
97
|
+
try {
|
|
98
|
+
const body = await readBody(req, o.maxBodyBytes ?? 1048576);
|
|
99
|
+
await server.connect(transport);
|
|
100
|
+
await transport.handleRequest(req, res, body);
|
|
101
|
+
res.once("close", () => {
|
|
102
|
+
void transport.close();
|
|
103
|
+
void server.close();
|
|
104
|
+
});
|
|
105
|
+
} catch (cause) {
|
|
106
|
+
void transport.close();
|
|
107
|
+
void server.close();
|
|
108
|
+
if (!res.headersSent) {
|
|
109
|
+
const status = cause.statusCode ?? 500;
|
|
110
|
+
error(res, status, status === 400 ? -32700 : -32603, status < 500 ? cause.message : "Internal server error");
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
});
|
|
114
|
+
}
|
|
115
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
116
|
+
0 && (module.exports = {
|
|
117
|
+
createHttpServer,
|
|
118
|
+
validateHttpOptions
|
|
119
|
+
});
|
package/dist/index.js
CHANGED
|
@@ -377,9 +377,21 @@ var SonicWallClient = class {
|
|
|
377
377
|
await this.auth.logout(this.baseUrl);
|
|
378
378
|
}
|
|
379
379
|
async request(method, path, options) {
|
|
380
|
-
|
|
380
|
+
const isWrite = WRITE_METHODS.has(method.toUpperCase());
|
|
381
|
+
if (this.profile.readOnly && isWrite) {
|
|
381
382
|
throw new ReadOnlyError(method.toUpperCase(), path);
|
|
382
383
|
}
|
|
384
|
+
try {
|
|
385
|
+
return await this.send(method, path, options);
|
|
386
|
+
} catch (e) {
|
|
387
|
+
if (isWrite && e instanceof APIError && e.code === "E_NON_CONFIG") {
|
|
388
|
+
await this.send("POST", "config-mode");
|
|
389
|
+
return this.send(method, path, options);
|
|
390
|
+
}
|
|
391
|
+
throw e;
|
|
392
|
+
}
|
|
393
|
+
}
|
|
394
|
+
async send(method, path, options) {
|
|
383
395
|
let url = `${this.baseUrl}/${path.replace(/^\//, "")}`;
|
|
384
396
|
if (options?.params && Object.keys(options.params).length > 0) {
|
|
385
397
|
const qs = new URLSearchParams(
|
|
@@ -1396,8 +1408,25 @@ function registerAccessRuleTools(server, registry) {
|
|
|
1396
1408
|
|
|
1397
1409
|
// src/tools/nat-policies.ts
|
|
1398
1410
|
var import_zod7 = require("zod");
|
|
1399
|
-
function
|
|
1400
|
-
|
|
1411
|
+
function interfaceValue(value) {
|
|
1412
|
+
return value.toLowerCase() === "any" ? "any" : value;
|
|
1413
|
+
}
|
|
1414
|
+
async function resolveRef(client, kind, value, ipVersion, allowOriginal) {
|
|
1415
|
+
const lower = value.toLowerCase();
|
|
1416
|
+
if (lower === "any") return { any: true };
|
|
1417
|
+
if (allowOriginal && lower === "original") return { original: true };
|
|
1418
|
+
const groupPath = kind === "address" ? `address-groups/${ipVersion}/name/${encodeURIComponent(value)}` : `service-groups/name/${encodeURIComponent(value)}`;
|
|
1419
|
+
try {
|
|
1420
|
+
await client.get(groupPath);
|
|
1421
|
+
return { group: value };
|
|
1422
|
+
} catch (e) {
|
|
1423
|
+
if (e instanceof APIError) return { name: value };
|
|
1424
|
+
throw e;
|
|
1425
|
+
}
|
|
1426
|
+
}
|
|
1427
|
+
var prioritySchema = import_zod7.z.union([import_zod7.z.number().int().min(1), import_zod7.z.literal("auto")]).optional().describe("Rule order: lower number = evaluated first. A positive integer sets a manual priority; 'auto' lets SonicOS assign it.");
|
|
1428
|
+
function priorityField(priority) {
|
|
1429
|
+
return priority === "auto" ? { auto: true } : { manual: priority };
|
|
1401
1430
|
}
|
|
1402
1431
|
function registerNatPolicyTools(server, registry) {
|
|
1403
1432
|
server.tool(
|
|
@@ -1437,7 +1466,7 @@ function registerNatPolicyTools(server, registry) {
|
|
|
1437
1466
|
);
|
|
1438
1467
|
server.tool(
|
|
1439
1468
|
"create_nat_policy",
|
|
1440
|
-
"Create a NAT policy.
|
|
1469
|
+
"Create a NAT policy. Address and service fields take an object or group name (groups are detected automatically) or 'Any'; translated fields also accept 'Original' for no translation. Interfaces take a name like 'X1' or 'Any'. Optional priority sets rule order (lower number = evaluated first). Changes are STAGED; call commit_config to apply.",
|
|
1441
1470
|
{
|
|
1442
1471
|
name: import_zod7.z.string(),
|
|
1443
1472
|
original_source: import_zod7.z.string(),
|
|
@@ -1451,26 +1480,28 @@ function registerNatPolicyTools(server, registry) {
|
|
|
1451
1480
|
ip_version: import_zod7.z.enum(["ipv4", "ipv6"]).optional().default("ipv4"),
|
|
1452
1481
|
enabled: import_zod7.z.boolean().optional().default(true),
|
|
1453
1482
|
dns_doctoring: import_zod7.z.boolean().optional().default(false),
|
|
1483
|
+
priority: prioritySchema,
|
|
1454
1484
|
comment: import_zod7.z.string().optional(),
|
|
1455
1485
|
device: import_zod7.z.string().optional()
|
|
1456
1486
|
},
|
|
1457
|
-
async ({ name, original_source, translated_source, original_destination, translated_destination, original_service, translated_service, inbound_interface, outbound_interface, ip_version, enabled, dns_doctoring, comment, device }) => {
|
|
1487
|
+
async ({ name, original_source, translated_source, original_destination, translated_destination, original_service, translated_service, inbound_interface, outbound_interface, ip_version, enabled, dns_doctoring, priority, comment, device }) => {
|
|
1458
1488
|
try {
|
|
1459
1489
|
const client = await registry.getClient(device);
|
|
1460
1490
|
const policy = {
|
|
1461
1491
|
name,
|
|
1462
1492
|
enable: enabled,
|
|
1463
|
-
source:
|
|
1464
|
-
translated_source:
|
|
1465
|
-
destination:
|
|
1466
|
-
translated_destination:
|
|
1467
|
-
service:
|
|
1468
|
-
translated_service:
|
|
1469
|
-
inbound:
|
|
1470
|
-
outbound:
|
|
1493
|
+
source: await resolveRef(client, "address", original_source, ip_version, false),
|
|
1494
|
+
translated_source: await resolveRef(client, "address", translated_source, ip_version, true),
|
|
1495
|
+
destination: await resolveRef(client, "address", original_destination, ip_version, false),
|
|
1496
|
+
translated_destination: await resolveRef(client, "address", translated_destination, ip_version, true),
|
|
1497
|
+
service: await resolveRef(client, "service", original_service, ip_version, false),
|
|
1498
|
+
translated_service: await resolveRef(client, "service", translated_service, ip_version, true),
|
|
1499
|
+
inbound: interfaceValue(inbound_interface),
|
|
1500
|
+
outbound: interfaceValue(outbound_interface),
|
|
1471
1501
|
dns_doctoring
|
|
1472
1502
|
};
|
|
1473
1503
|
if (comment) policy.comment = comment;
|
|
1504
|
+
if (priority !== void 0) policy.priority = priorityField(priority);
|
|
1474
1505
|
const result = await client.post(`nat-policies/${ip_version}`, {
|
|
1475
1506
|
nat_policies: [{ [ip_version]: policy }]
|
|
1476
1507
|
});
|
|
@@ -1482,7 +1513,7 @@ function registerNatPolicyTools(server, registry) {
|
|
|
1482
1513
|
);
|
|
1483
1514
|
server.tool(
|
|
1484
1515
|
"update_nat_policy",
|
|
1485
|
-
"Update a NAT policy selected by UUID (GET-then-PUT). Pass a policy's uuid value from list_nat_policies as policy_uuid. dns_doctoring can be changed independently. Changes are STAGED; call commit_config to apply.",
|
|
1516
|
+
"Update a NAT policy selected by UUID (GET-then-PUT). Pass a policy's uuid value from list_nat_policies as policy_uuid. dns_doctoring can be changed independently. priority changes rule order (lower number = evaluated first; 'auto' lets SonicOS assign it). Position 1 is held by the built-in IKE policy, so use 2 or higher; an unavailable priority can return success yet be ignored, so confirm with get_nat_policy. Changes are STAGED; call commit_config to apply.",
|
|
1486
1517
|
{
|
|
1487
1518
|
policy_uuid: import_zod7.z.string().uuid(),
|
|
1488
1519
|
ip_version: import_zod7.z.enum(["ipv4", "ipv6"]).optional().default("ipv4"),
|
|
@@ -1496,27 +1527,29 @@ function registerNatPolicyTools(server, registry) {
|
|
|
1496
1527
|
outbound_interface: import_zod7.z.string().optional(),
|
|
1497
1528
|
enabled: import_zod7.z.boolean().optional(),
|
|
1498
1529
|
dns_doctoring: import_zod7.z.boolean().optional(),
|
|
1530
|
+
priority: prioritySchema,
|
|
1499
1531
|
comment: import_zod7.z.string().optional(),
|
|
1500
1532
|
device: import_zod7.z.string().optional()
|
|
1501
1533
|
},
|
|
1502
|
-
async ({ policy_uuid, ip_version, original_source, translated_source, original_destination, translated_destination, original_service, translated_service, inbound_interface, outbound_interface, enabled, dns_doctoring, comment, device }) => {
|
|
1534
|
+
async ({ policy_uuid, ip_version, original_source, translated_source, original_destination, translated_destination, original_service, translated_service, inbound_interface, outbound_interface, enabled, dns_doctoring, priority, comment, device }) => {
|
|
1503
1535
|
try {
|
|
1504
1536
|
const client = await registry.getClient(device);
|
|
1505
1537
|
const path = `nat-policies/${ip_version}/uuid/${encodeURIComponent(policy_uuid)}`;
|
|
1506
1538
|
const current = await client.get(path);
|
|
1507
1539
|
const policies = current.nat_policies;
|
|
1508
1540
|
const policy = policies?.[0]?.[ip_version] ?? {};
|
|
1509
|
-
|
|
1510
|
-
|
|
1511
|
-
|
|
1512
|
-
|
|
1513
|
-
|
|
1514
|
-
|
|
1515
|
-
|
|
1516
|
-
|
|
1541
|
+
if (original_source !== void 0) policy.source = await resolveRef(client, "address", original_source, ip_version, false);
|
|
1542
|
+
if (translated_source !== void 0) policy.translated_source = await resolveRef(client, "address", translated_source, ip_version, true);
|
|
1543
|
+
if (original_destination !== void 0) policy.destination = await resolveRef(client, "address", original_destination, ip_version, false);
|
|
1544
|
+
if (translated_destination !== void 0) policy.translated_destination = await resolveRef(client, "address", translated_destination, ip_version, true);
|
|
1545
|
+
if (original_service !== void 0) policy.service = await resolveRef(client, "service", original_service, ip_version, false);
|
|
1546
|
+
if (translated_service !== void 0) policy.translated_service = await resolveRef(client, "service", translated_service, ip_version, true);
|
|
1547
|
+
if (inbound_interface !== void 0) policy.inbound = interfaceValue(inbound_interface);
|
|
1548
|
+
if (outbound_interface !== void 0) policy.outbound = interfaceValue(outbound_interface);
|
|
1517
1549
|
if (enabled !== void 0) policy.enable = enabled;
|
|
1518
1550
|
if (dns_doctoring !== void 0) policy.dns_doctoring = dns_doctoring;
|
|
1519
1551
|
if (comment !== void 0) policy.comment = comment;
|
|
1552
|
+
if (priority !== void 0) policy.priority = priorityField(priority);
|
|
1520
1553
|
const result = await client.put(path, {
|
|
1521
1554
|
nat_policies: [{ [ip_version]: policy }]
|
|
1522
1555
|
});
|
|
@@ -1827,12 +1860,12 @@ async function getWithFallback(client, candidates) {
|
|
|
1827
1860
|
for (const path of candidates) {
|
|
1828
1861
|
try {
|
|
1829
1862
|
return await client.get(path);
|
|
1830
|
-
} catch (
|
|
1831
|
-
if (
|
|
1832
|
-
lastError =
|
|
1863
|
+
} catch (error2) {
|
|
1864
|
+
if (error2 instanceof APIError && (error2.status === 400 || error2.status === 404)) {
|
|
1865
|
+
lastError = error2;
|
|
1833
1866
|
continue;
|
|
1834
1867
|
}
|
|
1835
|
-
throw
|
|
1868
|
+
throw error2;
|
|
1836
1869
|
}
|
|
1837
1870
|
}
|
|
1838
1871
|
throw lastError;
|
|
@@ -1847,8 +1880,8 @@ function registerLogTools(server, registry) {
|
|
|
1847
1880
|
const client = await registry.getClient(device);
|
|
1848
1881
|
const result = await getWithFallback(client, ["log/categories", "reporting/log/categories"]);
|
|
1849
1882
|
return { content: [{ type: "text", text: JSON.stringify(result, null, 2) }] };
|
|
1850
|
-
} catch (
|
|
1851
|
-
return { content: [{ type: "text", text: `Error: ${
|
|
1883
|
+
} catch (error2) {
|
|
1884
|
+
return { content: [{ type: "text", text: `Error: ${error2.message}` }], isError: true };
|
|
1852
1885
|
}
|
|
1853
1886
|
}
|
|
1854
1887
|
);
|
|
@@ -1861,8 +1894,8 @@ function registerLogTools(server, registry) {
|
|
|
1861
1894
|
const client = await registry.getClient(device);
|
|
1862
1895
|
const result = await getWithFallback(client, ["log/events", "reporting/log/events"]);
|
|
1863
1896
|
return { content: [{ type: "text", text: JSON.stringify(result, null, 2) }] };
|
|
1864
|
-
} catch (
|
|
1865
|
-
return { content: [{ type: "text", text: `Error: ${
|
|
1897
|
+
} catch (error2) {
|
|
1898
|
+
return { content: [{ type: "text", text: `Error: ${error2.message}` }], isError: true };
|
|
1866
1899
|
}
|
|
1867
1900
|
}
|
|
1868
1901
|
);
|
|
@@ -1903,6 +1936,17 @@ function registerZoneTools(server, registry) {
|
|
|
1903
1936
|
|
|
1904
1937
|
// src/tools/interfaces.ts
|
|
1905
1938
|
var import_zod12 = require("zod");
|
|
1939
|
+
var SECRET_FIELD = /(?:password|passwd|secret|private_key|pre_shared_key|community)$/i;
|
|
1940
|
+
function redactSecrets(value) {
|
|
1941
|
+
if (Array.isArray(value)) return value.map(redactSecrets);
|
|
1942
|
+
if (!value || typeof value !== "object") return value;
|
|
1943
|
+
return Object.fromEntries(
|
|
1944
|
+
Object.entries(value).map(([key, nested]) => [
|
|
1945
|
+
key,
|
|
1946
|
+
SECRET_FIELD.test(key) ? "[REDACTED]" : redactSecrets(nested)
|
|
1947
|
+
])
|
|
1948
|
+
);
|
|
1949
|
+
}
|
|
1906
1950
|
function registerInterfaceTools(server, registry) {
|
|
1907
1951
|
server.tool(
|
|
1908
1952
|
"list_interfaces",
|
|
@@ -1912,7 +1956,7 @@ function registerInterfaceTools(server, registry) {
|
|
|
1912
1956
|
try {
|
|
1913
1957
|
const client = await registry.getClient(device);
|
|
1914
1958
|
const result = await client.get("interfaces/ipv4");
|
|
1915
|
-
return { content: [{ type: "text", text: JSON.stringify(result, null, 2) }] };
|
|
1959
|
+
return { content: [{ type: "text", text: JSON.stringify(redactSecrets(result), null, 2) }] };
|
|
1916
1960
|
} catch (e) {
|
|
1917
1961
|
return { content: [{ type: "text", text: `Error: ${e.message}` }], isError: true };
|
|
1918
1962
|
}
|
|
@@ -1926,7 +1970,7 @@ function registerInterfaceTools(server, registry) {
|
|
|
1926
1970
|
try {
|
|
1927
1971
|
const client = await registry.getClient(device);
|
|
1928
1972
|
const result = await client.get(`interfaces/ipv4/name/${encodeURIComponent(name)}`);
|
|
1929
|
-
return { content: [{ type: "text", text: JSON.stringify(result, null, 2) }] };
|
|
1973
|
+
return { content: [{ type: "text", text: JSON.stringify(redactSecrets(result), null, 2) }] };
|
|
1930
1974
|
} catch (e) {
|
|
1931
1975
|
return { content: [{ type: "text", text: `Error: ${e.message}` }], isError: true };
|
|
1932
1976
|
}
|
|
@@ -1989,6 +2033,85 @@ function registerRoutePolicyTools(server, registry) {
|
|
|
1989
2033
|
);
|
|
1990
2034
|
}
|
|
1991
2035
|
|
|
2036
|
+
// src/http-server.ts
|
|
2037
|
+
var import_node_crypto = require("crypto");
|
|
2038
|
+
var import_node_http = __toESM(require("http"));
|
|
2039
|
+
var import_streamableHttp = require("@modelcontextprotocol/sdk/server/streamableHttp.js");
|
|
2040
|
+
var loopback = /* @__PURE__ */ new Set(["127.0.0.1", "::1", "localhost"]);
|
|
2041
|
+
var send = (res, status, body) => {
|
|
2042
|
+
res.writeHead(status, { "Content-Type": "application/json" }).end(JSON.stringify(body));
|
|
2043
|
+
};
|
|
2044
|
+
var error = (res, status, code, message) => send(res, status, { jsonrpc: "2.0", error: { code, message }, id: null });
|
|
2045
|
+
var matches = (value, expected) => {
|
|
2046
|
+
if (!value?.startsWith("Bearer ")) return false;
|
|
2047
|
+
const supplied = Buffer.from(value.slice(7));
|
|
2048
|
+
const wanted = Buffer.from(expected);
|
|
2049
|
+
return supplied.length === wanted.length && (0, import_node_crypto.timingSafeEqual)(supplied, wanted);
|
|
2050
|
+
};
|
|
2051
|
+
var readBody = (req, limit) => new Promise((resolve, reject) => {
|
|
2052
|
+
const chunks = [];
|
|
2053
|
+
let size = 0;
|
|
2054
|
+
let oversized = false;
|
|
2055
|
+
req.on("data", (chunk) => {
|
|
2056
|
+
size += chunk.length;
|
|
2057
|
+
oversized ||= size > limit;
|
|
2058
|
+
if (!oversized) chunks.push(chunk);
|
|
2059
|
+
});
|
|
2060
|
+
req.on("end", () => {
|
|
2061
|
+
if (oversized) return reject(Object.assign(new Error("Request body too large"), { statusCode: 413 }));
|
|
2062
|
+
try {
|
|
2063
|
+
resolve(JSON.parse(Buffer.concat(chunks).toString("utf8")));
|
|
2064
|
+
} catch {
|
|
2065
|
+
reject(Object.assign(new Error("Invalid JSON body"), { statusCode: 400 }));
|
|
2066
|
+
}
|
|
2067
|
+
});
|
|
2068
|
+
req.on("error", reject);
|
|
2069
|
+
});
|
|
2070
|
+
function validateHttpOptions(o) {
|
|
2071
|
+
if (!Number.isInteger(o.port) || o.port < 1 || o.port > 65535) throw new Error(`Invalid HTTP port: ${o.port}`);
|
|
2072
|
+
if (o.maxBodyBytes !== void 0 && (!Number.isInteger(o.maxBodyBytes) || o.maxBodyBytes < 1)) throw new Error(`Invalid HTTP body limit: ${o.maxBodyBytes}`);
|
|
2073
|
+
if (!loopback.has(o.host) && !o.bearerToken) throw new Error("A bearer token is required when binding beyond loopback");
|
|
2074
|
+
if (!loopback.has(o.host) && !o.allowedHosts?.length) throw new Error("HTTP_ALLOWED_HOSTS is required when binding beyond loopback");
|
|
2075
|
+
}
|
|
2076
|
+
function createHttpServer(factory, o) {
|
|
2077
|
+
validateHttpOptions(o);
|
|
2078
|
+
const hosts = new Set(o.allowedHosts?.map((v) => v.toLowerCase()));
|
|
2079
|
+
const origins = new Set(o.allowedOrigins?.map((v) => v.toLowerCase()));
|
|
2080
|
+
return import_node_http.default.createServer(async (req, res) => {
|
|
2081
|
+
const path = new URL(req.url ?? "/", "http://localhost").pathname;
|
|
2082
|
+
if (path === "/health") return send(res, 200, { status: "ok", server: o.serverName });
|
|
2083
|
+
if (path !== "/mcp") return void res.writeHead(404).end();
|
|
2084
|
+
if (hosts.size && !hosts.has((req.headers.host ?? "").toLowerCase())) return error(res, 403, -32e3, "Host not allowed");
|
|
2085
|
+
if (req.headers.origin && !origins.has(req.headers.origin.toLowerCase())) return error(res, 403, -32e3, "Origin not allowed");
|
|
2086
|
+
if (o.bearerToken && !matches(req.headers.authorization, o.bearerToken)) {
|
|
2087
|
+
res.setHeader("WWW-Authenticate", "Bearer");
|
|
2088
|
+
return error(res, 401, -32001, "Unauthorized");
|
|
2089
|
+
}
|
|
2090
|
+
if (req.method !== "POST") {
|
|
2091
|
+
res.setHeader("Allow", "POST");
|
|
2092
|
+
return error(res, 405, -32e3, "Method not allowed");
|
|
2093
|
+
}
|
|
2094
|
+
const server = factory();
|
|
2095
|
+
const transport = new import_streamableHttp.StreamableHTTPServerTransport({ sessionIdGenerator: void 0 });
|
|
2096
|
+
try {
|
|
2097
|
+
const body = await readBody(req, o.maxBodyBytes ?? 1048576);
|
|
2098
|
+
await server.connect(transport);
|
|
2099
|
+
await transport.handleRequest(req, res, body);
|
|
2100
|
+
res.once("close", () => {
|
|
2101
|
+
void transport.close();
|
|
2102
|
+
void server.close();
|
|
2103
|
+
});
|
|
2104
|
+
} catch (cause) {
|
|
2105
|
+
void transport.close();
|
|
2106
|
+
void server.close();
|
|
2107
|
+
if (!res.headersSent) {
|
|
2108
|
+
const status = cause.statusCode ?? 500;
|
|
2109
|
+
error(res, status, status === 400 ? -32700 : -32603, status < 500 ? cause.message : "Internal server error");
|
|
2110
|
+
}
|
|
2111
|
+
}
|
|
2112
|
+
});
|
|
2113
|
+
}
|
|
2114
|
+
|
|
1992
2115
|
// src/index.ts
|
|
1993
2116
|
async function main() {
|
|
1994
2117
|
let config;
|
|
@@ -2006,10 +2129,11 @@ async function main() {
|
|
|
2006
2129
|
process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0";
|
|
2007
2130
|
}
|
|
2008
2131
|
const registry = new DeviceRegistry(config.profiles);
|
|
2009
|
-
const
|
|
2010
|
-
|
|
2011
|
-
|
|
2012
|
-
|
|
2132
|
+
const createConfiguredServer = () => {
|
|
2133
|
+
const server = new import_mcp.McpServer({
|
|
2134
|
+
name: "sonicwall-mcp-server",
|
|
2135
|
+
version: "0.8.0",
|
|
2136
|
+
instructions: `SonicWall NSA MCP server \u2014 full read/write access to SonicWall firewalls via the SonicOS REST API.
|
|
2013
2137
|
|
|
2014
2138
|
## Device targeting
|
|
2015
2139
|
Use list_devices to discover available appliances and their names. Pass device='<name>' to target a specific appliance; omit it to use the primary/default device.
|
|
@@ -2035,28 +2159,34 @@ The SonicOS REST API is configuration-focused. Connection/flow ("traffic") logs
|
|
|
2035
2159
|
|
|
2036
2160
|
## Concurrent sessions
|
|
2037
2161
|
SonicOS allows only ONE concurrent API session per appliance. If a session error occurs, an existing admin or API session may need to be ended first.`
|
|
2038
|
-
|
|
2039
|
-
|
|
2040
|
-
|
|
2041
|
-
|
|
2042
|
-
|
|
2043
|
-
|
|
2044
|
-
|
|
2045
|
-
|
|
2046
|
-
|
|
2047
|
-
|
|
2048
|
-
|
|
2049
|
-
|
|
2050
|
-
|
|
2051
|
-
|
|
2052
|
-
|
|
2162
|
+
});
|
|
2163
|
+
registerSystemTools(server, registry);
|
|
2164
|
+
registerAddressObjectTools(server, registry);
|
|
2165
|
+
registerAddressGroupTools(server, registry);
|
|
2166
|
+
registerServiceObjectTools(server, registry);
|
|
2167
|
+
registerServiceGroupTools(server, registry);
|
|
2168
|
+
registerAccessRuleTools(server, registry);
|
|
2169
|
+
registerNatPolicyTools(server, registry);
|
|
2170
|
+
registerSecurityPolicyTools(server, registry);
|
|
2171
|
+
registerGeoIpTools(server, registry);
|
|
2172
|
+
registerLogTools(server, registry);
|
|
2173
|
+
registerZoneTools(server, registry);
|
|
2174
|
+
registerInterfaceTools(server, registry);
|
|
2175
|
+
registerScheduleTools(server, registry);
|
|
2176
|
+
registerRoutePolicyTools(server, registry);
|
|
2177
|
+
return server;
|
|
2178
|
+
};
|
|
2053
2179
|
const transport = config.transport;
|
|
2054
2180
|
if (transport === "stdio") {
|
|
2055
|
-
|
|
2056
|
-
|
|
2181
|
+
await createConfiguredServer().connect(new import_stdio.StdioServerTransport());
|
|
2182
|
+
} else if (transport === "http") {
|
|
2183
|
+
const host = process.env["SONICWALL_HTTP_HOST"] ?? "127.0.0.1";
|
|
2184
|
+
const port = Number.parseInt(process.env["SONICWALL_HTTP_PORT"] ?? "3000", 10);
|
|
2185
|
+
createHttpServer(createConfiguredServer, { host, port, serverName: "@teamnetwork-nz/sonicwall-api-mcp-server", bearerToken: process.env["SONICWALL_HTTP_BEARER_TOKEN"], allowedHosts: process.env["SONICWALL_HTTP_ALLOWED_HOSTS"]?.split(",").map((v) => v.trim()).filter(Boolean), allowedOrigins: process.env["SONICWALL_HTTP_ALLOWED_ORIGINS"]?.split(",").map((v) => v.trim()).filter(Boolean), maxBodyBytes: Number.parseInt(process.env["SONICWALL_HTTP_MAX_BODY_BYTES"] ?? "1048576", 10) }).listen(port, host, () => process.stderr.write(`sonicwall-mcp: HTTP listening on ${host}:${port}/mcp
|
|
2186
|
+
`));
|
|
2057
2187
|
} else {
|
|
2058
2188
|
process.stderr.write(
|
|
2059
|
-
`sonicwall-mcp: transport '${transport}'
|
|
2189
|
+
`sonicwall-mcp: unsupported transport '${transport}'. Use stdio or http.
|
|
2060
2190
|
`
|
|
2061
2191
|
);
|
|
2062
2192
|
process.exit(1);
|
package/package.json
CHANGED
|
@@ -1,51 +1,52 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "@teamnetwork-nz/sonicwall-api-mcp-server",
|
|
3
|
-
"version": "0.
|
|
1
|
+
{
|
|
2
|
+
"name": "@teamnetwork-nz/sonicwall-api-mcp-server",
|
|
3
|
+
"version": "0.8.0",
|
|
4
4
|
"description": "MCP server for SonicWall NSA appliance configuration — objects, rules, NAT, routing, and GeoIP lookup",
|
|
5
|
-
"keywords": [
|
|
6
|
-
"mcp",
|
|
7
|
-
"model-context-protocol",
|
|
8
|
-
"sonicwall",
|
|
9
|
-
"firewall",
|
|
10
|
-
"sonicOS",
|
|
11
|
-
"api",
|
|
12
|
-
"network",
|
|
13
|
-
"security"
|
|
14
|
-
],
|
|
15
|
-
"homepage": "https://github.com/teamnetwork-nz/sonicwall-api-mcp-server",
|
|
16
|
-
"bugs": {
|
|
17
|
-
"url": "https://github.com/teamnetwork-nz/sonicwall-api-mcp-server/issues"
|
|
18
|
-
},
|
|
19
|
-
"license": "MIT",
|
|
20
|
-
"author": "Daryl",
|
|
21
|
-
"repository": {
|
|
22
|
-
"type": "git",
|
|
23
|
-
"url": "https://github.com/teamnetwork-nz/sonicwall-api-mcp-server.git"
|
|
24
|
-
},
|
|
25
|
-
"main": "dist/index.js",
|
|
26
|
-
"bin": {
|
|
27
|
-
"sonicwall-mcp": "./dist/index.js"
|
|
28
|
-
},
|
|
29
|
-
"files": [
|
|
30
|
-
"dist/",
|
|
31
|
-
"README.md",
|
|
32
|
-
"LICENSE"
|
|
33
|
-
],
|
|
34
|
-
"scripts": {
|
|
35
|
-
"build": "tsup",
|
|
36
|
-
"
|
|
37
|
-
"
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
"
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
"
|
|
49
|
-
"
|
|
50
|
-
|
|
51
|
-
}
|
|
5
|
+
"keywords": [
|
|
6
|
+
"mcp",
|
|
7
|
+
"model-context-protocol",
|
|
8
|
+
"sonicwall",
|
|
9
|
+
"firewall",
|
|
10
|
+
"sonicOS",
|
|
11
|
+
"api",
|
|
12
|
+
"network",
|
|
13
|
+
"security"
|
|
14
|
+
],
|
|
15
|
+
"homepage": "https://github.com/teamnetwork-nz/sonicwall-api-mcp-server",
|
|
16
|
+
"bugs": {
|
|
17
|
+
"url": "https://github.com/teamnetwork-nz/sonicwall-api-mcp-server/issues"
|
|
18
|
+
},
|
|
19
|
+
"license": "MIT",
|
|
20
|
+
"author": "Daryl",
|
|
21
|
+
"repository": {
|
|
22
|
+
"type": "git",
|
|
23
|
+
"url": "https://github.com/teamnetwork-nz/sonicwall-api-mcp-server.git"
|
|
24
|
+
},
|
|
25
|
+
"main": "dist/index.js",
|
|
26
|
+
"bin": {
|
|
27
|
+
"sonicwall-mcp": "./dist/index.js"
|
|
28
|
+
},
|
|
29
|
+
"files": [
|
|
30
|
+
"dist/",
|
|
31
|
+
"README.md",
|
|
32
|
+
"LICENSE"
|
|
33
|
+
],
|
|
34
|
+
"scripts": {
|
|
35
|
+
"build": "tsup",
|
|
36
|
+
"test": "npm run build && node --test tests/*.test.mjs",
|
|
37
|
+
"prepublishOnly": "npm run build",
|
|
38
|
+
"start": "node dist/index.js"
|
|
39
|
+
},
|
|
40
|
+
"engines": {
|
|
41
|
+
"node": ">=18"
|
|
42
|
+
},
|
|
43
|
+
"dependencies": {
|
|
44
|
+
"@modelcontextprotocol/sdk": "^1.10.0",
|
|
45
|
+
"zod": "^3.23.0"
|
|
46
|
+
},
|
|
47
|
+
"devDependencies": {
|
|
48
|
+
"@types/node": "^20.0.0",
|
|
49
|
+
"tsup": "^8.0.0",
|
|
50
|
+
"typescript": "^5.4.0"
|
|
51
|
+
}
|
|
52
|
+
}
|