@sea-ecommerce/admin-mcp-server 0.1.4 → 0.1.5

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.
@@ -21708,8 +21708,31 @@ var MedusaAdminClient = class {
21708
21708
  listProducts(params = {}) {
21709
21709
  return this.request("GET", `/admin/products${buildQuery(params)}`);
21710
21710
  }
21711
- getProduct(id) {
21712
- return this.request("GET", `/admin/products/${encodeURIComponent(id)}`);
21711
+ /**
21712
+ * 把「商品 id 或 handle」解析为商品 id。
21713
+ * ⚠️ Medusa v2 的 GET /admin/products/{id} **只认 id**:传 handle 会 404(Product not found)。
21714
+ * 因此 handle 必须先走列表端点 `?handle=<h>`(精确匹配)解析成 id,再由调用方按 id 取详情。
21715
+ * 解析不到(id 不存在 / handle 无匹配)返回 null。
21716
+ */
21717
+ async resolveProductId(idOrHandle) {
21718
+ const s = idOrHandle.trim();
21719
+ if (/^prod_[a-z0-9]+$/i.test(s)) return s;
21720
+ try {
21721
+ const data = await this.request("GET", `/admin/products${buildQuery({ handle: s, limit: 10 })}`);
21722
+ const list = data?.products || [];
21723
+ const exact = list.find((p) => p.handle === s);
21724
+ return (exact ?? list[0])?.id || null;
21725
+ } catch {
21726
+ return null;
21727
+ }
21728
+ }
21729
+ /** 按商品 id 或 handle 获取商品详情(handle 自动解析为 id,见 resolveProductId)。 */
21730
+ async getProduct(idOrHandle) {
21731
+ const id = await this.resolveProductId(idOrHandle);
21732
+ if (id) {
21733
+ return this.request("GET", `/admin/products/${encodeURIComponent(id)}`);
21734
+ }
21735
+ return this.request("GET", `/admin/products/${encodeURIComponent(idOrHandle.trim())}`);
21713
21736
  }
21714
21737
  /** 列出销售渠道(用于默认渠道解析) */
21715
21738
  listSalesChannels(params = {}) {
@@ -22127,7 +22150,105 @@ location_id \u53EF\u9009\uFF1A\u4F20\u4E86\u5219\u53EA\u56DE\u8BE5\u4ED3\u5E93\u
22127
22150
  }
22128
22151
  ];
22129
22152
 
22153
+ // src/tools/translations.ts
22154
+ function canonLocaleKey(locale) {
22155
+ return normalizeLocale(locale) ?? locale;
22156
+ }
22157
+ async function planTranslationUpsert(client2, reference, referenceId, entries) {
22158
+ const data = await client2.listTranslations({ reference, reference_id: referenceId, limit: 100 });
22159
+ const existing = data?.translations || [];
22160
+ const byLocale = new Map(existing.map((r) => [r.locale_code, r]));
22161
+ const create = [];
22162
+ const update = [];
22163
+ const matched = [];
22164
+ for (const e of entries) {
22165
+ const target = canonLocaleKey(e.locale_code);
22166
+ let row = byLocale.get(e.locale_code) || byLocale.get(target);
22167
+ if (!row) {
22168
+ row = existing.find((r) => canonLocaleKey(r.locale_code) === target) || null;
22169
+ }
22170
+ if (row) {
22171
+ const merged = { ...row.translations || {} };
22172
+ for (const [k, v] of Object.entries(e.translations)) {
22173
+ if (v !== void 0 && v !== null) merged[k] = v;
22174
+ }
22175
+ const upd = { id: row.id, translations: merged };
22176
+ if (row.locale_code !== target) upd.locale_code = target;
22177
+ update.push(upd);
22178
+ matched.push({ id: row.id, from_locale: row.locale_code, to_locale: target });
22179
+ } else {
22180
+ const clean = {};
22181
+ for (const [k, v] of Object.entries(e.translations)) {
22182
+ if (v !== void 0 && v !== null) clean[k] = v;
22183
+ }
22184
+ create.push({ reference, reference_id: referenceId, locale_code: target, translations: clean });
22185
+ }
22186
+ }
22187
+ return { create, update, matched };
22188
+ }
22189
+ async function upsertTranslations(client2, reference, referenceId, entries) {
22190
+ const plan = await planTranslationUpsert(client2, reference, referenceId, entries);
22191
+ const result = await client2.batchTranslations(plan.create, plan.update);
22192
+ return { created: plan.create.length, updated: plan.update.length, result };
22193
+ }
22194
+ var translationTools = [
22195
+ {
22196
+ name: "set_translations",
22197
+ description: `\u4E3A\u5B9E\u4F53\uFF08\u5546\u54C1\u7B49\uFF09**\u521B\u5EFA\u6216\u66F4\u65B0\uFF08upsert\uFF09**\u591A\u8BED\u8A00\u7FFB\u8BD1\uFF08title/subtitle/description\uFF09\uFF0C\u8D70 /admin/translations/batch\u3002
22198
+ upsert \u8BED\u4E49\uFF1A\u540C\u8BED\u8A00\u5DF2\u6709\u7FFB\u8BD1 \u2192 \u66F4\u65B0\uFF08\u5B57\u6BB5\u5408\u5E76\uFF0C\u672A\u7ED9\u7684\u65E7\u5B57\u6BB5\u4FDD\u7559\uFF0C\u7EDD\u4E0D\u4EA7\u751F\u91CD\u590D\u884C\uFF09\uFF1B\u6CA1\u6709 \u2192 \u65B0\u5EFA\u3002
22199
+ \u26A0\uFE0F locale \u952E\u63A5\u53D7\u77ED\u7801\uFF08en/zh/ms/id\uFF09\u6216 BCP-47 \u533A\u57DF\u7801\uFF08en-US/zh-CN/ms-MY/id-ID\uFF09\uFF0C\u5DE5\u5177\u7EDF\u4E00\u5F52\u4E00\u5316\u4E3A BCP-47 \u843D\u5E93\u2014\u2014
22200
+ storefront \u5BF9\u7FFB\u8BD1\u63A5\u53E3\u4E00\u5F8B\u8BF7\u6C42 BCP-47\u3001\u4E0E\u5E93\u4E2D locale_code \u7CBE\u786E\u5339\u914D\uFF1B\u5386\u53F2\u6309\u77ED\u7801\u843D\u5E93\u7684\u884C\u4F1A\u88AB\u672C\u6B21\u8C03\u7528**\u6539\u7801\u4FEE\u590D**\u3002
22201
+ \u7528\u9014\uFF1A\u5546\u54C1\u521B\u5EFA\u540E\u8865\u7FFB\u8BD1/\u6539\u7FFB\u8BD1/\u4FEE\u590D\u591A\u8BED\u8A00\u3002\u9ED8\u8BA4 dry_run\uFF08confirm=false\uFF09\u9884\u89C8\u5C06\u521B\u5EFA/\u66F4\u65B0\u54EA\u4E9B\u884C\u3002`,
22202
+ schema: {
22203
+ reference: external_exports.string().default("product").describe("\u5B9E\u4F53\u7C7B\u578B\uFF0C\u5546\u54C1\u7528 product"),
22204
+ reference_id: external_exports.string().describe("\u5B9E\u4F53 id\uFF1B\u5546\u54C1\u53EF\u4F20 handle\uFF08\u81EA\u52A8\u89E3\u6790\u4E3A id\uFF09"),
22205
+ translations: external_exports.record(external_exports.string(), external_exports.object({
22206
+ title: external_exports.string().optional(),
22207
+ subtitle: external_exports.string().optional(),
22208
+ description: external_exports.string().optional()
22209
+ })).describe('locale \u2192 \u7FFB\u8BD1\u5B57\u6BB5\uFF0C\u5982 {"zh-CN":{"title":"...","description":"..."}}\uFF1B\u77ED\u7801/\u533A\u57DF\u7801\u5747\u53EF'),
22210
+ confirm: external_exports.boolean().default(false)
22211
+ },
22212
+ async run(input, { client: client2 }) {
22213
+ let refId = String(input.reference_id).trim();
22214
+ if (input.reference === "product" && !/^prod_[a-z0-9]+$/i.test(refId)) {
22215
+ const id = await client2.resolveProductId(refId);
22216
+ if (!id) return `\u9519\u8BEF\uFF1A\u627E\u4E0D\u5230\u5546\u54C1 "${refId}"\uFF08\u8BF7\u4F20\u5546\u54C1 id \u6216\u6709\u6548 handle\uFF09`;
22217
+ refId = id;
22218
+ }
22219
+ const entries = Object.entries(input.translations).map(([locale, t]) => ({
22220
+ locale_code: canonLocaleKey(locale),
22221
+ translations: Object.fromEntries(
22222
+ Object.entries(t ?? {}).filter(([, v]) => v !== void 0 && v !== null)
22223
+ )
22224
+ }));
22225
+ if (!entries.length) return "\u9519\u8BEF\uFF1Atranslations \u4E3A\u7A7A\uFF0C\u65E0\u4E8B\u53EF\u505A\u3002";
22226
+ const plan = await planTranslationUpsert(client2, input.reference, refId, entries);
22227
+ if (!input.confirm) {
22228
+ return {
22229
+ dry_run: true,
22230
+ reference: input.reference,
22231
+ reference_id: refId,
22232
+ will_create: plan.create,
22233
+ will_update: plan.update,
22234
+ matched_existing: plan.matched,
22235
+ note: "\u672A confirm\uFF0C\u4EC5\u9884\u89C8\u3002\u786E\u8BA4\u8BF7\u8BBE confirm=true\u3002"
22236
+ };
22237
+ }
22238
+ const { created, updated, result } = await upsertTranslations(client2, input.reference, refId, entries);
22239
+ return { created, updated, result };
22240
+ }
22241
+ }
22242
+ ];
22243
+
22130
22244
  // src/tools/products.ts
22245
+ function canonicalizeTranslations(translations) {
22246
+ const out = {};
22247
+ for (const [key, val] of Object.entries(translations ?? {})) {
22248
+ out[normalizeLocale(key) ?? key] = val;
22249
+ }
22250
+ return out;
22251
+ }
22131
22252
  var PRODUCT_STATUS = [
22132
22253
  "draft",
22133
22254
  "published",
@@ -22282,10 +22403,10 @@ SKU / \u89C4\u683C\uFF1Asku \u5199\u5165\u9ED8\u8BA4\u53D8\u4F53\uFF1Bmetadata \
22282
22403
  if (!HANDLE_RE.test(handle)) {
22283
22404
  return `\u9519\u8BEF\uFF1A\u5F52\u4E00\u5316\u540E\u7684 Handle "${handle}" \u4ECD\u975E\u6CD5\uFF08\u53EA\u80FD\u5C0F\u5199\u5B57\u6BCD/\u6570\u5B57/\u8FDE\u5B57\u7B26\uFF09\u3002\u8BF7\u68C0\u67E5\u8F93\u5165\u3002`;
22284
22405
  }
22285
- const provided = input.translations ? Object.keys(input.translations) : [];
22286
- const missing = config3.requiredLocales.filter(
22287
- (l) => !provided.includes(l) || !input.translations?.[l]?.title
22288
- );
22406
+ const canon = (l) => normalizeLocale(l) ?? l;
22407
+ const normTranslations = canonicalizeTranslations(input.translations);
22408
+ const provided = Object.keys(normTranslations);
22409
+ const missing = config3.requiredLocales.map(canon).filter((l) => !provided.includes(l) || !normTranslations[l]?.title);
22289
22410
  let channelIds = [];
22290
22411
  if (input.sales_channels?.length) {
22291
22412
  const ch = await client2.listSalesChannels({ limit: 100 });
@@ -22303,7 +22424,22 @@ SKU / \u89C4\u683C\uFF1Asku \u5199\u5165\u9ED8\u8BA4\u53D8\u4F53\uFF1Bmetadata \
22303
22424
  if (!hit) return `\u9519\u8BEF\uFF1A\u627E\u4E0D\u5230\u96C6\u5408 "${collectionId}"\uFF08\u7528 list_collections \u67E5\u770B\u53EF\u7528\u96C6\u5408\uFF09`;
22304
22425
  collectionId = hit.id;
22305
22426
  }
22427
+ let handleCollision = null;
22428
+ try {
22429
+ const existing = await client2.listProducts({ handle, limit: 5 });
22430
+ const dup = (existing?.products || []).find((p) => p.handle === handle);
22431
+ if (dup) handleCollision = { id: dup.id, title: dup.title, status: dup.status };
22432
+ } catch {
22433
+ }
22306
22434
  const warnings = [];
22435
+ if (handleCollision) {
22436
+ warnings.push(
22437
+ `handle \u51B2\u7A81\uFF1A\u5DF2\u5B58\u5728\u540C handle \u5546\u54C1\u300C${handleCollision.title}\u300D(id: ${handleCollision.id}, status: ${handleCollision.status})\u3002\u82E5\u662F\u5728\u91CD\u65B0\u5F55\u5165\u540C\u4E00\u5546\u54C1\uFF0C\u8BF7\u6539\u7528 update_product \u4FEE\u6539\u8BE5\u5546\u54C1\uFF0C\u800C\u975E create_product\u3002`
22438
+ );
22439
+ if (input.confirm) {
22440
+ return `\u9519\u8BEF\uFF1Ahandle "${handle}" \u5DF2\u88AB\u5546\u54C1 ${handleCollision.id}\uFF08\u300C${handleCollision.title}\u300D\uFF09\u5360\u7528\uFF0C\u65E0\u6CD5\u91CD\u590D\u521B\u5EFA\u3002\u82E5\u8FD9\u662F\u540C\u4E00\u5546\u54C1\u7684\u91CD\u65B0\u5F55\u5165\uFF0C\u8BF7\u7528 update_product(id=${handleCollision.id}) \u66F4\u65B0\uFF1B\u82E5\u662F\u4E0D\u540C\u5546\u54C1\uFF0C\u8BF7\u6362\u4E00\u4E2A handle \u6216\u6807\u9898\u3002`;
22441
+ }
22442
+ }
22307
22443
  if (input.manage_inventory !== false && !input.initial_stock) {
22308
22444
  warnings.push(
22309
22445
  "manage_inventory=true \u4F46\u672A\u7ED9 initial_stock\uFF1A\u521B\u5EFA\u540E\u4E3A 0 \u5E93\u5B58\uFF0C\u524D\u7AEF\u5C06\u663E\u793A\u7F3A\u8D27\u3002\u6B63\u5F0F\u4E0A\u67B6\u524D\u5FC5\u987B\u8C03\u7528 set_product_stock \u5EFA\u4ED3\u3002"
@@ -22327,9 +22463,12 @@ SKU / \u89C4\u683C\uFF1Asku \u5199\u5165\u9ED8\u8BA4\u53D8\u4F53\uFF1Bmetadata \
22327
22463
  weight: input.weight,
22328
22464
  manage_inventory: input.manage_inventory,
22329
22465
  initial_stock: input.initial_stock,
22466
+ // 实际将写入翻译表的 locale_code(已归一化为 BCP-47,见 canonicalizeTranslations)
22467
+ translation_locales: provided,
22330
22468
  missing_translations: missing
22331
22469
  },
22332
22470
  warnings,
22471
+ handle_collision: handleCollision,
22333
22472
  note: "\u672A confirm\uFF0C\u4EC5\u9884\u89C8\u3002\u786E\u8BA4\u8BF7\u8BBE confirm=true\u3002"
22334
22473
  };
22335
22474
  }
@@ -22376,15 +22515,17 @@ SKU / \u89C4\u683C\uFF1Asku \u5199\u5165\u9ED8\u8BA4\u53D8\u4F53\uFF1Bmetadata \
22376
22515
  const data = await client2.createProduct(payload);
22377
22516
  const productId = data?.product?.id || data?.id;
22378
22517
  let translationResult = null;
22379
- if (input.translations && Object.keys(input.translations).length) {
22380
- const create = Object.entries(input.translations).map(([locale, t]) => ({
22381
- reference: "product",
22382
- reference_id: productId,
22383
- locale_code: locale,
22384
- translations: { title: t?.title, description: t?.description }
22385
- }));
22518
+ if (Object.keys(normTranslations).length) {
22386
22519
  try {
22387
- translationResult = await client2.batchTranslations(create);
22520
+ translationResult = await upsertTranslations(
22521
+ client2,
22522
+ "product",
22523
+ productId,
22524
+ Object.entries(normTranslations).map(([locale, t]) => ({
22525
+ locale_code: locale,
22526
+ translations: { title: t?.title, description: t?.description }
22527
+ }))
22528
+ );
22388
22529
  } catch (e) {
22389
22530
  return { created: data, warning_translations: `\u7FFB\u8BD1\u5199\u5165\u5931\u8D25 \u2014\u2014 ${e?.message || e}` };
22390
22531
  }
@@ -22435,6 +22576,7 @@ metadata \u4E3A**\u5408\u5E76**\u8BED\u4E49\uFF1A\u65B0\u952E\u8986\u76D6\u540C\
22435
22576
  confirm: external_exports.boolean().default(false)
22436
22577
  },
22437
22578
  async run(input, { client: client2 }) {
22579
+ const productId = await client2.resolveProductId(input.id) || String(input.id).trim();
22438
22580
  const patch = {};
22439
22581
  if (input.title) patch.title = input.title;
22440
22582
  if (input.handle) {
@@ -22462,22 +22604,25 @@ metadata \u4E3A**\u5408\u5E76**\u8BED\u4E49\uFF1A\u65B0\u952E\u8986\u76D6\u540C\
22462
22604
  if (input[k]) patch[k] = input[k];
22463
22605
  }
22464
22606
  if (input.metadata && Object.keys(input.metadata).length) {
22465
- const cur = await client2.getProduct(input.id).catch(() => null);
22607
+ const cur = await client2.getProduct(productId).catch(() => null);
22466
22608
  const existingMeta = (cur?.product ?? cur)?.metadata || {};
22467
22609
  patch.metadata = { ...existingMeta, ...input.metadata };
22468
22610
  }
22469
- if (!input.confirm) return { dry_run: true, will_update: patch };
22470
- const data = await client2.updateProduct(input.id, patch);
22611
+ const normTranslations = canonicalizeTranslations(input.translations);
22612
+ if (!input.confirm) return { dry_run: true, will_update: { ...patch, translation_locales: Object.keys(normTranslations) } };
22613
+ const data = await client2.updateProduct(productId, patch);
22471
22614
  let translationResult = null;
22472
- if (input.translations && Object.keys(input.translations).length) {
22473
- const create = Object.entries(input.translations).map(([locale, t]) => ({
22474
- reference: "product",
22475
- reference_id: input.id,
22476
- locale_code: locale,
22477
- translations: { title: t?.title, description: t?.description }
22478
- }));
22615
+ if (Object.keys(normTranslations).length) {
22479
22616
  try {
22480
- translationResult = await client2.batchTranslations(create);
22617
+ translationResult = await upsertTranslations(
22618
+ client2,
22619
+ "product",
22620
+ productId,
22621
+ Object.entries(normTranslations).map(([locale, t]) => ({
22622
+ locale_code: locale,
22623
+ translations: { title: t?.title, description: t?.description }
22624
+ }))
22625
+ );
22481
22626
  } catch (e) {
22482
22627
  return { updated: data, warning_translations: `\u7FFB\u8BD1\u5199\u5165\u5931\u8D25 \u2014\u2014 ${e?.message || e}` };
22483
22628
  }
@@ -22505,7 +22650,8 @@ metadata \u4E3A**\u5408\u5E76**\u8BED\u4E49\uFF1A\u65B0\u952E\u8986\u76D6\u540C\
22505
22650
  variant_id: input.variant_id || "(\u7B2C\u4E00\u4E2A\u53D8\u4F53)"
22506
22651
  };
22507
22652
  }
22508
- const prod = await client2.getProduct(input.product_id).catch(() => null);
22653
+ const productId = await client2.resolveProductId(input.product_id) || String(input.product_id).trim();
22654
+ const prod = await client2.getProduct(productId).catch(() => null);
22509
22655
  const product = prod?.product ?? prod;
22510
22656
  if (!product) return `\u9519\u8BEF\uFF1A\u627E\u4E0D\u5230\u5546\u54C1 ${input.product_id}`;
22511
22657
  const variants = product.variants || [];
@@ -22513,7 +22659,7 @@ metadata \u4E3A**\u5408\u5E76**\u8BED\u4E49\uFF1A\u65B0\u952E\u8986\u76D6\u540C\
22513
22659
  const target = input.variant_id ? variants.find((v) => v.id === input.variant_id) : variants[0];
22514
22660
  if (!target) return `\u9519\u8BEF\uFF1A\u627E\u4E0D\u5230\u76EE\u6807\u53D8\u4F53\uFF08\u6307\u5B9A ${input.variant_id} \u6216\u7B2C\u4E00\u4E2A\u53D8\u4F53\uFF09\u3002`;
22515
22661
  const data = await client2.updateProductPrices(
22516
- input.product_id,
22662
+ productId,
22517
22663
  target.id,
22518
22664
  input.prices.map((p) => ({ amount: p.amount, currency_code: p.currency_code }))
22519
22665
  );
@@ -22529,7 +22675,8 @@ metadata \u4E3A**\u5408\u5E76**\u8BED\u4E49\uFF1A\u65B0\u952E\u8986\u76D6\u540C\
22529
22675
  },
22530
22676
  async run(input, { client: client2 }) {
22531
22677
  if (!input.confirm) return { dry_run: true, will_set_status: "archived" };
22532
- return client2.updateProduct(input.id, { status: "archived" });
22678
+ const productId = await client2.resolveProductId(input.id) || String(input.id).trim();
22679
+ return client2.updateProduct(productId, { status: "archived" });
22533
22680
  }
22534
22681
  },
22535
22682
  {
@@ -22824,6 +22971,7 @@ ${r.snippet}`).join("\n\n");
22824
22971
  // src/tools/index.ts
22825
22972
  var toolDefs = [
22826
22973
  ...productTools,
22974
+ ...translationTools,
22827
22975
  ...stockTools,
22828
22976
  ...opsTools,
22829
22977
  ...docTools
@@ -22847,7 +22995,7 @@ var config2 = loadConfig();
22847
22995
  var auth = new AuthManager(config2);
22848
22996
  var client = new MedusaAdminClient(config2, auth);
22849
22997
  var ctx = { client, config: config2 };
22850
- var server = new McpServer({ name: "derlux-admin", version: "0.1.4" });
22998
+ var server = new McpServer({ name: "derlux-admin", version: "0.1.5" });
22851
22999
  for (const def of toolDefs) {
22852
23000
  server.tool(def.name, def.description, def.schema, async (args) => {
22853
23001
  try {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sea-ecommerce/admin-mcp-server",
3
- "version": "0.1.4",
3
+ "version": "0.1.5",
4
4
  "private": false,
5
5
  "type": "module",
6
6
  "description": "让运营同学用本地 AI agent(Codex / Workbuddy / Claude / Cursor / CodeBuddy)运营 Medusa 后台的 MCP server。",
package/skill/SKILL.md CHANGED
@@ -31,8 +31,11 @@ description: 运营使用本地 AI agent(derlux-admin MCP server)操作 DERL
31
31
  - 商品标题/描述的**翻译**(`create_product` 的 `translations`、后端 `/admin/translations/batch`):用**简码** `en` / `zh` / `ms` / `id`。store `/store/products?locale=zh` 即回退到 zh 翻译。
32
32
  - 商品详情页**富内容块**(`create_product_content` / `get_product_content` 的 `locale`):推荐用**区域码** `en-US` / `zh-CN` / `ms-MY` / `id-ID`(与前端 `useProductContent` 的 localeMap 一致);**简码 `en`/`zh`/`ms`/`id` 也会被工具自动归一化为区域码**,两种写法都能命中,但请统一用区域码避免歧义。
33
33
  8. **详情页「富内容」区靠 `create_product_content`,不是 `description`**:storefront 详情页下方大块内容由 `product_content` 模块渲染(`richtext`/`specs`/`image_gallery` 等)。只填 `product.description` 不会显示在那里——录入完整商品时必须再建一份 `product_content`(建议每语言一份)。`get_product` 默认不含该字段,核对请用 `get_product_content`。
34
+ - ⚠️ **storefront 对富内容按 `locale` 精确匹配、无回退(实测)**:`/store/product-contents?handle=&locale=zh-CN` 只取 `locale_code===zh-CN` 且 `status=published` 的那一行,**缺该语言行即返回 `null`=该语言详情页空白,不会回退到英文**。故四语言(`en-US/zh-CN/ms-MY/id-ID`)都要各建一份 `product_content`,漏任一语言则该语言详情页空。
35
+ - ⚠️ **富内容块字段名易错**:`richtext` 用 `data.html`(**不是** `data.content`)、`specs` 用 `data.rows`(**不是** `data.specs`);`image_gallery` 用 `data.images`。写错字段名块会渲染为空。
34
36
  9. **不建库存 = 前端缺货**:变体 `manage_inventory` 默认 `true`,但新商品默认 0 库存——`create_product` 时必须给 `initial_stock`(如 `{quantity:100}`,默认写 Main Warehouse),或对已有商品调 `set_product_stock`,否则前台显示缺货、无法下单。
35
37
  10. **多语言核对用 `get_translations`**:`get_product` 默认字段**不含** translations,不要凭它为空就判断没翻译。用 `get_translations`(reference_id=商品id或handle) 读取实际写入的各语言 title/description。
38
+ 11. **创建前先查重 + 警惕 handle 归一化产生不同结果**:运营给的名字经归一化(小写、`空格/下划线/点→连字符`)后,**不同写法可能落到不同 handle**(例:`DR CYJ X`→`dr-cyj-x`、`DR.CYJ X`→`drcyj-x`),从而**静默建出一个近乎重复的空商品**(本包实际踩过:`dr-cyj-hair-filler-hair-grow` 与 `drcyj-hair-filler-hair-grow` 是两个商品)。故 `create_product` 前必先用 `list_products`/`get_product`(按相近名或 handle 搜一遍)确认无同款;已存在则转「补齐/修改」流程(`update_product` / `set_translations` / `create_product_content` / `set_product_stock`),不要重复 `create_product`。
36
39
 
37
40
  ## 工具速查表
38
41
 
@@ -43,6 +46,7 @@ description: 运营使用本地 AI agent(derlux-admin MCP server)操作 DERL
43
46
  | `list_sales_channels` | 读 | 否 | 直接返回(渠道 id/名称,建商品前确认用)|
44
47
  | `list_collections` | 读 | 否 | 直接返回 |
45
48
  | `get_translations` | 读 | 否 | 直接返回(某商品各语言的 title/description,多语言核对唯一可靠途径)|
49
+ | `set_translations` | 写 | 是(`confirm=true` 才写)| **upsert** 多语言翻译(title/subtitle/description):同语言已存在→更新(字段合并、绝不重复行),否则新建;短码/区域码统一归一化为 BCP-47 落库,并**改码修复**历史短码行;`confirm=false` 仅预览 |
46
50
  | `list_stock_locations` | 读 | 否 | 直接返回 |
47
51
  | `get_inventory_levels` | 读 | 否 | 直接返回(location_id 可选:按仓库列库存条目与水平;q 按 sku/title 过滤)|
48
52
  | `set_product_stock` | 写 | 是(`confirm=true` 才写)| 幂等建仓:自动找/建库存条目 + 写仓库数量 + manage_inventory;`confirm=false` 仅预览 |
@@ -124,6 +128,12 @@ description: 运营使用本地 AI agent(derlux-admin MCP server)操作 DERL
124
128
  - `faq`:`data.{ items: {question,answer}[] }`
125
129
  - `specs`:`data.{ rows: {label,value}[] }`
126
130
  - `confirm=false` 返回预览(含块数量与类型);`confirm=true` 才写入。
131
+ - **`set_translations`**:**创建或更新(upsert)**某实体的多语言翻译(title/subtitle/description),走 `/admin/translations/batch`。用于商品创建后补/改翻译、修复多语言。
132
+ - `reference`(string,默认 `product`);`reference_id`(string,**必填**,商品可传 handle,自动解析为 id);`translations`(`record<locale,{title?,subtitle?,description?}>`,**必填**,如 `{"zh-CN":{"title":"…","description":"…"},"ms-MY":{…},"id-ID":{…}}`);`confirm`(默认 false)。
133
+ - **upsert 语义**:目标语言已有翻译 → 更新(字段**合并**,调用里没给的旧字段保留,**绝不产生重复行**);没有 → 新建。
134
+ - **locale 归一化**:key 接受短码(`en`/`zh`/`ms`/`id`)或 BCP-47 区域码(`en-US`/`zh-CN`/`ms-MY`/`id-ID`),工具统一归一化为 **BCP-47** 落库。storefront 对翻译接口一律请求 BCP-47、与库中 `locale_code` 精确匹配——历史按短码(如 `zh`)落库的旧行会被本次调用**改码修复**(update 时把 `locale_code` 顺带改成区域码,见 `matched_existing`)。请统一用区域码避免歧义。
135
+ - `confirm=false` 返回 `{dry_run:true, will_create, will_update, matched_existing, note}` 预览;`confirm=true` 才写入并返回 `{created, updated, result}`。
136
+ - 与 `create_product`/`update_product` 内嵌的 `translations` 区别:本工具对**已存在商品**逐语言 upsert 更精准(合并而非整表覆盖、不重复行),补翻译/改翻译优先用它。
127
137
 
128
138
  ### 写 —— 仓库与库存
129
139
  - **`create_stock_location`**:新增仓库(StockLocation)。
@@ -138,12 +148,18 @@ description: 运营使用本地 AI agent(derlux-admin MCP server)操作 DERL
138
148
 
139
149
  ## 推荐操作流程(SOP)
140
150
 
141
- **创建商品**
142
- 1. 先收集:标题、handle(或告知由标题生成)、价格(主单位)、四语言标题、状态、图片、规格、重量、初始库存数量、目标渠道、详情页富文本内容(按语言)。
143
- 2. 调用 `create_product`(`confirm=false`)→ 展示预览与「缺语言 / 缺货」告警。
144
- 3. 提醒运营补全缺失语言与 `initial_stock`;确认无误后调用 `create_product`(`confirm=true`,`sales_channels` 里带上目标渠道,必要时 `acknowledge_missing_languages=true`)。创建即完成:图片上传、SKU、翻译、渠道挂载、建库存。
145
- 4. **`create_product_content`(`confirm=true`,逐语言 `en-US/zh-CN/ms-MY/id-ID`)** → 写详情页富内容块(richtext 描述 + specs 规格 + image_gallery 图集)。否则详情页主体空白。
146
- 5. 用 `get_translations` + `get_product_content` 核对多语言与富内容;把新商品 id/handle 告知运营。
151
+ **创建商品(稳妥 6 步,含查重与上线核对)**
152
+ 1. **查重 + 收料**:先 `list_products`/`get_product` 按相近名或 handle 搜一遍,确认无已存在同款(handle 归一化后不同写法可能产生**不同** handle → 静默重复商品,见硬规则 11);若已存在,转「修改/补齐」流程而非新建。同时收集:标题、价格(主单位)、四语言标题/描述、图片、规格、重量、初始库存、目标渠道、各语言富文本。
153
+ 2. **预览**:`create_product`(`confirm=false`)→ 把「缺语言 / 缺货 / 是否疑似重复」读给运营。
154
+ 3. 补全缺失语言与 `initial_stock`;运营确认后 `create_product`(`confirm=true`,带 `sales_channels`、`initial_stock`,正式上架加 `status: "published"`,必要时 `acknowledge_missing_languages=true`)。此步即完成:图片上传、SKU、四语言翻译、渠道挂载、建库存。
155
+ 4. **`create_product_content`(`confirm=true`,逐语言 `en-US/zh-CN/ms-MY/id-ID` 各建一份)** → 写详情页富内容(`richtext` + `specs` + `image_gallery`)。storefront 对富内容**按语言精确匹配、无回退**,漏任一语言则该语言详情页空白(见硬规则 8)。
156
+ 5. **(可选)** 仅补/改已存在语言的 title/description 时用 `set_translations`(upsert/合并不重复、自动把历史短码改码为区域码,见硬规则 7 / 该工具说明)。
157
+ 6. **上线核对(go-live checklist)——全绿才算交付完成**:
158
+ - **前台可见**:`get_product` 看 `status=published` 且已挂销售渠道;
159
+ - **多语言齐全**:`get_translations` 看 4 条 locale 都有 title/description;
160
+ - **各语言详情页富内容齐全**:`get_product_content` 逐语言各取一次,4 语言均非空(无回退,见硬规则 8);
161
+ - **有货可下单**:`get_inventory_levels`(目标仓)quantity>0。
162
+ 全绿后把新商品 id/handle 告知运营。
147
163
 
148
164
  **修改 / 下架商品**
149
165
  1. `get_product` 或 `list_products` 确认目标 id。
@@ -160,12 +176,14 @@ description: 运营使用本地 AI agent(derlux-admin MCP server)操作 DERL
160
176
  ## 常见任务示例(运营怎么说 / 你怎么调)
161
177
 
162
178
  - 「下架 handle 为 `summer-serum` 的商品」→ `get_product`(id_or_handle=`summer-serum`) 取 id → `deactivate_product`(id, confirm=false 预览 → 确认后 confirm=true)。
179
+ - 「录入 DR.CYJ Hair Filler,价格 420,4 图,补全多语言」→ **先** `list_products`(q=`hair filler`) / `get_product`(handle=候选) 查重;若已存在同款 → 走 `update_product`/`set_translations`/`create_product_content`/`set_product_stock` 补齐,**不要新建**。确认无同款后再按「创建商品 6 步」执行。
163
180
  - 「给 A 商品补 100 件库存」→ `set_product_stock`(product_id=A 的 id, quantity=100, confirm=false 预览 → 确认后 confirm=true;默认写 Main Warehouse,指定仓传 location_id)。
164
181
  - 「查一下最近 10 个待发货订单」→ `list_orders`(status=`pending`, limit=10)。
165
182
  - 「后台怎么新建优惠券」→ `ask_usage`(question=`如何新建优惠券`)。
166
183
  - 「列出所有退货请求」→ `list_return_requests`()。
167
184
  - 「把这份抓取的 Lazada 商品录入:标题 DR.CYJ Hair Filler、价格 420 MYR、4 张本地图、SKU `4820...`、规格存 metadata、补全中/马来/印尼语翻译」→ 先整理数据,`create_product`(`confirm=false` 看预览含 images/sku/metadata/缺语言/缺货告警 → 确认后 `confirm=true`,带 `sales_channels: ["MY Storefront"]`、`initial_stock: {quantity: 100}`、`weight: 150`,必要时 `acknowledge_missing_languages=true`)。工具会自动上传图片、写 SKU/metadata/物理字段、四语言翻译、渠道挂载并建库存。再 `create_product_content`(`locale=en-US/zh-CN/ms-MY/id-ID`, `confirm=true`) 逐语言写富内容块(richtext 描述 + specs 规格 + image_gallery 图集),最后 `get_translations` + `get_product_content` 核对。
168
185
  - 「核对某商品多语言是否齐全」→ `get_translations`(reference_id=商品 id 或 handle),看 4 条 locale 是否都有 title/description。
186
+ - 「给某商品补全 / 更新中·马来·印尼语标题描述」→ `set_translations`(reference_id=商品 id 或 handle, `translations={"zh-CN":{title,description},"ms-MY":{…},"id-ID":{…}}`, confirm=false 预览 → confirm=true)。同语言已存在则合并更新、不重复行;旧短码行会被自动改码为区域码。
169
187
  - 「让这个商品在前端能搜到」→ `get_product` 取 id → `add_product_to_sales_channel`(`product_ids=[id]`, `confirm=true`)。
170
188
  - 「补全某商品中文详情页内容」→ `create_product_content`(`product_id`, `locale=zh-CN`, `blocks=[{type:'richtext',data:{html:'...'}},{type:'specs',data:{rows:[...]}}]`, `confirm=true`)。可用 `get_product_content` 先核对现状。
171
189