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
package/index.ts
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import { definePluginEntry, type AnyAgentTool } from "openclaw/plugin-sdk/plugin-entry";
|
|
2
|
+
import type { N8nClient } from "./src/client.ts";
|
|
3
|
+
import { makeClient, resolveConfig, type N8nPluginConfig } from "./src/config.ts";
|
|
4
|
+
import { createListWorkflowsTool } from "./src/tools/list-workflows.ts";
|
|
5
|
+
import { createGetWorkflowTool } from "./src/tools/get-workflow.ts";
|
|
6
|
+
import { createListExecutionsTool } from "./src/tools/list-executions.ts";
|
|
7
|
+
import { createGetExecutionTool } from "./src/tools/get-execution.ts";
|
|
8
|
+
import { createTriggerTool } from "./src/tools/trigger.ts";
|
|
9
|
+
import { createListWebhooksTool } from "./src/tools/list-webhooks.ts";
|
|
10
|
+
import { createValidateWorkflowTool } from "./src/tools/validate-workflow.ts";
|
|
11
|
+
import { createActivateTool } from "./src/tools/activate.ts";
|
|
12
|
+
import { createDeactivateTool } from "./src/tools/deactivate.ts";
|
|
13
|
+
import { createSaveWorkflowTool } from "./src/tools/save-workflow.ts";
|
|
14
|
+
|
|
15
|
+
export default definePluginEntry({
|
|
16
|
+
id: "n8n",
|
|
17
|
+
name: "n8n Ops",
|
|
18
|
+
description:
|
|
19
|
+
"List, inspect, and trigger n8n workflows from OpenClaw agents. Optional edit tools behind a flag with auto-backup and rollback on failure.",
|
|
20
|
+
register(api) {
|
|
21
|
+
if (api.registrationMode !== "full") return;
|
|
22
|
+
|
|
23
|
+
const config = resolveConfig(api.pluginConfig);
|
|
24
|
+
const getClient = lazyClient(config);
|
|
25
|
+
|
|
26
|
+
api.registerTool(createListWorkflowsTool(getClient) as AnyAgentTool);
|
|
27
|
+
api.registerTool(createGetWorkflowTool(getClient) as AnyAgentTool);
|
|
28
|
+
api.registerTool(createListExecutionsTool(getClient) as AnyAgentTool);
|
|
29
|
+
api.registerTool(
|
|
30
|
+
createGetExecutionTool({
|
|
31
|
+
getClient,
|
|
32
|
+
maxLogBytes: config.maxExecutionLogBytes,
|
|
33
|
+
}) as AnyAgentTool,
|
|
34
|
+
);
|
|
35
|
+
api.registerTool(createTriggerTool(getClient) as AnyAgentTool);
|
|
36
|
+
api.registerTool(
|
|
37
|
+
createListWebhooksTool({ getClient, baseUrl: config.baseUrl }) as AnyAgentTool,
|
|
38
|
+
);
|
|
39
|
+
api.registerTool(createValidateWorkflowTool(getClient) as AnyAgentTool);
|
|
40
|
+
|
|
41
|
+
if (config.enableEdit) {
|
|
42
|
+
api.registerTool(createActivateTool(getClient) as AnyAgentTool);
|
|
43
|
+
api.registerTool(createDeactivateTool(getClient) as AnyAgentTool);
|
|
44
|
+
api.registerTool(
|
|
45
|
+
createSaveWorkflowTool({
|
|
46
|
+
getClient,
|
|
47
|
+
backupDir: config.backupDir,
|
|
48
|
+
}) as AnyAgentTool,
|
|
49
|
+
);
|
|
50
|
+
}
|
|
51
|
+
},
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
function lazyClient(config: N8nPluginConfig): () => N8nClient {
|
|
55
|
+
let cached: N8nClient | undefined;
|
|
56
|
+
return () => {
|
|
57
|
+
if (!cached) cached = makeClient(config);
|
|
58
|
+
return cached;
|
|
59
|
+
};
|
|
60
|
+
}
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
{
|
|
2
|
+
"id": "n8n",
|
|
3
|
+
"name": "n8n Ops",
|
|
4
|
+
"description": "List, inspect, and trigger n8n workflows from OpenClaw agents. Optional edit tools behind a flag with auto-backup and rollback on failure.",
|
|
5
|
+
"configSchema": {
|
|
6
|
+
"type": "object",
|
|
7
|
+
"additionalProperties": false,
|
|
8
|
+
"required": ["baseUrl"],
|
|
9
|
+
"properties": {
|
|
10
|
+
"baseUrl": {
|
|
11
|
+
"type": "string",
|
|
12
|
+
"format": "uri",
|
|
13
|
+
"description": "Base URL of the n8n instance, e.g. http://192.168.4.89:5678"
|
|
14
|
+
},
|
|
15
|
+
"apiKey": {
|
|
16
|
+
"type": "string",
|
|
17
|
+
"description": "n8n public API key (X-N8N-API-KEY). Leave blank and set N8N_API_KEY in the environment instead."
|
|
18
|
+
},
|
|
19
|
+
"apiKeyEnv": {
|
|
20
|
+
"type": "string",
|
|
21
|
+
"default": "N8N_API_KEY",
|
|
22
|
+
"description": "Environment variable to read the API key from when apiKey is blank."
|
|
23
|
+
},
|
|
24
|
+
"enableEdit": {
|
|
25
|
+
"type": "boolean",
|
|
26
|
+
"default": false,
|
|
27
|
+
"description": "Enable write tools (save/activate/deactivate). Off by default."
|
|
28
|
+
},
|
|
29
|
+
"maxExecutionLogBytes": {
|
|
30
|
+
"type": "number",
|
|
31
|
+
"minimum": 1024,
|
|
32
|
+
"default": 65536,
|
|
33
|
+
"description": "Max bytes of execution log returned inline. Larger logs are truncated with a tail hint."
|
|
34
|
+
},
|
|
35
|
+
"requestTimeoutMs": {
|
|
36
|
+
"type": "number",
|
|
37
|
+
"minimum": 1000,
|
|
38
|
+
"default": 15000,
|
|
39
|
+
"description": "HTTP timeout for n8n API calls."
|
|
40
|
+
},
|
|
41
|
+
"backupDir": {
|
|
42
|
+
"type": "string",
|
|
43
|
+
"description": "Directory for pre-save workflow snapshots. Defaults to ~/.n8n-backups."
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
},
|
|
47
|
+
"uiHints": {
|
|
48
|
+
"baseUrl": {
|
|
49
|
+
"label": "Base URL",
|
|
50
|
+
"help": "Your n8n instance, e.g. http://host:5678. Container-direct IPs work fine if routable."
|
|
51
|
+
},
|
|
52
|
+
"apiKey": {
|
|
53
|
+
"label": "API Key",
|
|
54
|
+
"help": "Generate in n8n Settings -> API. Use keyRef to an env var, not inline."
|
|
55
|
+
},
|
|
56
|
+
"enableEdit": {
|
|
57
|
+
"label": "Enable Edit Tools",
|
|
58
|
+
"help": "Exposes save/activate/deactivate with confirm-gates and auto-rollback."
|
|
59
|
+
},
|
|
60
|
+
"maxExecutionLogBytes": {
|
|
61
|
+
"label": "Max Log Bytes",
|
|
62
|
+
"help": "Truncate large execution logs to keep tool responses within token budgets.",
|
|
63
|
+
"advanced": true
|
|
64
|
+
},
|
|
65
|
+
"requestTimeoutMs": {
|
|
66
|
+
"label": "Request Timeout (ms)",
|
|
67
|
+
"advanced": true
|
|
68
|
+
},
|
|
69
|
+
"backupDir": {
|
|
70
|
+
"label": "Backup Directory",
|
|
71
|
+
"advanced": true
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "n8n-ops-mcp",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Ops-focused n8n tools (list, inspect, trigger, validate, save) for Claude-compatible clients. MCP server + first-class OpenClaw plugin.",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"author": "Solomon Neas <srneas@gmail.com>",
|
|
8
|
+
"keywords": ["n8n", "mcp", "model-context-protocol", "openclaw", "workflow", "ops", "automation"],
|
|
9
|
+
"repository": {
|
|
10
|
+
"type": "git",
|
|
11
|
+
"url": "https://github.com/solomonneas/n8n-ops-mcp.git"
|
|
12
|
+
},
|
|
13
|
+
"homepage": "https://github.com/solomonneas/n8n-ops-mcp#readme",
|
|
14
|
+
"bugs": {
|
|
15
|
+
"url": "https://github.com/solomonneas/n8n-ops-mcp/issues"
|
|
16
|
+
},
|
|
17
|
+
"engines": {
|
|
18
|
+
"node": ">=20.0.0"
|
|
19
|
+
},
|
|
20
|
+
"bin": {
|
|
21
|
+
"n8n-ops-mcp": "dist/mcp-server.js"
|
|
22
|
+
},
|
|
23
|
+
"main": "dist/mcp-server.js",
|
|
24
|
+
"files": [
|
|
25
|
+
"dist",
|
|
26
|
+
"index.ts",
|
|
27
|
+
"src",
|
|
28
|
+
"openclaw.plugin.json",
|
|
29
|
+
"README.md",
|
|
30
|
+
"LICENSE"
|
|
31
|
+
],
|
|
32
|
+
"openclaw": {
|
|
33
|
+
"extensions": ["./index.ts"]
|
|
34
|
+
},
|
|
35
|
+
"dependencies": {
|
|
36
|
+
"@modelcontextprotocol/sdk": "^1.29.0",
|
|
37
|
+
"@sinclair/typebox": "^0.34.0",
|
|
38
|
+
"zod": "^4.3.0"
|
|
39
|
+
},
|
|
40
|
+
"peerDependencies": {
|
|
41
|
+
"openclaw": "*"
|
|
42
|
+
},
|
|
43
|
+
"peerDependenciesMeta": {
|
|
44
|
+
"openclaw": {
|
|
45
|
+
"optional": true
|
|
46
|
+
}
|
|
47
|
+
},
|
|
48
|
+
"devDependencies": {
|
|
49
|
+
"@types/node": "^22.0.0",
|
|
50
|
+
"tsup": "^8.4.0",
|
|
51
|
+
"tsx": "^4.19.0",
|
|
52
|
+
"typescript": "^5.5.0"
|
|
53
|
+
},
|
|
54
|
+
"scripts": {
|
|
55
|
+
"typecheck": "tsc --noEmit",
|
|
56
|
+
"build": "tsup",
|
|
57
|
+
"start": "node dist/mcp-server.js",
|
|
58
|
+
"dev": "tsx mcp-server.ts",
|
|
59
|
+
"prepublishOnly": "npm run typecheck && npm run build"
|
|
60
|
+
}
|
|
61
|
+
}
|
package/src/client.ts
ADDED
|
@@ -0,0 +1,272 @@
|
|
|
1
|
+
export interface N8nClientOptions {
|
|
2
|
+
baseUrl: string;
|
|
3
|
+
apiKey: string;
|
|
4
|
+
timeoutMs?: number;
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
export interface N8nWorkflowSummary {
|
|
8
|
+
id: string;
|
|
9
|
+
name: string;
|
|
10
|
+
active: boolean;
|
|
11
|
+
isArchived?: boolean;
|
|
12
|
+
tags?: Array<{ id: string; name: string }>;
|
|
13
|
+
createdAt: string;
|
|
14
|
+
updatedAt: string;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export interface N8nWorkflow extends N8nWorkflowSummary {
|
|
18
|
+
nodes: unknown[];
|
|
19
|
+
connections: Record<string, unknown>;
|
|
20
|
+
settings?: Record<string, unknown>;
|
|
21
|
+
staticData?: unknown;
|
|
22
|
+
pinData?: Record<string, unknown>;
|
|
23
|
+
versionId?: string;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export interface N8nListResponse<T> {
|
|
27
|
+
data: T[];
|
|
28
|
+
nextCursor?: string;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export type N8nExecutionStatus =
|
|
32
|
+
| "success"
|
|
33
|
+
| "error"
|
|
34
|
+
| "running"
|
|
35
|
+
| "waiting"
|
|
36
|
+
| "canceled"
|
|
37
|
+
| "new"
|
|
38
|
+
| "unknown";
|
|
39
|
+
|
|
40
|
+
export interface N8nExecutionSummary {
|
|
41
|
+
id: string | number;
|
|
42
|
+
finished: boolean;
|
|
43
|
+
mode: string;
|
|
44
|
+
retryOf?: string | number | null;
|
|
45
|
+
retrySuccessId?: string | number | null;
|
|
46
|
+
status?: N8nExecutionStatus | string;
|
|
47
|
+
workflowId: string;
|
|
48
|
+
startedAt?: string;
|
|
49
|
+
stoppedAt?: string;
|
|
50
|
+
createdAt?: string;
|
|
51
|
+
waitTill?: string | null;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
export interface N8nExecution extends N8nExecutionSummary {
|
|
55
|
+
data?: {
|
|
56
|
+
resultData?: {
|
|
57
|
+
runData?: Record<string, unknown>;
|
|
58
|
+
error?: unknown;
|
|
59
|
+
lastNodeExecuted?: string;
|
|
60
|
+
};
|
|
61
|
+
executionData?: unknown;
|
|
62
|
+
startData?: unknown;
|
|
63
|
+
};
|
|
64
|
+
workflowData?: {
|
|
65
|
+
id?: string;
|
|
66
|
+
name?: string;
|
|
67
|
+
active?: boolean;
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
export class N8nApiError extends Error {
|
|
72
|
+
constructor(
|
|
73
|
+
public readonly status: number,
|
|
74
|
+
public readonly path: string,
|
|
75
|
+
message: string,
|
|
76
|
+
) {
|
|
77
|
+
super(`n8n ${status} on ${path}: ${message}`);
|
|
78
|
+
this.name = "N8nApiError";
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
export class N8nClient {
|
|
83
|
+
private readonly baseUrl: string;
|
|
84
|
+
private readonly apiKey: string;
|
|
85
|
+
private readonly timeoutMs: number;
|
|
86
|
+
|
|
87
|
+
constructor(opts: N8nClientOptions) {
|
|
88
|
+
this.baseUrl = opts.baseUrl.replace(/\/+$/, "");
|
|
89
|
+
this.apiKey = opts.apiKey;
|
|
90
|
+
this.timeoutMs = opts.timeoutMs ?? 15_000;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
async listWorkflows(params: {
|
|
94
|
+
active?: boolean;
|
|
95
|
+
tags?: string;
|
|
96
|
+
name?: string;
|
|
97
|
+
limit?: number;
|
|
98
|
+
cursor?: string;
|
|
99
|
+
} = {}): Promise<N8nListResponse<N8nWorkflowSummary>> {
|
|
100
|
+
const qs = new URLSearchParams();
|
|
101
|
+
if (params.active !== undefined) qs.set("active", String(params.active));
|
|
102
|
+
if (params.tags) qs.set("tags", params.tags);
|
|
103
|
+
if (params.name) qs.set("name", params.name);
|
|
104
|
+
if (params.limit) qs.set("limit", String(params.limit));
|
|
105
|
+
if (params.cursor) qs.set("cursor", params.cursor);
|
|
106
|
+
return this.request<N8nListResponse<N8nWorkflowSummary>>(
|
|
107
|
+
`/api/v1/workflows${qs.toString() ? `?${qs}` : ""}`,
|
|
108
|
+
);
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
async getWorkflow(id: string): Promise<N8nWorkflow> {
|
|
112
|
+
if (!/^[A-Za-z0-9_-]+$/.test(id)) {
|
|
113
|
+
throw new Error(`Invalid workflow id: ${id}`);
|
|
114
|
+
}
|
|
115
|
+
return this.request<N8nWorkflow>(`/api/v1/workflows/${id}`);
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
async saveWorkflow(
|
|
119
|
+
id: string,
|
|
120
|
+
body: Record<string, unknown>,
|
|
121
|
+
): Promise<N8nWorkflow> {
|
|
122
|
+
if (!/^[A-Za-z0-9_-]+$/.test(id)) {
|
|
123
|
+
throw new Error(`Invalid workflow id: ${id}`);
|
|
124
|
+
}
|
|
125
|
+
return this.request<N8nWorkflow>(`/api/v1/workflows/${id}`, {
|
|
126
|
+
method: "PUT",
|
|
127
|
+
body: JSON.stringify(body),
|
|
128
|
+
});
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
async activateWorkflow(id: string): Promise<N8nWorkflow> {
|
|
132
|
+
if (!/^[A-Za-z0-9_-]+$/.test(id)) {
|
|
133
|
+
throw new Error(`Invalid workflow id: ${id}`);
|
|
134
|
+
}
|
|
135
|
+
return this.request<N8nWorkflow>(`/api/v1/workflows/${id}/activate`, {
|
|
136
|
+
method: "POST",
|
|
137
|
+
});
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
async deactivateWorkflow(id: string): Promise<N8nWorkflow> {
|
|
141
|
+
if (!/^[A-Za-z0-9_-]+$/.test(id)) {
|
|
142
|
+
throw new Error(`Invalid workflow id: ${id}`);
|
|
143
|
+
}
|
|
144
|
+
return this.request<N8nWorkflow>(`/api/v1/workflows/${id}/deactivate`, {
|
|
145
|
+
method: "POST",
|
|
146
|
+
});
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
async executeWorkflow(
|
|
150
|
+
id: string,
|
|
151
|
+
payload?: Record<string, unknown>,
|
|
152
|
+
): Promise<unknown> {
|
|
153
|
+
if (!/^[A-Za-z0-9_-]+$/.test(id)) {
|
|
154
|
+
throw new Error(`Invalid workflow id: ${id}`);
|
|
155
|
+
}
|
|
156
|
+
return this.request<unknown>(`/api/v1/workflows/${id}/execute`, {
|
|
157
|
+
method: "POST",
|
|
158
|
+
body: JSON.stringify(payload ?? {}),
|
|
159
|
+
});
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
async postWebhook(
|
|
163
|
+
webhookPath: string,
|
|
164
|
+
payload: unknown,
|
|
165
|
+
opts: { method?: string } = {},
|
|
166
|
+
): Promise<{ status: number; body: unknown }> {
|
|
167
|
+
const path = webhookPath.startsWith("/") ? webhookPath : `/${webhookPath}`;
|
|
168
|
+
const url = `${this.baseUrl}${path}`;
|
|
169
|
+
const method = (opts.method ?? "POST").toUpperCase();
|
|
170
|
+
const controller = new AbortController();
|
|
171
|
+
const timer = setTimeout(() => controller.abort(), this.timeoutMs);
|
|
172
|
+
try {
|
|
173
|
+
const hasBody = method !== "GET" && method !== "HEAD";
|
|
174
|
+
const res = await fetch(url, {
|
|
175
|
+
method,
|
|
176
|
+
headers: {
|
|
177
|
+
Accept: "application/json",
|
|
178
|
+
...(hasBody ? { "Content-Type": "application/json" } : {}),
|
|
179
|
+
},
|
|
180
|
+
body: hasBody ? JSON.stringify(payload ?? {}) : undefined,
|
|
181
|
+
signal: controller.signal,
|
|
182
|
+
});
|
|
183
|
+
const text = await res.text();
|
|
184
|
+
let body: unknown = text;
|
|
185
|
+
if (text) {
|
|
186
|
+
try {
|
|
187
|
+
body = JSON.parse(text);
|
|
188
|
+
} catch {
|
|
189
|
+
body = text;
|
|
190
|
+
}
|
|
191
|
+
} else {
|
|
192
|
+
body = null;
|
|
193
|
+
}
|
|
194
|
+
return { status: res.status, body };
|
|
195
|
+
} catch (err) {
|
|
196
|
+
const msg = err instanceof Error ? err.message : String(err);
|
|
197
|
+
throw new Error(`n8n webhook to ${path} failed: ${redactKey(msg, this.apiKey)}`);
|
|
198
|
+
} finally {
|
|
199
|
+
clearTimeout(timer);
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
async getExecution(
|
|
204
|
+
id: string,
|
|
205
|
+
opts: { includeData?: boolean } = {},
|
|
206
|
+
): Promise<N8nExecution> {
|
|
207
|
+
if (!/^[A-Za-z0-9_-]+$/.test(id)) {
|
|
208
|
+
throw new Error(`Invalid execution id: ${id}`);
|
|
209
|
+
}
|
|
210
|
+
const qs = new URLSearchParams();
|
|
211
|
+
if (opts.includeData) qs.set("includeData", "true");
|
|
212
|
+
return this.request<N8nExecution>(
|
|
213
|
+
`/api/v1/executions/${id}${qs.toString() ? `?${qs}` : ""}`,
|
|
214
|
+
);
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
async listExecutions(params: {
|
|
218
|
+
workflowId?: string;
|
|
219
|
+
status?: string;
|
|
220
|
+
limit?: number;
|
|
221
|
+
cursor?: string;
|
|
222
|
+
includeData?: boolean;
|
|
223
|
+
} = {}): Promise<N8nListResponse<N8nExecutionSummary>> {
|
|
224
|
+
const qs = new URLSearchParams();
|
|
225
|
+
if (params.workflowId) qs.set("workflowId", params.workflowId);
|
|
226
|
+
if (params.status) qs.set("status", params.status);
|
|
227
|
+
if (params.limit) qs.set("limit", String(params.limit));
|
|
228
|
+
if (params.cursor) qs.set("cursor", params.cursor);
|
|
229
|
+
if (params.includeData) qs.set("includeData", "true");
|
|
230
|
+
return this.request<N8nListResponse<N8nExecutionSummary>>(
|
|
231
|
+
`/api/v1/executions${qs.toString() ? `?${qs}` : ""}`,
|
|
232
|
+
);
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
private async request<T>(
|
|
236
|
+
path: string,
|
|
237
|
+
init: RequestInit = {},
|
|
238
|
+
): Promise<T> {
|
|
239
|
+
const url = `${this.baseUrl}${path}`;
|
|
240
|
+
const controller = new AbortController();
|
|
241
|
+
const timer = setTimeout(() => controller.abort(), this.timeoutMs);
|
|
242
|
+
try {
|
|
243
|
+
const res = await fetch(url, {
|
|
244
|
+
...init,
|
|
245
|
+
headers: {
|
|
246
|
+
"X-N8N-API-KEY": this.apiKey,
|
|
247
|
+
"Accept": "application/json",
|
|
248
|
+
...(init.body ? { "Content-Type": "application/json" } : {}),
|
|
249
|
+
...(init.headers ?? {}),
|
|
250
|
+
},
|
|
251
|
+
signal: controller.signal,
|
|
252
|
+
});
|
|
253
|
+
const text = await res.text();
|
|
254
|
+
if (!res.ok) {
|
|
255
|
+
throw new N8nApiError(res.status, path, redactKey(text, this.apiKey));
|
|
256
|
+
}
|
|
257
|
+
if (!text) return {} as T;
|
|
258
|
+
return JSON.parse(text) as T;
|
|
259
|
+
} catch (err) {
|
|
260
|
+
if (err instanceof N8nApiError) throw err;
|
|
261
|
+
const msg = err instanceof Error ? err.message : String(err);
|
|
262
|
+
throw new Error(`n8n request to ${path} failed: ${redactKey(msg, this.apiKey)}`);
|
|
263
|
+
} finally {
|
|
264
|
+
clearTimeout(timer);
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
function redactKey(text: string, apiKey: string): string {
|
|
270
|
+
if (!apiKey) return text;
|
|
271
|
+
return text.split(apiKey).join("***REDACTED***");
|
|
272
|
+
}
|
package/src/config.ts
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import { N8nClient } from "./client.ts";
|
|
2
|
+
|
|
3
|
+
export interface N8nPluginConfig {
|
|
4
|
+
baseUrl: string;
|
|
5
|
+
apiKeyInline: string;
|
|
6
|
+
apiKeyEnv: string;
|
|
7
|
+
enableEdit: boolean;
|
|
8
|
+
maxExecutionLogBytes: number;
|
|
9
|
+
requestTimeoutMs: number;
|
|
10
|
+
backupDir?: string;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export function resolveConfig(raw: unknown): N8nPluginConfig {
|
|
14
|
+
if (!raw || typeof raw !== "object") {
|
|
15
|
+
throw new Error("openclaw-n8n: plugin config missing");
|
|
16
|
+
}
|
|
17
|
+
const c = raw as Record<string, unknown>;
|
|
18
|
+
const baseUrl = typeof c.baseUrl === "string" ? c.baseUrl.trim() : "";
|
|
19
|
+
if (!baseUrl) throw new Error("openclaw-n8n: baseUrl is required");
|
|
20
|
+
const apiKeyEnv =
|
|
21
|
+
typeof c.apiKeyEnv === "string" && c.apiKeyEnv.trim() ? c.apiKeyEnv.trim() : "N8N_API_KEY";
|
|
22
|
+
const apiKeyInline = typeof c.apiKey === "string" ? c.apiKey.trim() : "";
|
|
23
|
+
return {
|
|
24
|
+
baseUrl,
|
|
25
|
+
apiKeyInline,
|
|
26
|
+
apiKeyEnv,
|
|
27
|
+
enableEdit: c.enableEdit === true,
|
|
28
|
+
maxExecutionLogBytes:
|
|
29
|
+
typeof c.maxExecutionLogBytes === "number" ? c.maxExecutionLogBytes : 65_536,
|
|
30
|
+
requestTimeoutMs:
|
|
31
|
+
typeof c.requestTimeoutMs === "number" ? c.requestTimeoutMs : 15_000,
|
|
32
|
+
backupDir:
|
|
33
|
+
typeof c.backupDir === "string" && c.backupDir ? c.backupDir : undefined,
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export function resolveApiKey(config: N8nPluginConfig): string {
|
|
38
|
+
if (config.apiKeyInline) return config.apiKeyInline;
|
|
39
|
+
const fromEnv = (process.env[config.apiKeyEnv] ?? "").trim();
|
|
40
|
+
if (!fromEnv) {
|
|
41
|
+
throw new Error(
|
|
42
|
+
`openclaw-n8n: apiKey is empty and env var ${config.apiKeyEnv} is not set`,
|
|
43
|
+
);
|
|
44
|
+
}
|
|
45
|
+
return fromEnv;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export function makeClient(config: N8nPluginConfig): N8nClient {
|
|
49
|
+
return new N8nClient({
|
|
50
|
+
baseUrl: config.baseUrl,
|
|
51
|
+
apiKey: resolveApiKey(config),
|
|
52
|
+
timeoutMs: config.requestTimeoutMs,
|
|
53
|
+
});
|
|
54
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { Type } from "@sinclair/typebox";
|
|
2
|
+
import type { N8nClient } from "../client.ts";
|
|
3
|
+
import { jsonToolResult } from "./result.ts";
|
|
4
|
+
|
|
5
|
+
const Schema = Type.Object(
|
|
6
|
+
{
|
|
7
|
+
id: Type.String({ description: "Workflow id to activate." }),
|
|
8
|
+
},
|
|
9
|
+
{ additionalProperties: false },
|
|
10
|
+
);
|
|
11
|
+
|
|
12
|
+
export function createActivateTool(getClient: () => N8nClient) {
|
|
13
|
+
return {
|
|
14
|
+
name: "n8n_activate",
|
|
15
|
+
label: "n8n: activate workflow",
|
|
16
|
+
description:
|
|
17
|
+
"Activate an n8n workflow so its triggers (webhooks, schedules, polling) start running. Idempotent - activating an already-active workflow returns the current state. Requires enableEdit.",
|
|
18
|
+
parameters: Schema,
|
|
19
|
+
execute: async (_toolCallId: string, rawParams: Record<string, unknown>) => {
|
|
20
|
+
const { id } = rawParams as { id: string };
|
|
21
|
+
const wf = await getClient().activateWorkflow(id);
|
|
22
|
+
return jsonToolResult({
|
|
23
|
+
ok: true,
|
|
24
|
+
action: "activate",
|
|
25
|
+
workflowId: wf.id,
|
|
26
|
+
workflowName: wf.name,
|
|
27
|
+
active: wf.active,
|
|
28
|
+
updatedAt: wf.updatedAt,
|
|
29
|
+
});
|
|
30
|
+
},
|
|
31
|
+
};
|
|
32
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { Type } from "@sinclair/typebox";
|
|
2
|
+
import type { N8nClient } from "../client.ts";
|
|
3
|
+
import { jsonToolResult } from "./result.ts";
|
|
4
|
+
|
|
5
|
+
const Schema = Type.Object(
|
|
6
|
+
{
|
|
7
|
+
id: Type.String({ description: "Workflow id to deactivate." }),
|
|
8
|
+
},
|
|
9
|
+
{ additionalProperties: false },
|
|
10
|
+
);
|
|
11
|
+
|
|
12
|
+
export function createDeactivateTool(getClient: () => N8nClient) {
|
|
13
|
+
return {
|
|
14
|
+
name: "n8n_deactivate",
|
|
15
|
+
label: "n8n: deactivate workflow",
|
|
16
|
+
description:
|
|
17
|
+
"Deactivate an n8n workflow so its triggers stop firing. Running executions are not cancelled. Idempotent. Requires enableEdit.",
|
|
18
|
+
parameters: Schema,
|
|
19
|
+
execute: async (_toolCallId: string, rawParams: Record<string, unknown>) => {
|
|
20
|
+
const { id } = rawParams as { id: string };
|
|
21
|
+
const wf = await getClient().deactivateWorkflow(id);
|
|
22
|
+
return jsonToolResult({
|
|
23
|
+
ok: true,
|
|
24
|
+
action: "deactivate",
|
|
25
|
+
workflowId: wf.id,
|
|
26
|
+
workflowName: wf.name,
|
|
27
|
+
active: wf.active,
|
|
28
|
+
updatedAt: wf.updatedAt,
|
|
29
|
+
});
|
|
30
|
+
},
|
|
31
|
+
};
|
|
32
|
+
}
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
import { Type } from "@sinclair/typebox";
|
|
2
|
+
import type { N8nClient, N8nExecution } from "../client.ts";
|
|
3
|
+
import { jsonToolResult } from "./result.ts";
|
|
4
|
+
|
|
5
|
+
const Schema = Type.Object(
|
|
6
|
+
{
|
|
7
|
+
id: Type.String({
|
|
8
|
+
description: "Execution id (from n8n_list_executions).",
|
|
9
|
+
}),
|
|
10
|
+
includeRunData: Type.Optional(
|
|
11
|
+
Type.Boolean({
|
|
12
|
+
description:
|
|
13
|
+
"Include the per-node run log. Default true. Turn off to get just status + error summary.",
|
|
14
|
+
}),
|
|
15
|
+
),
|
|
16
|
+
},
|
|
17
|
+
{ additionalProperties: false },
|
|
18
|
+
);
|
|
19
|
+
|
|
20
|
+
export interface GetExecutionDeps {
|
|
21
|
+
getClient: () => N8nClient;
|
|
22
|
+
maxLogBytes: number;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export function createGetExecutionTool(deps: GetExecutionDeps) {
|
|
26
|
+
return {
|
|
27
|
+
name: "n8n_get_execution",
|
|
28
|
+
label: "n8n: get execution",
|
|
29
|
+
description:
|
|
30
|
+
"Fetch a single n8n execution by id. Returns status, mode, timing, and per-node run data. Large run logs are truncated with a tail hint. Error executions include the raw error verbatim.",
|
|
31
|
+
parameters: Schema,
|
|
32
|
+
execute: async (_toolCallId: string, rawParams: Record<string, unknown>) => {
|
|
33
|
+
const params = rawParams as { id: string; includeRunData?: boolean };
|
|
34
|
+
const includeRunData = params.includeRunData !== false;
|
|
35
|
+
const ex: N8nExecution = await deps.getClient().getExecution(params.id, {
|
|
36
|
+
includeData: includeRunData,
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
const status =
|
|
40
|
+
ex.status ?? (ex.finished ? "success" : "running");
|
|
41
|
+
const base = {
|
|
42
|
+
id: String(ex.id),
|
|
43
|
+
workflowId: String(ex.workflowId ?? ""),
|
|
44
|
+
workflowName: ex.workflowData?.name ?? null,
|
|
45
|
+
status,
|
|
46
|
+
mode: ex.mode,
|
|
47
|
+
finished: ex.finished,
|
|
48
|
+
startedAt: ex.startedAt ?? null,
|
|
49
|
+
stoppedAt: ex.stoppedAt ?? null,
|
|
50
|
+
waitTill: ex.waitTill ?? null,
|
|
51
|
+
retryOf: ex.retryOf ?? null,
|
|
52
|
+
retrySuccessId: ex.retrySuccessId ?? null,
|
|
53
|
+
lastNodeExecuted: ex.data?.resultData?.lastNodeExecuted ?? null,
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
const error = ex.data?.resultData?.error;
|
|
57
|
+
const runData = ex.data?.resultData?.runData;
|
|
58
|
+
|
|
59
|
+
const body: Record<string, unknown> = { ...base };
|
|
60
|
+
if (error !== undefined) {
|
|
61
|
+
body.error = error;
|
|
62
|
+
}
|
|
63
|
+
if (includeRunData && runData !== undefined) {
|
|
64
|
+
body.runData = truncateRunData(runData, deps.maxLogBytes);
|
|
65
|
+
}
|
|
66
|
+
return jsonToolResult(body);
|
|
67
|
+
},
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
function truncateRunData(
|
|
72
|
+
runData: unknown,
|
|
73
|
+
maxBytes: number,
|
|
74
|
+
): unknown {
|
|
75
|
+
const serialized = JSON.stringify(runData);
|
|
76
|
+
const size = Buffer.byteLength(serialized, "utf8");
|
|
77
|
+
if (size <= maxBytes) {
|
|
78
|
+
return runData;
|
|
79
|
+
}
|
|
80
|
+
const tail = serialized.slice(-maxBytes);
|
|
81
|
+
return {
|
|
82
|
+
_truncated: true,
|
|
83
|
+
_hint: `log exceeded ${maxBytes} bytes (actual ${size}), showing tail`,
|
|
84
|
+
_tail: tail,
|
|
85
|
+
};
|
|
86
|
+
}
|