n8n-ops-mcp 0.6.0 → 0.7.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 +17 -0
- package/dist/mcp-server.js +220 -20
- package/index.ts +13 -0
- package/openclaw.plugin.json +2 -2
- package/package.json +1 -1
- package/src/client.ts +27 -0
- package/src/tools/archive-workflow.ts +68 -0
- package/src/tools/delete-workflow.ts +130 -0
package/README.md
CHANGED
|
@@ -29,6 +29,9 @@ For a catalog/docs tool that indexes n8n's node library, see [n8n-mcp](https://w
|
|
|
29
29
|
| `n8n_activate` | Enable a workflow's triggers | ✓ |
|
|
30
30
|
| `n8n_deactivate` | Disable a workflow's triggers | ✓ |
|
|
31
31
|
| `n8n_save_workflow` | Overwrite a workflow with auto-backup + validation + confirm gate | ✓ |
|
|
32
|
+
| `n8n_archive_workflow` | Soft-delete a workflow (reversible; prefer over delete) | ✓ |
|
|
33
|
+
| `n8n_unarchive_workflow` | Restore an archived workflow (does NOT reactivate) | ✓ |
|
|
34
|
+
| `n8n_delete_workflow` | Permanently delete a workflow (confirm-gated, snapshot-before-delete) | ✓ |
|
|
32
35
|
| `n8n_cancel_execution` | Stop a running or waiting execution by id | ✓ |
|
|
33
36
|
| `n8n_retry_execution` | Retry a failed execution by id (returns a new execution) | ✓ |
|
|
34
37
|
| `n8n_delete_execution` | Permanently delete an execution record (confirm-gated, irreversible) | ✓ |
|
|
@@ -61,6 +64,12 @@ Write tools are hidden unless `N8N_ENABLE_EDIT=true`.
|
|
|
61
64
|
|
|
62
65
|
**`n8n_save_workflow`** — before writing: fetches the current version, snapshots it to `backupDir` as `<id>-<timestamp>.json` (mode 0600), runs `validateWorkflow` on the proposed state, and aborts on error-severity issues (pass `skipValidation: true` to bypass). Requires `confirm: true` to actually PUT; calling with `confirm: false` returns `ok: false` and never touches the API (omitting `confirm` is rejected at the MCP schema layer). Response includes the backup path and a `restoreHint`.
|
|
63
66
|
|
|
67
|
+
**`n8n_archive_workflow`** — `POST /workflows/{id}/archive`. Soft-deletes a workflow: triggers stop firing, the workflow disappears from the default UI list, but the definition and execution history are preserved. Idempotent (archiving an already-archived workflow returns the current state). No confirm gate — this is the safe cleanup path. Archiving deactivates as a side effect; the response surfaces `active: false` explicitly. Returns `ok: false` with `reason: "not_found"` on 404.
|
|
68
|
+
|
|
69
|
+
**`n8n_unarchive_workflow`** — `POST /workflows/{id}/unarchive`. Restores an archived workflow. Does NOT reactivate — triggers stay off until you call `n8n_activate` explicitly. Returns `ok: false` with `reason: "not_found"` on 404.
|
|
70
|
+
|
|
71
|
+
**`n8n_delete_workflow`** — `DELETE /workflows/{id}`. Permanent, irreversible. Before firing the DELETE: fetches the current workflow and snapshots it to `backupDir` as `<id>-DELETED-<timestamp>.json` (mode 0600). If the snapshot can't be written, the DELETE is aborted — there is no un-safety-netted path. Requires `confirm: true`; omitting it or passing `false` returns `ok: false` and never touches the API. Returns `ok: false` with `reason: "not_found"` on 404 (either before or after the snapshot). **Restore is NOT a one-call operation:** `n8n_save_workflow` overwrites an existing id, it does not recreate a deleted one. To restore, create a new workflow in the n8n UI (or via a future `n8n_create_workflow`) and paste the snapshot's nodes/connections/settings. Deleting does NOT cancel running executions — use `n8n_list_executions(workflowId, status='running')` + `n8n_cancel_execution` first if needed. **Prefer `n8n_archive_workflow` for cleanup** unless you truly need to scrub the workflow from n8n.
|
|
72
|
+
|
|
64
73
|
**`n8n_cancel_execution`** — `POST /executions/{id}/stop`. Closes the triage loop after `n8n_search_executions` locates a stuck run. Returns a success summary with the execution's final status, or `ok: false` with `reason: "not_found_or_finished"` if the id no longer matches a running execution (404).
|
|
65
74
|
|
|
66
75
|
**`n8n_retry_execution`** — `POST /executions/{id}/retry`. Creates a NEW execution — the response surfaces both `originalExecutionId` and `newExecutionId` so agents can follow up with `n8n_get_execution` on the retry. Optional `loadWorkflow: true` retries against the currently saved workflow instead of the version captured at original execution time. Returns `ok: false` with `reason: "not_found"` on 404 or `reason: "not_retryable"` on 409 (e.g. still running); all other API errors rethrow.
|
|
@@ -253,6 +262,14 @@ Calls `n8n_search_executions` to find the failed id, then `n8n_retry_execution`
|
|
|
253
262
|
|
|
254
263
|
Calls `n8n_search_executions` to find the ids, then `n8n_delete_executions` with `confirm: true` to purge up to 50 in one call. Deletion is irreversible.
|
|
255
264
|
|
|
265
|
+
> Archive the old "staging-bot" workflow — I might need it back someday *(requires `N8N_ENABLE_EDIT=true`)*
|
|
266
|
+
|
|
267
|
+
Calls `n8n_list_workflows` with a name filter, then `n8n_archive_workflow` on the match. Reversible via `n8n_unarchive_workflow` (you'll still need `n8n_activate` to turn triggers back on).
|
|
268
|
+
|
|
269
|
+
> Delete the abandoned "poc-scraper" workflow — it's been dead for months *(requires `N8N_ENABLE_EDIT=true`)*
|
|
270
|
+
|
|
271
|
+
Calls `n8n_list_workflows` to find the id, then `n8n_delete_workflow` with `confirm: true`. A snapshot lands in `backupDir` first, but restore from backup is not a one-call operation — prefer `n8n_archive_workflow` unless you're sure.
|
|
272
|
+
|
|
256
273
|
## Development
|
|
257
274
|
|
|
258
275
|
```bash
|
package/dist/mcp-server.js
CHANGED
|
@@ -7,10 +7,10 @@ import { z } from "zod";
|
|
|
7
7
|
|
|
8
8
|
// src/client.ts
|
|
9
9
|
var N8nApiError = class extends Error {
|
|
10
|
-
constructor(status,
|
|
11
|
-
super(`n8n ${status} on ${
|
|
10
|
+
constructor(status, path3, message) {
|
|
11
|
+
super(`n8n ${status} on ${path3}: ${message}`);
|
|
12
12
|
this.status = status;
|
|
13
|
-
this.path =
|
|
13
|
+
this.path = path3;
|
|
14
14
|
this.name = "N8nApiError";
|
|
15
15
|
}
|
|
16
16
|
status;
|
|
@@ -70,6 +70,30 @@ var N8nClient = class {
|
|
|
70
70
|
method: "POST"
|
|
71
71
|
});
|
|
72
72
|
}
|
|
73
|
+
async archiveWorkflow(id) {
|
|
74
|
+
if (!/^[A-Za-z0-9_-]+$/.test(id)) {
|
|
75
|
+
throw new Error(`Invalid workflow id: ${id}`);
|
|
76
|
+
}
|
|
77
|
+
return this.request(`/api/v1/workflows/${id}/archive`, {
|
|
78
|
+
method: "POST"
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
async unarchiveWorkflow(id) {
|
|
82
|
+
if (!/^[A-Za-z0-9_-]+$/.test(id)) {
|
|
83
|
+
throw new Error(`Invalid workflow id: ${id}`);
|
|
84
|
+
}
|
|
85
|
+
return this.request(`/api/v1/workflows/${id}/unarchive`, {
|
|
86
|
+
method: "POST"
|
|
87
|
+
});
|
|
88
|
+
}
|
|
89
|
+
async deleteWorkflow(id) {
|
|
90
|
+
if (!/^[A-Za-z0-9_-]+$/.test(id)) {
|
|
91
|
+
throw new Error(`Invalid workflow id: ${id}`);
|
|
92
|
+
}
|
|
93
|
+
return this.request(`/api/v1/workflows/${id}`, {
|
|
94
|
+
method: "DELETE"
|
|
95
|
+
});
|
|
96
|
+
}
|
|
73
97
|
async executeWorkflow(id, payload) {
|
|
74
98
|
if (!/^[A-Za-z0-9_-]+$/.test(id)) {
|
|
75
99
|
throw new Error(`Invalid workflow id: ${id}`);
|
|
@@ -80,8 +104,8 @@ var N8nClient = class {
|
|
|
80
104
|
});
|
|
81
105
|
}
|
|
82
106
|
async postWebhook(webhookPath, payload, opts = {}) {
|
|
83
|
-
const
|
|
84
|
-
const url = `${this.baseUrl}${
|
|
107
|
+
const path3 = webhookPath.startsWith("/") ? webhookPath : `/${webhookPath}`;
|
|
108
|
+
const url = `${this.baseUrl}${path3}`;
|
|
85
109
|
const method = (opts.method ?? "POST").toUpperCase();
|
|
86
110
|
const controller = new AbortController();
|
|
87
111
|
const timer = setTimeout(() => controller.abort(), this.timeoutMs);
|
|
@@ -110,7 +134,7 @@ var N8nClient = class {
|
|
|
110
134
|
return { status: res.status, body };
|
|
111
135
|
} catch (err) {
|
|
112
136
|
const msg = err instanceof Error ? err.message : String(err);
|
|
113
|
-
throw new Error(`n8n webhook to ${
|
|
137
|
+
throw new Error(`n8n webhook to ${path3} failed: ${redactKey(msg, this.apiKey)}`);
|
|
114
138
|
} finally {
|
|
115
139
|
clearTimeout(timer);
|
|
116
140
|
}
|
|
@@ -208,8 +232,8 @@ var N8nClient = class {
|
|
|
208
232
|
`/api/v1/executions${qs.toString() ? `?${qs}` : ""}`
|
|
209
233
|
);
|
|
210
234
|
}
|
|
211
|
-
async request(
|
|
212
|
-
const url = `${this.baseUrl}${
|
|
235
|
+
async request(path3, init = {}) {
|
|
236
|
+
const url = `${this.baseUrl}${path3}`;
|
|
213
237
|
const controller = new AbortController();
|
|
214
238
|
const timer = setTimeout(() => controller.abort(), this.timeoutMs);
|
|
215
239
|
const caller = init.signal;
|
|
@@ -235,19 +259,19 @@ var N8nClient = class {
|
|
|
235
259
|
});
|
|
236
260
|
const text = await res.text();
|
|
237
261
|
if (!res.ok) {
|
|
238
|
-
throw new N8nApiError(res.status,
|
|
262
|
+
throw new N8nApiError(res.status, path3, redactKey(text, this.apiKey));
|
|
239
263
|
}
|
|
240
264
|
if (!text) return {};
|
|
241
265
|
return JSON.parse(text);
|
|
242
266
|
} catch (err) {
|
|
243
267
|
if (err instanceof N8nApiError) throw err;
|
|
244
268
|
if (isAbortError(err) && caller?.aborted) {
|
|
245
|
-
const e = new Error(`n8n request to ${
|
|
269
|
+
const e = new Error(`n8n request to ${path3} aborted`);
|
|
246
270
|
e.name = "AbortError";
|
|
247
271
|
throw e;
|
|
248
272
|
}
|
|
249
273
|
const msg = err instanceof Error ? err.message : String(err);
|
|
250
|
-
throw new Error(`n8n request to ${
|
|
274
|
+
throw new Error(`n8n request to ${path3} failed: ${redactKey(msg, this.apiKey)}`);
|
|
251
275
|
} finally {
|
|
252
276
|
clearTimeout(timer);
|
|
253
277
|
if (caller && onCallerAbort) {
|
|
@@ -996,7 +1020,7 @@ function extractWebhook(rawNode, wf, baseUrl) {
|
|
|
996
1020
|
if (type === "n8n-nodes-base.webhook") {
|
|
997
1021
|
const pathRaw = typeof params.path === "string" && params.path.trim() ? params.path.trim() : typeof n.webhookId === "string" ? String(n.webhookId) : "";
|
|
998
1022
|
if (!pathRaw) return null;
|
|
999
|
-
const
|
|
1023
|
+
const path3 = pathRaw.replace(/^\/+/, "");
|
|
1000
1024
|
const method = typeof params.httpMethod === "string" && params.httpMethod.trim() ? params.httpMethod.toUpperCase() : "POST";
|
|
1001
1025
|
return {
|
|
1002
1026
|
workflowId: wf.id,
|
|
@@ -1005,15 +1029,15 @@ function extractWebhook(rawNode, wf, baseUrl) {
|
|
|
1005
1029
|
nodeName: name,
|
|
1006
1030
|
nodeType: type,
|
|
1007
1031
|
method,
|
|
1008
|
-
path: `/webhook/${
|
|
1009
|
-
triggerUrl: `${base}/webhook/${
|
|
1010
|
-
testTriggerUrl: `${base}/webhook-test/${
|
|
1032
|
+
path: `/webhook/${path3}`,
|
|
1033
|
+
triggerUrl: `${base}/webhook/${path3}`,
|
|
1034
|
+
testTriggerUrl: `${base}/webhook-test/${path3}`
|
|
1011
1035
|
};
|
|
1012
1036
|
}
|
|
1013
1037
|
if (type === "n8n-nodes-base.formTrigger") {
|
|
1014
1038
|
const pathRaw = typeof params.path === "string" && params.path.trim() ? params.path.trim() : typeof n.webhookId === "string" ? String(n.webhookId) : "";
|
|
1015
1039
|
if (!pathRaw) return null;
|
|
1016
|
-
const
|
|
1040
|
+
const path3 = pathRaw.replace(/^\/+/, "");
|
|
1017
1041
|
return {
|
|
1018
1042
|
workflowId: wf.id,
|
|
1019
1043
|
workflowName: wf.name,
|
|
@@ -1021,8 +1045,8 @@ function extractWebhook(rawNode, wf, baseUrl) {
|
|
|
1021
1045
|
nodeName: name,
|
|
1022
1046
|
nodeType: type,
|
|
1023
1047
|
method: "POST",
|
|
1024
|
-
path: `/form/${
|
|
1025
|
-
triggerUrl: `${base}/form/${
|
|
1048
|
+
path: `/form/${path3}`,
|
|
1049
|
+
triggerUrl: `${base}/form/${path3}`
|
|
1026
1050
|
};
|
|
1027
1051
|
}
|
|
1028
1052
|
return null;
|
|
@@ -1647,8 +1671,168 @@ function createDeleteExecutionsTool(getClient) {
|
|
|
1647
1671
|
};
|
|
1648
1672
|
}
|
|
1649
1673
|
|
|
1674
|
+
// src/tools/archive-workflow.ts
|
|
1675
|
+
import { Type as Type16 } from "@sinclair/typebox";
|
|
1676
|
+
var Schema16 = Type16.Object(
|
|
1677
|
+
{
|
|
1678
|
+
id: Type16.String({ description: "Workflow id (from n8n_list_workflows)." })
|
|
1679
|
+
},
|
|
1680
|
+
{ additionalProperties: false }
|
|
1681
|
+
);
|
|
1682
|
+
function createArchiveWorkflowTool(getClient) {
|
|
1683
|
+
return buildTool(getClient, "archive");
|
|
1684
|
+
}
|
|
1685
|
+
function createUnarchiveWorkflowTool(getClient) {
|
|
1686
|
+
return buildTool(getClient, "unarchive");
|
|
1687
|
+
}
|
|
1688
|
+
function buildTool(getClient, action) {
|
|
1689
|
+
const name = action === "archive" ? "n8n_archive_workflow" : "n8n_unarchive_workflow";
|
|
1690
|
+
const label = action === "archive" ? "n8n: archive workflow" : "n8n: unarchive workflow";
|
|
1691
|
+
const description = action === "archive" ? "Soft-delete (archive) an n8n workflow via POST /workflows/{id}/archive. Reversible: pair with n8n_unarchive_workflow to restore. Idempotent: archiving an already-archived workflow returns the current state. Side effect: archiving deactivates the workflow, so triggers (webhooks, schedules) stop firing. Prefer this over n8n_delete_workflow for cleanup; it keeps history and definitions intact. Requires enableEdit." : "Restore an archived workflow via POST /workflows/{id}/unarchive. Does NOT reactivate: triggers stay off until n8n_activate is called explicitly. Requires enableEdit.";
|
|
1692
|
+
return {
|
|
1693
|
+
name,
|
|
1694
|
+
label,
|
|
1695
|
+
description,
|
|
1696
|
+
parameters: Schema16,
|
|
1697
|
+
execute: async (_toolCallId, rawParams) => {
|
|
1698
|
+
const { id } = rawParams;
|
|
1699
|
+
const client = getClient();
|
|
1700
|
+
try {
|
|
1701
|
+
const wf = action === "archive" ? await client.archiveWorkflow(id) : await client.unarchiveWorkflow(id);
|
|
1702
|
+
return jsonToolResult({
|
|
1703
|
+
ok: true,
|
|
1704
|
+
action,
|
|
1705
|
+
workflowId: wf.id,
|
|
1706
|
+
workflowName: wf.name,
|
|
1707
|
+
active: wf.active,
|
|
1708
|
+
isArchived: wf.isArchived ?? action === "archive",
|
|
1709
|
+
updatedAt: wf.updatedAt
|
|
1710
|
+
});
|
|
1711
|
+
} catch (err) {
|
|
1712
|
+
if (err instanceof N8nApiError && err.status === 404) {
|
|
1713
|
+
return jsonToolResult({
|
|
1714
|
+
ok: false,
|
|
1715
|
+
action,
|
|
1716
|
+
workflowId: id,
|
|
1717
|
+
reason: "not_found",
|
|
1718
|
+
message: "Workflow not found. It may have been deleted or never existed."
|
|
1719
|
+
});
|
|
1720
|
+
}
|
|
1721
|
+
throw err;
|
|
1722
|
+
}
|
|
1723
|
+
}
|
|
1724
|
+
};
|
|
1725
|
+
}
|
|
1726
|
+
|
|
1727
|
+
// src/tools/delete-workflow.ts
|
|
1728
|
+
import { Type as Type17 } from "@sinclair/typebox";
|
|
1729
|
+
import { promises as fs2 } from "fs";
|
|
1730
|
+
import { homedir as homedir2 } from "os";
|
|
1731
|
+
import path2 from "path";
|
|
1732
|
+
var Schema17 = Type17.Object(
|
|
1733
|
+
{
|
|
1734
|
+
id: Type17.String({ description: "Workflow id to delete (from n8n_list_workflows)." }),
|
|
1735
|
+
confirm: Type17.Boolean({
|
|
1736
|
+
description: "Must be true to actually delete. A snapshot of the workflow is written to backupDir before the DELETE fires. Deletion is irreversible via the Public API; restore is not a one-call operation (see restoreHint). Prefer n8n_archive_workflow for reversible cleanup."
|
|
1737
|
+
})
|
|
1738
|
+
},
|
|
1739
|
+
{ additionalProperties: false }
|
|
1740
|
+
);
|
|
1741
|
+
function createDeleteWorkflowTool(deps) {
|
|
1742
|
+
return {
|
|
1743
|
+
name: "n8n_delete_workflow",
|
|
1744
|
+
label: "n8n: delete workflow",
|
|
1745
|
+
description: "Permanently delete an n8n workflow via DELETE /workflows/{id}. Irreversible. Snapshots the current workflow (nodes + connections + settings) to backupDir BEFORE firing the DELETE; if the snapshot fails, the delete is aborted. Does NOT cancel running executions first; those become orphans. Use n8n_list_executions(workflowId, status='running') + n8n_cancel_execution first if needed. Prefer n8n_archive_workflow for reversible cleanup. Requires enableEdit and explicit confirm=true.",
|
|
1746
|
+
parameters: Schema17,
|
|
1747
|
+
execute: async (_toolCallId, rawParams) => {
|
|
1748
|
+
const { id, confirm } = rawParams;
|
|
1749
|
+
if (!confirm) {
|
|
1750
|
+
return jsonToolResult({
|
|
1751
|
+
ok: false,
|
|
1752
|
+
action: "delete",
|
|
1753
|
+
workflowId: id,
|
|
1754
|
+
error: "confirm must be true to delete",
|
|
1755
|
+
hint: "Deletion is irreversible. Prefer n8n_archive_workflow for reversible cleanup, or fetch n8n_get_workflow with includeDefinition=true and save the output before calling this with confirm=true."
|
|
1756
|
+
});
|
|
1757
|
+
}
|
|
1758
|
+
const client = deps.getClient();
|
|
1759
|
+
let current;
|
|
1760
|
+
try {
|
|
1761
|
+
current = await client.getWorkflow(id);
|
|
1762
|
+
} catch (err) {
|
|
1763
|
+
if (err instanceof N8nApiError && err.status === 404) {
|
|
1764
|
+
return jsonToolResult({
|
|
1765
|
+
ok: false,
|
|
1766
|
+
action: "delete",
|
|
1767
|
+
workflowId: id,
|
|
1768
|
+
reason: "not_found",
|
|
1769
|
+
message: "Workflow not found. It may have already been deleted or never existed."
|
|
1770
|
+
});
|
|
1771
|
+
}
|
|
1772
|
+
throw err;
|
|
1773
|
+
}
|
|
1774
|
+
let backupPath;
|
|
1775
|
+
try {
|
|
1776
|
+
backupPath = await writeBackup2(resolveBackupDir2(deps.backupDir), current);
|
|
1777
|
+
} catch (err) {
|
|
1778
|
+
const msg = err instanceof Error ? err.message : String(err);
|
|
1779
|
+
return jsonToolResult({
|
|
1780
|
+
ok: false,
|
|
1781
|
+
action: "delete",
|
|
1782
|
+
workflowId: id,
|
|
1783
|
+
error: `backup failed; delete aborted: ${msg}`,
|
|
1784
|
+
hint: "The workflow was NOT deleted because the safety snapshot could not be written. Check backupDir permissions and retry."
|
|
1785
|
+
});
|
|
1786
|
+
}
|
|
1787
|
+
try {
|
|
1788
|
+
const deleted = await client.deleteWorkflow(id);
|
|
1789
|
+
return jsonToolResult({
|
|
1790
|
+
ok: true,
|
|
1791
|
+
action: "delete",
|
|
1792
|
+
workflowId: deleted.id ?? id,
|
|
1793
|
+
workflowName: deleted.name ?? current.name,
|
|
1794
|
+
backupPath,
|
|
1795
|
+
restoreHint: `Snapshot saved at ${backupPath}. Restore is NOT a one-call operation: n8n_save_workflow overwrites an existing workflow id, it does not recreate a deleted one. To restore, create a new workflow in the n8n UI (or via a future n8n_create_workflow) and paste the nodes/connections/settings from the snapshot.`
|
|
1796
|
+
});
|
|
1797
|
+
} catch (err) {
|
|
1798
|
+
if (err instanceof N8nApiError && err.status === 404) {
|
|
1799
|
+
return jsonToolResult({
|
|
1800
|
+
ok: false,
|
|
1801
|
+
action: "delete",
|
|
1802
|
+
workflowId: id,
|
|
1803
|
+
reason: "not_found",
|
|
1804
|
+
message: "Workflow disappeared between snapshot and delete (404). A snapshot was still captured.",
|
|
1805
|
+
backupPath
|
|
1806
|
+
});
|
|
1807
|
+
}
|
|
1808
|
+
const msg = err instanceof Error ? err.message : String(err);
|
|
1809
|
+
return jsonToolResult({
|
|
1810
|
+
ok: false,
|
|
1811
|
+
action: "delete",
|
|
1812
|
+
workflowId: id,
|
|
1813
|
+
error: `delete failed: ${msg}`,
|
|
1814
|
+
backupPath,
|
|
1815
|
+
hint: `Snapshot preserved at ${backupPath}. Server state may or may not have been mutated; fetch n8n_get_workflow to verify.`
|
|
1816
|
+
});
|
|
1817
|
+
}
|
|
1818
|
+
}
|
|
1819
|
+
};
|
|
1820
|
+
}
|
|
1821
|
+
function resolveBackupDir2(configured) {
|
|
1822
|
+
const raw = configured?.trim() || "~/.n8n-backups";
|
|
1823
|
+
return raw.startsWith("~") ? path2.join(homedir2(), raw.slice(1).replace(/^\/+/, "")) : raw;
|
|
1824
|
+
}
|
|
1825
|
+
async function writeBackup2(dir, wf) {
|
|
1826
|
+
await fs2.mkdir(dir, { recursive: true });
|
|
1827
|
+
const stamp = (/* @__PURE__ */ new Date()).toISOString().replace(/[:.]/g, "-").replace("T", "_").replace("Z", "");
|
|
1828
|
+
const safeName = String(wf.id).replace(/[^A-Za-z0-9_-]/g, "_");
|
|
1829
|
+
const file = path2.join(dir, `${safeName}-DELETED-${stamp}.json`);
|
|
1830
|
+
await fs2.writeFile(file, JSON.stringify(wf, null, 2), { mode: 384 });
|
|
1831
|
+
return file;
|
|
1832
|
+
}
|
|
1833
|
+
|
|
1650
1834
|
// mcp-server.ts
|
|
1651
|
-
var VERSION = "0.
|
|
1835
|
+
var VERSION = "0.7.0";
|
|
1652
1836
|
function readConfigFromEnv() {
|
|
1653
1837
|
const baseUrl = (process.env.N8N_BASE_URL ?? "").trim();
|
|
1654
1838
|
if (!baseUrl) {
|
|
@@ -1832,8 +2016,24 @@ async function main() {
|
|
|
1832
2016
|
confirm: z.boolean().describe(
|
|
1833
2017
|
"Must be true to actually delete. Deletion is irreversible: execution logs, per-node run data, and error payloads are erased."
|
|
1834
2018
|
),
|
|
1835
|
-
concurrency: z.number().int().min(1).max(10).optional().describe("Parallel DELETE requests. Default 3. Keep low
|
|
2019
|
+
concurrency: z.number().int().min(1).max(10).optional().describe("Parallel DELETE requests. Default 3. Keep low - n8n shares a database.")
|
|
1836
2020
|
});
|
|
2021
|
+
bind(server, createArchiveWorkflowTool(getClient), {
|
|
2022
|
+
id: z.string().describe("Workflow id to archive (from n8n_list_workflows).")
|
|
2023
|
+
});
|
|
2024
|
+
bind(server, createUnarchiveWorkflowTool(getClient), {
|
|
2025
|
+
id: z.string().describe("Workflow id to unarchive (from n8n_list_workflows).")
|
|
2026
|
+
});
|
|
2027
|
+
bind(
|
|
2028
|
+
server,
|
|
2029
|
+
createDeleteWorkflowTool({ getClient, backupDir: config.backupDir }),
|
|
2030
|
+
{
|
|
2031
|
+
id: z.string().describe("Workflow id to delete (from n8n_list_workflows)."),
|
|
2032
|
+
confirm: z.boolean().describe(
|
|
2033
|
+
"Must be true to actually delete. A snapshot is written to backupDir before the DELETE; restore is NOT a one-call operation. Prefer n8n_archive_workflow for reversible cleanup."
|
|
2034
|
+
)
|
|
2035
|
+
}
|
|
2036
|
+
);
|
|
1837
2037
|
}
|
|
1838
2038
|
const transport = new StdioServerTransport();
|
|
1839
2039
|
await server.connect(transport);
|
package/index.ts
CHANGED
|
@@ -16,6 +16,11 @@ import { createCancelExecutionTool } from "./src/tools/cancel-execution.ts";
|
|
|
16
16
|
import { createRetryExecutionTool } from "./src/tools/retry-execution.ts";
|
|
17
17
|
import { createDeleteExecutionTool } from "./src/tools/delete-execution.ts";
|
|
18
18
|
import { createDeleteExecutionsTool } from "./src/tools/delete-executions.ts";
|
|
19
|
+
import {
|
|
20
|
+
createArchiveWorkflowTool,
|
|
21
|
+
createUnarchiveWorkflowTool,
|
|
22
|
+
} from "./src/tools/archive-workflow.ts";
|
|
23
|
+
import { createDeleteWorkflowTool } from "./src/tools/delete-workflow.ts";
|
|
19
24
|
|
|
20
25
|
export default definePluginEntry({
|
|
21
26
|
id: "n8n",
|
|
@@ -57,6 +62,14 @@ export default definePluginEntry({
|
|
|
57
62
|
api.registerTool(createRetryExecutionTool(getClient) as AnyAgentTool);
|
|
58
63
|
api.registerTool(createDeleteExecutionTool(getClient) as AnyAgentTool);
|
|
59
64
|
api.registerTool(createDeleteExecutionsTool(getClient) as AnyAgentTool);
|
|
65
|
+
api.registerTool(createArchiveWorkflowTool(getClient) as AnyAgentTool);
|
|
66
|
+
api.registerTool(createUnarchiveWorkflowTool(getClient) as AnyAgentTool);
|
|
67
|
+
api.registerTool(
|
|
68
|
+
createDeleteWorkflowTool({
|
|
69
|
+
getClient,
|
|
70
|
+
backupDir: config.backupDir,
|
|
71
|
+
}) as AnyAgentTool,
|
|
72
|
+
);
|
|
60
73
|
}
|
|
61
74
|
},
|
|
62
75
|
});
|
package/openclaw.plugin.json
CHANGED
|
@@ -24,7 +24,7 @@
|
|
|
24
24
|
"enableEdit": {
|
|
25
25
|
"type": "boolean",
|
|
26
26
|
"default": false,
|
|
27
|
-
"description": "Enable write tools (
|
|
27
|
+
"description": "Enable the full workflow + execution lifecycle write tools (activate, deactivate, save, archive, unarchive, delete-workflow, cancel-execution, retry-execution, delete-execution, delete-executions). Off by default. Destructive tools (save, delete-workflow, delete-execution, delete-executions) are confirm-gated; save and delete-workflow snapshot to backupDir first."
|
|
28
28
|
},
|
|
29
29
|
"maxExecutionLogBytes": {
|
|
30
30
|
"type": "number",
|
|
@@ -55,7 +55,7 @@
|
|
|
55
55
|
},
|
|
56
56
|
"enableEdit": {
|
|
57
57
|
"label": "Enable Edit Tools",
|
|
58
|
-
"help": "Exposes
|
|
58
|
+
"help": "Exposes all workflow + execution lifecycle write tools. Workflows: activate, deactivate, save (confirm-gated, auto-backup), archive (reversible soft-delete), unarchive, delete-workflow (confirm-gated, snapshot-before-delete, restore is not one-call). Executions: cancel, retry, delete, delete-executions (batch fan-out, confirm-gated, capped at 50 ids, idempotent on 404, 5xx aborts the batch). Prefer archive over delete-workflow for cleanup."
|
|
59
59
|
},
|
|
60
60
|
"maxExecutionLogBytes": {
|
|
61
61
|
"label": "Max Log Bytes",
|
package/package.json
CHANGED
package/src/client.ts
CHANGED
|
@@ -157,6 +157,33 @@ export class N8nClient {
|
|
|
157
157
|
});
|
|
158
158
|
}
|
|
159
159
|
|
|
160
|
+
async archiveWorkflow(id: string): Promise<N8nWorkflow> {
|
|
161
|
+
if (!/^[A-Za-z0-9_-]+$/.test(id)) {
|
|
162
|
+
throw new Error(`Invalid workflow id: ${id}`);
|
|
163
|
+
}
|
|
164
|
+
return this.request<N8nWorkflow>(`/api/v1/workflows/${id}/archive`, {
|
|
165
|
+
method: "POST",
|
|
166
|
+
});
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
async unarchiveWorkflow(id: string): Promise<N8nWorkflow> {
|
|
170
|
+
if (!/^[A-Za-z0-9_-]+$/.test(id)) {
|
|
171
|
+
throw new Error(`Invalid workflow id: ${id}`);
|
|
172
|
+
}
|
|
173
|
+
return this.request<N8nWorkflow>(`/api/v1/workflows/${id}/unarchive`, {
|
|
174
|
+
method: "POST",
|
|
175
|
+
});
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
async deleteWorkflow(id: string): Promise<N8nWorkflow> {
|
|
179
|
+
if (!/^[A-Za-z0-9_-]+$/.test(id)) {
|
|
180
|
+
throw new Error(`Invalid workflow id: ${id}`);
|
|
181
|
+
}
|
|
182
|
+
return this.request<N8nWorkflow>(`/api/v1/workflows/${id}`, {
|
|
183
|
+
method: "DELETE",
|
|
184
|
+
});
|
|
185
|
+
}
|
|
186
|
+
|
|
160
187
|
async executeWorkflow(
|
|
161
188
|
id: string,
|
|
162
189
|
payload?: Record<string, unknown>,
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import { Type } from "@sinclair/typebox";
|
|
2
|
+
import { N8nApiError, type N8nClient, type N8nWorkflow } from "../client.ts";
|
|
3
|
+
import { jsonToolResult } from "./result.ts";
|
|
4
|
+
|
|
5
|
+
const Schema = Type.Object(
|
|
6
|
+
{
|
|
7
|
+
id: Type.String({ description: "Workflow id (from n8n_list_workflows)." }),
|
|
8
|
+
},
|
|
9
|
+
{ additionalProperties: false },
|
|
10
|
+
);
|
|
11
|
+
|
|
12
|
+
type Action = "archive" | "unarchive";
|
|
13
|
+
|
|
14
|
+
export function createArchiveWorkflowTool(getClient: () => N8nClient) {
|
|
15
|
+
return buildTool(getClient, "archive");
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export function createUnarchiveWorkflowTool(getClient: () => N8nClient) {
|
|
19
|
+
return buildTool(getClient, "unarchive");
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
function buildTool(getClient: () => N8nClient, action: Action) {
|
|
23
|
+
const name = action === "archive" ? "n8n_archive_workflow" : "n8n_unarchive_workflow";
|
|
24
|
+
const label =
|
|
25
|
+
action === "archive" ? "n8n: archive workflow" : "n8n: unarchive workflow";
|
|
26
|
+
const description =
|
|
27
|
+
action === "archive"
|
|
28
|
+
? "Soft-delete (archive) an n8n workflow via POST /workflows/{id}/archive. Reversible: pair with n8n_unarchive_workflow to restore. Idempotent: archiving an already-archived workflow returns the current state. Side effect: archiving deactivates the workflow, so triggers (webhooks, schedules) stop firing. Prefer this over n8n_delete_workflow for cleanup; it keeps history and definitions intact. Requires enableEdit."
|
|
29
|
+
: "Restore an archived workflow via POST /workflows/{id}/unarchive. Does NOT reactivate: triggers stay off until n8n_activate is called explicitly. Requires enableEdit.";
|
|
30
|
+
|
|
31
|
+
return {
|
|
32
|
+
name,
|
|
33
|
+
label,
|
|
34
|
+
description,
|
|
35
|
+
parameters: Schema,
|
|
36
|
+
execute: async (_toolCallId: string, rawParams: Record<string, unknown>) => {
|
|
37
|
+
const { id } = rawParams as { id: string };
|
|
38
|
+
const client = getClient();
|
|
39
|
+
try {
|
|
40
|
+
const wf: N8nWorkflow =
|
|
41
|
+
action === "archive"
|
|
42
|
+
? await client.archiveWorkflow(id)
|
|
43
|
+
: await client.unarchiveWorkflow(id);
|
|
44
|
+
return jsonToolResult({
|
|
45
|
+
ok: true,
|
|
46
|
+
action,
|
|
47
|
+
workflowId: wf.id,
|
|
48
|
+
workflowName: wf.name,
|
|
49
|
+
active: wf.active,
|
|
50
|
+
isArchived: wf.isArchived ?? (action === "archive"),
|
|
51
|
+
updatedAt: wf.updatedAt,
|
|
52
|
+
});
|
|
53
|
+
} catch (err) {
|
|
54
|
+
if (err instanceof N8nApiError && err.status === 404) {
|
|
55
|
+
return jsonToolResult({
|
|
56
|
+
ok: false,
|
|
57
|
+
action,
|
|
58
|
+
workflowId: id,
|
|
59
|
+
reason: "not_found",
|
|
60
|
+
message:
|
|
61
|
+
"Workflow not found. It may have been deleted or never existed.",
|
|
62
|
+
});
|
|
63
|
+
}
|
|
64
|
+
throw err;
|
|
65
|
+
}
|
|
66
|
+
},
|
|
67
|
+
};
|
|
68
|
+
}
|
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
import { Type } from "@sinclair/typebox";
|
|
2
|
+
import { promises as fs } from "node:fs";
|
|
3
|
+
import { homedir } from "node:os";
|
|
4
|
+
import path from "node:path";
|
|
5
|
+
import { N8nApiError, type N8nClient, type N8nWorkflow } from "../client.ts";
|
|
6
|
+
import { jsonToolResult } from "./result.ts";
|
|
7
|
+
|
|
8
|
+
const Schema = Type.Object(
|
|
9
|
+
{
|
|
10
|
+
id: Type.String({ description: "Workflow id to delete (from n8n_list_workflows)." }),
|
|
11
|
+
confirm: Type.Boolean({
|
|
12
|
+
description:
|
|
13
|
+
"Must be true to actually delete. A snapshot of the workflow is written to backupDir before the DELETE fires. Deletion is irreversible via the Public API; restore is not a one-call operation (see restoreHint). Prefer n8n_archive_workflow for reversible cleanup.",
|
|
14
|
+
}),
|
|
15
|
+
},
|
|
16
|
+
{ additionalProperties: false },
|
|
17
|
+
);
|
|
18
|
+
|
|
19
|
+
export interface DeleteWorkflowDeps {
|
|
20
|
+
getClient: () => N8nClient;
|
|
21
|
+
backupDir?: string;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export function createDeleteWorkflowTool(deps: DeleteWorkflowDeps) {
|
|
25
|
+
return {
|
|
26
|
+
name: "n8n_delete_workflow",
|
|
27
|
+
label: "n8n: delete workflow",
|
|
28
|
+
description:
|
|
29
|
+
"Permanently delete an n8n workflow via DELETE /workflows/{id}. Irreversible. Snapshots the current workflow (nodes + connections + settings) to backupDir BEFORE firing the DELETE; if the snapshot fails, the delete is aborted. Does NOT cancel running executions first; those become orphans. Use n8n_list_executions(workflowId, status='running') + n8n_cancel_execution first if needed. Prefer n8n_archive_workflow for reversible cleanup. Requires enableEdit and explicit confirm=true.",
|
|
30
|
+
parameters: Schema,
|
|
31
|
+
execute: async (_toolCallId: string, rawParams: Record<string, unknown>) => {
|
|
32
|
+
const { id, confirm } = rawParams as { id: string; confirm: boolean };
|
|
33
|
+
if (!confirm) {
|
|
34
|
+
return jsonToolResult({
|
|
35
|
+
ok: false,
|
|
36
|
+
action: "delete",
|
|
37
|
+
workflowId: id,
|
|
38
|
+
error: "confirm must be true to delete",
|
|
39
|
+
hint: "Deletion is irreversible. Prefer n8n_archive_workflow for reversible cleanup, or fetch n8n_get_workflow with includeDefinition=true and save the output before calling this with confirm=true.",
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
const client = deps.getClient();
|
|
44
|
+
|
|
45
|
+
let current: N8nWorkflow;
|
|
46
|
+
try {
|
|
47
|
+
current = await client.getWorkflow(id);
|
|
48
|
+
} catch (err) {
|
|
49
|
+
if (err instanceof N8nApiError && err.status === 404) {
|
|
50
|
+
return jsonToolResult({
|
|
51
|
+
ok: false,
|
|
52
|
+
action: "delete",
|
|
53
|
+
workflowId: id,
|
|
54
|
+
reason: "not_found",
|
|
55
|
+
message:
|
|
56
|
+
"Workflow not found. It may have already been deleted or never existed.",
|
|
57
|
+
});
|
|
58
|
+
}
|
|
59
|
+
throw err;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
let backupPath: string;
|
|
63
|
+
try {
|
|
64
|
+
backupPath = await writeBackup(resolveBackupDir(deps.backupDir), current);
|
|
65
|
+
} catch (err) {
|
|
66
|
+
const msg = err instanceof Error ? err.message : String(err);
|
|
67
|
+
return jsonToolResult({
|
|
68
|
+
ok: false,
|
|
69
|
+
action: "delete",
|
|
70
|
+
workflowId: id,
|
|
71
|
+
error: `backup failed; delete aborted: ${msg}`,
|
|
72
|
+
hint: "The workflow was NOT deleted because the safety snapshot could not be written. Check backupDir permissions and retry.",
|
|
73
|
+
});
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
try {
|
|
77
|
+
const deleted = await client.deleteWorkflow(id);
|
|
78
|
+
return jsonToolResult({
|
|
79
|
+
ok: true,
|
|
80
|
+
action: "delete",
|
|
81
|
+
workflowId: deleted.id ?? id,
|
|
82
|
+
workflowName: deleted.name ?? current.name,
|
|
83
|
+
backupPath,
|
|
84
|
+
restoreHint: `Snapshot saved at ${backupPath}. Restore is NOT a one-call operation: n8n_save_workflow overwrites an existing workflow id, it does not recreate a deleted one. To restore, create a new workflow in the n8n UI (or via a future n8n_create_workflow) and paste the nodes/connections/settings from the snapshot.`,
|
|
85
|
+
});
|
|
86
|
+
} catch (err) {
|
|
87
|
+
if (err instanceof N8nApiError && err.status === 404) {
|
|
88
|
+
return jsonToolResult({
|
|
89
|
+
ok: false,
|
|
90
|
+
action: "delete",
|
|
91
|
+
workflowId: id,
|
|
92
|
+
reason: "not_found",
|
|
93
|
+
message:
|
|
94
|
+
"Workflow disappeared between snapshot and delete (404). A snapshot was still captured.",
|
|
95
|
+
backupPath,
|
|
96
|
+
});
|
|
97
|
+
}
|
|
98
|
+
const msg = err instanceof Error ? err.message : String(err);
|
|
99
|
+
return jsonToolResult({
|
|
100
|
+
ok: false,
|
|
101
|
+
action: "delete",
|
|
102
|
+
workflowId: id,
|
|
103
|
+
error: `delete failed: ${msg}`,
|
|
104
|
+
backupPath,
|
|
105
|
+
hint: `Snapshot preserved at ${backupPath}. Server state may or may not have been mutated; fetch n8n_get_workflow to verify.`,
|
|
106
|
+
});
|
|
107
|
+
}
|
|
108
|
+
},
|
|
109
|
+
};
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
function resolveBackupDir(configured?: string): string {
|
|
113
|
+
const raw = configured?.trim() || "~/.n8n-backups";
|
|
114
|
+
return raw.startsWith("~")
|
|
115
|
+
? path.join(homedir(), raw.slice(1).replace(/^\/+/, ""))
|
|
116
|
+
: raw;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
async function writeBackup(dir: string, wf: N8nWorkflow): Promise<string> {
|
|
120
|
+
await fs.mkdir(dir, { recursive: true });
|
|
121
|
+
const stamp = new Date()
|
|
122
|
+
.toISOString()
|
|
123
|
+
.replace(/[:.]/g, "-")
|
|
124
|
+
.replace("T", "_")
|
|
125
|
+
.replace("Z", "");
|
|
126
|
+
const safeName = String(wf.id).replace(/[^A-Za-z0-9_-]/g, "_");
|
|
127
|
+
const file = path.join(dir, `${safeName}-DELETED-${stamp}.json`);
|
|
128
|
+
await fs.writeFile(file, JSON.stringify(wf, null, 2), { mode: 0o600 });
|
|
129
|
+
return file;
|
|
130
|
+
}
|