n8n-ops-mcp 0.1.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/LICENSE +21 -0
- package/README.md +225 -0
- package/dist/mcp-server.js +1241 -0
- package/index.ts +60 -0
- package/openclaw.plugin.json +74 -0
- package/package.json +61 -0
- package/src/client.ts +272 -0
- package/src/config.ts +54 -0
- package/src/tools/activate.ts +32 -0
- package/src/tools/deactivate.ts +32 -0
- package/src/tools/get-execution.ts +86 -0
- package/src/tools/get-workflow.ts +67 -0
- package/src/tools/list-executions.ts +100 -0
- package/src/tools/list-webhooks.ts +145 -0
- package/src/tools/list-workflows.ts +68 -0
- package/src/tools/result.ts +9 -0
- package/src/tools/save-workflow.ts +159 -0
- package/src/tools/trigger.ts +185 -0
- package/src/tools/validate-workflow.ts +214 -0
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import { Type } from "@sinclair/typebox";
|
|
2
|
+
import type { N8nClient, 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
|
+
includeDefinition: Type.Optional(
|
|
9
|
+
Type.Boolean({
|
|
10
|
+
description:
|
|
11
|
+
"Include full nodes+connections JSON. Off by default to keep responses small. Turn on when you need to inspect or edit the workflow.",
|
|
12
|
+
}),
|
|
13
|
+
),
|
|
14
|
+
},
|
|
15
|
+
{ additionalProperties: false },
|
|
16
|
+
);
|
|
17
|
+
|
|
18
|
+
export function createGetWorkflowTool(getClient: () => N8nClient) {
|
|
19
|
+
return {
|
|
20
|
+
name: "n8n_get_workflow",
|
|
21
|
+
label: "n8n: get workflow",
|
|
22
|
+
description:
|
|
23
|
+
"Fetch a single n8n workflow by id. Returns metadata and optionally the full node graph. Resolves against the live workflow, not the workflow_entity row directly.",
|
|
24
|
+
parameters: Schema,
|
|
25
|
+
execute: async (_toolCallId: string, rawParams: Record<string, unknown>) => {
|
|
26
|
+
const params = rawParams as { id: string; includeDefinition?: boolean };
|
|
27
|
+
const wf: N8nWorkflow = await getClient().getWorkflow(params.id);
|
|
28
|
+
const base = {
|
|
29
|
+
id: wf.id,
|
|
30
|
+
name: wf.name,
|
|
31
|
+
active: wf.active,
|
|
32
|
+
archived: wf.isArchived === true,
|
|
33
|
+
tags: (wf.tags ?? []).map((t) => t.name),
|
|
34
|
+
versionId: wf.versionId ?? null,
|
|
35
|
+
nodeCount: Array.isArray(wf.nodes) ? wf.nodes.length : 0,
|
|
36
|
+
nodeTypes: summarizeNodeTypes(wf.nodes),
|
|
37
|
+
createdAt: wf.createdAt,
|
|
38
|
+
updatedAt: wf.updatedAt,
|
|
39
|
+
};
|
|
40
|
+
if (!params.includeDefinition) {
|
|
41
|
+
return jsonToolResult(base);
|
|
42
|
+
}
|
|
43
|
+
return jsonToolResult({
|
|
44
|
+
...base,
|
|
45
|
+
definition: {
|
|
46
|
+
nodes: wf.nodes,
|
|
47
|
+
connections: wf.connections,
|
|
48
|
+
settings: wf.settings ?? null,
|
|
49
|
+
staticData: wf.staticData ?? null,
|
|
50
|
+
pinData: wf.pinData ?? null,
|
|
51
|
+
},
|
|
52
|
+
});
|
|
53
|
+
},
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
function summarizeNodeTypes(nodes: unknown): Record<string, number> {
|
|
58
|
+
if (!Array.isArray(nodes)) return {};
|
|
59
|
+
const counts: Record<string, number> = {};
|
|
60
|
+
for (const n of nodes) {
|
|
61
|
+
if (n && typeof n === "object" && "type" in n) {
|
|
62
|
+
const t = String((n as { type: unknown }).type);
|
|
63
|
+
counts[t] = (counts[t] ?? 0) + 1;
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
return counts;
|
|
67
|
+
}
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
import { Type } from "@sinclair/typebox";
|
|
2
|
+
import type { N8nClient, N8nExecutionSummary } from "../client.ts";
|
|
3
|
+
import { jsonToolResult } from "./result.ts";
|
|
4
|
+
|
|
5
|
+
const Schema = Type.Object(
|
|
6
|
+
{
|
|
7
|
+
workflowId: Type.Optional(
|
|
8
|
+
Type.String({
|
|
9
|
+
description: "Filter to a single workflow id. Omit for all workflows.",
|
|
10
|
+
}),
|
|
11
|
+
),
|
|
12
|
+
status: Type.Optional(
|
|
13
|
+
Type.Union(
|
|
14
|
+
[
|
|
15
|
+
Type.Literal("success"),
|
|
16
|
+
Type.Literal("error"),
|
|
17
|
+
Type.Literal("running"),
|
|
18
|
+
Type.Literal("waiting"),
|
|
19
|
+
Type.Literal("canceled"),
|
|
20
|
+
],
|
|
21
|
+
{ description: "Filter by execution status." },
|
|
22
|
+
),
|
|
23
|
+
),
|
|
24
|
+
limit: Type.Optional(
|
|
25
|
+
Type.Number({
|
|
26
|
+
minimum: 1,
|
|
27
|
+
maximum: 250,
|
|
28
|
+
description: "Max rows (default 50).",
|
|
29
|
+
}),
|
|
30
|
+
),
|
|
31
|
+
},
|
|
32
|
+
{ additionalProperties: false },
|
|
33
|
+
);
|
|
34
|
+
|
|
35
|
+
export function createListExecutionsTool(getClient: () => N8nClient) {
|
|
36
|
+
return {
|
|
37
|
+
name: "n8n_list_executions",
|
|
38
|
+
label: "n8n: list executions",
|
|
39
|
+
description:
|
|
40
|
+
"List recent n8n executions with optional filters. Returns id, workflowId, workflowName, status, mode, startedAt, stoppedAt. Use n8n_get_execution for the full run log.",
|
|
41
|
+
parameters: Schema,
|
|
42
|
+
execute: async (_toolCallId: string, rawParams: Record<string, unknown>) => {
|
|
43
|
+
const params = rawParams as {
|
|
44
|
+
workflowId?: string;
|
|
45
|
+
status?: string;
|
|
46
|
+
limit?: number;
|
|
47
|
+
};
|
|
48
|
+
const client = getClient();
|
|
49
|
+
const [executions, workflowIndex] = await Promise.all([
|
|
50
|
+
client.listExecutions({
|
|
51
|
+
workflowId: params.workflowId,
|
|
52
|
+
status: params.status,
|
|
53
|
+
limit: params.limit ?? 50,
|
|
54
|
+
}),
|
|
55
|
+
loadWorkflowNames(client),
|
|
56
|
+
]);
|
|
57
|
+
const rows = executions.data.map((ex) =>
|
|
58
|
+
summaryRow(ex, workflowIndex),
|
|
59
|
+
);
|
|
60
|
+
return jsonToolResult({
|
|
61
|
+
count: rows.length,
|
|
62
|
+
executions: rows,
|
|
63
|
+
nextCursor: executions.nextCursor ?? null,
|
|
64
|
+
});
|
|
65
|
+
},
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
async function loadWorkflowNames(
|
|
70
|
+
client: N8nClient,
|
|
71
|
+
): Promise<Map<string, string>> {
|
|
72
|
+
const index = new Map<string, string>();
|
|
73
|
+
try {
|
|
74
|
+
const res = await client.listWorkflows({ limit: 250 });
|
|
75
|
+
for (const w of res.data) {
|
|
76
|
+
index.set(String(w.id), w.name);
|
|
77
|
+
}
|
|
78
|
+
} catch {
|
|
79
|
+
// Best-effort. Tool still returns executions without names if this fails.
|
|
80
|
+
}
|
|
81
|
+
return index;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
function summaryRow(
|
|
85
|
+
ex: N8nExecutionSummary,
|
|
86
|
+
workflowIndex: Map<string, string>,
|
|
87
|
+
): Record<string, unknown> {
|
|
88
|
+
const workflowId = String(ex.workflowId ?? "");
|
|
89
|
+
return {
|
|
90
|
+
id: String(ex.id),
|
|
91
|
+
workflowId,
|
|
92
|
+
workflowName: workflowIndex.get(workflowId) ?? null,
|
|
93
|
+
status: ex.status ?? (ex.finished ? "success" : "running"),
|
|
94
|
+
mode: ex.mode,
|
|
95
|
+
startedAt: ex.startedAt ?? null,
|
|
96
|
+
stoppedAt: ex.stoppedAt ?? null,
|
|
97
|
+
finished: ex.finished,
|
|
98
|
+
};
|
|
99
|
+
}
|
|
100
|
+
|
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
import { Type } from "@sinclair/typebox";
|
|
2
|
+
import type { N8nClient, N8nWorkflow } from "../client.ts";
|
|
3
|
+
import { jsonToolResult } from "./result.ts";
|
|
4
|
+
|
|
5
|
+
const Schema = Type.Object(
|
|
6
|
+
{
|
|
7
|
+
workflowId: Type.Optional(
|
|
8
|
+
Type.String({
|
|
9
|
+
description:
|
|
10
|
+
"Restrict to a single workflow. Omit to scan recent workflows.",
|
|
11
|
+
}),
|
|
12
|
+
),
|
|
13
|
+
activeOnly: Type.Optional(
|
|
14
|
+
Type.Boolean({
|
|
15
|
+
description: "Only include active workflows. Default true.",
|
|
16
|
+
}),
|
|
17
|
+
),
|
|
18
|
+
limit: Type.Optional(
|
|
19
|
+
Type.Number({
|
|
20
|
+
minimum: 1,
|
|
21
|
+
maximum: 100,
|
|
22
|
+
description:
|
|
23
|
+
"When workflowId is omitted, max workflows to fetch and scan (default 50).",
|
|
24
|
+
}),
|
|
25
|
+
),
|
|
26
|
+
},
|
|
27
|
+
{ additionalProperties: false },
|
|
28
|
+
);
|
|
29
|
+
|
|
30
|
+
export interface ListWebhooksDeps {
|
|
31
|
+
getClient: () => N8nClient;
|
|
32
|
+
baseUrl: string;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export function createListWebhooksTool(deps: ListWebhooksDeps) {
|
|
36
|
+
return {
|
|
37
|
+
name: "n8n_list_webhooks",
|
|
38
|
+
label: "n8n: list webhooks",
|
|
39
|
+
description:
|
|
40
|
+
"Surface webhook and form-trigger paths from n8n workflows so agents can call n8n_trigger with mode='webhook' without opening the n8n UI. Returns workflowId, workflowName, nodeName, method, path, and a fully-formed triggerUrl.",
|
|
41
|
+
parameters: Schema,
|
|
42
|
+
execute: async (_toolCallId: string, rawParams: Record<string, unknown>) => {
|
|
43
|
+
const params = rawParams as {
|
|
44
|
+
workflowId?: string;
|
|
45
|
+
activeOnly?: boolean;
|
|
46
|
+
limit?: number;
|
|
47
|
+
};
|
|
48
|
+
const activeOnly = params.activeOnly !== false;
|
|
49
|
+
const client = deps.getClient();
|
|
50
|
+
|
|
51
|
+
const workflows: N8nWorkflow[] = [];
|
|
52
|
+
if (params.workflowId) {
|
|
53
|
+
workflows.push(await client.getWorkflow(params.workflowId));
|
|
54
|
+
} else {
|
|
55
|
+
const list = await client.listWorkflows({
|
|
56
|
+
active: activeOnly ? true : undefined,
|
|
57
|
+
limit: params.limit ?? 50,
|
|
58
|
+
});
|
|
59
|
+
const defs = await Promise.all(
|
|
60
|
+
list.data.map((w) => client.getWorkflow(w.id)),
|
|
61
|
+
);
|
|
62
|
+
workflows.push(...defs);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
const webhooks: Record<string, unknown>[] = [];
|
|
66
|
+
for (const wf of workflows) {
|
|
67
|
+
if (activeOnly && !wf.active) continue;
|
|
68
|
+
if (!Array.isArray(wf.nodes)) continue;
|
|
69
|
+
for (const node of wf.nodes) {
|
|
70
|
+
const hook = extractWebhook(node, wf, deps.baseUrl);
|
|
71
|
+
if (hook) webhooks.push(hook);
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
return jsonToolResult({
|
|
76
|
+
count: webhooks.length,
|
|
77
|
+
scannedWorkflows: workflows.length,
|
|
78
|
+
activeOnly,
|
|
79
|
+
webhooks,
|
|
80
|
+
});
|
|
81
|
+
},
|
|
82
|
+
};
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
function extractWebhook(
|
|
86
|
+
rawNode: unknown,
|
|
87
|
+
wf: N8nWorkflow,
|
|
88
|
+
baseUrl: string,
|
|
89
|
+
): Record<string, unknown> | null {
|
|
90
|
+
if (!rawNode || typeof rawNode !== "object") return null;
|
|
91
|
+
const n = rawNode as Record<string, unknown>;
|
|
92
|
+
const type = String(n.type ?? "");
|
|
93
|
+
const name = typeof n.name === "string" ? n.name : "";
|
|
94
|
+
const params = (n.parameters as Record<string, unknown>) ?? {};
|
|
95
|
+
const base = baseUrl.replace(/\/+$/, "");
|
|
96
|
+
|
|
97
|
+
if (type === "n8n-nodes-base.webhook") {
|
|
98
|
+
const pathRaw =
|
|
99
|
+
typeof params.path === "string" && params.path.trim()
|
|
100
|
+
? params.path.trim()
|
|
101
|
+
: typeof n.webhookId === "string"
|
|
102
|
+
? String(n.webhookId)
|
|
103
|
+
: "";
|
|
104
|
+
if (!pathRaw) return null;
|
|
105
|
+
const path = pathRaw.replace(/^\/+/, "");
|
|
106
|
+
const method =
|
|
107
|
+
typeof params.httpMethod === "string" && params.httpMethod.trim()
|
|
108
|
+
? params.httpMethod.toUpperCase()
|
|
109
|
+
: "POST";
|
|
110
|
+
return {
|
|
111
|
+
workflowId: wf.id,
|
|
112
|
+
workflowName: wf.name,
|
|
113
|
+
active: wf.active,
|
|
114
|
+
nodeName: name,
|
|
115
|
+
nodeType: type,
|
|
116
|
+
method,
|
|
117
|
+
path: `/webhook/${path}`,
|
|
118
|
+
triggerUrl: `${base}/webhook/${path}`,
|
|
119
|
+
testTriggerUrl: `${base}/webhook-test/${path}`,
|
|
120
|
+
};
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
if (type === "n8n-nodes-base.formTrigger") {
|
|
124
|
+
const pathRaw =
|
|
125
|
+
typeof params.path === "string" && params.path.trim()
|
|
126
|
+
? params.path.trim()
|
|
127
|
+
: typeof n.webhookId === "string"
|
|
128
|
+
? String(n.webhookId)
|
|
129
|
+
: "";
|
|
130
|
+
if (!pathRaw) return null;
|
|
131
|
+
const path = pathRaw.replace(/^\/+/, "");
|
|
132
|
+
return {
|
|
133
|
+
workflowId: wf.id,
|
|
134
|
+
workflowName: wf.name,
|
|
135
|
+
active: wf.active,
|
|
136
|
+
nodeName: name,
|
|
137
|
+
nodeType: type,
|
|
138
|
+
method: "POST",
|
|
139
|
+
path: `/form/${path}`,
|
|
140
|
+
triggerUrl: `${base}/form/${path}`,
|
|
141
|
+
};
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
return null;
|
|
145
|
+
}
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import { Type } from "@sinclair/typebox";
|
|
2
|
+
import type { N8nClient, N8nWorkflowSummary } from "../client.ts";
|
|
3
|
+
import { jsonToolResult } from "./result.ts";
|
|
4
|
+
|
|
5
|
+
const Schema = Type.Object(
|
|
6
|
+
{
|
|
7
|
+
active: Type.Optional(
|
|
8
|
+
Type.Boolean({ description: "Filter by active state. Omit for all." }),
|
|
9
|
+
),
|
|
10
|
+
tags: Type.Optional(
|
|
11
|
+
Type.String({ description: "Comma-separated tag names to filter by." }),
|
|
12
|
+
),
|
|
13
|
+
name: Type.Optional(
|
|
14
|
+
Type.String({ description: "Case-insensitive substring match on workflow name." }),
|
|
15
|
+
),
|
|
16
|
+
limit: Type.Optional(
|
|
17
|
+
Type.Number({ minimum: 1, maximum: 250, description: "Max rows (default 100)." }),
|
|
18
|
+
),
|
|
19
|
+
},
|
|
20
|
+
{ additionalProperties: false },
|
|
21
|
+
);
|
|
22
|
+
|
|
23
|
+
export function createListWorkflowsTool(getClient: () => N8nClient) {
|
|
24
|
+
return {
|
|
25
|
+
name: "n8n_list_workflows",
|
|
26
|
+
label: "n8n: list workflows",
|
|
27
|
+
description:
|
|
28
|
+
"List n8n workflows with optional filters. Returns id, name, active state, tags, updatedAt. Use n8n_get_workflow to pull the full definition.",
|
|
29
|
+
parameters: Schema,
|
|
30
|
+
execute: async (_toolCallId: string, rawParams: Record<string, unknown>) => {
|
|
31
|
+
const params = rawParams as {
|
|
32
|
+
active?: boolean;
|
|
33
|
+
tags?: string;
|
|
34
|
+
name?: string;
|
|
35
|
+
limit?: number;
|
|
36
|
+
};
|
|
37
|
+
const res = await getClient().listWorkflows({
|
|
38
|
+
active: params.active,
|
|
39
|
+
tags: params.tags,
|
|
40
|
+
limit: params.limit ?? 100,
|
|
41
|
+
});
|
|
42
|
+
const rows = res.data
|
|
43
|
+
.filter((w) => matchName(w, params.name))
|
|
44
|
+
.map(summaryRow);
|
|
45
|
+
return jsonToolResult({
|
|
46
|
+
count: rows.length,
|
|
47
|
+
workflows: rows,
|
|
48
|
+
nextCursor: res.nextCursor ?? null,
|
|
49
|
+
});
|
|
50
|
+
},
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
function matchName(w: N8nWorkflowSummary, q?: string): boolean {
|
|
55
|
+
if (!q) return true;
|
|
56
|
+
return w.name.toLowerCase().includes(q.toLowerCase());
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
function summaryRow(w: N8nWorkflowSummary) {
|
|
60
|
+
return {
|
|
61
|
+
id: w.id,
|
|
62
|
+
name: w.name,
|
|
63
|
+
active: w.active,
|
|
64
|
+
archived: w.isArchived === true,
|
|
65
|
+
tags: (w.tags ?? []).map((t) => t.name),
|
|
66
|
+
updatedAt: w.updatedAt,
|
|
67
|
+
};
|
|
68
|
+
}
|
|
@@ -0,0 +1,159 @@
|
|
|
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 type { N8nClient, N8nWorkflow } from "../client.ts";
|
|
6
|
+
import { jsonToolResult } from "./result.ts";
|
|
7
|
+
import { validateWorkflow } from "./validate-workflow.ts";
|
|
8
|
+
|
|
9
|
+
const Schema = Type.Object(
|
|
10
|
+
{
|
|
11
|
+
id: Type.String({ description: "Workflow id to overwrite." }),
|
|
12
|
+
definition: Type.Object(
|
|
13
|
+
{
|
|
14
|
+
name: Type.Optional(Type.String()),
|
|
15
|
+
nodes: Type.Array(Type.Record(Type.String(), Type.Unknown())),
|
|
16
|
+
connections: Type.Record(Type.String(), Type.Unknown()),
|
|
17
|
+
settings: Type.Optional(Type.Record(Type.String(), Type.Unknown())),
|
|
18
|
+
staticData: Type.Optional(Type.Unknown()),
|
|
19
|
+
},
|
|
20
|
+
{
|
|
21
|
+
additionalProperties: true,
|
|
22
|
+
description:
|
|
23
|
+
"Full new workflow body. Copy from n8n_get_workflow with includeDefinition=true, modify, then pass the nodes + connections (+ optional settings/staticData/name) here.",
|
|
24
|
+
},
|
|
25
|
+
),
|
|
26
|
+
skipValidation: Type.Optional(
|
|
27
|
+
Type.Boolean({
|
|
28
|
+
description:
|
|
29
|
+
"Skip the n8n_validate_workflow pre-check. Default false. Validation errors (not warnings) block the save by default.",
|
|
30
|
+
}),
|
|
31
|
+
),
|
|
32
|
+
confirm: Type.Boolean({
|
|
33
|
+
description:
|
|
34
|
+
"Must be true to actually write. A snapshot of the current workflow is saved to backupDir regardless.",
|
|
35
|
+
}),
|
|
36
|
+
},
|
|
37
|
+
{ additionalProperties: false },
|
|
38
|
+
);
|
|
39
|
+
|
|
40
|
+
export interface SaveWorkflowDeps {
|
|
41
|
+
getClient: () => N8nClient;
|
|
42
|
+
backupDir?: string;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export function createSaveWorkflowTool(deps: SaveWorkflowDeps) {
|
|
46
|
+
return {
|
|
47
|
+
name: "n8n_save_workflow",
|
|
48
|
+
label: "n8n: save workflow",
|
|
49
|
+
description:
|
|
50
|
+
"Overwrite an n8n workflow. Snapshots the current version to backupDir first, runs n8n_validate_workflow on the proposed new state (errors block, warnings pass through), then PUTs the new body. Requires enableEdit and explicit confirm=true.",
|
|
51
|
+
parameters: Schema,
|
|
52
|
+
execute: async (_toolCallId: string, rawParams: Record<string, unknown>) => {
|
|
53
|
+
const params = rawParams as {
|
|
54
|
+
id: string;
|
|
55
|
+
definition: Record<string, unknown>;
|
|
56
|
+
skipValidation?: boolean;
|
|
57
|
+
confirm: boolean;
|
|
58
|
+
};
|
|
59
|
+
if (!params.confirm) {
|
|
60
|
+
return jsonToolResult({
|
|
61
|
+
ok: false,
|
|
62
|
+
error: "confirm must be true to save",
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
const client = deps.getClient();
|
|
66
|
+
|
|
67
|
+
const current = await client.getWorkflow(params.id);
|
|
68
|
+
const backupPath = await writeBackup(
|
|
69
|
+
resolveBackupDir(deps.backupDir),
|
|
70
|
+
current,
|
|
71
|
+
);
|
|
72
|
+
|
|
73
|
+
if (!params.skipValidation) {
|
|
74
|
+
const proposed: N8nWorkflow = {
|
|
75
|
+
...current,
|
|
76
|
+
...(params.definition as Partial<N8nWorkflow>),
|
|
77
|
+
id: current.id,
|
|
78
|
+
};
|
|
79
|
+
const issues = validateWorkflow(proposed);
|
|
80
|
+
const errors = issues.filter((i) => i.severity === "error");
|
|
81
|
+
if (errors.length > 0) {
|
|
82
|
+
return jsonToolResult({
|
|
83
|
+
ok: false,
|
|
84
|
+
error: "validation failed; save aborted",
|
|
85
|
+
backupPath,
|
|
86
|
+
issues,
|
|
87
|
+
});
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
const body = buildSaveBody(current, params.definition);
|
|
92
|
+
try {
|
|
93
|
+
const saved = await client.saveWorkflow(params.id, body);
|
|
94
|
+
return jsonToolResult({
|
|
95
|
+
ok: true,
|
|
96
|
+
action: "save",
|
|
97
|
+
workflowId: saved.id,
|
|
98
|
+
workflowName: saved.name,
|
|
99
|
+
active: saved.active,
|
|
100
|
+
versionId: saved.versionId ?? null,
|
|
101
|
+
updatedAt: saved.updatedAt,
|
|
102
|
+
backupPath,
|
|
103
|
+
restoreHint: `To restore: n8n_save_workflow with id=${saved.id}, confirm=true, definition=<contents of ${backupPath}>.`,
|
|
104
|
+
});
|
|
105
|
+
} catch (err) {
|
|
106
|
+
const msg = err instanceof Error ? err.message : String(err);
|
|
107
|
+
return jsonToolResult({
|
|
108
|
+
ok: false,
|
|
109
|
+
error: `save failed: ${msg}`,
|
|
110
|
+
backupPath,
|
|
111
|
+
restoreHint: `Snapshot preserved at ${backupPath}. Server state may or may not have been mutated; fetch n8n_get_workflow to verify.`,
|
|
112
|
+
});
|
|
113
|
+
}
|
|
114
|
+
},
|
|
115
|
+
};
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
function buildSaveBody(
|
|
119
|
+
current: N8nWorkflow,
|
|
120
|
+
proposed: Record<string, unknown>,
|
|
121
|
+
): Record<string, unknown> {
|
|
122
|
+
// n8n PUT expects the editable subset. Pass through only the fields
|
|
123
|
+
// that the Public API accepts to avoid 400s from read-only fields.
|
|
124
|
+
const body: Record<string, unknown> = {
|
|
125
|
+
name: (proposed.name as string) ?? current.name,
|
|
126
|
+
nodes: proposed.nodes ?? current.nodes,
|
|
127
|
+
connections: proposed.connections ?? current.connections,
|
|
128
|
+
settings: proposed.settings ?? current.settings ?? {},
|
|
129
|
+
};
|
|
130
|
+
if (proposed.staticData !== undefined) {
|
|
131
|
+
body.staticData = proposed.staticData;
|
|
132
|
+
} else if (current.staticData !== undefined) {
|
|
133
|
+
body.staticData = current.staticData;
|
|
134
|
+
}
|
|
135
|
+
return body;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
function resolveBackupDir(configured?: string): string {
|
|
139
|
+
const raw = configured?.trim() || "~/.n8n-backups";
|
|
140
|
+
return raw.startsWith("~")
|
|
141
|
+
? path.join(homedir(), raw.slice(1).replace(/^\/+/, ""))
|
|
142
|
+
: raw;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
async function writeBackup(
|
|
146
|
+
dir: string,
|
|
147
|
+
wf: N8nWorkflow,
|
|
148
|
+
): Promise<string> {
|
|
149
|
+
await fs.mkdir(dir, { recursive: true });
|
|
150
|
+
const stamp = new Date()
|
|
151
|
+
.toISOString()
|
|
152
|
+
.replace(/[:.]/g, "-")
|
|
153
|
+
.replace("T", "_")
|
|
154
|
+
.replace("Z", "");
|
|
155
|
+
const safeName = String(wf.id).replace(/[^A-Za-z0-9_-]/g, "_");
|
|
156
|
+
const file = path.join(dir, `${safeName}-${stamp}.json`);
|
|
157
|
+
await fs.writeFile(file, JSON.stringify(wf, null, 2), { mode: 0o600 });
|
|
158
|
+
return file;
|
|
159
|
+
}
|