nodered-mcp-full 0.1.0 → 0.2.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 +2 -1
- package/index.js +35 -4
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -34,7 +34,8 @@ Built for real-world setups:
|
|
|
34
34
|
| Variable | Default | Description |
|
|
35
35
|
|----------|---------|-------------|
|
|
36
36
|
| `NODE_RED_URL` | `http://localhost:1880` | Base URL of the editor/admin API (no trailing slash) |
|
|
37
|
-
| `NODE_RED_TOKEN` | — |
|
|
37
|
+
| `NODE_RED_TOKEN` | — | Static bearer token (if you manage tokens yourself) |
|
|
38
|
+
| `NODE_RED_USER` / `NODE_RED_PASS` | — | **Recommended with adminAuth**: the server logs in automatically (`POST /auth/token`), caches and renews the token |
|
|
38
39
|
| `NODE_RED_CA_FILE` | — | Path to a CA certificate file for HTTPS behind a private reverse proxy |
|
|
39
40
|
| `NODE_RED_INSECURE_TLS` | `false` | Skip TLS verification (self-signed, testing only) |
|
|
40
41
|
| `NODE_RED_READ_ONLY` | `false` | Disable every write tool |
|
package/index.js
CHANGED
|
@@ -22,6 +22,8 @@ import os from "node:os";
|
|
|
22
22
|
// ---------------- environment ----------------
|
|
23
23
|
const URL_BASE = (process.env.NODE_RED_URL || "http://localhost:1880").replace(/\/+$/, "");
|
|
24
24
|
const TOKEN = process.env.NODE_RED_TOKEN || "";
|
|
25
|
+
const AUTH_USER = process.env.NODE_RED_USER || "";
|
|
26
|
+
const AUTH_PASS = process.env.NODE_RED_PASS || "";
|
|
25
27
|
const CA_FILE = process.env.NODE_RED_CA_FILE || "";
|
|
26
28
|
const INSECURE_TLS = process.env.NODE_RED_INSECURE_TLS === "true";
|
|
27
29
|
const READ_ONLY = process.env.NODE_RED_READ_ONLY === "true";
|
|
@@ -38,15 +40,42 @@ if (CA_FILE) {
|
|
|
38
40
|
catch (e) { console.error("nodered-mcp: cannot read NODE_RED_CA_FILE: " + CA_FILE); process.exit(1); }
|
|
39
41
|
}
|
|
40
42
|
|
|
43
|
+
// ---------------- auth token (auto login + cache) ----------------
|
|
44
|
+
let cachedToken = null, tokenExp = 0;
|
|
45
|
+
async function getToken(force) {
|
|
46
|
+
if (!AUTH_USER) return TOKEN;
|
|
47
|
+
const now = Date.now();
|
|
48
|
+
if (!force && cachedToken && now < tokenExp - 60000) return cachedToken;
|
|
49
|
+
return new Promise((resolve, reject) => {
|
|
50
|
+
let u;
|
|
51
|
+
try { u = new URL(URL_BASE + "/auth/token"); } catch (e) { reject(e); return; }
|
|
52
|
+
const mod = u.protocol === "https:" ? https : http;
|
|
53
|
+
const form = new URLSearchParams({ client_id: "node-red-editor", grant_type: "password", scope: "*", username: AUTH_USER, password: AUTH_PASS }).toString();
|
|
54
|
+
const req = mod.request({ hostname: u.hostname, port: u.port || (u.protocol === "https:" ? 443 : 80), path: u.pathname, method: "POST", headers: Object.assign({}, EXTRA_HEADERS, { "Content-Type": "application/x-www-form-urlencoded", "Content-Length": Buffer.byteLength(form) }), ca: ca || undefined, rejectUnauthorized: !INSECURE_TLS }, (res) => {
|
|
55
|
+
let data = ""; res.setEncoding("utf8");
|
|
56
|
+
res.on("data", c => data += c);
|
|
57
|
+
res.on("end", () => {
|
|
58
|
+
if (res.statusCode !== 200) { reject(new Error("auth login failed HTTP " + res.statusCode)); return; }
|
|
59
|
+
try { const j = JSON.parse(data); cachedToken = j.access_token; tokenExp = now + (j.expires_in || 604800) * 1000; resolve(cachedToken); }
|
|
60
|
+
catch (e) { reject(new Error("auth login: bad response")); }
|
|
61
|
+
});
|
|
62
|
+
});
|
|
63
|
+
req.setTimeout(TIMEOUT_MS, () => req.destroy(new Error("auth login timeout")));
|
|
64
|
+
req.on("error", reject);
|
|
65
|
+
req.write(form); req.end();
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
|
|
41
69
|
// ---------------- http client ----------------
|
|
42
|
-
function request(method, urlPath, body, extraHeaders) {
|
|
70
|
+
async function request(method, urlPath, body, extraHeaders) {
|
|
71
|
+
const tk = await getToken(false);
|
|
43
72
|
return new Promise((resolve, reject) => {
|
|
44
73
|
let u;
|
|
45
74
|
try { u = new URL(URL_BASE + urlPath); }
|
|
46
75
|
catch (e) { reject(new Error("invalid path or base URL")); return; }
|
|
47
76
|
const mod = u.protocol === "https:" ? https : http;
|
|
48
77
|
const headers = Object.assign({}, EXTRA_HEADERS, extraHeaders || {}, { Accept: "application/json" });
|
|
49
|
-
if (
|
|
78
|
+
if (tk) headers.Authorization = "Bearer " + tk;
|
|
50
79
|
let payload;
|
|
51
80
|
if (body !== undefined) { payload = JSON.stringify(body); headers["Content-Type"] = "application/json"; headers["Content-Length"] = Buffer.byteLength(payload); }
|
|
52
81
|
const req = mod.request({
|
|
@@ -71,7 +100,8 @@ function request(method, urlPath, body, extraHeaders) {
|
|
|
71
100
|
}
|
|
72
101
|
|
|
73
102
|
async function apiGet(p) {
|
|
74
|
-
|
|
103
|
+
let r = await request("GET", p);
|
|
104
|
+
if (r.status === 401 && AUTH_USER) { await getToken(true); r = await request("GET", p); }
|
|
75
105
|
if (r.status !== 200) throw new Error("GET " + p + " -> HTTP " + r.status + ": " + r.body.slice(0, 400));
|
|
76
106
|
return r.body;
|
|
77
107
|
}
|
|
@@ -108,7 +138,8 @@ async function backupFlows(label) {
|
|
|
108
138
|
// ---------------- deploy (full, the safe way) ----------------
|
|
109
139
|
async function deployFlows(flows) {
|
|
110
140
|
const backup = await backupFlows("pre-deploy");
|
|
111
|
-
|
|
141
|
+
let r = await request("POST", "/flows", flows, { "Node-RED-Deployment-Type": "flows" });
|
|
142
|
+
if (r.status === 401 && AUTH_USER) { await getToken(true); r = await request("POST", "/flows", flows, { "Node-RED-Deployment-Type": "flows" }); }
|
|
112
143
|
if (r.status !== 204 && r.status !== 200) {
|
|
113
144
|
throw new Error("deploy failed HTTP " + r.status + " (backup kept: " + backup + "): " + r.body.slice(0, 400));
|
|
114
145
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "nodered-mcp-full",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.0",
|
|
4
4
|
"description": "Full-featured MCP server for Node-RED: complete admin-API access for AI agents — read AND safe write (confirm + automatic backups + restore), env-driven for any setup: direct, reverse proxy with custom CA, adminAuth token, read-only mode.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"bin": {
|