@mambasms/mcp-server 0.6.18 → 0.6.19
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/index.js
CHANGED
|
@@ -1,1419 +1,1420 @@
|
|
|
1
|
-
#!/usr/bin/env node
|
|
2
|
-
/**
|
|
3
|
-
* MambaSMS MCP server (stdio)
|
|
4
|
-
*
|
|
5
|
-
* 环境变量:
|
|
6
|
-
* MAMBA_API_BASE API 域名(默认 https://api.mambasms.com;测试环境 https://olapapi.mambasend.com)
|
|
7
|
-
* MAMBA_ACCESS_TOKEN 店铺 API 密钥(后台「店铺设置 → API 密钥」获取,必填)
|
|
8
|
-
*
|
|
9
|
-
* Codex 配置示例(~/.codex/config.toml):
|
|
10
|
-
* [mcp_servers.mambasms]
|
|
11
|
-
* command = "npx"
|
|
12
|
-
* args = ["-y", "@mambasms/mcp-server"]
|
|
13
|
-
* env = { "MAMBA_API_BASE" = "https://api.mambasms.com", "MAMBA_ACCESS_TOKEN" = "shop_xxx" }
|
|
14
|
-
*/
|
|
15
|
-
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
|
|
16
|
-
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
|
|
17
|
-
import { z } from "zod";
|
|
18
|
-
|
|
19
|
-
const API_BASE = (process.env.MAMBA_API_BASE || "https://api.mambasms.com").replace(/\/+$/, "");
|
|
20
|
-
|
|
21
|
-
/** 客户端版本(用于服务端统计/兼容判断;从同目录 package.json 读,失败则 unknown) */
|
|
22
|
-
const CLIENT_VERSION = await (async () => {
|
|
23
|
-
try {
|
|
24
|
-
const { readFile } = await import("node:fs/promises");
|
|
25
|
-
const { fileURLToPath } = await import("node:url");
|
|
26
|
-
const { dirname, join } = await import("node:path");
|
|
27
|
-
const here = dirname(fileURLToPath(import.meta.url));
|
|
28
|
-
return JSON.parse(await readFile(join(here, "package.json"), "utf8")).version || "unknown";
|
|
29
|
-
} catch {
|
|
30
|
-
return "unknown";
|
|
31
|
-
}
|
|
32
|
-
})();
|
|
33
|
-
const TOKEN = process.env.MAMBA_ACCESS_TOKEN;
|
|
34
|
-
|
|
35
|
-
const PLACEHOLDER_GUIDE = `## 变量占位符速查(模板 HTML 内可用)
|
|
36
|
-
- 退订链接(必须): {Unsubscribe} —— 放在邮件页脚,缺失时 template_create 会返回 warning
|
|
37
|
-
- 联系人加密ID: {ContactEncryption} —— 拼接退订/个性化链接用
|
|
38
|
-
- 订单变量(自动化场景): \${order.number} \${order.total_price} \${order.created_at} \${order.address1}
|
|
39
|
-
- 订单商品循环: [#list order.products as p] ... \${p.title} \${p.quantity} \${p.price} ... [/#list]
|
|
40
|
-
- 商品数量: \${order.products?size}
|
|
41
|
-
- 空值兜底(强烈建议): \${order.address1?default('')} —— 取不到值时给默认,避免渲染失败
|
|
42
|
-
- 花括号规范: 只用单花括号,禁止 {{ }} 混用(引擎只认单花括号,混用会导致发送失败)`;
|
|
43
|
-
|
|
44
|
-
async function callApi(path, body) {
|
|
45
|
-
if (!TOKEN) {
|
|
46
|
-
throw new Error("MAMBA_ACCESS_TOKEN env var is not set");
|
|
47
|
-
}
|
|
48
|
-
const resp = await fetch(`${API_BASE}${path}`, {
|
|
49
|
-
method: "POST",
|
|
50
|
-
headers: {
|
|
51
|
-
"Content-Type": "application/json",
|
|
52
|
-
"X-Mamba-Access-Token": TOKEN,
|
|
53
|
-
"X-Mamba-Source": "mcp",
|
|
54
|
-
"X-Mamba-Client-Version": CLIENT_VERSION,
|
|
55
|
-
},
|
|
56
|
-
body: JSON.stringify(body ?? {}),
|
|
57
|
-
});
|
|
58
|
-
const text = await resp.text();
|
|
59
|
-
let json = null;
|
|
60
|
-
try {
|
|
61
|
-
json = JSON.parse(text);
|
|
62
|
-
} catch {
|
|
63
|
-
json = null;
|
|
64
|
-
}
|
|
65
|
-
if (!resp.ok || (json && json.success === false)) {
|
|
66
|
-
const msg = json ? (json.message || JSON.stringify(json)) : text.slice(0, 300);
|
|
67
|
-
if (resp.status === 429) {
|
|
68
|
-
throw new Error(
|
|
69
|
-
`API 429 ${path}: 触发限流。店铺级上限 10 请求/秒;另外 campaign_list 与开放API的 campaign/report、` +
|
|
70
|
-
`getCampaignLink 是 10 秒 1 次(同店铺)。请退避后重试(活动列表建议隔 ≥10 秒再查,或改用 campaign_report / ` +
|
|
71
|
-
`campaign_link_report 查明细)。原始返回: ${msg}`
|
|
72
|
-
);
|
|
73
|
-
}
|
|
74
|
-
throw new Error(`API ${resp.status} ${path}: ${msg}`);
|
|
75
|
-
}
|
|
76
|
-
return json ? (json.data !== undefined ? json.data : json) : text;
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
// ==================== 工具注册中心(供通用发现/调度工具使用) ====================
|
|
81
|
-
// 参考 Omnisend MCP 的紧凑工具设计:除逐个命名工具外,再暴露 mamba_search / mamba_tool_schema / mamba_execute,
|
|
82
|
-
// 让 AI 能在不了解全部工具名时先发现、再执行;工具变多时上下文占用不随数量膨胀。
|
|
83
|
-
const READONLY_MODE = /^(1|true|yes)$/i.test(process.env.MAMBA_READONLY || "");
|
|
84
|
-
|
|
85
|
-
/** 有副作用/会改数据的工具(默认全部只读,新增写工具必须登记在这里) */
|
|
86
|
-
const WRITE_TOOLS = new Set([
|
|
87
|
-
"template_create",
|
|
88
|
-
"template_update",
|
|
89
|
-
"template_delete",
|
|
90
|
-
"template_create_from_design",
|
|
91
|
-
"template_send_test",
|
|
92
|
-
"template_export",
|
|
93
|
-
"template_import",
|
|
94
|
-
"upload_image",
|
|
95
|
-
"campaign_create_email",
|
|
96
|
-
"campaign_create_sms",
|
|
97
|
-
"campaign_create_abtest",
|
|
98
|
-
"campaign_create_sms_abtest",
|
|
99
|
-
"campaign_update_content",
|
|
100
|
-
"flow_create",
|
|
101
|
-
"flow_update_content",
|
|
102
|
-
"user_group_create",
|
|
103
|
-
]);
|
|
104
|
-
|
|
105
|
-
const TOOL_REGISTRY = new Map();
|
|
106
|
-
|
|
107
|
-
// 建组前必须查分组条件目录(与平台 AI 助手同一道闸门):字段/nestedPath 只能来自真源目录,
|
|
108
|
-
// 凭印象写字段必被后端校验打回,来回重试比多查一次更贵。30 分钟内查过一次即免查。
|
|
109
|
-
const GROUP_CATALOG_TTL_MS = 30 * 60 * 1000;
|
|
110
|
-
let groupCatalogQueriedAt = 0;
|
|
111
|
-
|
|
112
|
-
/**
|
|
113
|
-
* 注册一个工具,同时登记元数据(供 mamba_search / mamba_execute 调度)
|
|
114
|
-
* MAMBA_READONLY=1 时只注册只读工具(等价 Omnisend 的 read-only 授权预设)
|
|
115
|
-
*/
|
|
116
|
-
function defineTool(name, description, shape, handler) {
|
|
117
|
-
const readOnly = !WRITE_TOOLS.has(name);
|
|
118
|
-
TOOL_REGISTRY.set(name, { name, description, shape, handler, readOnly });
|
|
119
|
-
if (READONLY_MODE && !readOnly) {
|
|
120
|
-
return; // 只读模式:跳过写工具注册
|
|
121
|
-
}
|
|
122
|
-
server.tool(name, description, shape, handler);
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
const server = new McpServer(
|
|
126
|
-
{ name: "mambasms-mcp-server", version: "0.6.
|
|
127
|
-
{
|
|
128
|
-
instructions: `MambaSMS 营销自动化(店铺级,鉴权用 MAMBA_ACCESS_TOKEN 店铺密钥)。共 52 个工具,分四组。
|
|
129
|
-
不确定用什么/想先看总览 → mamba_guide;只知道关键词 → mamba_search;要参数细节 → mamba_tool_schema。
|
|
130
|
-
|
|
131
|
-
【模板(17)】四条创建路径:A) template_create_from_design —— 首选,传结构化 emailDesign(style/theme/hero/intro/products/offer/cta/social),写前先 catalog_query(topic=emailDesign);要骨架传 catalog_query(topic=layouts);套已有模板传 templateId。B) template_create(html) —— 迁移现成 HTML/精细控制。C) template_create(sections) —— 动态区块(购物车/浏览列表),发送时填该联系人的数据。D) template_import —— mambatpl 或 zipHtml。参考面:template_list_system/template_get_system、block_library_query、catalog_query、template_snippets(页头页脚片段,社交图标走 link.mambasms.com/dynamicImage/{platform}/{size}/{shape}/{color},key 大小写敏感)。效果:template_preview(免登永久链接)、template_send_test。图片用 upload_image 转存 CDN。退订必须 {Unsubscribe}。
|
|
132
|
-
【先确认再动手】第一次给客户做邮件(模板或活动)时,先问一次要哪种形式(除非客户已说清):A. 用平台模板(推荐,页脚自带社媒图标与退订、风格统一);B. 从零生成组件(内容由你生成,客户之后可在后台拖拽微调);C. 源码模式HTML(由你直接产出整份 HTML 源码,后台可继续改源码;客户贴现成 HTML 时也走这个)。注意:内容是**你(AI)产出**,不要向客户索要 HTML;只需让客户选形式。客户说"随便/你决定"→选 A。社媒默认用平台页脚图标(推荐),客户有指定账号/图标再按其要求。【社媒与页脚】社媒入口**不要自己发明**:优先套系统模板(template_list_system→template_get_system→传 templateId),模板自带页脚社媒图标+退订;手写 HTML 或骨架时,社媒必须用 template_snippets(type=footer-social) 的片段(平台图标直链),放在**最底部(退订上方)**;禁止用文字按钮充当社媒入口,也不要在正文中部放社媒区。emailDesign 的 social 字段可省略(页脚社媒由模板/骨架生成)。
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
"
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
"
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
.
|
|
182
|
-
.
|
|
183
|
-
.
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
.
|
|
196
|
-
.
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
if (
|
|
203
|
-
if (
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
.
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
if (
|
|
235
|
-
if (
|
|
236
|
-
if (
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
"
|
|
245
|
-
|
|
246
|
-
"
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
const {
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
const
|
|
272
|
-
form
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
"X-Mamba-
|
|
278
|
-
"X-Mamba-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
"
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
"
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
.
|
|
323
|
-
.
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
if (
|
|
330
|
-
if (
|
|
331
|
-
if (
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
"
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
"
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
"
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
.
|
|
368
|
-
.
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
"
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
if (
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
emailDesign
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
.
|
|
406
|
-
.
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
.
|
|
412
|
-
.
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
.
|
|
416
|
-
.
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
.
|
|
420
|
-
.
|
|
421
|
-
.
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
.
|
|
425
|
-
.
|
|
426
|
-
.
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
["
|
|
434
|
-
["
|
|
435
|
-
["
|
|
436
|
-
["
|
|
437
|
-
["
|
|
438
|
-
["
|
|
439
|
-
["
|
|
440
|
-
["
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
"
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
const
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
"
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
"
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
"
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
"
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
const {
|
|
512
|
-
const {
|
|
513
|
-
const
|
|
514
|
-
const
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
}
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
"
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
if (
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
"
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
if (
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
"
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
...
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
if (
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
"
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
if (
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
"
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
if (
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
"
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
if (
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
"
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
if (
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
"
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
if (
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
"
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
if (
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
"
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
"
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
"
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
|
|
728
|
-
|
|
729
|
-
|
|
730
|
-
"
|
|
731
|
-
|
|
732
|
-
|
|
733
|
-
|
|
734
|
-
|
|
735
|
-
|
|
736
|
-
|
|
737
|
-
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
"
|
|
748
|
-
|
|
749
|
-
|
|
750
|
-
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
|
|
755
|
-
|
|
756
|
-
if (
|
|
757
|
-
if (
|
|
758
|
-
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
|
|
762
|
-
|
|
763
|
-
|
|
764
|
-
|
|
765
|
-
"
|
|
766
|
-
|
|
767
|
-
|
|
768
|
-
|
|
769
|
-
|
|
770
|
-
|
|
771
|
-
|
|
772
|
-
|
|
773
|
-
|
|
774
|
-
if (
|
|
775
|
-
|
|
776
|
-
|
|
777
|
-
|
|
778
|
-
|
|
779
|
-
|
|
780
|
-
|
|
781
|
-
|
|
782
|
-
"
|
|
783
|
-
|
|
784
|
-
|
|
785
|
-
|
|
786
|
-
|
|
787
|
-
|
|
788
|
-
|
|
789
|
-
|
|
790
|
-
|
|
791
|
-
|
|
792
|
-
|
|
793
|
-
|
|
794
|
-
if (
|
|
795
|
-
if (
|
|
796
|
-
|
|
797
|
-
|
|
798
|
-
|
|
799
|
-
|
|
800
|
-
|
|
801
|
-
|
|
802
|
-
|
|
803
|
-
"
|
|
804
|
-
|
|
805
|
-
|
|
806
|
-
|
|
807
|
-
|
|
808
|
-
|
|
809
|
-
|
|
810
|
-
|
|
811
|
-
|
|
812
|
-
|
|
813
|
-
"
|
|
814
|
-
|
|
815
|
-
|
|
816
|
-
|
|
817
|
-
|
|
818
|
-
|
|
819
|
-
|
|
820
|
-
|
|
821
|
-
|
|
822
|
-
|
|
823
|
-
|
|
824
|
-
|
|
825
|
-
"
|
|
826
|
-
|
|
827
|
-
|
|
828
|
-
|
|
829
|
-
|
|
830
|
-
|
|
831
|
-
|
|
832
|
-
|
|
833
|
-
|
|
834
|
-
|
|
835
|
-
|
|
836
|
-
|
|
837
|
-
|
|
838
|
-
|
|
839
|
-
|
|
840
|
-
"
|
|
841
|
-
|
|
842
|
-
|
|
843
|
-
|
|
844
|
-
|
|
845
|
-
|
|
846
|
-
|
|
847
|
-
|
|
848
|
-
|
|
849
|
-
|
|
850
|
-
"
|
|
851
|
-
|
|
852
|
-
|
|
853
|
-
|
|
854
|
-
|
|
855
|
-
|
|
856
|
-
|
|
857
|
-
if (
|
|
858
|
-
|
|
859
|
-
|
|
860
|
-
|
|
861
|
-
|
|
862
|
-
|
|
863
|
-
|
|
864
|
-
|
|
865
|
-
"
|
|
866
|
-
|
|
867
|
-
|
|
868
|
-
|
|
869
|
-
|
|
870
|
-
|
|
871
|
-
|
|
872
|
-
if (
|
|
873
|
-
|
|
874
|
-
|
|
875
|
-
|
|
876
|
-
|
|
877
|
-
|
|
878
|
-
|
|
879
|
-
|
|
880
|
-
"
|
|
881
|
-
|
|
882
|
-
|
|
883
|
-
|
|
884
|
-
|
|
885
|
-
|
|
886
|
-
|
|
887
|
-
|
|
888
|
-
|
|
889
|
-
|
|
890
|
-
|
|
891
|
-
|
|
892
|
-
}
|
|
893
|
-
|
|
894
|
-
|
|
895
|
-
|
|
896
|
-
|
|
897
|
-
|
|
898
|
-
|
|
899
|
-
|
|
900
|
-
|
|
901
|
-
|
|
902
|
-
|
|
903
|
-
|
|
904
|
-
.
|
|
905
|
-
.
|
|
906
|
-
|
|
907
|
-
|
|
908
|
-
.
|
|
909
|
-
.
|
|
910
|
-
|
|
911
|
-
|
|
912
|
-
.
|
|
913
|
-
.
|
|
914
|
-
|
|
915
|
-
|
|
916
|
-
.
|
|
917
|
-
.
|
|
918
|
-
|
|
919
|
-
|
|
920
|
-
|
|
921
|
-
|
|
922
|
-
|
|
923
|
-
|
|
924
|
-
|
|
925
|
-
|
|
926
|
-
|
|
927
|
-
|
|
928
|
-
|
|
929
|
-
|
|
930
|
-
|
|
931
|
-
|
|
932
|
-
|
|
933
|
-
|
|
934
|
-
|
|
935
|
-
|
|
936
|
-
|
|
937
|
-
|
|
938
|
-
|
|
939
|
-
|
|
940
|
-
|
|
941
|
-
|
|
942
|
-
|
|
943
|
-
|
|
944
|
-
|
|
945
|
-
|
|
946
|
-
|
|
947
|
-
|
|
948
|
-
|
|
949
|
-
|
|
950
|
-
|
|
951
|
-
|
|
952
|
-
|
|
953
|
-
|
|
954
|
-
|
|
955
|
-
|
|
956
|
-
|
|
957
|
-
|
|
958
|
-
|
|
959
|
-
|
|
960
|
-
const
|
|
961
|
-
|
|
962
|
-
|
|
963
|
-
|
|
964
|
-
|
|
965
|
-
|
|
966
|
-
|
|
967
|
-
|
|
968
|
-
|
|
969
|
-
|
|
970
|
-
|
|
971
|
-
|
|
972
|
-
|
|
973
|
-
|
|
974
|
-
|
|
975
|
-
|
|
976
|
-
|
|
977
|
-
|
|
978
|
-
const
|
|
979
|
-
|
|
980
|
-
|
|
981
|
-
|
|
982
|
-
|
|
983
|
-
|
|
984
|
-
|
|
985
|
-
|
|
986
|
-
{
|
|
987
|
-
|
|
988
|
-
|
|
989
|
-
|
|
990
|
-
|
|
991
|
-
|
|
992
|
-
|
|
993
|
-
|
|
994
|
-
|
|
995
|
-
|
|
996
|
-
|
|
997
|
-
|
|
998
|
-
const
|
|
999
|
-
|
|
1000
|
-
|
|
1001
|
-
|
|
1002
|
-
|
|
1003
|
-
|
|
1004
|
-
|
|
1005
|
-
|
|
1006
|
-
|
|
1007
|
-
const
|
|
1008
|
-
|
|
1009
|
-
|
|
1010
|
-
|
|
1011
|
-
|
|
1012
|
-
|
|
1013
|
-
|
|
1014
|
-
|
|
1015
|
-
{
|
|
1016
|
-
|
|
1017
|
-
|
|
1018
|
-
|
|
1019
|
-
|
|
1020
|
-
|
|
1021
|
-
|
|
1022
|
-
|
|
1023
|
-
|
|
1024
|
-
|
|
1025
|
-
|
|
1026
|
-
|
|
1027
|
-
|
|
1028
|
-
|
|
1029
|
-
|
|
1030
|
-
|
|
1031
|
-
|
|
1032
|
-
|
|
1033
|
-
|
|
1034
|
-
|
|
1035
|
-
|
|
1036
|
-
|
|
1037
|
-
|
|
1038
|
-
|
|
1039
|
-
|
|
1040
|
-
|
|
1041
|
-
|
|
1042
|
-
|
|
1043
|
-
|
|
1044
|
-
|
|
1045
|
-
const
|
|
1046
|
-
|
|
1047
|
-
|
|
1048
|
-
|
|
1049
|
-
|
|
1050
|
-
|
|
1051
|
-
|
|
1052
|
-
|
|
1053
|
-
{
|
|
1054
|
-
|
|
1055
|
-
|
|
1056
|
-
|
|
1057
|
-
|
|
1058
|
-
|
|
1059
|
-
|
|
1060
|
-
|
|
1061
|
-
|
|
1062
|
-
|
|
1063
|
-
|
|
1064
|
-
|
|
1065
|
-
|
|
1066
|
-
|
|
1067
|
-
|
|
1068
|
-
|
|
1069
|
-
|
|
1070
|
-
|
|
1071
|
-
|
|
1072
|
-
|
|
1073
|
-
|
|
1074
|
-
|
|
1075
|
-
|
|
1076
|
-
|
|
1077
|
-
|
|
1078
|
-
|
|
1079
|
-
|
|
1080
|
-
|
|
1081
|
-
const
|
|
1082
|
-
|
|
1083
|
-
|
|
1084
|
-
|
|
1085
|
-
|
|
1086
|
-
|
|
1087
|
-
|
|
1088
|
-
"
|
|
1089
|
-
|
|
1090
|
-
|
|
1091
|
-
|
|
1092
|
-
|
|
1093
|
-
|
|
1094
|
-
|
|
1095
|
-
|
|
1096
|
-
|
|
1097
|
-
.
|
|
1098
|
-
.
|
|
1099
|
-
|
|
1100
|
-
|
|
1101
|
-
|
|
1102
|
-
|
|
1103
|
-
|
|
1104
|
-
|
|
1105
|
-
|
|
1106
|
-
if (a.
|
|
1107
|
-
|
|
1108
|
-
|
|
1109
|
-
|
|
1110
|
-
|
|
1111
|
-
const
|
|
1112
|
-
|
|
1113
|
-
|
|
1114
|
-
|
|
1115
|
-
|
|
1116
|
-
|
|
1117
|
-
|
|
1118
|
-
|
|
1119
|
-
|
|
1120
|
-
const
|
|
1121
|
-
|
|
1122
|
-
|
|
1123
|
-
|
|
1124
|
-
|
|
1125
|
-
|
|
1126
|
-
|
|
1127
|
-
|
|
1128
|
-
|
|
1129
|
-
|
|
1130
|
-
|
|
1131
|
-
|
|
1132
|
-
|
|
1133
|
-
|
|
1134
|
-
|
|
1135
|
-
|
|
1136
|
-
|
|
1137
|
-
|
|
1138
|
-
|
|
1139
|
-
|
|
1140
|
-
|
|
1141
|
-
|
|
1142
|
-
|
|
1143
|
-
|
|
1144
|
-
|
|
1145
|
-
|
|
1146
|
-
|
|
1147
|
-
|
|
1148
|
-
|
|
1149
|
-
|
|
1150
|
-
|
|
1151
|
-
|
|
1152
|
-
|
|
1153
|
-
|
|
1154
|
-
|
|
1155
|
-
|
|
1156
|
-
|
|
1157
|
-
|
|
1158
|
-
|
|
1159
|
-
|
|
1160
|
-
|
|
1161
|
-
|
|
1162
|
-
|
|
1163
|
-
|
|
1164
|
-
|
|
1165
|
-
|
|
1166
|
-
|
|
1167
|
-
|
|
1168
|
-
|
|
1169
|
-
|
|
1170
|
-
|
|
1171
|
-
|
|
1172
|
-
|
|
1173
|
-
|
|
1174
|
-
const
|
|
1175
|
-
|
|
1176
|
-
|
|
1177
|
-
|
|
1178
|
-
|
|
1179
|
-
|
|
1180
|
-
|
|
1181
|
-
"
|
|
1182
|
-
|
|
1183
|
-
|
|
1184
|
-
|
|
1185
|
-
|
|
1186
|
-
|
|
1187
|
-
|
|
1188
|
-
|
|
1189
|
-
|
|
1190
|
-
|
|
1191
|
-
const
|
|
1192
|
-
|
|
1193
|
-
|
|
1194
|
-
|
|
1195
|
-
|
|
1196
|
-
|
|
1197
|
-
const
|
|
1198
|
-
|
|
1199
|
-
|
|
1200
|
-
|
|
1201
|
-
|
|
1202
|
-
|
|
1203
|
-
|
|
1204
|
-
|
|
1205
|
-
|
|
1206
|
-
|
|
1207
|
-
|
|
1208
|
-
-
|
|
1209
|
-
- condition
|
|
1210
|
-
|
|
1211
|
-
|
|
1212
|
-
-
|
|
1213
|
-
-
|
|
1214
|
-
-
|
|
1215
|
-
|
|
1216
|
-
|
|
1217
|
-
|
|
1218
|
-
|
|
1219
|
-
|
|
1220
|
-
|
|
1221
|
-
|
|
1222
|
-
|
|
1223
|
-
|
|
1224
|
-
|
|
1225
|
-
|
|
1226
|
-
|
|
1227
|
-
|
|
1228
|
-
|
|
1229
|
-
|
|
1230
|
-
|
|
1231
|
-
|
|
1232
|
-
|
|
1233
|
-
|
|
1234
|
-
|
|
1235
|
-
|
|
1236
|
-
const
|
|
1237
|
-
|
|
1238
|
-
|
|
1239
|
-
|
|
1240
|
-
|
|
1241
|
-
|
|
1242
|
-
|
|
1243
|
-
|
|
1244
|
-
|
|
1245
|
-
|
|
1246
|
-
|
|
1247
|
-
|
|
1248
|
-
"
|
|
1249
|
-
"
|
|
1250
|
-
"
|
|
1251
|
-
"
|
|
1252
|
-
"
|
|
1253
|
-
"
|
|
1254
|
-
"
|
|
1255
|
-
"
|
|
1256
|
-
"
|
|
1257
|
-
|
|
1258
|
-
|
|
1259
|
-
|
|
1260
|
-
"
|
|
1261
|
-
"-
|
|
1262
|
-
"-
|
|
1263
|
-
"-
|
|
1264
|
-
"-
|
|
1265
|
-
"-
|
|
1266
|
-
"
|
|
1267
|
-
|
|
1268
|
-
|
|
1269
|
-
|
|
1270
|
-
"
|
|
1271
|
-
"-
|
|
1272
|
-
"-
|
|
1273
|
-
"-
|
|
1274
|
-
"-
|
|
1275
|
-
|
|
1276
|
-
|
|
1277
|
-
|
|
1278
|
-
"
|
|
1279
|
-
|
|
1280
|
-
'-
|
|
1281
|
-
'-
|
|
1282
|
-
|
|
1283
|
-
"-
|
|
1284
|
-
|
|
1285
|
-
|
|
1286
|
-
|
|
1287
|
-
"
|
|
1288
|
-
"
|
|
1289
|
-
"
|
|
1290
|
-
"
|
|
1291
|
-
"
|
|
1292
|
-
"
|
|
1293
|
-
|
|
1294
|
-
|
|
1295
|
-
|
|
1296
|
-
"
|
|
1297
|
-
"
|
|
1298
|
-
|
|
1299
|
-
|
|
1300
|
-
|
|
1301
|
-
|
|
1302
|
-
|
|
1303
|
-
"
|
|
1304
|
-
|
|
1305
|
-
|
|
1306
|
-
|
|
1307
|
-
|
|
1308
|
-
.
|
|
1309
|
-
.
|
|
1310
|
-
|
|
1311
|
-
|
|
1312
|
-
|
|
1313
|
-
const
|
|
1314
|
-
|
|
1315
|
-
|
|
1316
|
-
|
|
1317
|
-
|
|
1318
|
-
|
|
1319
|
-
|
|
1320
|
-
|
|
1321
|
-
|
|
1322
|
-
|
|
1323
|
-
"
|
|
1324
|
-
|
|
1325
|
-
|
|
1326
|
-
|
|
1327
|
-
|
|
1328
|
-
|
|
1329
|
-
const
|
|
1330
|
-
|
|
1331
|
-
.filter(
|
|
1332
|
-
|
|
1333
|
-
|
|
1334
|
-
|
|
1335
|
-
|
|
1336
|
-
|
|
1337
|
-
|
|
1338
|
-
|
|
1339
|
-
|
|
1340
|
-
|
|
1341
|
-
|
|
1342
|
-
.
|
|
1343
|
-
|
|
1344
|
-
|
|
1345
|
-
|
|
1346
|
-
|
|
1347
|
-
|
|
1348
|
-
|
|
1349
|
-
|
|
1350
|
-
|
|
1351
|
-
|
|
1352
|
-
|
|
1353
|
-
|
|
1354
|
-
|
|
1355
|
-
|
|
1356
|
-
|
|
1357
|
-
|
|
1358
|
-
|
|
1359
|
-
|
|
1360
|
-
|
|
1361
|
-
"
|
|
1362
|
-
|
|
1363
|
-
|
|
1364
|
-
|
|
1365
|
-
|
|
1366
|
-
|
|
1367
|
-
|
|
1368
|
-
|
|
1369
|
-
|
|
1370
|
-
|
|
1371
|
-
|
|
1372
|
-
|
|
1373
|
-
|
|
1374
|
-
|
|
1375
|
-
|
|
1376
|
-
|
|
1377
|
-
|
|
1378
|
-
|
|
1379
|
-
|
|
1380
|
-
|
|
1381
|
-
|
|
1382
|
-
|
|
1383
|
-
|
|
1384
|
-
|
|
1385
|
-
|
|
1386
|
-
|
|
1387
|
-
|
|
1388
|
-
|
|
1389
|
-
|
|
1390
|
-
|
|
1391
|
-
|
|
1392
|
-
|
|
1393
|
-
|
|
1394
|
-
|
|
1395
|
-
|
|
1396
|
-
|
|
1397
|
-
|
|
1398
|
-
|
|
1399
|
-
|
|
1400
|
-
"
|
|
1401
|
-
|
|
1402
|
-
|
|
1403
|
-
|
|
1404
|
-
|
|
1405
|
-
|
|
1406
|
-
|
|
1407
|
-
|
|
1408
|
-
|
|
1409
|
-
|
|
1410
|
-
|
|
1411
|
-
|
|
1412
|
-
|
|
1413
|
-
|
|
1414
|
-
|
|
1415
|
-
|
|
1416
|
-
|
|
1417
|
-
|
|
1418
|
-
|
|
1419
|
-
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
/**
|
|
3
|
+
* MambaSMS MCP server (stdio)
|
|
4
|
+
*
|
|
5
|
+
* 环境变量:
|
|
6
|
+
* MAMBA_API_BASE API 域名(默认 https://api.mambasms.com;测试环境 https://olapapi.mambasend.com)
|
|
7
|
+
* MAMBA_ACCESS_TOKEN 店铺 API 密钥(后台「店铺设置 → API 密钥」获取,必填)
|
|
8
|
+
*
|
|
9
|
+
* Codex 配置示例(~/.codex/config.toml):
|
|
10
|
+
* [mcp_servers.mambasms]
|
|
11
|
+
* command = "npx"
|
|
12
|
+
* args = ["-y", "@mambasms/mcp-server"]
|
|
13
|
+
* env = { "MAMBA_API_BASE" = "https://api.mambasms.com", "MAMBA_ACCESS_TOKEN" = "shop_xxx" }
|
|
14
|
+
*/
|
|
15
|
+
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
|
|
16
|
+
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
|
|
17
|
+
import { z } from "zod";
|
|
18
|
+
|
|
19
|
+
const API_BASE = (process.env.MAMBA_API_BASE || "https://api.mambasms.com").replace(/\/+$/, "");
|
|
20
|
+
|
|
21
|
+
/** 客户端版本(用于服务端统计/兼容判断;从同目录 package.json 读,失败则 unknown) */
|
|
22
|
+
const CLIENT_VERSION = await (async () => {
|
|
23
|
+
try {
|
|
24
|
+
const { readFile } = await import("node:fs/promises");
|
|
25
|
+
const { fileURLToPath } = await import("node:url");
|
|
26
|
+
const { dirname, join } = await import("node:path");
|
|
27
|
+
const here = dirname(fileURLToPath(import.meta.url));
|
|
28
|
+
return JSON.parse(await readFile(join(here, "package.json"), "utf8")).version || "unknown";
|
|
29
|
+
} catch {
|
|
30
|
+
return "unknown";
|
|
31
|
+
}
|
|
32
|
+
})();
|
|
33
|
+
const TOKEN = process.env.MAMBA_ACCESS_TOKEN;
|
|
34
|
+
|
|
35
|
+
const PLACEHOLDER_GUIDE = `## 变量占位符速查(模板 HTML 内可用)
|
|
36
|
+
- 退订链接(必须): {Unsubscribe} —— 放在邮件页脚,缺失时 template_create 会返回 warning
|
|
37
|
+
- 联系人加密ID: {ContactEncryption} —— 拼接退订/个性化链接用
|
|
38
|
+
- 订单变量(自动化场景): \${order.number} \${order.total_price} \${order.created_at} \${order.address1}
|
|
39
|
+
- 订单商品循环: [#list order.products as p] ... \${p.title} \${p.quantity} \${p.price} ... [/#list]
|
|
40
|
+
- 商品数量: \${order.products?size}
|
|
41
|
+
- 空值兜底(强烈建议): \${order.address1?default('')} —— 取不到值时给默认,避免渲染失败
|
|
42
|
+
- 花括号规范: 只用单花括号,禁止 {{ }} 混用(引擎只认单花括号,混用会导致发送失败)`;
|
|
43
|
+
|
|
44
|
+
async function callApi(path, body) {
|
|
45
|
+
if (!TOKEN) {
|
|
46
|
+
throw new Error("MAMBA_ACCESS_TOKEN env var is not set");
|
|
47
|
+
}
|
|
48
|
+
const resp = await fetch(`${API_BASE}${path}`, {
|
|
49
|
+
method: "POST",
|
|
50
|
+
headers: {
|
|
51
|
+
"Content-Type": "application/json",
|
|
52
|
+
"X-Mamba-Access-Token": TOKEN,
|
|
53
|
+
"X-Mamba-Source": "mcp",
|
|
54
|
+
"X-Mamba-Client-Version": CLIENT_VERSION,
|
|
55
|
+
},
|
|
56
|
+
body: JSON.stringify(body ?? {}),
|
|
57
|
+
});
|
|
58
|
+
const text = await resp.text();
|
|
59
|
+
let json = null;
|
|
60
|
+
try {
|
|
61
|
+
json = JSON.parse(text);
|
|
62
|
+
} catch {
|
|
63
|
+
json = null;
|
|
64
|
+
}
|
|
65
|
+
if (!resp.ok || (json && json.success === false)) {
|
|
66
|
+
const msg = json ? (json.message || JSON.stringify(json)) : text.slice(0, 300);
|
|
67
|
+
if (resp.status === 429) {
|
|
68
|
+
throw new Error(
|
|
69
|
+
`API 429 ${path}: 触发限流。店铺级上限 10 请求/秒;另外 campaign_list 与开放API的 campaign/report、` +
|
|
70
|
+
`getCampaignLink 是 10 秒 1 次(同店铺)。请退避后重试(活动列表建议隔 ≥10 秒再查,或改用 campaign_report / ` +
|
|
71
|
+
`campaign_link_report 查明细)。原始返回: ${msg}`
|
|
72
|
+
);
|
|
73
|
+
}
|
|
74
|
+
throw new Error(`API ${resp.status} ${path}: ${msg}`);
|
|
75
|
+
}
|
|
76
|
+
return json ? (json.data !== undefined ? json.data : json) : text;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
|
|
80
|
+
// ==================== 工具注册中心(供通用发现/调度工具使用) ====================
|
|
81
|
+
// 参考 Omnisend MCP 的紧凑工具设计:除逐个命名工具外,再暴露 mamba_search / mamba_tool_schema / mamba_execute,
|
|
82
|
+
// 让 AI 能在不了解全部工具名时先发现、再执行;工具变多时上下文占用不随数量膨胀。
|
|
83
|
+
const READONLY_MODE = /^(1|true|yes)$/i.test(process.env.MAMBA_READONLY || "");
|
|
84
|
+
|
|
85
|
+
/** 有副作用/会改数据的工具(默认全部只读,新增写工具必须登记在这里) */
|
|
86
|
+
const WRITE_TOOLS = new Set([
|
|
87
|
+
"template_create",
|
|
88
|
+
"template_update",
|
|
89
|
+
"template_delete",
|
|
90
|
+
"template_create_from_design",
|
|
91
|
+
"template_send_test",
|
|
92
|
+
"template_export",
|
|
93
|
+
"template_import",
|
|
94
|
+
"upload_image",
|
|
95
|
+
"campaign_create_email",
|
|
96
|
+
"campaign_create_sms",
|
|
97
|
+
"campaign_create_abtest",
|
|
98
|
+
"campaign_create_sms_abtest",
|
|
99
|
+
"campaign_update_content",
|
|
100
|
+
"flow_create",
|
|
101
|
+
"flow_update_content",
|
|
102
|
+
"user_group_create",
|
|
103
|
+
]);
|
|
104
|
+
|
|
105
|
+
const TOOL_REGISTRY = new Map();
|
|
106
|
+
|
|
107
|
+
// 建组前必须查分组条件目录(与平台 AI 助手同一道闸门):字段/nestedPath 只能来自真源目录,
|
|
108
|
+
// 凭印象写字段必被后端校验打回,来回重试比多查一次更贵。30 分钟内查过一次即免查。
|
|
109
|
+
const GROUP_CATALOG_TTL_MS = 30 * 60 * 1000;
|
|
110
|
+
let groupCatalogQueriedAt = 0;
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
* 注册一个工具,同时登记元数据(供 mamba_search / mamba_execute 调度)
|
|
114
|
+
* MAMBA_READONLY=1 时只注册只读工具(等价 Omnisend 的 read-only 授权预设)
|
|
115
|
+
*/
|
|
116
|
+
function defineTool(name, description, shape, handler) {
|
|
117
|
+
const readOnly = !WRITE_TOOLS.has(name);
|
|
118
|
+
TOOL_REGISTRY.set(name, { name, description, shape, handler, readOnly });
|
|
119
|
+
if (READONLY_MODE && !readOnly) {
|
|
120
|
+
return; // 只读模式:跳过写工具注册
|
|
121
|
+
}
|
|
122
|
+
server.tool(name, description, shape, handler);
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
const server = new McpServer(
|
|
126
|
+
{ name: "mambasms-mcp-server", version: "0.6.19" },
|
|
127
|
+
{
|
|
128
|
+
instructions: `MambaSMS 营销自动化(店铺级,鉴权用 MAMBA_ACCESS_TOKEN 店铺密钥)。共 52 个工具,分四组。
|
|
129
|
+
不确定用什么/想先看总览 → mamba_guide;只知道关键词 → mamba_search;要参数细节 → mamba_tool_schema。
|
|
130
|
+
|
|
131
|
+
【模板(17)】四条创建路径:A) template_create_from_design —— 首选,传结构化 emailDesign(style/theme/hero/intro/products/offer/cta/social),写前先 catalog_query(topic=emailDesign);要骨架传 catalog_query(topic=layouts);套已有模板传 templateId。B) template_create(html) —— 迁移现成 HTML/精细控制。C) template_create(sections) —— 动态区块(购物车/浏览列表),发送时填该联系人的数据。D) template_import —— mambatpl 或 zipHtml。参考面:template_list_system/template_get_system、block_library_query、catalog_query、template_snippets(页头页脚片段,社交图标走 link.mambasms.com/dynamicImage/{platform}/{size}/{shape}/{color},key 大小写敏感)。效果:template_preview(免登永久链接)、template_send_test。图片用 upload_image 转存 CDN。退订必须 {Unsubscribe}。
|
|
132
|
+
【先确认再动手】第一次给客户做邮件(模板或活动)时,先问一次要哪种形式(除非客户已说清):A. 用平台模板(推荐,页脚自带社媒图标与退订、风格统一);B. 从零生成组件(内容由你生成,客户之后可在后台拖拽微调);C. 源码模式HTML(由你直接产出整份 HTML 源码,后台可继续改源码;客户贴现成 HTML 时也走这个)。注意:内容是**你(AI)产出**,不要向客户索要 HTML;只需让客户选形式。客户说"随便/你决定"→选 A。社媒默认用平台页脚图标(推荐),客户有指定账号/图标再按其要求。【社媒与页脚】社媒入口**不要自己发明**:优先套系统模板(template_list_system→template_get_system→传 templateId),模板自带页脚社媒图标+退订;手写 HTML 或骨架时,社媒必须用 template_snippets(type=footer-social) 的片段(平台图标直链),放在**最底部(退订上方)**;禁止用文字按钮充当社媒入口,也不要在正文中部放社媒区。emailDesign 的 social 字段可省略(页脚社媒由模板/骨架生成)。
|
|
133
|
+
【分页有边界】列表类接口一律带 page/page_size:默认 page_size=20,建议≤50,上限100(模板列表≤500但建议50);禁止"循环翻页拉全量"(通常只取第一页/前50条够用),先用 keyword/status/时间窗/条件收窄再取;看汇总用报表接口(order_stats/campaign_report/flow_report),不要拉明细自己算;分组成员抽样看 20~50 人即可,不要枚举全组。
|
|
134
|
+
【只读/质检(23)】活动 campaign_*、流程 flow_*、分组 group_*、联系人 contact_get/contact_send_status、报表 audience_growth/form_performance/domain_health/store_overview/order_stats/account_balance、质检 email_audit、品牌 brand_profile。列表统一 page/page_size(≤100,默认 20);报表默认近 180 天,可传 start_date/end_date 覆盖;订单已排除 CANCELLED;余额单位为美元且是 group 级(同集团多店共享);联系人只支持精确查。
|
|
135
|
+
【写=只建草稿(8)】campaign_create_email/sms/abtest/smsabtest、campaign_update_content、flow_create、flow_update_content、user_group_create。一律 draft:不发送、不启用、不扣费;update 仅 draft 可改(否则 412);建分组前必须先 group_catalog 取真源(否则拒绝);活动指定人群用 communication_select=tag + tag_id。正文质量闸门:活动渲染后 ≥4200 字符、流程邮件节点 ≥3000 字符、须含 {Unsubscribe}、买家可见文案英文。
|
|
136
|
+
【通用(4)】mamba_guide 总览、mamba_search 搜索、mamba_tool_schema 查参数、mamba_execute 按名执行。MAMBA_READONLY=1 时只注册只读工具(52→35)。
|
|
137
|
+
明确不做:发送/启用/中断发送、删除活动、联系人模糊全表搜、拖拽式精修(留给后台)。
|
|
138
|
+
${PLACEHOLDER_GUIDE}`,
|
|
139
|
+
}
|
|
140
|
+
);
|
|
141
|
+
|
|
142
|
+
defineTool(
|
|
143
|
+
"template_list",
|
|
144
|
+
"列出当前店铺的全部邮件模板(id+标题),用于查询已有模板/确认创建结果",
|
|
145
|
+
{
|
|
146
|
+
page_number: z.number().int().min(1).default(1).describe("页码,从1开始"),
|
|
147
|
+
page_size: z.number().int().min(1).max(500).default(20).describe("每页条数(默认20,建议≤50;最大500但不要一次拉全量)"),
|
|
148
|
+
},
|
|
149
|
+
async ({ page_number, page_size }) => {
|
|
150
|
+
const data = await callApi("/api/v1/template/list", {
|
|
151
|
+
page: 1,
|
|
152
|
+
pager: { pageNumber: page_number, pageSize: page_size },
|
|
153
|
+
});
|
|
154
|
+
return { content: [{ type: "text", text: JSON.stringify(data, null, 2) }] };
|
|
155
|
+
}
|
|
156
|
+
);
|
|
157
|
+
|
|
158
|
+
defineTool(
|
|
159
|
+
"template_get",
|
|
160
|
+
"获取模板详情。注意:返回的是变量已替换成示例数据的渲染预览HTML(适合确认视觉效果),原始模板中的占位符会被替换,不要把预览当原模板再改回去",
|
|
161
|
+
{ id: z.string().min(1).describe("模板id(template_list 返回的 id)") },
|
|
162
|
+
async ({ id }) => {
|
|
163
|
+
const data = await callApi("/api/v1/template/details", [id]);
|
|
164
|
+
return { content: [{ type: "text", text: JSON.stringify(data, null, 2) }] };
|
|
165
|
+
}
|
|
166
|
+
);
|
|
167
|
+
|
|
168
|
+
defineTool(
|
|
169
|
+
"template_create",
|
|
170
|
+
`创建邮件模板:两种方式二选一——
|
|
171
|
+
1) html:传纯HTML(适合从零生成/迁移现有邮件);
|
|
172
|
+
2) sections:语义化区块列表(推荐,支持购物车列表/浏览商品推荐等动态区块):
|
|
173
|
+
[{type:"html",html:"..."}, {type:"cart_list",buttonText:"Back to Cart",limit:3},
|
|
174
|
+
{type:"browse_list",buttonText:"TAKE ANOTHER LOOK",limit:2}, ...]
|
|
175
|
+
动态区块在发送时自动填充该联系人的购物车/浏览商品数据。
|
|
176
|
+
html/sections 都要求页脚含 {Unsubscribe} 退订占位符。创建后出现在后台「我的模板」,可被营销活动/自动化流程引用,也可在后台可视化微调。
|
|
177
|
+
${PLACEHOLDER_GUIDE}`,
|
|
178
|
+
{
|
|
179
|
+
title: z.string().min(1).max(100).describe("模板标题"),
|
|
180
|
+
html: z
|
|
181
|
+
.string()
|
|
182
|
+
.max(500000)
|
|
183
|
+
.optional()
|
|
184
|
+
.describe("完整邮件HTML(与 sections 二选一)"),
|
|
185
|
+
sections: z
|
|
186
|
+
.array(
|
|
187
|
+
z.object({
|
|
188
|
+
type: z.enum(["html", "cart_list", "browse_list"]).describe("区块类型"),
|
|
189
|
+
html: z.string().optional().describe("type=html 时的HTML内容"),
|
|
190
|
+
buttonText: z.string().optional().describe("动态区块按钮文案"),
|
|
191
|
+
buttonStyle: z.enum(["primaryButton", "secondaryButton"]).optional().describe("按钮样式"),
|
|
192
|
+
limit: z.number().int().min(1).max(10).optional().describe("动态区块展示商品数(1~10)"),
|
|
193
|
+
})
|
|
194
|
+
)
|
|
195
|
+
.max(20)
|
|
196
|
+
.optional()
|
|
197
|
+
.describe("语义化区块列表(与 html 二选一)"),
|
|
198
|
+
summary: z.string().optional().describe("模板备注/摘要,可选"),
|
|
199
|
+
},
|
|
200
|
+
async ({ title, html, sections, summary }) => {
|
|
201
|
+
const body = { title };
|
|
202
|
+
if (html !== undefined) body.html = html;
|
|
203
|
+
if (sections !== undefined) body.sections = sections;
|
|
204
|
+
if (summary !== undefined) body.summary = summary;
|
|
205
|
+
const data = await callApi("/api/v1/template/create", body);
|
|
206
|
+
return { content: [{ type: "text", text: JSON.stringify(data, null, 2) }] };
|
|
207
|
+
}
|
|
208
|
+
);
|
|
209
|
+
|
|
210
|
+
defineTool(
|
|
211
|
+
"template_update",
|
|
212
|
+
`更新已有模板(id 必填;title/html/sections/summary 至少传一个)。html/sections 不传则保留原内容(可只改标题/摘要)。
|
|
213
|
+
${PLACEHOLDER_GUIDE}`,
|
|
214
|
+
{
|
|
215
|
+
id: z.string().min(1).describe("模板id"),
|
|
216
|
+
title: z.string().min(1).max(100).optional().describe("新标题,可选"),
|
|
217
|
+
html: z.string().min(1).max(500000).optional().describe("新HTML,可选;不传则保留原内容"),
|
|
218
|
+
sections: z
|
|
219
|
+
.array(
|
|
220
|
+
z.object({
|
|
221
|
+
type: z.enum(["html", "cart_list", "browse_list"]).describe("区块类型"),
|
|
222
|
+
html: z.string().optional().describe("type=html 时的HTML内容"),
|
|
223
|
+
buttonText: z.string().optional().describe("动态区块按钮文案"),
|
|
224
|
+
buttonStyle: z.enum(["primaryButton", "secondaryButton"]).optional().describe("按钮样式"),
|
|
225
|
+
limit: z.number().int().min(1).max(10).optional().describe("动态区块展示商品数(1~10)"),
|
|
226
|
+
})
|
|
227
|
+
)
|
|
228
|
+
.optional()
|
|
229
|
+
.describe("语义化区块列表(整体替换原内容)"),
|
|
230
|
+
summary: z.string().optional().describe("新摘要,可选"),
|
|
231
|
+
},
|
|
232
|
+
async ({ id, title, html, sections, summary }) => {
|
|
233
|
+
const body = { id };
|
|
234
|
+
if (title !== undefined) body.title = title;
|
|
235
|
+
if (html !== undefined) body.html = html;
|
|
236
|
+
if (sections !== undefined) body.sections = sections;
|
|
237
|
+
if (summary !== undefined) body.summary = summary;
|
|
238
|
+
const data = await callApi("/api/v1/template/create", body);
|
|
239
|
+
return { content: [{ type: "text", text: JSON.stringify(data, null, 2) }] };
|
|
240
|
+
}
|
|
241
|
+
);
|
|
242
|
+
|
|
243
|
+
defineTool(
|
|
244
|
+
"upload_image",
|
|
245
|
+
"上传图片到平台CDN,返回可直接用于模板HTML的图片URL。" +
|
|
246
|
+
"两种方式:①url=公网图片直链(平台代下载转存);②file_path=本机图片绝对路径(直接上传,适合图床防盗链 403/429 或需要登录才能访问的图)。" +
|
|
247
|
+
"若 url 方式报 403/429(源站拒绝抓取),请下载到本机后改用 file_path。",
|
|
248
|
+
{
|
|
249
|
+
url: z.string().optional().describe("图片URL(http/https,jpg/jpeg/png/gif/webp,≤5MB)"),
|
|
250
|
+
file_path: z.string().optional().describe("本机图片绝对路径(如 /Users/me/banner.png 或 C:\\\\img\\\\a.jpg),≤5MB"),
|
|
251
|
+
},
|
|
252
|
+
async ({ url, file_path }) => {
|
|
253
|
+
if (!url && !file_path) {
|
|
254
|
+
throw new Error("需要 url(公网直链)或 file_path(本机绝对路径)其中一个");
|
|
255
|
+
}
|
|
256
|
+
if (url && file_path) {
|
|
257
|
+
throw new Error("url 与 file_path 只能二选一");
|
|
258
|
+
}
|
|
259
|
+
if (file_path) {
|
|
260
|
+
const { readFile, stat } = await import("node:fs/promises");
|
|
261
|
+
const { basename } = await import("node:path");
|
|
262
|
+
let info;
|
|
263
|
+
try {
|
|
264
|
+
info = await stat(file_path);
|
|
265
|
+
} catch (e) {
|
|
266
|
+
throw new Error(`读取本地文件失败: ${file_path} (${e.code || e.message})`);
|
|
267
|
+
}
|
|
268
|
+
if (!info.isFile()) throw new Error(`不是文件: ${file_path}`);
|
|
269
|
+
const MAX = 5 * 1024 * 1024;
|
|
270
|
+
if (info.size > MAX) throw new Error(`文件过大(${info.size} 字节),上限 5MB`);
|
|
271
|
+
const buf = await readFile(file_path);
|
|
272
|
+
const form = new FormData();
|
|
273
|
+
form.append("file", new Blob([buf]), basename(file_path));
|
|
274
|
+
const resp = await fetch(`${API_BASE}/api/v1/file/upload`, {
|
|
275
|
+
method: "POST",
|
|
276
|
+
headers: {
|
|
277
|
+
"X-Mamba-Access-Token": TOKEN,
|
|
278
|
+
"X-Mamba-Source": "mcp",
|
|
279
|
+
"X-Mamba-Client-Version": CLIENT_VERSION,
|
|
280
|
+
},
|
|
281
|
+
body: form,
|
|
282
|
+
});
|
|
283
|
+
const text = await resp.text();
|
|
284
|
+
let json = null;
|
|
285
|
+
try {
|
|
286
|
+
json = JSON.parse(text);
|
|
287
|
+
} catch {
|
|
288
|
+
json = null;
|
|
289
|
+
}
|
|
290
|
+
if (!resp.ok || (json && json.success === false)) {
|
|
291
|
+
const msg = json ? json.message || JSON.stringify(json) : text.slice(0, 300);
|
|
292
|
+
throw new Error(`API ${resp.status} /api/v1/file/upload: ${msg}`);
|
|
293
|
+
}
|
|
294
|
+
const data = json && json.data !== undefined ? json.data : json;
|
|
295
|
+
return { content: [{ type: "text", text: JSON.stringify(data, null, 2) }] };
|
|
296
|
+
}
|
|
297
|
+
const data = await callApi("/api/v1/file/upload-from-url", { url });
|
|
298
|
+
return { content: [{ type: "text", text: JSON.stringify(data, null, 2) }] };
|
|
299
|
+
}
|
|
300
|
+
);
|
|
301
|
+
|
|
302
|
+
defineTool(
|
|
303
|
+
"template_delete",
|
|
304
|
+
"删除模板。⚠️ 被营销活动/自动化流程引用中的模板无法删除(会返回引用数量)。删除前建议先和用户确认。",
|
|
305
|
+
{
|
|
306
|
+
id: z.string().min(1).describe("模板id"),
|
|
307
|
+
},
|
|
308
|
+
async ({ id }) => {
|
|
309
|
+
const data = await callApi("/api/v1/template/delete", { id });
|
|
310
|
+
return { content: [{ type: "text", text: JSON.stringify(data, null, 2) }] };
|
|
311
|
+
}
|
|
312
|
+
);
|
|
313
|
+
|
|
314
|
+
defineTool(
|
|
315
|
+
"template_list_system",
|
|
316
|
+
"列出系统模板库(平台公共模板,campaigns/automation)。返回标题、摘要、分类、contentSummary(板块+核心文案)。用于让 AI 参考结构与风格,或在 template_create_from_design 里以 templateId 直接套用。",
|
|
317
|
+
{
|
|
318
|
+
template_type: z.enum(["campaigns", "automation"]).optional().describe("campaigns=营销活动模板,automation=自动化流程模板"),
|
|
319
|
+
category_id: z.string().optional().describe("分类id(先调 template_system_categories 获取)"),
|
|
320
|
+
keyword: z.string().optional().describe("标题模糊筛选"),
|
|
321
|
+
trigger_key: z
|
|
322
|
+
.string()
|
|
323
|
+
.optional()
|
|
324
|
+
.describe("自动化场景:join_cart/abandon=弃单, order_paid=订单确认, order_fulfilled=发货, subscribe/customer_created=订阅欢迎, viewed_product=浏览商品"),
|
|
325
|
+
limit: z.number().int().min(1).max(30).default(15).describe("返回条数,最大30"),
|
|
326
|
+
},
|
|
327
|
+
async ({ template_type, category_id, keyword, trigger_key, limit }) => {
|
|
328
|
+
const body = { limit };
|
|
329
|
+
if (template_type) body.templateType = template_type;
|
|
330
|
+
if (category_id) body.categoryId = category_id;
|
|
331
|
+
if (keyword) body.keyword = keyword;
|
|
332
|
+
if (trigger_key) body.triggerKey = trigger_key;
|
|
333
|
+
const data = await callApi("/api/v1/template/system/list", body);
|
|
334
|
+
return { content: [{ type: "text", text: JSON.stringify(data, null, 2) }] };
|
|
335
|
+
}
|
|
336
|
+
);
|
|
337
|
+
|
|
338
|
+
defineTool(
|
|
339
|
+
"template_get_system",
|
|
340
|
+
"读系统模板详情:板块摘要 + textNodes(可定位的文案节点)+ 渲染 HTML。用于学习结构,或配合 template_create_from_design 的 templateId/textOverrides 精确改写文案。",
|
|
341
|
+
{
|
|
342
|
+
id: z.string().min(1).describe("系统模板id(template_list_system 返回)"),
|
|
343
|
+
},
|
|
344
|
+
async ({ id }) => {
|
|
345
|
+
const data = await callApi("/api/v1/template/system/get", { id });
|
|
346
|
+
return { content: [{ type: "text", text: JSON.stringify(data, null, 2) }] };
|
|
347
|
+
}
|
|
348
|
+
);
|
|
349
|
+
|
|
350
|
+
defineTool(
|
|
351
|
+
"template_system_categories",
|
|
352
|
+
"列出系统模板分类(campaigns 或 automation),用于 template_list_system 的 category_id 筛选。",
|
|
353
|
+
{
|
|
354
|
+
template_type: z.enum(["campaigns", "automation"]).default("campaigns").describe("模板类型"),
|
|
355
|
+
},
|
|
356
|
+
async ({ template_type }) => {
|
|
357
|
+
const data = await callApi("/api/v1/template/system/categories", { templateType: template_type });
|
|
358
|
+
return { content: [{ type: "text", text: JSON.stringify(data, null, 2) }] };
|
|
359
|
+
}
|
|
360
|
+
);
|
|
361
|
+
|
|
362
|
+
defineTool(
|
|
363
|
+
"catalog_query",
|
|
364
|
+
"查创建规范目录(唯一真源): emailDesign(内容结构全文)/styles(4风格+行业配色)/layouts(6种骨架)/components(倒计时等额外组件)/discount/variants(多版本)/templateOverlay(套模板步骤)/handwriteHtml。不传 topic 返回目录清单。写 emailDesign 前必查。",
|
|
365
|
+
{
|
|
366
|
+
topic: z
|
|
367
|
+
.enum(["emailDesign", "styles", "layouts", "components", "discount", "variants", "templateOverlay", "handwriteHtml"])
|
|
368
|
+
.optional()
|
|
369
|
+
.describe("规范主题,不传返回清单"),
|
|
370
|
+
},
|
|
371
|
+
async ({ topic }) => {
|
|
372
|
+
const data = await callApi("/api/v1/template/catalog", topic ? { topic } : {});
|
|
373
|
+
return { content: [{ type: "text", text: JSON.stringify(data, null, 2) }] };
|
|
374
|
+
}
|
|
375
|
+
);
|
|
376
|
+
|
|
377
|
+
defineTool(
|
|
378
|
+
"block_library_query",
|
|
379
|
+
"查编辑器预置区块库:不传 category_id 返回分类清单;传了返回区块明细(blockId + textNodes)。区块可作为 template_create_from_design 的 blocks 参数插入。",
|
|
380
|
+
{
|
|
381
|
+
category_id: z.string().optional().describe("区块分类id(不传返回分类清单)"),
|
|
382
|
+
keyword: z.string().optional().describe("按标题筛选"),
|
|
383
|
+
limit: z.number().int().min(1).max(40).default(20).describe("条数,最大40"),
|
|
384
|
+
offset: z.number().int().min(0).default(0).describe("翻页偏移"),
|
|
385
|
+
},
|
|
386
|
+
async ({ category_id, keyword, limit, offset }) => {
|
|
387
|
+
const body = { limit, offset };
|
|
388
|
+
if (category_id) body.categoryId = category_id;
|
|
389
|
+
if (keyword) body.keyword = keyword;
|
|
390
|
+
const data = await callApi("/api/v1/template/block-library", body);
|
|
391
|
+
return { content: [{ type: "text", text: JSON.stringify(data, null, 2) }] };
|
|
392
|
+
}
|
|
393
|
+
);
|
|
394
|
+
|
|
395
|
+
defineTool(
|
|
396
|
+
"template_create_from_design",
|
|
397
|
+
`用结构化 emailDesign 生成模板(首选路径)。字段规范先查 catalog_query(topic=emailDesign)。
|
|
398
|
+
emailDesign 示例:{"style":"bold","theme":{"primaryColor":"#0f3460","accentColor":"#00adb5"},"announcement":"BLACK FRIDAY · 48H","preheader":"Up to 40% off","hero":{"title":"BLACK FRIDAY SALE","subtitle":"UP TO 40% OFF"},"intro":"...","products":[{"name":"Earbuds Pro","price":"$59.00","originalPrice":"$99.00"}],"offer":{"headline":"40% OFF","code":"{DiscountCode}"},"cta":{"text":"SHOP NOW","link":"{SiteUrl}"},"social":["instagram","facebook"]}
|
|
399
|
+
买家可见文案一律英文;商品位会尽量匹配店铺真实商品图。`,
|
|
400
|
+
{
|
|
401
|
+
title: z.string().min(1).max(100).describe("模板标题"),
|
|
402
|
+
email_design: z.string().min(1).describe("emailDesign JSON 字符串(数组可传1-3版,首版落库)"),
|
|
403
|
+
template_id: z.string().optional().describe("套用的系统模板 id(优先于 layout_skeleton)"),
|
|
404
|
+
layout_skeleton: z
|
|
405
|
+
.enum(["promo-hero", "promo-flash", "single-spotlight", "new-arrival", "newsletter", "win-back"])
|
|
406
|
+
.optional()
|
|
407
|
+
.describe("版式骨架(不套模板时用;与 text_overrides/component_overrides 互斥)"),
|
|
408
|
+
category_id: z.string().optional().describe("自动匹配模板时的行业分类"),
|
|
409
|
+
scene_keyword: z.string().optional().describe("自动匹配模板时的场景关键词(如 弃单/新品/圣诞)"),
|
|
410
|
+
text_overrides: z
|
|
411
|
+
.array(z.object({ node_id: z.string(), text: z.string() }))
|
|
412
|
+
.optional()
|
|
413
|
+
.describe("按节点改文案(套模板时用,template_get_system 的 textNodes 提供 node_id)"),
|
|
414
|
+
component_overrides: z
|
|
415
|
+
.array(z.object({ node_id: z.string(), settings: z.record(z.any()) }))
|
|
416
|
+
.optional()
|
|
417
|
+
.describe("按节点覆盖字段(链接/图片/颜色等)"),
|
|
418
|
+
components: z
|
|
419
|
+
.array(z.record(z.any()))
|
|
420
|
+
.max(4)
|
|
421
|
+
.optional()
|
|
422
|
+
.describe("额外组件(如 [{type:'countdown',endTime:'2026-12-01 23:59:59'}],规范查 catalog_query(topic=components))"),
|
|
423
|
+
blocks: z
|
|
424
|
+
.array(z.record(z.any()))
|
|
425
|
+
.max(4)
|
|
426
|
+
.optional()
|
|
427
|
+
.describe("预置区块([{blockId, textOverrides:[{nodeId,text}]}],来自 block_library_query)"),
|
|
428
|
+
summary: z.string().optional().describe("模板备注"),
|
|
429
|
+
},
|
|
430
|
+
async (a) => {
|
|
431
|
+
const body = { title: a.title, emailDesign: a.email_design };
|
|
432
|
+
for (const [k, v] of [
|
|
433
|
+
["templateId", a.template_id],
|
|
434
|
+
["layoutSkeleton", a.layout_skeleton],
|
|
435
|
+
["categoryId", a.category_id],
|
|
436
|
+
["sceneKeyword", a.scene_keyword],
|
|
437
|
+
["textOverrides", a.text_overrides?.map((x) => ({ nodeId: x.node_id, text: x.text }))],
|
|
438
|
+
["componentOverrides", a.component_overrides?.map((x) => ({ nodeId: x.node_id, settings: x.settings }))],
|
|
439
|
+
["components", a.components],
|
|
440
|
+
["blocks", a.blocks],
|
|
441
|
+
["summary", a.summary],
|
|
442
|
+
]) {
|
|
443
|
+
if (v !== undefined) body[k] = v;
|
|
444
|
+
}
|
|
445
|
+
const data = await callApi("/api/v1/template/create-from-design", body);
|
|
446
|
+
return { content: [{ type: "text", text: JSON.stringify(data, null, 2) }] };
|
|
447
|
+
}
|
|
448
|
+
);
|
|
449
|
+
|
|
450
|
+
defineTool(
|
|
451
|
+
"template_preview",
|
|
452
|
+
"预览模板:返回渲染好的 HTML(可视化确认)和免登永久预览链接(可直接发给同事/客户在浏览器打开)。",
|
|
453
|
+
{
|
|
454
|
+
id: z.string().min(1).describe("模板id"),
|
|
455
|
+
},
|
|
456
|
+
async ({ id }) => {
|
|
457
|
+
const data = await callApi("/api/v1/template/preview", { id });
|
|
458
|
+
const text = `预览链接(免登):${API_BASE}${data.url}\n\n渲染HTML(${(data.html || "").length} 字符):\n${(data.html || "").slice(0, 4000)}${(data.html || "").length > 4000 ? "\n...(已截断,完整内容请打开预览链接)" : ""}`;
|
|
459
|
+
return { content: [{ type: "text", text }] };
|
|
460
|
+
}
|
|
461
|
+
);
|
|
462
|
+
|
|
463
|
+
defineTool(
|
|
464
|
+
"template_send_test",
|
|
465
|
+
"发送测试邮件(主题前缀 MambaSend Preview,1~10 个收件人,同店铺 2 分钟内只能发一次)。用于看真实收件箱效果。",
|
|
466
|
+
{
|
|
467
|
+
id: z.string().min(1).describe("模板id"),
|
|
468
|
+
emails: z.array(z.string()).min(1).max(10).describe("收件邮箱,最多10个"),
|
|
469
|
+
},
|
|
470
|
+
async ({ id, emails }) => {
|
|
471
|
+
const data = await callApi("/api/v1/template/send-test", { id, emails });
|
|
472
|
+
return { content: [{ type: "text", text: JSON.stringify(data, null, 2) }] };
|
|
473
|
+
}
|
|
474
|
+
);
|
|
475
|
+
|
|
476
|
+
defineTool(
|
|
477
|
+
"template_export",
|
|
478
|
+
"导出模板为 .mambatpl 平台模板文件(加密),返回 CDN 下载地址。可用于备份或跨店铺迁移。",
|
|
479
|
+
{
|
|
480
|
+
id: z.string().min(1).describe("模板id"),
|
|
481
|
+
},
|
|
482
|
+
async ({ id }) => {
|
|
483
|
+
const data = await callApi("/api/v1/template/export", { id });
|
|
484
|
+
return { content: [{ type: "text", text: JSON.stringify(data, null, 2) }] };
|
|
485
|
+
}
|
|
486
|
+
);
|
|
487
|
+
|
|
488
|
+
defineTool(
|
|
489
|
+
"template_import",
|
|
490
|
+
"从 URL 导入模板。source_type=mambatpl:平台导出的加密模板文件;source_type=zipHtml:外部设计工具导出的 zip(后端解包并把图片转存 CDN)。",
|
|
491
|
+
{
|
|
492
|
+
url: z.string().url().describe("文件下载地址(http/https)"),
|
|
493
|
+
source_type: z.enum(["mambatpl", "zipHtml"]).default("mambatpl").describe("文件类型"),
|
|
494
|
+
title: z.string().optional().describe("新模板标题(不传则用文件内的标题)"),
|
|
495
|
+
},
|
|
496
|
+
async ({ url, source_type, title }) => {
|
|
497
|
+
const body = { url, sourceType: source_type };
|
|
498
|
+
if (title !== undefined) body.title = title;
|
|
499
|
+
const data = await callApi("/api/v1/template/import", body);
|
|
500
|
+
return { content: [{ type: "text", text: JSON.stringify(data, null, 2) }] };
|
|
501
|
+
}
|
|
502
|
+
);
|
|
503
|
+
|
|
504
|
+
defineTool(
|
|
505
|
+
"template_snippets",
|
|
506
|
+
"取页头/页脚 HTML 参考片段(可直接嵌进模板 HTML,已是邮件客户端兼容的内联样式表格)。页脚含 LOGO/链接列/联系方式/退订;footer-social 含社交图标(平台动态图服务)——**社媒入口的唯一推荐来源**,放邮件最底部退订上方,不要用文字按钮冒充社媒",
|
|
507
|
+
{
|
|
508
|
+
type: z.enum(["header-basic", "footer-basic", "footer-social"]).describe("片段类型"),
|
|
509
|
+
},
|
|
510
|
+
async ({ type }) => {
|
|
511
|
+
const { readFile } = await import("node:fs/promises");
|
|
512
|
+
const { fileURLToPath } = await import("node:url");
|
|
513
|
+
const { dirname, join } = await import("node:path");
|
|
514
|
+
const here = dirname(fileURLToPath(import.meta.url));
|
|
515
|
+
const html = await readFile(join(here, "snippets", `${type}.html`), "utf8");
|
|
516
|
+
return { content: [{ type: "text", text: html }] };
|
|
517
|
+
}
|
|
518
|
+
);
|
|
519
|
+
|
|
520
|
+
// ==================== 2A:只读洞察(分页 + 180 天默认窗口) ====================
|
|
521
|
+
|
|
522
|
+
async function callAssistantTool(tool, args) {
|
|
523
|
+
return callApi("/api/v1/assistant/tool", { tool, arguments: args ?? {} });
|
|
524
|
+
}
|
|
525
|
+
|
|
526
|
+
const PAGE_PARAMS = {
|
|
527
|
+
page: z.number().int().min(1).default(1).describe("页码,从1开始"),
|
|
528
|
+
page_size: z.number().int().min(1).max(100).default(20).describe("每页条数(默认20,建议≤50;最大100,越大越占服务端资源)"),
|
|
529
|
+
};
|
|
530
|
+
|
|
531
|
+
const WINDOW_PARAMS = {
|
|
532
|
+
start_date: z.string().optional().describe("开始日期 yyyy-MM-dd(UTC),默认最近180天"),
|
|
533
|
+
end_date: z.string().optional().describe("结束日期 yyyy-MM-dd(UTC),默认今天"),
|
|
534
|
+
};
|
|
535
|
+
|
|
536
|
+
defineTool(
|
|
537
|
+
"campaign_list",
|
|
538
|
+
"营销活动列表(分页):标题/类型/状态/发送数与打开点击退订投诉计数。默认最近180天,可传时间覆盖。",
|
|
539
|
+
{
|
|
540
|
+
...PAGE_PARAMS,
|
|
541
|
+
start_time: z.string().optional().describe("开始时间 yyyy-MM-dd HH:mm:ss"),
|
|
542
|
+
end_time: z.string().optional().describe("结束时间 yyyy-MM-dd HH:mm:ss"),
|
|
543
|
+
},
|
|
544
|
+
async ({ page, page_size, start_time, end_time }) => {
|
|
545
|
+
const body = { page, pageSize: page_size };
|
|
546
|
+
if (start_time) body.startTime = start_time;
|
|
547
|
+
if (end_time) body.endTime = end_time;
|
|
548
|
+
const data = await callApi("/api/v1/campaign/list", body);
|
|
549
|
+
return { content: [{ type: "text", text: JSON.stringify(data, null, 2) }] };
|
|
550
|
+
}
|
|
551
|
+
);
|
|
552
|
+
|
|
553
|
+
defineTool(
|
|
554
|
+
"campaign_report",
|
|
555
|
+
"单活动效果报告:发送/成功/打开/点击/退订/投诉与送达率、打开率、点击率(附口径说明)。不传 id 时可用 campaign_name。",
|
|
556
|
+
{
|
|
557
|
+
campaigns_id: z.string().optional().describe("活动id(优先)"),
|
|
558
|
+
campaign_name: z.string().optional().describe("活动标题(模糊匹配,取最新)"),
|
|
559
|
+
},
|
|
560
|
+
async ({ campaigns_id, campaign_name }) => {
|
|
561
|
+
const args = {};
|
|
562
|
+
if (campaigns_id) args.campaignsId = campaigns_id;
|
|
563
|
+
if (campaign_name) args.campaignName = campaign_name;
|
|
564
|
+
const data = await callAssistantTool("query_campaign_report", args);
|
|
565
|
+
return { content: [{ type: "text", text: JSON.stringify(data, null, 2) }] };
|
|
566
|
+
}
|
|
567
|
+
);
|
|
568
|
+
|
|
569
|
+
defineTool(
|
|
570
|
+
"campaign_link_report",
|
|
571
|
+
"单活动链接级点击报告(分页,默认180天):每个链接的点击数/唯一用户数。",
|
|
572
|
+
{
|
|
573
|
+
campaigns_id: z.string().min(1).describe("活动id"),
|
|
574
|
+
...PAGE_PARAMS,
|
|
575
|
+
...WINDOW_PARAMS,
|
|
576
|
+
},
|
|
577
|
+
async ({ campaigns_id, page, page_size, start_date, end_date }) => {
|
|
578
|
+
const body = { campaignsId: campaigns_id, page, pageSize: page_size };
|
|
579
|
+
if (start_date) body.startDate = start_date;
|
|
580
|
+
if (end_date) body.endDate = end_date;
|
|
581
|
+
const data = await callApi("/api/v1/insight/campaign/link-report", body);
|
|
582
|
+
return { content: [{ type: "text", text: JSON.stringify(data, null, 2) }] };
|
|
583
|
+
}
|
|
584
|
+
);
|
|
585
|
+
|
|
586
|
+
defineTool(
|
|
587
|
+
"campaign_abtest_result",
|
|
588
|
+
"活动 A/B 测试结果:各版本标题/主题/状态/发送状态/目标分组/赢家。",
|
|
589
|
+
{
|
|
590
|
+
campaigns_id: z.string().optional().describe("活动id(优先)"),
|
|
591
|
+
campaign_name: z.string().optional().describe("活动标题"),
|
|
592
|
+
},
|
|
593
|
+
async ({ campaigns_id, campaign_name }) => {
|
|
594
|
+
const args = {};
|
|
595
|
+
if (campaigns_id) args.campaignsId = campaigns_id;
|
|
596
|
+
if (campaign_name) args.campaignName = campaign_name;
|
|
597
|
+
const data = await callAssistantTool("query_abtest_result", args);
|
|
598
|
+
return { content: [{ type: "text", text: JSON.stringify(data, null, 2) }] };
|
|
599
|
+
}
|
|
600
|
+
);
|
|
601
|
+
|
|
602
|
+
defineTool(
|
|
603
|
+
"campaign_content",
|
|
604
|
+
"读活动当前内容(主题/预览文本/发件人/受众设置/内容摘要/质量信号),用于审核或作为改写依据。",
|
|
605
|
+
{
|
|
606
|
+
campaigns_id: z.string().optional().describe("活动id(优先)"),
|
|
607
|
+
campaign_name: z.string().optional().describe("活动标题"),
|
|
608
|
+
include_full_content: z.boolean().default(false).describe("是否返回完整正文(默认只给摘要)"),
|
|
609
|
+
},
|
|
610
|
+
async ({ campaigns_id, campaign_name, include_full_content }) => {
|
|
611
|
+
const args = { includeFullContent: include_full_content };
|
|
612
|
+
if (campaigns_id) args.campaignsId = campaigns_id;
|
|
613
|
+
if (campaign_name) args.campaignName = campaign_name;
|
|
614
|
+
const data = await callAssistantTool("query_campaign_content", args);
|
|
615
|
+
return { content: [{ type: "text", text: JSON.stringify(data, null, 2) }] };
|
|
616
|
+
}
|
|
617
|
+
);
|
|
618
|
+
|
|
619
|
+
defineTool(
|
|
620
|
+
"flow_list",
|
|
621
|
+
"自动化流程列表(分页):名称/状态(enabled/disabled/draft)/触发事件/增改时间。",
|
|
622
|
+
{
|
|
623
|
+
...PAGE_PARAMS,
|
|
624
|
+
keyword: z.string().optional().describe("流程名模糊筛选"),
|
|
625
|
+
status: z.enum(["enabled", "disabled", "draft"]).optional().describe("按状态筛选"),
|
|
626
|
+
},
|
|
627
|
+
async ({ page, page_size, keyword, status }) => {
|
|
628
|
+
const body = { page, pageSize: page_size };
|
|
629
|
+
if (keyword) body.keyword = keyword;
|
|
630
|
+
if (status) body.status = status;
|
|
631
|
+
const data = await callApi("/api/v1/insight/flow/list", body);
|
|
632
|
+
return { content: [{ type: "text", text: JSON.stringify(data, null, 2) }] };
|
|
633
|
+
}
|
|
634
|
+
);
|
|
635
|
+
|
|
636
|
+
defineTool(
|
|
637
|
+
"flow_report",
|
|
638
|
+
"单流程效果报告(默认180天):进入/退出/完成/挂起/发送/成功/失败/打开/点击/销售与完成率、漏斗。不传则汇总全店。",
|
|
639
|
+
{
|
|
640
|
+
flow_id: z.string().optional().describe("流程id(优先)"),
|
|
641
|
+
flow_name: z.string().optional().describe("流程名"),
|
|
642
|
+
},
|
|
643
|
+
async ({ flow_id, flow_name }) => {
|
|
644
|
+
const args = {};
|
|
645
|
+
if (flow_id) args.flowId = flow_id;
|
|
646
|
+
if (flow_name) args.flowName = flow_name;
|
|
647
|
+
const data = await callAssistantTool("query_flow_report", args);
|
|
648
|
+
return { content: [{ type: "text", text: JSON.stringify(data, null, 2) }] };
|
|
649
|
+
}
|
|
650
|
+
);
|
|
651
|
+
|
|
652
|
+
defineTool(
|
|
653
|
+
"flow_send_failure",
|
|
654
|
+
"单流程发送失败明细(最近20条)+ 失败原因 Top5。",
|
|
655
|
+
{
|
|
656
|
+
flow_id: z.string().optional().describe("流程id(优先)"),
|
|
657
|
+
flow_name: z.string().optional().describe("流程名"),
|
|
658
|
+
},
|
|
659
|
+
async ({ flow_id, flow_name }) => {
|
|
660
|
+
const args = {};
|
|
661
|
+
if (flow_id) args.flowId = flow_id;
|
|
662
|
+
if (flow_name) args.flowName = flow_name;
|
|
663
|
+
const data = await callAssistantTool("query_flow_send_failure", args);
|
|
664
|
+
return { content: [{ type: "text", text: JSON.stringify(data, null, 2) }] };
|
|
665
|
+
}
|
|
666
|
+
);
|
|
667
|
+
|
|
668
|
+
defineTool(
|
|
669
|
+
"flow_content",
|
|
670
|
+
"读流程结构与节点内容(触发器/延迟/条件/邮件或短信节点),用于审核或作为改写依据。",
|
|
671
|
+
{
|
|
672
|
+
flow_id: z.string().optional().describe("流程id(优先)"),
|
|
673
|
+
flow_name: z.string().optional().describe("流程名"),
|
|
674
|
+
},
|
|
675
|
+
async ({ flow_id, flow_name }) => {
|
|
676
|
+
const args = {};
|
|
677
|
+
if (flow_id) args.flowId = flow_id;
|
|
678
|
+
if (flow_name) args.flowName = flow_name;
|
|
679
|
+
const data = await callAssistantTool("query_flow_content", args);
|
|
680
|
+
return { content: [{ type: "text", text: JSON.stringify(data, null, 2) }] };
|
|
681
|
+
}
|
|
682
|
+
);
|
|
683
|
+
|
|
684
|
+
defineTool(
|
|
685
|
+
"flow_triggers",
|
|
686
|
+
"自动化触发器定义目录:不传 trigger_key 返回全部触发器(含说明);传了返回其筛选项/分流项定义。",
|
|
687
|
+
{
|
|
688
|
+
trigger_key: z.string().optional().describe("触发器key,如 join_cart/order_paid/subscribe"),
|
|
689
|
+
},
|
|
690
|
+
async ({ trigger_key }) => {
|
|
691
|
+
const args = {};
|
|
692
|
+
if (trigger_key) args.triggerKey = trigger_key;
|
|
693
|
+
const data = await callAssistantTool("query_flow_trigger", args);
|
|
694
|
+
return { content: [{ type: "text", text: JSON.stringify(data, null, 2) }] };
|
|
695
|
+
}
|
|
696
|
+
);
|
|
697
|
+
|
|
698
|
+
defineTool(
|
|
699
|
+
"group_list",
|
|
700
|
+
"用户分组列表(分页,不含平台自动分组):名称/人数/动态状态/来源。",
|
|
701
|
+
{
|
|
702
|
+
...PAGE_PARAMS,
|
|
703
|
+
keyword: z.string().optional().describe("分组名模糊筛选"),
|
|
704
|
+
},
|
|
705
|
+
async ({ page, page_size, keyword }) => {
|
|
706
|
+
const body = { page, pageSize: page_size };
|
|
707
|
+
if (keyword) body.keyword = keyword;
|
|
708
|
+
const data = await callApi("/api/v1/insight/group/list", body);
|
|
709
|
+
return { content: [{ type: "text", text: JSON.stringify(data, null, 2) }] };
|
|
710
|
+
}
|
|
711
|
+
);
|
|
712
|
+
|
|
713
|
+
defineTool(
|
|
714
|
+
"group_catalog",
|
|
715
|
+
"用户分组条件目录(唯一真源,建分组前必查):不传 nested_path 返回可用数据域清单+通用规则;" +
|
|
716
|
+
"传了返回该域的真实字段(含中文名/类型)与该类别写条件的规则。多个域需分多次查询。",
|
|
717
|
+
{
|
|
718
|
+
nested_path: z.string().optional().describe("数据域,如 success_checkout / audience_address / campaign_open_number(单个,不能拼接)"),
|
|
719
|
+
},
|
|
720
|
+
async ({ nested_path }) => {
|
|
721
|
+
const args = {};
|
|
722
|
+
if (nested_path) args.nestedPath = nested_path;
|
|
723
|
+
const data = await callAssistantTool("query_group_catalog", args);
|
|
724
|
+
if (data && !data.error) groupCatalogQueriedAt = Date.now();
|
|
725
|
+
return { content: [{ type: "text", text: JSON.stringify(data, null, 2) }] };
|
|
726
|
+
}
|
|
727
|
+
);
|
|
728
|
+
|
|
729
|
+
defineTool(
|
|
730
|
+
"group_members",
|
|
731
|
+
"指定分组的成员列表(分页,自动区分手动/条件分组):邮箱/手机/姓名/国家/订阅状态。",
|
|
732
|
+
{
|
|
733
|
+
group_id: z.string().min(1).describe("分组id(group_list 返回)"),
|
|
734
|
+
...PAGE_PARAMS,
|
|
735
|
+
},
|
|
736
|
+
async ({ group_id, page, page_size }) => {
|
|
737
|
+
const data = await callApi("/api/v1/insight/group/members", {
|
|
738
|
+
groupId: group_id,
|
|
739
|
+
page,
|
|
740
|
+
pageSize: page_size,
|
|
741
|
+
});
|
|
742
|
+
return { content: [{ type: "text", text: JSON.stringify(data, null, 2) }] };
|
|
743
|
+
}
|
|
744
|
+
);
|
|
745
|
+
|
|
746
|
+
defineTool(
|
|
747
|
+
"contact_get",
|
|
748
|
+
"精确查单个联系人(email/phone/originId 三选一,不支持模糊搜):订阅状态、标签外的画像与订单汇总(已排除取消单)。",
|
|
749
|
+
{
|
|
750
|
+
email: z.string().optional().describe("完整邮箱(精确匹配)"),
|
|
751
|
+
phone: z.string().optional().describe("完整手机号(精确匹配)"),
|
|
752
|
+
origin_id: z.string().optional().describe("平台顾客id(精确匹配)"),
|
|
753
|
+
},
|
|
754
|
+
async ({ email, phone, origin_id }) => {
|
|
755
|
+
const body = {};
|
|
756
|
+
if (email) body.email = email;
|
|
757
|
+
if (phone) body.phone = phone;
|
|
758
|
+
if (origin_id) body.originId = origin_id;
|
|
759
|
+
const data = await callApi("/api/v1/insight/contact/get", body);
|
|
760
|
+
return { content: [{ type: "text", text: JSON.stringify(data, null, 2) }] };
|
|
761
|
+
}
|
|
762
|
+
);
|
|
763
|
+
|
|
764
|
+
defineTool(
|
|
765
|
+
"contact_send_status",
|
|
766
|
+
"某联系人收到过的邮件/短信及其送达、打开、点击、退订状态(可限定活动或流程)。",
|
|
767
|
+
{
|
|
768
|
+
keyword: z.string().min(1).describe("完整邮箱或手机号"),
|
|
769
|
+
campaigns_id: z.string().optional().describe("限定活动id"),
|
|
770
|
+
flow_id: z.string().optional().describe("限定流程id"),
|
|
771
|
+
},
|
|
772
|
+
async ({ keyword, campaigns_id, flow_id }) => {
|
|
773
|
+
const args = { keyword };
|
|
774
|
+
if (campaigns_id) args.campaignsId = campaigns_id;
|
|
775
|
+
if (flow_id) args.flowId = flow_id;
|
|
776
|
+
const data = await callAssistantTool("query_contact_send_status", args);
|
|
777
|
+
return { content: [{ type: "text", text: JSON.stringify(data, null, 2) }] };
|
|
778
|
+
}
|
|
779
|
+
);
|
|
780
|
+
|
|
781
|
+
defineTool(
|
|
782
|
+
"email_audit",
|
|
783
|
+
"邮件质检(与平台 AI 助手同一套 13 条规则):返回评分 score/等级 level/问题清单 issues(规则+位置 locator+修改建议)。" +
|
|
784
|
+
"可审活动正文(source=campaigns)或流程邮件节点(source=flow,需 flow_id+node_uuid);deep=true 追加模型语义审查。",
|
|
785
|
+
{
|
|
786
|
+
source: z.enum(["campaigns", "flow"]).default("campaigns").describe("审活动还是自动化流程节点"),
|
|
787
|
+
campaigns_id: z.string().optional().describe("活动id(source=campaigns 时必传)"),
|
|
788
|
+
flow_id: z.string().optional().describe("流程id(source=flow 时必传)"),
|
|
789
|
+
node_uuid: z.string().optional().describe("流程邮件节点uuid(source=flow:flow_content 可查到)"),
|
|
790
|
+
deep: z.boolean().default(false).describe("true=额外跑模型语义审查(更慢,会消耗模型额度)"),
|
|
791
|
+
},
|
|
792
|
+
async ({ source, campaigns_id, flow_id, node_uuid, deep }) => {
|
|
793
|
+
const args = { source, deep: !!deep };
|
|
794
|
+
if (campaigns_id) args.campaignsId = campaigns_id;
|
|
795
|
+
if (flow_id) args.flowId = flow_id;
|
|
796
|
+
if (node_uuid) args.nodeUuid = node_uuid;
|
|
797
|
+
const data = await callAssistantTool("audit_email", args);
|
|
798
|
+
return { content: [{ type: "text", text: JSON.stringify(data, null, 2) }] };
|
|
799
|
+
}
|
|
800
|
+
);
|
|
801
|
+
|
|
802
|
+
defineTool(
|
|
803
|
+
"brand_profile",
|
|
804
|
+
"读取店铺品牌声音档案(品牌语气/禁用词/核心卖点),用于写出与品牌一致的文案;未设置时返回 status=empty 与提示。",
|
|
805
|
+
{},
|
|
806
|
+
async () => {
|
|
807
|
+
const data = await callAssistantTool("get_brand_profile", {});
|
|
808
|
+
return { content: [{ type: "text", text: JSON.stringify(data, null, 2) }] };
|
|
809
|
+
}
|
|
810
|
+
);
|
|
811
|
+
|
|
812
|
+
defineTool(
|
|
813
|
+
"audience_growth",
|
|
814
|
+
"受众增长趋势(按天):新增联系人/邮件订阅/短信订阅。默认30天(上限90)。",
|
|
815
|
+
{
|
|
816
|
+
days: z.number().int().min(1).max(90).default(30).describe("回推天数,最大90"),
|
|
817
|
+
},
|
|
818
|
+
async ({ days }) => {
|
|
819
|
+
const data = await callAssistantTool("query_audience_growth", { days });
|
|
820
|
+
return { content: [{ type: "text", text: JSON.stringify(data, null, 2) }] };
|
|
821
|
+
}
|
|
822
|
+
);
|
|
823
|
+
|
|
824
|
+
defineTool(
|
|
825
|
+
"form_performance",
|
|
826
|
+
"表单表现:不传 forms_id 返回表单清单(浏览/登记);传了返回该表单按国家/设备的明细。",
|
|
827
|
+
{
|
|
828
|
+
forms_id: z.string().optional().describe("表单id"),
|
|
829
|
+
days: z.number().int().min(1).max(180).default(30).describe("传 forms_id 时的统计天数"),
|
|
830
|
+
},
|
|
831
|
+
async ({ forms_id, days }) => {
|
|
832
|
+
const args = { days };
|
|
833
|
+
if (forms_id) args.formsId = forms_id;
|
|
834
|
+
const data = await callAssistantTool("query_form_performance", args);
|
|
835
|
+
return { content: [{ type: "text", text: JSON.stringify(data, null, 2) }] };
|
|
836
|
+
}
|
|
837
|
+
);
|
|
838
|
+
|
|
839
|
+
defineTool(
|
|
840
|
+
"domain_health",
|
|
841
|
+
"发件域名健康度:各域的 DKIM/CNAME/SPF/DMARC/MX/追踪状态与需关注清单。",
|
|
842
|
+
{},
|
|
843
|
+
async () => {
|
|
844
|
+
const data = await callAssistantTool("query_sender_domain_health", {});
|
|
845
|
+
return { content: [{ type: "text", text: JSON.stringify(data, null, 2) }] };
|
|
846
|
+
}
|
|
847
|
+
);
|
|
848
|
+
|
|
849
|
+
defineTool(
|
|
850
|
+
"store_overview",
|
|
851
|
+
"店铺经营概览(默认180天):订单数、已支付订单数、已支付收入、有归因订单数。已排除取消单。",
|
|
852
|
+
{
|
|
853
|
+
...WINDOW_PARAMS,
|
|
854
|
+
},
|
|
855
|
+
async ({ start_date, end_date }) => {
|
|
856
|
+
const body = {};
|
|
857
|
+
if (start_date) body.startDate = start_date;
|
|
858
|
+
if (end_date) body.endDate = end_date;
|
|
859
|
+
const data = await callApi("/api/v1/insight/store/overview", body);
|
|
860
|
+
return { content: [{ type: "text", text: JSON.stringify(data, null, 2) }] };
|
|
861
|
+
}
|
|
862
|
+
);
|
|
863
|
+
|
|
864
|
+
defineTool(
|
|
865
|
+
"order_stats",
|
|
866
|
+
"订单统计(默认180天):下单数、已支付单数、已支付金额、含币种提示。已排除取消单,金额跨币种不折算。",
|
|
867
|
+
{
|
|
868
|
+
...WINDOW_PARAMS,
|
|
869
|
+
},
|
|
870
|
+
async ({ start_date, end_date }) => {
|
|
871
|
+
const body = {};
|
|
872
|
+
if (start_date) body.startDate = start_date;
|
|
873
|
+
if (end_date) body.endDate = end_date;
|
|
874
|
+
const data = await callApi("/api/v1/insight/order/stats", body);
|
|
875
|
+
return { content: [{ type: "text", text: JSON.stringify(data, null, 2) }] };
|
|
876
|
+
}
|
|
877
|
+
);
|
|
878
|
+
|
|
879
|
+
defineTool(
|
|
880
|
+
"account_balance",
|
|
881
|
+
"店铺账户余额(美元,group 级;同 group 多店共享)。",
|
|
882
|
+
{},
|
|
883
|
+
async () => {
|
|
884
|
+
const data = await callApi("/api/v1/insight/balance", {});
|
|
885
|
+
return { content: [{ type: "text", text: JSON.stringify(data, null, 2) }] };
|
|
886
|
+
}
|
|
887
|
+
);
|
|
888
|
+
|
|
889
|
+
// ==================== 2B:写能力(只建草稿,不发送不启用) ====================
|
|
890
|
+
|
|
891
|
+
async function callWriteTool(tool, args) {
|
|
892
|
+
return callApi("/api/v1/assistant/tool", { tool, arguments: args ?? {} });
|
|
893
|
+
}
|
|
894
|
+
|
|
895
|
+
const DRAFT_NOTE = "⚠️本工具只创建/修改草稿,不会发送、不会启用、不扣费。发送需商家到后台活动/自动化页确认。";
|
|
896
|
+
|
|
897
|
+
defineTool(
|
|
898
|
+
"campaign_create_email",
|
|
899
|
+
`创建邮件营销活动草稿。${DRAFT_NOTE}
|
|
900
|
+
正文两种写法:email_design(结构化JSON,推荐;先 catalog_query(topic=emailDesign) 查规范,或 template_list_system 选模板后传 template_id)或 html(整份 HTML,走后台 HTML 编辑器)。二者必传其一、互斥。`,
|
|
901
|
+
{
|
|
902
|
+
title: z.string().min(1).describe("活动名称"),
|
|
903
|
+
email_design: z
|
|
904
|
+
.string()
|
|
905
|
+
.optional()
|
|
906
|
+
.describe("邮件内容结构化 JSON 字符串(推荐;数组可传1-3版)。与 html 二选一"),
|
|
907
|
+
html: z
|
|
908
|
+
.string()
|
|
909
|
+
.optional()
|
|
910
|
+
.describe("整份邮件 HTML(贴现成 HTML / 迁移时用;走后台同款 HTML 编辑器 edition=richtext)。与 email_design 二选一"),
|
|
911
|
+
template_id: z
|
|
912
|
+
.string()
|
|
913
|
+
.optional()
|
|
914
|
+
.describe("套层用的【系统模板】id(来自 template_list_system)。⚠️只是套层目标,仍必须同时传 email_design;不接受「我的模板」的 id"),
|
|
915
|
+
layout_skeleton: z
|
|
916
|
+
.enum(["promo-hero", "promo-flash", "single-spotlight", "new-arrival", "newsletter", "win-back"])
|
|
917
|
+
.optional()
|
|
918
|
+
.describe("版式骨架(不套系统模板时用;与 template_id 二选一)"),
|
|
919
|
+
subject_line: z.string().optional().describe("邮件主题(不传则按内容推导)"),
|
|
920
|
+
preheader: z.string().optional().describe("预览文本"),
|
|
921
|
+
send_name: z.string().optional().describe("发件人名称(不传用店铺名)"),
|
|
922
|
+
send_email: z.string().optional().describe("发件邮箱(不传用店铺默认已验证发件人)"),
|
|
923
|
+
communication_select: z.enum(["all", "tag"]).optional().describe("发送对象:all=全部订阅者,tag=指定分组"),
|
|
924
|
+
tag_id: z.string().optional().describe("分组id(communication_select=tag 时用)"),
|
|
925
|
+
non_send_tag_id: z.string().optional().describe("排除的分组id"),
|
|
926
|
+
text_overrides: z.array(z.object({ node_id: z.string(), text: z.string() })).optional().describe("按节点改文案(套模板时)"),
|
|
927
|
+
components: z.array(z.record(z.any())).max(4).optional().describe("额外组件(倒计时等,规范查 catalog_query(topic=components))"),
|
|
928
|
+
blocks: z.array(z.record(z.any())).max(4).optional().describe("预置区块(block_library_query 拿 blockId)"),
|
|
929
|
+
discount_type: z.enum(["percentage", "fixed"]).optional().describe("折扣类型"),
|
|
930
|
+
discount_value: z.string().optional().describe("折扣值"),
|
|
931
|
+
discount_code: z.string().optional().describe("折扣码"),
|
|
932
|
+
},
|
|
933
|
+
async (a) => {
|
|
934
|
+
if (!a.email_design && !a.html) {
|
|
935
|
+
if (a.template_id) {
|
|
936
|
+
throw new Error(
|
|
937
|
+
"template_id 只是套层目标,不能单独使用:还需要同时提供 email_design(结构化内容)才能生成正文。" +
|
|
938
|
+
"要么传 email_design + template_id,要么直接传 html,或用 template_create_from_design 先建模板再在后台选模板。"
|
|
939
|
+
);
|
|
940
|
+
}
|
|
941
|
+
throw new Error("either email_design or html is required (结构化内容 或 整份 HTML,二选一)");
|
|
942
|
+
}
|
|
943
|
+
if (a.email_design && a.html) {
|
|
944
|
+
throw new Error("email_design and html are mutually exclusive (结构化内容与整份 HTML 只能选一个)");
|
|
945
|
+
}
|
|
946
|
+
if (a.html) {
|
|
947
|
+
const body = { title: a.title, html: a.html };
|
|
948
|
+
for (const [k, v] of Object.entries({
|
|
949
|
+
subjectLine: a.subject_line,
|
|
950
|
+
preheader: a.preheader,
|
|
951
|
+
sendEmail: a.send_email,
|
|
952
|
+
sendName: a.send_name,
|
|
953
|
+
communicationSelect: a.communication_select,
|
|
954
|
+
tagId: a.tag_id,
|
|
955
|
+
nonSendTagId: a.non_send_tag_id,
|
|
956
|
+
})) if (v !== undefined) body[k] = v;
|
|
957
|
+
const data = await callApi("/api/v1/campaign/draft/create-html", body);
|
|
958
|
+
return { content: [{ type: "text", text: JSON.stringify(data, null, 2) }] };
|
|
959
|
+
}
|
|
960
|
+
const args = { title: a.title, emailDesign: a.email_design };
|
|
961
|
+
const map = {
|
|
962
|
+
templateId: a.template_id,
|
|
963
|
+
layoutSkeleton: a.layout_skeleton,
|
|
964
|
+
subjectLine: a.subject_line,
|
|
965
|
+
preheader: a.preheader,
|
|
966
|
+
sendName: a.send_name,
|
|
967
|
+
sendEmail: a.send_email,
|
|
968
|
+
communicationSelect: a.communication_select,
|
|
969
|
+
tagId: a.tag_id,
|
|
970
|
+
nonSendTagId: a.non_send_tag_id,
|
|
971
|
+
textOverrides: a.text_overrides?.map((x) => ({ nodeId: x.node_id, text: x.text })),
|
|
972
|
+
components: a.components,
|
|
973
|
+
blocks: a.blocks,
|
|
974
|
+
discountType: a.discount_type,
|
|
975
|
+
discountValue: a.discount_value,
|
|
976
|
+
discountCode: a.discount_code,
|
|
977
|
+
};
|
|
978
|
+
for (const [k, v] of Object.entries(map)) if (v !== undefined) args[k] = v;
|
|
979
|
+
const data = await callWriteTool("create_campaign_email", args);
|
|
980
|
+
return { content: [{ type: "text", text: JSON.stringify(data, null, 2) }] };
|
|
981
|
+
}
|
|
982
|
+
);
|
|
983
|
+
|
|
984
|
+
defineTool(
|
|
985
|
+
"campaign_create_sms",
|
|
986
|
+
`创建短信营销活动草稿。${DRAFT_NOTE} 文案建议英文≤160字符/中文≤70字。`,
|
|
987
|
+
{
|
|
988
|
+
title: z.string().min(1).describe("活动名称"),
|
|
989
|
+
message_text: z.string().min(1).describe("短信文案(可传 2-3 条候选的 JSON 数组字符串)"),
|
|
990
|
+
communication_select: z.enum(["all", "tag"]).optional().describe("发送对象"),
|
|
991
|
+
tag_id: z.string().optional().describe("分组id"),
|
|
992
|
+
non_send_tag_id: z.string().optional().describe("排除分组id"),
|
|
993
|
+
discount_type: z.enum(["percentage", "fixed"]).optional().describe("折扣类型"),
|
|
994
|
+
discount_value: z.string().optional().describe("折扣值"),
|
|
995
|
+
discount_code: z.string().optional().describe("折扣码"),
|
|
996
|
+
},
|
|
997
|
+
async (a) => {
|
|
998
|
+
const args = { title: a.title, messageText: a.message_text };
|
|
999
|
+
const map = {
|
|
1000
|
+
communicationSelect: a.communication_select,
|
|
1001
|
+
tagId: a.tag_id,
|
|
1002
|
+
nonSendTagId: a.non_send_tag_id,
|
|
1003
|
+
discountType: a.discount_type,
|
|
1004
|
+
discountValue: a.discount_value,
|
|
1005
|
+
discountCode: a.discount_code,
|
|
1006
|
+
};
|
|
1007
|
+
for (const [k, v] of Object.entries(map)) if (v !== undefined) args[k] = v;
|
|
1008
|
+
const data = await callWriteTool("create_campaign_sms", args);
|
|
1009
|
+
return { content: [{ type: "text", text: JSON.stringify(data, null, 2) }] };
|
|
1010
|
+
}
|
|
1011
|
+
);
|
|
1012
|
+
|
|
1013
|
+
defineTool(
|
|
1014
|
+
"campaign_create_abtest",
|
|
1015
|
+
`创建邮件 A/B 测试活动草稿。${DRAFT_NOTE}`,
|
|
1016
|
+
{
|
|
1017
|
+
title: z.string().min(1).describe("活动名称"),
|
|
1018
|
+
subject_line: z.string().min(1).describe("A 版主题"),
|
|
1019
|
+
ab_subject_line: z.string().min(1).describe("B 版主题"),
|
|
1020
|
+
email_design: z.string().min(1).describe("邮件内容结构化 JSON"),
|
|
1021
|
+
template_id: z.string().optional().describe("套用的系统模板 id"),
|
|
1022
|
+
ab_test_ratio: z.number().int().min(1).max(99).optional().describe("测试人群比例(%)"),
|
|
1023
|
+
calculation_type: z.enum(["clickRate", "openRate"]).optional().describe("赢家判定指标"),
|
|
1024
|
+
number: z.number().int().min(1).optional().describe("测试时长数值"),
|
|
1025
|
+
date_type: z.enum(["hour", "day"]).optional().describe("测试时长单位"),
|
|
1026
|
+
communication_select: z.enum(["all", "tag"]).optional().describe("发送对象"),
|
|
1027
|
+
tag_id: z.string().optional().describe("分组id"),
|
|
1028
|
+
},
|
|
1029
|
+
async (a) => {
|
|
1030
|
+
const args = {
|
|
1031
|
+
title: a.title,
|
|
1032
|
+
subjectLine: a.subject_line,
|
|
1033
|
+
abSubjectLine: a.ab_subject_line,
|
|
1034
|
+
emailDesign: a.email_design,
|
|
1035
|
+
};
|
|
1036
|
+
const map = {
|
|
1037
|
+
templateId: a.template_id,
|
|
1038
|
+
abTestRatio: a.ab_test_ratio,
|
|
1039
|
+
calculationType: a.calculation_type,
|
|
1040
|
+
number: a.number,
|
|
1041
|
+
dateType: a.date_type,
|
|
1042
|
+
communicationSelect: a.communication_select,
|
|
1043
|
+
tagId: a.tag_id,
|
|
1044
|
+
};
|
|
1045
|
+
for (const [k, v] of Object.entries(map)) if (v !== undefined) args[k] = v;
|
|
1046
|
+
const data = await callWriteTool("create_campaign_abtest", args);
|
|
1047
|
+
return { content: [{ type: "text", text: JSON.stringify(data, null, 2) }] };
|
|
1048
|
+
}
|
|
1049
|
+
);
|
|
1050
|
+
|
|
1051
|
+
defineTool(
|
|
1052
|
+
"campaign_create_sms_abtest",
|
|
1053
|
+
`创建短信 A/B 测试活动草稿。${DRAFT_NOTE}`,
|
|
1054
|
+
{
|
|
1055
|
+
title: z.string().min(1).describe("活动名称"),
|
|
1056
|
+
sms_content_a: z.string().min(1).describe("A 版文案"),
|
|
1057
|
+
sms_content_b: z.string().min(1).describe("B 版文案"),
|
|
1058
|
+
test_ratio: z.number().int().min(1).max(99).optional().describe("测试人群比例(%)"),
|
|
1059
|
+
winner_ratio: z.number().int().min(1).max(99).optional().describe("赢家放量比例(%)"),
|
|
1060
|
+
calculation_type: z.enum(["clickRate", "openRate"]).optional().describe("赢家判定指标"),
|
|
1061
|
+
number: z.number().int().min(1).optional().describe("测试时长数值"),
|
|
1062
|
+
date_type: z.enum(["hour", "day"]).optional().describe("测试时长单位"),
|
|
1063
|
+
communication_select: z.enum(["all", "tag"]).optional().describe("发送对象"),
|
|
1064
|
+
tag_id: z.string().optional().describe("分组id"),
|
|
1065
|
+
},
|
|
1066
|
+
async (a) => {
|
|
1067
|
+
const args = {
|
|
1068
|
+
title: a.title,
|
|
1069
|
+
smsContentA: a.sms_content_a,
|
|
1070
|
+
smsContentB: a.sms_content_b,
|
|
1071
|
+
};
|
|
1072
|
+
const map = {
|
|
1073
|
+
testRatio: a.test_ratio,
|
|
1074
|
+
winnerRatio: a.winner_ratio,
|
|
1075
|
+
calculationType: a.calculation_type,
|
|
1076
|
+
number: a.number,
|
|
1077
|
+
dateType: a.date_type,
|
|
1078
|
+
communicationSelect: a.communication_select,
|
|
1079
|
+
tagId: a.tag_id,
|
|
1080
|
+
};
|
|
1081
|
+
for (const [k, v] of Object.entries(map)) if (v !== undefined) args[k] = v;
|
|
1082
|
+
const data = await callWriteTool("create_campaign_smsabtest", args);
|
|
1083
|
+
return { content: [{ type: "text", text: JSON.stringify(data, null, 2) }] };
|
|
1084
|
+
}
|
|
1085
|
+
);
|
|
1086
|
+
|
|
1087
|
+
defineTool(
|
|
1088
|
+
"campaign_update_content",
|
|
1089
|
+
"修改活动草稿内容(主题/预览文本/正文)。⚠️仅 status=draft 的活动可改,已发送/已定时会被拒绝。",
|
|
1090
|
+
{
|
|
1091
|
+
campaigns_id: z.string().min(1).describe("活动id(campaign_list 返回)"),
|
|
1092
|
+
subject_line: z.string().optional().describe("新主题"),
|
|
1093
|
+
preheader: z.string().optional().describe("新预览文本"),
|
|
1094
|
+
email_design: z.string().optional().describe("新的 emailDesign JSON(与 message_text/html 互斥)"),
|
|
1095
|
+
html: z.string().optional().describe("整份新邮件 HTML(走 HTML 编辑器;与 email_design/message_text 互斥)"),
|
|
1096
|
+
layout_skeleton: z
|
|
1097
|
+
.enum(["promo-hero", "promo-flash", "single-spotlight", "new-arrival", "newsletter", "win-back"])
|
|
1098
|
+
.optional()
|
|
1099
|
+
.describe("重建时使用的版式骨架"),
|
|
1100
|
+
message_text: z.string().optional().describe("短信新文案"),
|
|
1101
|
+
variant: z.enum(["A", "B"]).optional().describe("AB 测试时指定改哪一版"),
|
|
1102
|
+
},
|
|
1103
|
+
async (a) => {
|
|
1104
|
+
if (a.html) {
|
|
1105
|
+
const body = { campaignsId: a.campaigns_id, html: a.html };
|
|
1106
|
+
if (a.subject_line !== undefined) body.subjectLine = a.subject_line;
|
|
1107
|
+
if (a.preheader !== undefined) body.preheader = a.preheader;
|
|
1108
|
+
const data = await callApi("/api/v1/campaign/draft/update-html", body);
|
|
1109
|
+
return { content: [{ type: "text", text: JSON.stringify(data, null, 2) }] };
|
|
1110
|
+
}
|
|
1111
|
+
const args = { campaignsId: a.campaigns_id };
|
|
1112
|
+
const map = {
|
|
1113
|
+
subjectLine: a.subject_line,
|
|
1114
|
+
preheader: a.preheader,
|
|
1115
|
+
emailDesign: a.email_design,
|
|
1116
|
+
layoutSkeleton: a.layout_skeleton,
|
|
1117
|
+
messageText: a.message_text,
|
|
1118
|
+
variant: a.variant,
|
|
1119
|
+
};
|
|
1120
|
+
for (const [k, v] of Object.entries(map)) if (v !== undefined) args[k] = v;
|
|
1121
|
+
const data = await callWriteTool("update_campaign_content", args);
|
|
1122
|
+
return { content: [{ type: "text", text: JSON.stringify(data, null, 2) }] };
|
|
1123
|
+
}
|
|
1124
|
+
);
|
|
1125
|
+
|
|
1126
|
+
defineTool(
|
|
1127
|
+
"flow_create",
|
|
1128
|
+
`创建自动化流程草稿。${DRAFT_NOTE}
|
|
1129
|
+
先用 flow_triggers 查触发器定义(trigger_key 与筛选项),邮件节点正文用 email_design;可选多步骤 steps。`,
|
|
1130
|
+
{
|
|
1131
|
+
flow_name: z.string().min(1).describe("流程名称"),
|
|
1132
|
+
trigger_key: z.string().min(1).describe("触发事件,如 join_cart/order_paid/subscribe/viewed_product(用 flow_triggers 查全部)"),
|
|
1133
|
+
channel: z.enum(["Email", "SMS"]).default("Email").describe("主节点渠道"),
|
|
1134
|
+
delay_minutes: z.number().int().min(0).optional().describe("首个动作延迟(数值)"),
|
|
1135
|
+
delay_unit: z.enum(["minute", "hour", "day"]).optional().describe("延迟单位"),
|
|
1136
|
+
send_name: z.string().optional().describe("发件人名称"),
|
|
1137
|
+
send_email: z.string().optional().describe("发件邮箱"),
|
|
1138
|
+
email_subject: z.string().optional().describe("邮件主题"),
|
|
1139
|
+
email_design: z.string().optional().describe("邮件内容结构化 JSON(Email 渠道建议必传)"),
|
|
1140
|
+
template_id: z.string().optional().describe("套用的自动化模板 id"),
|
|
1141
|
+
email_content: z.string().optional().describe("邮件节点整份 HTML(后台 HTML 编辑器同款;与 email_design 二选一)"),
|
|
1142
|
+
html: z.string().optional().describe("email_content 的别名(整份 HTML),二选一即可"),
|
|
1143
|
+
sms_content: z.string().optional().describe("短信文案(SMS 渠道必传)"),
|
|
1144
|
+
steps: z.array(z.record(z.any())).optional().describe("多步骤:[{delayMinutes,delayUnit,channel,emailSubject,emailDesign,smsContent}]"),
|
|
1145
|
+
exit_conditions: z.array(z.string()).optional().describe("退出条件(如 order_placed/checkout_started)"),
|
|
1146
|
+
trigger_filters: z.array(z.record(z.any())).optional().describe("触发过滤器(定义见 flow_triggers)"),
|
|
1147
|
+
frequency_type: z.enum(["atTime", "inLast", "none"]).optional().describe("频率控制类型"),
|
|
1148
|
+
frequency_value: z.number().int().optional().describe("频率数值"),
|
|
1149
|
+
frequency_unit: z.enum(["minute", "hour", "day"]).optional().describe("频率单位"),
|
|
1150
|
+
},
|
|
1151
|
+
async (a) => {
|
|
1152
|
+
const args = {
|
|
1153
|
+
flowName: a.flow_name,
|
|
1154
|
+
triggerKey: a.trigger_key,
|
|
1155
|
+
channel: a.channel,
|
|
1156
|
+
};
|
|
1157
|
+
const map = {
|
|
1158
|
+
delayMinutes: a.delay_minutes,
|
|
1159
|
+
delayUnit: a.delay_unit,
|
|
1160
|
+
sendName: a.send_name,
|
|
1161
|
+
sendEmail: a.send_email,
|
|
1162
|
+
emailSubject: a.email_subject,
|
|
1163
|
+
emailDesign: a.email_design,
|
|
1164
|
+
templateId: a.template_id,
|
|
1165
|
+
emailContent: a.email_content !== undefined ? a.email_content : a.html,
|
|
1166
|
+
smsContent: a.sms_content,
|
|
1167
|
+
steps: a.steps,
|
|
1168
|
+
exitConditions: a.exit_conditions,
|
|
1169
|
+
triggerFilters: a.trigger_filters,
|
|
1170
|
+
frequencyType: a.frequency_type,
|
|
1171
|
+
frequencyValue: a.frequency_value,
|
|
1172
|
+
frequencyUnit: a.frequency_unit,
|
|
1173
|
+
};
|
|
1174
|
+
for (const [k, v] of Object.entries(map)) if (v !== undefined) args[k] = v;
|
|
1175
|
+
const data = await callWriteTool("create_flow", args);
|
|
1176
|
+
return { content: [{ type: "text", text: JSON.stringify(data, null, 2) }] };
|
|
1177
|
+
}
|
|
1178
|
+
);
|
|
1179
|
+
|
|
1180
|
+
defineTool(
|
|
1181
|
+
"flow_update_content",
|
|
1182
|
+
"修改自动化流程草稿(名称/邮件主题/邮件正文)。⚠️仅 status=draft 的流程可改,已启用流程会被拒绝。",
|
|
1183
|
+
{
|
|
1184
|
+
flow_id: z.string().min(1).describe("流程id(flow_list 返回)"),
|
|
1185
|
+
flow_name: z.string().optional().describe("新名称"),
|
|
1186
|
+
email_subject: z.string().optional().describe("新邮件主题"),
|
|
1187
|
+
email_content: z.string().optional().describe("新邮件正文整份 HTML"),
|
|
1188
|
+
html: z.string().optional().describe("email_content 的别名"),
|
|
1189
|
+
},
|
|
1190
|
+
async (a) => {
|
|
1191
|
+
const args = { flowId: a.flow_id };
|
|
1192
|
+
const map = {
|
|
1193
|
+
flowName: a.flow_name,
|
|
1194
|
+
emailSubject: a.email_subject,
|
|
1195
|
+
emailContent: a.email_content !== undefined ? a.email_content : a.html,
|
|
1196
|
+
};
|
|
1197
|
+
for (const [k, v] of Object.entries(map)) if (v !== undefined) args[k] = v;
|
|
1198
|
+
const data = await callWriteTool("update_flow_content", args);
|
|
1199
|
+
return { content: [{ type: "text", text: JSON.stringify(data, null, 2) }] };
|
|
1200
|
+
}
|
|
1201
|
+
);
|
|
1202
|
+
|
|
1203
|
+
defineTool(
|
|
1204
|
+
"user_group_create",
|
|
1205
|
+
`创建一个用户分组(只建分组,不发信)。⚠️必须先调用 group_catalog 取真实字段与规则,没查会被拒绝(查过一次 30 分钟内免查)。
|
|
1206
|
+
|
|
1207
|
+
query_content = 二维数组的 JSON 字符串:外层=OR 组,内层=AND 条件(组内同时满足)。
|
|
1208
|
+
- "A或B" = [[A],[B]],禁止 [[A,B]];"A且(B或C)" = [[A,B],[A,C]];"(A或B)且没有C" = [[A,NOT C],[B,NOT C]]。
|
|
1209
|
+
- 条件对象:{"condition","field","logic","nestedPath","script":false,"value"} 或多值用 "termsValue":[...]。
|
|
1210
|
+
- condition 只用:gte gt lt lte term_query terms_query fuzzy_query not_fuzzy_query exists_field exists_field_not。
|
|
1211
|
+
禁止 eq/un_eq/empty/don_empty(引擎不认识,会被静默丢弃);精确匹配=term_query;否定=logic:"must_not" 或 exists_field_not。
|
|
1212
|
+
- 时间条件必须写在 value 字符串里:{"field":"date_day","nestedPath":"success_checkout","condition":"gte","logic":"must","script":false,"value":"formerly:30"} = 近30天下单;"超过"=gt,"不低于"=gte,"少于"=lt。
|
|
1213
|
+
- 行为的时间窗必须配该行为自己的 date_day+nestedPath;不传 nestedPath 的 date_day 是注册时间。
|
|
1214
|
+
- 次数用 number:下过单=success_checkout+number+gte+"1";加购=shopping_cart;弃单=abandon_checkout。
|
|
1215
|
+
- 用户没提订阅就不要加 is_email_subscriptions/is_sms_subscriptions(1=已订阅,0=退订)。
|
|
1216
|
+
|
|
1217
|
+
name 最长 20 字。创建后 status=draft,人数由后台异步打包(可用 group_list / group_members 复查)。`,
|
|
1218
|
+
{
|
|
1219
|
+
name: z.string().min(1).describe("分组名称(最长 20 字)"),
|
|
1220
|
+
query_content: z.string().min(2).describe("二维条件数组的 JSON 字符串,例:[[{\"field\":\"country\",\"condition\":\"term_query\",\"value\":\"US\",\"logic\":\"must\",\"nestedPath\":\"\"}]]"),
|
|
1221
|
+
},
|
|
1222
|
+
async ({ name, query_content }) => {
|
|
1223
|
+
if (Date.now() - groupCatalogQueriedAt > GROUP_CATALOG_TTL_MS) {
|
|
1224
|
+
return {
|
|
1225
|
+
isError: true,
|
|
1226
|
+
content: [{
|
|
1227
|
+
type: "text",
|
|
1228
|
+
text: JSON.stringify({
|
|
1229
|
+
error: "创建分组前必须先调用 group_catalog",
|
|
1230
|
+
next: "先调 group_catalog(不传参=数据域清单+通用规则;传 nested_path 如 success_checkout=该域真实字段+写条件规则,多个域分多次查),再按返回的字段生成 query_content 重试",
|
|
1231
|
+
why: "query_content 只能使用目录里真实存在的 field/nestedPath,凭印象写会被后端校验拒绝",
|
|
1232
|
+
}, null, 2),
|
|
1233
|
+
}],
|
|
1234
|
+
};
|
|
1235
|
+
}
|
|
1236
|
+
const data = await callWriteTool("create_user_group", { name, queryContent: query_content });
|
|
1237
|
+
const failed = !!(data && typeof data === "object" && data.error);
|
|
1238
|
+
return { isError: failed, content: [{ type: "text", text: JSON.stringify(data, null, 2) }] };
|
|
1239
|
+
}
|
|
1240
|
+
);
|
|
1241
|
+
|
|
1242
|
+
|
|
1243
|
+
// ==================== 通用发现/调度工具(参考 Omnisend MCP 紧凑设计) ====================
|
|
1244
|
+
|
|
1245
|
+
/** 能力总览(按主题返回,避免一次性灌 50 条工具说明) */
|
|
1246
|
+
const MCP_GUIDE = {
|
|
1247
|
+
templates: [
|
|
1248
|
+
"【模板 17 个】四条创建路径:",
|
|
1249
|
+
"A) template_create_from_design —— 首选。传结构化 emailDesign(style/theme/hero/intro/products/offer/cta/social),先 catalog_query(topic=emailDesign) 查规范;要骨架传 catalog_query(topic=layouts);套已有模板传 templateId。",
|
|
1250
|
+
"B) template_create(html) —— 迁移现成 HTML / 精细控制。",
|
|
1251
|
+
"C) template_create(sections) —— 动态区块(购物车/浏览列表),发送时自动填该联系人数据。",
|
|
1252
|
+
"D) template_import —— .mambatpl 平台模板 或 zipHtml(设计工具导出的 zip,图片自动转存 CDN)。",
|
|
1253
|
+
"辅助:template_list / template_get / template_update / template_delete / upload_image /",
|
|
1254
|
+
" template_list_system / template_get_system / template_system_categories(系统模板参考)/",
|
|
1255
|
+
" catalog_query(规范目录)/ block_library_query(预置区块)/ template_snippets(页头页脚片段)/",
|
|
1256
|
+
" template_preview(免登永久链接)/ template_send_test(发测试邮件)/ template_export / template_import。",
|
|
1257
|
+
"硬规则:退订必须 {Unsubscribe};社交图标用 template_snippets(type=footer-social) 的地址,key 大小写敏感。",
|
|
1258
|
+
].join("\n"),
|
|
1259
|
+
insights: [
|
|
1260
|
+
"【只读/质检 23 个】列表统一 page/page_size(默认20,上限100):",
|
|
1261
|
+
"- 活动:campaign_list / campaign_report / campaign_link_report / campaign_abtest_result / campaign_content",
|
|
1262
|
+
"- 自动化:flow_list / flow_report / flow_send_failure / flow_content / flow_triggers",
|
|
1263
|
+
"- 分组:group_list / group_catalog(条件真源)/ group_members(自动区分手动/条件分组)",
|
|
1264
|
+
"- 联系人:contact_get(精确查,含订单聚合)/ contact_send_status",
|
|
1265
|
+
"- 报表:audience_growth / form_performance / domain_health / store_overview / order_stats / account_balance",
|
|
1266
|
+
"- 质检:email_audit(13 条规则+评分,deep=true 追加模型审查)/ brand_profile(品牌语气档案)",
|
|
1267
|
+
"口径:报表默认近 180 天(可传 start_date/end_date 覆盖);订单已排除 CANCELLED;余额单位美元且为 group 级;联系人只支持精确查。",
|
|
1268
|
+
].join("\n"),
|
|
1269
|
+
drafts: [
|
|
1270
|
+
"【写=只建草稿 8 个】campaign_create_email / _sms / _abtest / _smsabtest、campaign_update_content、flow_create、flow_update_content、user_group_create。",
|
|
1271
|
+
"- 一律 draft:不发送、不启用、不扣费;发送与启用仍需商家在后台确认。",
|
|
1272
|
+
"- update_* 仅 draft 可改,已发送/已启用返回 412。",
|
|
1273
|
+
"- 活动正文二选一:结构化 email_design 或整份 html(等同后台 HTML 编辑器);流程邮件节点同样支持整份 html。",
|
|
1274
|
+
"- 指定人群:communication_select=tag + tag_id;排除人群 non_send_tag_id。",
|
|
1275
|
+
"- 质量闸门:活动渲染后 ≥4200 字符、流程邮件节点 ≥3000 字符、须含 {Unsubscribe}、买家可见文案英文。",
|
|
1276
|
+
].join("\n"),
|
|
1277
|
+
groups: [
|
|
1278
|
+
"【建分组三步】① group_catalog 取真源(必做,否则被拒;30 分钟内免再查)② user_group_create(name≤20字 + query_content)③ group_list / group_members 复查人数与成员。",
|
|
1279
|
+
"query_content = 二维数组 JSON 字符串:外层 OR、内层 AND。",
|
|
1280
|
+
'- "A或B" = [[A],[B]](不是 [[A,B]]);"A且(B或C)" = [[A,B],[A,C]];"(A或B)且没有C" = [[A,NOT C],[B,NOT C]]。',
|
|
1281
|
+
'- 时间写进 value:近30天下单 = {"field":"date_day","nestedPath":"success_checkout","condition":"gte","logic":"must","script":false,"value":"formerly:30"}。',
|
|
1282
|
+
'- 次数用 number:下过单=success_checkout、加购=shopping_cart、弃单=abandon_checkout。',
|
|
1283
|
+
"- 操作符只用 gte/gt/lt/lte/term_query/terms_query/fuzzy_query/not_fuzzy_query/exists_field/exists_field_not;禁止 eq/un_eq/empty/don_empty。",
|
|
1284
|
+
"- 人数由后台异步打包(实测数十秒),dynamicStatus=complete 后 userCount 才准。",
|
|
1285
|
+
].join("\n"),
|
|
1286
|
+
workflow: [
|
|
1287
|
+
"【推荐工作流】",
|
|
1288
|
+
"1) 从零做营销邮件:catalog_query(emailDesign) → template_create_from_design → template_preview → template_send_test → campaign_create_email(tag_id 指定人群) → email_audit",
|
|
1289
|
+
"2) 迁移现成 HTML:template_create(html) → template_preview → 用 template_id 建活动",
|
|
1290
|
+
"3) 按条件圈人:group_catalog → user_group_create → group_list(看人数)→ group_members(看明细)",
|
|
1291
|
+
"4) 弃单/欢迎自动化:flow_triggers(查 triggerKey 与 filters)→ flow_create(html 或 email_design)→ flow_list 确认 draft",
|
|
1292
|
+
"5) 复盘:campaign_list → campaign_report → campaign_link_report;或 flow_report / flow_send_failure",
|
|
1293
|
+
"6) 工具不熟:mamba_guide(本总览)→ mamba_search(关键词)→ mamba_tool_schema(参数)→ mamba_execute",
|
|
1294
|
+
].join("\n"),
|
|
1295
|
+
boundaries: [
|
|
1296
|
+
"【边界】不做发送/启用/中断发送、不删除活动、不做联系人模糊全表搜索、不做拖拽式精修(留给后台)。",
|
|
1297
|
+
"写操作全部落 mb_mcp_operation_log 审计(X-Mamba-Source=mcp)。",
|
|
1298
|
+
"MAMBA_READONLY=1 只注册只读工具(52→35),写工具与 mamba_execute 的写操作都会被拒绝。",
|
|
1299
|
+
].join("\n"),
|
|
1300
|
+
};
|
|
1301
|
+
|
|
1302
|
+
defineTool(
|
|
1303
|
+
"mamba_guide",
|
|
1304
|
+
"本 MCP 的能力总览(不传 topic 返回四组能力+推荐工作流+边界)。AI 刚接入、或想知道'你能做什么/怎么做'时先调它," +
|
|
1305
|
+
"再按需 mamba_search 找具体工具。",
|
|
1306
|
+
{
|
|
1307
|
+
topic: z
|
|
1308
|
+
.enum(["templates", "insights", "drafts", "groups", "workflow", "boundaries"])
|
|
1309
|
+
.optional()
|
|
1310
|
+
.describe("只看某一主题;不传=全部"),
|
|
1311
|
+
},
|
|
1312
|
+
async ({ topic }) => {
|
|
1313
|
+
const keys = topic ? [topic] : Object.keys(MCP_GUIDE);
|
|
1314
|
+
const text = [
|
|
1315
|
+
`MambaSMS MCP 能力总览(v${CLIENT_VERSION},共 ${TOOL_REGISTRY.size} 个工具)`,
|
|
1316
|
+
...keys.map((k) => MCP_GUIDE[k]),
|
|
1317
|
+
].join("\n\n");
|
|
1318
|
+
return { content: [{ type: "text", text }] };
|
|
1319
|
+
}
|
|
1320
|
+
);
|
|
1321
|
+
|
|
1322
|
+
defineTool(
|
|
1323
|
+
"mamba_search",
|
|
1324
|
+
"搜索本 MCP 可用的操作(工具名+一句话说明+是否只读)。当你不确定该用哪个工具、或工具很多时,先 search 再 execute。",
|
|
1325
|
+
{
|
|
1326
|
+
query: z.string().optional().describe("关键词(如 campaign/flow/group/template/report),不传返回全部"),
|
|
1327
|
+
},
|
|
1328
|
+
async ({ query }) => {
|
|
1329
|
+
const q = (query || "").toLowerCase();
|
|
1330
|
+
const items = [...TOOL_REGISTRY.values()]
|
|
1331
|
+
.filter((t) => t.name !== "mamba_search")
|
|
1332
|
+
.filter(
|
|
1333
|
+
(t) =>
|
|
1334
|
+
!q ||
|
|
1335
|
+
t.name.toLowerCase().includes(q) ||
|
|
1336
|
+
String(t.description || "").toLowerCase().includes(q)
|
|
1337
|
+
)
|
|
1338
|
+
.map((t) => ({
|
|
1339
|
+
name: t.name,
|
|
1340
|
+
readOnly: t.readOnly,
|
|
1341
|
+
description: String(t.description || "")
|
|
1342
|
+
.split(/\r?\n/)[0]
|
|
1343
|
+
.slice(0, 150),
|
|
1344
|
+
}));
|
|
1345
|
+
return {
|
|
1346
|
+
content: [
|
|
1347
|
+
{
|
|
1348
|
+
type: "text",
|
|
1349
|
+
text: JSON.stringify(
|
|
1350
|
+
{ total: items.length, readonlyMode: READONLY_MODE, items },
|
|
1351
|
+
null,
|
|
1352
|
+
2
|
|
1353
|
+
),
|
|
1354
|
+
},
|
|
1355
|
+
],
|
|
1356
|
+
};
|
|
1357
|
+
}
|
|
1358
|
+
);
|
|
1359
|
+
|
|
1360
|
+
defineTool(
|
|
1361
|
+
"mamba_tool_schema",
|
|
1362
|
+
"取某个操作的参数说明(参数名、是否必填、说明)。配合 mamba_search 使用,确认参数后再 mamba_execute。",
|
|
1363
|
+
{
|
|
1364
|
+
name: z.string().min(1).describe("工具名(mamba_search 返回的 name)"),
|
|
1365
|
+
},
|
|
1366
|
+
async ({ name }) => {
|
|
1367
|
+
const t = TOOL_REGISTRY.get(name);
|
|
1368
|
+
if (!t) {
|
|
1369
|
+
throw new Error(`unknown tool: ${name} (use mamba_search to discover)`);
|
|
1370
|
+
}
|
|
1371
|
+
const params = Object.entries(t.shape || {}).map(([k, v]) => {
|
|
1372
|
+
const optional = !!(v && v.isOptional && v.isOptional());
|
|
1373
|
+
return {
|
|
1374
|
+
name: k,
|
|
1375
|
+
required: !optional,
|
|
1376
|
+
description: (v && (v.description || (v._def && v._def.description))) || "",
|
|
1377
|
+
};
|
|
1378
|
+
});
|
|
1379
|
+
return {
|
|
1380
|
+
content: [
|
|
1381
|
+
{
|
|
1382
|
+
type: "text",
|
|
1383
|
+
text: JSON.stringify(
|
|
1384
|
+
{
|
|
1385
|
+
name: t.name,
|
|
1386
|
+
readOnly: t.readOnly,
|
|
1387
|
+
description: t.description,
|
|
1388
|
+
parameters: params,
|
|
1389
|
+
},
|
|
1390
|
+
null,
|
|
1391
|
+
2
|
|
1392
|
+
),
|
|
1393
|
+
},
|
|
1394
|
+
],
|
|
1395
|
+
};
|
|
1396
|
+
}
|
|
1397
|
+
);
|
|
1398
|
+
|
|
1399
|
+
defineTool(
|
|
1400
|
+
"mamba_execute",
|
|
1401
|
+
"执行一个操作(name + arguments),等价于直接调用同名工具。只读模式下写操作会被拒绝。",
|
|
1402
|
+
{
|
|
1403
|
+
name: z.string().min(1).describe("工具名(mamba_search 返回的 name)"),
|
|
1404
|
+
arguments: z.record(z.any()).optional().describe("该工具的参数对象(字段见 mamba_tool_schema)"),
|
|
1405
|
+
},
|
|
1406
|
+
async ({ name, arguments: args }) => {
|
|
1407
|
+
const t = TOOL_REGISTRY.get(name);
|
|
1408
|
+
if (!t) {
|
|
1409
|
+
throw new Error(`unknown tool: ${name} (use mamba_search to discover)`);
|
|
1410
|
+
}
|
|
1411
|
+
if (READONLY_MODE && !t.readOnly) {
|
|
1412
|
+
throw new Error(`tool ${name} is a write operation; server is in read-only mode (MAMBA_READONLY=1)`);
|
|
1413
|
+
}
|
|
1414
|
+
return t.handler(args ?? {});
|
|
1415
|
+
}
|
|
1416
|
+
);
|
|
1417
|
+
|
|
1418
|
+
const transport = new StdioServerTransport();
|
|
1419
|
+
await server.connect(transport);
|
|
1420
|
+
console.error(`mambasms-mcp-server ready v${CLIENT_VERSION}, api=${API_BASE}`);
|