webcake-storefront-mcp 1.31.8 → 1.31.9

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/dist/api.js CHANGED
@@ -1,4 +1,7 @@
1
1
  const DEFAULT_TIMEOUT = 15000;
2
+ /** Stamped on every page this MCP server creates (backend column `pages.by_ai`),
3
+ * so the builder can flag AI-authored pages. Mirrors the landing-page convention. */
4
+ export const BY_AI_MARKER = "mcp";
2
5
  export class WebcakeCmsApi {
3
6
  baseUrl;
4
7
  token;
@@ -149,13 +152,17 @@ export class WebcakeCmsApi {
149
152
  }
150
153
  /** Create a page. The backend creates the page AND its source in one call, so `source`
151
154
  * is REQUIRED and must be a JSON string (stringified here if an object is passed).
152
- * `slug`/`is_homepage` are NOT applied at create — set them afterwards via updatePage. */
155
+ * `slug`/`is_homepage` are NOT applied at create — set them afterwards via updatePage.
156
+ * Every page born here is AI-authored, so we stamp `by_ai` (persisted to `pages.by_ai`)
157
+ * and the builder shows an "AI" tag next to the page name. */
153
158
  createPage(params, opts) {
154
159
  const body = { ...params };
155
160
  if (body.source != null && typeof body.source !== "string")
156
161
  body.source = JSON.stringify(body.source);
157
162
  if (body.source == null)
158
163
  body.source = JSON.stringify({ sections: [] });
164
+ if (body.by_ai == null)
165
+ body.by_ai = BY_AI_MARKER;
159
166
  return this.request("POST", `/api/v1/site/${this.siteId}/page`, { body, timeout: opts?.timeout });
160
167
  }
161
168
  updatePage(pageId, params) {
@@ -301,6 +301,14 @@ Rule of thumb: if the page shows products, a cart, customer/order data, or blog
301
301
  set \`type\` accordingly so the binding source is turned on. A binding target like
302
302
  \`product::product_price\` REQUIRES its page to be the matching type.
303
303
 
304
+ ### ONE page per singleton type — only \`custom\` is unlimited
305
+ A site has exactly ONE homepage (\`main\`), ONE \`error\` page and ONE \`maintain\` page; a second
306
+ one only shadows the first, so \`create_page\` / \`build_page\` / \`start_page_draft\` REFUSE it and
307
+ hand you the existing \`page_id\` — edit that page (replace_page_source / add_section /
308
+ update_page) instead of creating another. \`store\`/\`member\`/\`blog\` may have several pages
309
+ (cart + checkout + collections…, login + register…) but each SLUG is unique per site, so those
310
+ are refused on a duplicate slug. \`custom\` pages are unlimited as long as their slugs differ.
311
+
304
312
  ## Build the WHOLE storefront — every page to the SAME standard (NOT just the home page)
305
313
  A shop is multi-page. Build EACH page to a real e-commerce standard with the same palette,
306
314
  spacing and header/footer — never leave the home page rich and the rest as bare stubs.
@@ -1,4 +1,11 @@
1
1
  [
2
+ {
3
+ "v": "1.31.9",
4
+ "d": "07/09/2026",
5
+ "type": "Added",
6
+ "en": "Pages created by create_page, build_page, and commit_page_draft are now stamped with by_ai: \"mcp\" (persisted to the backend pages.by_ai column) so…",
7
+ "vi": "Các trang được tạo bởi create_page, build_page và commit_page_draft nay được đánh dấu by_ai: \"mcp\" (lưu vào cột pages.by_ai ở backend) để builder có…"
8
+ },
2
9
  {
3
10
  "v": "1.31.8",
4
11
  "d": "29/06/2026",
@@ -33,12 +40,5 @@
33
40
  "type": "Added",
34
41
  "en": "The guide returned by get_http_function and get_site_custom_code now includes a verified end-to-end \"Custom data TABLES (collections)\" section…",
35
42
  "vi": "Hướng dẫn trả về bởi get_http_function và get_site_custom_code nay bổ sung phần \"Custom data TABLES (collections)\" đã được xác minh thực tế, ghi lại…"
36
- },
37
- {
38
- "v": "1.31.3",
39
- "d": "26/06/2026",
40
- "type": "Added",
41
- "en": "New update_collection_columns tool reads the current collection schema and PATCHes it with the system columns plus the provided custom columns,…",
42
- "vi": "Tool mới update_collection_columns đọc schema hiện tại của collection rồi PATCH lại với các cột hệ thống cộng các cột tùy chỉnh được cung cấp, cho…"
43
43
  }
44
44
  ]
@@ -38,6 +38,59 @@ export const PAGE_TYPE_FLAG = {
38
38
  error: "use_error", maintain: "use_maintain",
39
39
  };
40
40
  export const PAGE_KINDS = ["main", "store", "member", "blog", "custom", "error", "maintain"];
41
+ /** Page kinds a site only ever has ONE of. The homepage is `main`, and the storefront
42
+ * resolves exactly one `error` / `maintain` page — a second one just shadows the first,
43
+ * so creating it is always a mistake. `store` / `member` / `blog` are NOT here: a site
44
+ * legitimately has several of each (cart + checkout + collections…, login + register…),
45
+ * they are kept unique by SLUG instead. `custom` is unlimited. */
46
+ export const SINGLETON_PAGE_KINDS = ["main", "error", "maintain"];
47
+ function briefPage(p) {
48
+ return { id: p.id, name: p.name, slug: p.slug ?? null, type: p.type ?? null, is_homepage: !!p.is_homepage };
49
+ }
50
+ /** Guard run before CREATING a page, so the agent edits the existing page instead of
51
+ * piling up duplicates the storefront will never route to. Two rules:
52
+ * 1. singleton kinds (main/error/maintain, incl. `is_homepage`) — one per site;
53
+ * 2. every other kind — the slug must be free (the backend has a (site_id, slug)
54
+ * unique index, so a duplicate slug fails there anyway, just with a vaguer error).
55
+ * `custom` pages stay unlimited as long as their slugs differ.
56
+ * Returns null when the page may be created. A failed lookup never blocks the create. */
57
+ export async function checkPageCreateConflict(api, { kind, slug, is_homepage }) {
58
+ let pages;
59
+ try {
60
+ const res = await api.listPages();
61
+ pages = (res && res.data) || res || [];
62
+ }
63
+ catch {
64
+ return null;
65
+ }
66
+ if (!Array.isArray(pages))
67
+ return null;
68
+ const singleton = is_homepage ? "main" : kind;
69
+ if (singleton && SINGLETON_PAGE_KINDS.includes(singleton)) {
70
+ const typeNum = PAGE_TYPE_NUM[singleton];
71
+ const found = pages.find((p) => (singleton === "main" ? !!p.is_homepage || p.type === typeNum : p.type === typeNum));
72
+ if (found) {
73
+ return {
74
+ error: `This site already has a '${singleton}' page ("${found.name}"), and only one is allowed — ` +
75
+ `only 'custom' pages can be created repeatedly. Edit page ${found.id} instead ` +
76
+ `(replace_page_source / add_section / update_page), or create a 'custom' page.`,
77
+ existing_page: briefPage(found),
78
+ };
79
+ }
80
+ }
81
+ const cleanSlug = normalizeSlug(slug ?? undefined);
82
+ if (cleanSlug) {
83
+ const found = pages.find((p) => p.slug === cleanSlug);
84
+ if (found) {
85
+ return {
86
+ error: `This site already has a page at slug "${cleanSlug}" ("${found.name}"). Slugs are unique per site — ` +
87
+ `edit page ${found.id} instead (replace_page_source / add_section / update_page), or pick another slug.`,
88
+ existing_page: briefPage(found),
89
+ };
90
+ }
91
+ }
92
+ return null;
93
+ }
41
94
  /** Build the page.settings.seo block from simple inputs (the real shape; tokens like
42
95
  * {{name_page}} / {{name_site}} are resolved by the storefront). */
43
96
  export function buildPageSeo(seo = {}) {
@@ -118,7 +171,8 @@ Example children: [{ "type":"container", "children":[{"type":"image","opts":{...
118
171
  }));
119
172
  server.tool("build_page", `Create a brand-new page AND set its full content source in one step.
120
173
  Two-step safety: call with dry_run=true (default) to validate and preview, then dry_run=false to actually create + save.
121
- The source must be { sections: [...] } — build sections with new_section. Validation errors block the real save.`, {
174
+ The source must be { sections: [...] } — build sections with new_section. Validation errors block the real save.
175
+ ONE page per site for type main (homepage) / error / maintain, and slugs are unique per site: a duplicate is refused (dry_run reports blocked:true) and you get the existing page_id to edit instead. Only 'custom' pages can be created over and over.`, {
122
176
  name: z.string().describe("Page name"),
123
177
  slug: z.string().describe("URL slug WITHOUT a leading slash, e.g. 'about', 'collections', 'cart'. A leading '/' is stripped automatically (the storefront matches the bare path segment, so '/cart' would 404). Store pages MUST use the conventional slugs: category='collections', product detail='products', cart='cart', checkout='checkout', thank-you='complete'. The homepage needs no slug (pass is_homepage:true)."),
124
178
  source: z.any().describe("Full page source { sections: [...] } (object or JSON string)"),
@@ -148,18 +202,27 @@ The source must be { sections: [...] } — build sections with new_section. Vali
148
202
  // Strip a leading "/" — the storefront matches `page.slug == "<segment>"` (no slash),
149
203
  // so "/cart" would 404. Homepage (blank/"/") → undefined (matched by is_nil(slug)).
150
204
  const cleanSlug = normalizeSlug(slug);
205
+ // Only 'custom' pages may be created over and over; singleton kinds and taken
206
+ // slugs must be edited in place instead of duplicated.
207
+ const conflict = await checkPageCreateConflict(api, { kind, slug: cleanSlug, is_homepage });
151
208
  if (dry_run) {
152
209
  return {
153
210
  dry_run: true,
211
+ ...(conflict ? { blocked: true, conflict } : {}),
154
212
  validation,
155
213
  request: { name, slug: cleanSlug ?? null, type: kind ?? null, page_type_num: typeNum ?? null, is_homepage, sections: (parsed && parsed.sections || []).length },
156
214
  will_enable_feature: requiredFlag ?? null,
157
215
  renders_at_breakpoints: ["bp1", "bp2", "bp3", "bp4"],
158
- hint: validation.valid
159
- ? `Looks valid. On save, every node's runtime is expanded into the bp1..bp4 keys the storefront renders. Call again with dry_run=false to create and save the page.${requiredFlag ? ` Will also enable site.settings.${requiredFlag} so its data bindings resolve.` : ""}`
160
- : "Fix the errors above before saving.",
216
+ hint: conflict
217
+ ? conflict.error
218
+ : validation.valid
219
+ ? `Looks valid. On save, every node's runtime is expanded into the bp1..bp4 keys the storefront renders. Call again with dry_run=false to create and save the page.${requiredFlag ? ` Will also enable site.settings.${requiredFlag} so its data bindings resolve.` : ""}`
220
+ : "Fix the errors above before saving.",
161
221
  };
162
222
  }
223
+ if (conflict) {
224
+ return { error: conflict.error, existing_page: conflict.existing_page };
225
+ }
163
226
  if (!validation.valid) {
164
227
  return { error: "Validation failed — not saving.", validation };
165
228
  }
@@ -1,5 +1,5 @@
1
1
  import { z } from "zod";
2
- import { PAGE_TYPE_NUM, PAGE_TYPE_FLAG, PAGE_KINDS, buildPageSeo, normalizeSlug } from "./builder.js";
2
+ import { PAGE_TYPE_NUM, PAGE_TYPE_FLAG, PAGE_KINDS, buildPageSeo, normalizeSlug, checkPageCreateConflict } from "./builder.js";
3
3
  import { validatePage, finalizeForRender, reassignIds } from "../builder/page.js";
4
4
  import { createDraft, getDraft, setDraft, appendDraftSection, listDrafts, delDraft, } from "../persistence/draft-cache.js";
5
5
  // Friendly result when a draft is gone (disposable cache: expired ~2h or restart).
@@ -25,7 +25,8 @@ function newPageId(res) {
25
25
  * commit, so re-running commit_page_draft continues from where it stopped.
26
26
  */
27
27
  export function registerPageDraftTools(server, api, handle) {
28
- server.tool("start_page_draft", `Start a page draft (no network). Build a multi-section page safely: cache each section with add_draft_section, then commit_page_draft persists it to the backend INCREMENTALLY (resumable on timeout). Use this instead of build_page for large/multi-section pages. The draft cache is DISPOSABLE (Redis on the remote server when REDIS_URL is set, in-memory otherwise; sliding ~2h TTL) — if a draft is ever lost, just re-send the sections, never a failure.`, {
28
+ server.tool("start_page_draft", `Start a page draft (no network). Build a multi-section page safely: cache each section with add_draft_section, then commit_page_draft persists it to the backend INCREMENTALLY (resumable on timeout). Use this instead of build_page for large/multi-section pages. The draft cache is DISPOSABLE (Redis on the remote server when REDIS_URL is set, in-memory otherwise; sliding ~2h TTL) — if a draft is ever lost, just re-send the sections, never a failure.
29
+ ONE page per site for type main (homepage) / error / maintain, and slugs are unique per site: the draft is refused up-front with the existing page_id to edit instead. Only 'custom' pages can be created over and over.`, {
29
30
  name: z.string().describe("Page name"),
30
31
  slug: z.string().describe("URL slug WITHOUT a leading slash, e.g. 'about', 'collections', 'cart'. A leading '/' is stripped automatically (the storefront matches the bare path segment, so '/cart' would 404). Store pages MUST use: category='collections', product='products', cart='cart', checkout='checkout', thank-you='complete'. Homepage needs no slug (is_homepage:true)."),
31
32
  type: z
@@ -44,6 +45,11 @@ export function registerPageDraftTools(server, api, handle) {
44
45
  .optional()
45
46
  .describe("SEO for this page → settings.seo (applied on commit)."),
46
47
  }, ({ name, slug, type, is_homepage, seo }) => handle(async () => {
48
+ // Fail before the agent builds any sections: only 'custom' pages may be created
49
+ // repeatedly — singleton kinds and taken slugs must be edited in place.
50
+ const conflict = await checkPageCreateConflict(api, { kind: type, slug, is_homepage });
51
+ if (conflict)
52
+ return { error: conflict.error, existing_page: conflict.existing_page };
47
53
  const draft = await createDraft(api.siteId, { name, slug, type, is_homepage, seo });
48
54
  return {
49
55
  draft_id: draft.draft_id,
@@ -101,8 +107,16 @@ RESUMABLE: if a request fails mid-commit, the draft keeps its page_id + committe
101
107
  const full = { sections: draft.sections };
102
108
  const validation = validatePage(full);
103
109
  const total = draft.sections.length;
110
+ // Re-check on commit — a draft can sit for hours, and the page may have been
111
+ // created meanwhile. Skipped when resuming: the page already exists.
112
+ const conflict = draft.page_id
113
+ ? null
114
+ : await checkPageCreateConflict(api, { kind: draft.meta.type, slug: draft.meta.slug, is_homepage: draft.meta.is_homepage });
104
115
  if (dry_run) {
105
- return { dry_run: true, draft_id, total_sections: total, validation, stats: validation.stats };
116
+ return { dry_run: true, draft_id, total_sections: total, ...(conflict ? { blocked: true, conflict } : {}), validation, stats: validation.stats };
117
+ }
118
+ if (conflict) {
119
+ return { error: conflict.error, existing_page: conflict.existing_page };
106
120
  }
107
121
  if (!validation.valid) {
108
122
  return { error: "Validation failed — not committing.", validation };
@@ -3,7 +3,7 @@ import { CUSTOM_CODE_GUIDE } from "../guides.js";
3
3
  import { getConfirmMode } from "./context.js";
4
4
  import { normalizeEvents } from "../builder/events.js";
5
5
  import { normalizeBindings } from "../builder/bindings.js";
6
- import { PAGE_TYPE_NUM, PAGE_KINDS, buildPageSeo, normalizeSlug } from "./builder.js";
6
+ import { PAGE_TYPE_NUM, PAGE_KINDS, buildPageSeo, normalizeSlug, checkPageCreateConflict } from "./builder.js";
7
7
  /**
8
8
  * Page source utilities.
9
9
  *
@@ -294,10 +294,10 @@ Examples:
294
294
  const results = searchElements(source, filters);
295
295
  return { page_id, matched: results.length, elements: results };
296
296
  }));
297
- server.tool("create_page", "Create a new (empty) page. For a page with content use build_page instead. type is a KIND (main/store/member/blog/custom/error/maintain) mapped to the numeric backend type; pass seo so it doesn't publish with an empty title.", {
297
+ server.tool("create_page", "Create a new (empty) page. For a page with content use build_page instead. type is a KIND (main/store/member/blog/custom/error/maintain) mapped to the numeric backend type; pass seo so it doesn't publish with an empty title. ONE page per site for main (homepage) / error / maintain, and slugs are unique per site — a duplicate is refused with the existing page_id to edit instead; only 'custom' pages can be created over and over.", {
298
298
  name: z.string().describe("Page name"),
299
299
  slug: z.string().describe("URL slug WITHOUT a leading slash, e.g. 'about', 'collections', 'cart'. A leading '/' is stripped automatically (the storefront matches the bare path segment, so '/cart' would 404). Homepage needs no slug (pass is_homepage:true)."),
300
- type: z.enum(PAGE_KINDS).optional().describe("Page kind (main/store/member/blog/custom/error/maintain). store/member/blog need their data-source flag enabled — prefer build_page which auto-enables it."),
300
+ type: z.enum(PAGE_KINDS).optional().describe("Page kind (main/store/member/blog/custom/error/maintain). store/member/blog need their data-source flag enabled — prefer build_page which auto-enables it. main/error/maintain are one-per-site; use 'custom' for extra pages."),
301
301
  is_homepage: z.boolean().default(false).describe("Set as homepage"),
302
302
  seo: z
303
303
  .object({ title: z.string().optional(), description: z.string().optional(), keyword: z.string().optional(), favicon: z.string().optional(), thumbnail: z.string().optional() })
@@ -306,6 +306,11 @@ Examples:
306
306
  }, ({ name, slug, type, is_homepage, seo }) => handle(async () => {
307
307
  const cleanSlug = normalizeSlug(slug);
308
308
  const typeNum = type ? PAGE_TYPE_NUM[type] : undefined;
309
+ // Only 'custom' pages may be created repeatedly — singleton kinds (homepage/error/
310
+ // maintain) and taken slugs must be edited in place instead of duplicated.
311
+ const conflict = await checkPageCreateConflict(api, { kind: type, slug: cleanSlug, is_homepage });
312
+ if (conflict)
313
+ return { error: conflict.error, existing_page: conflict.existing_page };
309
314
  const created = await api.createPage({ name, ...(typeNum != null ? { type: typeNum } : {}) });
310
315
  invalidatePageCache();
311
316
  const pageId = (created && (created.id || created.data?.id || created.page?.id)) || null;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "webcake-storefront-mcp",
3
- "version": "1.31.8",
3
+ "version": "1.31.9",
4
4
  "description": "MCP server for the WebCake/StoreCake storefront builder — page CRUD, page authoring, products, orders, and more",
5
5
  "mcpName": "io.github.vuluu2k/webcake-storefront-mcp",
6
6
  "license": "MIT",
@@ -32,7 +32,7 @@
32
32
  "smoke": "node dist/smoke.js",
33
33
  "prepare": "npm run build",
34
34
  "prepublishOnly": "npm run build && npm run smoke",
35
- "test": "node --test test/"
35
+ "test": "node --test \"test/*.test.mjs\""
36
36
  },
37
37
  "dependencies": {
38
38
  "@modelcontextprotocol/sdk": "^1.12.1",