Files
Jordan Diaz e84a36c83d mcp tablas
2026-04-25 08:51:17 +00:00

52 lines
2.8 KiB
JavaScript

import { z } from "zod";
import { withAuth } from "../../auth/index.js";
import { withAuthParams } from "../helpers/authSchema.js";
import { handleToolError } from "../helpers/errorHandler.js";
import { callSchemaEndpoint } from "./_schemaEndpoint.js";
// Tool: create_table
// Crea una tabla nueva delegando en /api/schema/create-table. Enviamos solo
// "intencion" (menuType + flags) — el server Python construye el schemaPreset
// por defecto. Los tableNames viajan SIN el prefijo `cms_`; la PK siempre es `num`.
export function registerCreateTableTool(server) {
server.tool(
"create_table",
`Create a new database table/module for the current Acai project.
Menu types:
- 'multi': regular table with many records (news, products, contacts...)
- 'single': single-record page (homepage, about us...)
- 'category': category container — groups other tables under a menu node
- 'separador': visual separator in the admin menu
Parameters:
- tableName: technical name, lowercase + underscores, WITHOUT 'cms_' prefix. Primary key is always 'num'.
- menuName: display name in the admin sidebar.
- enlace: REQUIRED. Whether the table participates in public URLs (generates the 'enlace' field + slug). This is an architectural decision — ALWAYS ask the user before calling this tool.
- seoMetas: adds SEO meta fields (title, description, og:image). Default false.
- menuOrder: optional integer for sidebar order. Backend assigns one if omitted.`,
withAuthParams({
tableName: z.string().describe("Technical table name, lowercase + underscores, without 'cms_' prefix"),
menuName: z.string().describe("Display name shown in the admin sidebar"),
menuType: z.enum(["multi", "single", "category", "separador"]).describe("'multi' | 'single' | 'category' | 'separador'"),
enlace: z.boolean().describe("Whether the table has public URLs (generates 'enlace' field). REQUIRED — ask the user first."),
seoMetas: z.boolean().optional().describe("Include SEO meta fields. Default false."),
menuOrder: z.number().int().optional().describe("Order in the admin sidebar. Backend picks one if omitted."),
}),
{ readOnlyHint: false, destructiveHint: false },
withAuth(async ({ tableName, menuName, menuType, enlace, seoMetas, menuOrder }, _extra) => {
try {
const body = { tableName, menuName, menuType, enlace };
if (typeof seoMetas === "boolean") body.seoMetas = seoMetas;
if (typeof menuOrder === "number") body.menuOrder = menuOrder;
const { mcp } = await callSchemaEndpoint("/api/schema/create-table", body);
return mcp;
} catch (error) {
return handleToolError(error, "create_table", { tableName, menuType });
}
})
);
}