56 lines
2.3 KiB
JavaScript
56 lines
2.3 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_field
|
|
// Crea un nuevo campo en una tabla existente. Backend aplica los defaults
|
|
// segun `type` y permite overrides via `initialProps`.
|
|
|
|
const FIELD_TYPES = [
|
|
"textfield", "textbox", "wysiwyg", "date", "list",
|
|
"checkbox", "upload", "multitext", "codigo", "separator",
|
|
];
|
|
|
|
export function registerCreateFieldTool(server) {
|
|
server.tool(
|
|
"create_field",
|
|
`Add a new field to an existing table.
|
|
|
|
Field types:
|
|
- textfield: single-line text
|
|
- textbox: multi-line plain text
|
|
- wysiwyg: rich text editor
|
|
- codigo: code editor (HTML/JS/CSS snippet)
|
|
- date: date/datetime picker
|
|
- list: select/radio/checkboxes (needs listType + optionsType in initialProps)
|
|
- checkbox: boolean
|
|
- upload: file upload (images/docs)
|
|
- multitext: repeater of text entries
|
|
- separator: visual separator in the form (no data column)
|
|
|
|
'initialProps' is optional; use it to override defaults (e.g. {isRequired:1, maxLength:100}).
|
|
Table names WITHOUT 'cms_' prefix. Primary key is always 'num'.`,
|
|
withAuthParams({
|
|
tableName: z.string().describe("Table name without 'cms_' prefix"),
|
|
fieldName: z.string().describe("New field name (SQL-safe identifier)"),
|
|
label: z.string().describe("Human-readable label shown in the admin form"),
|
|
type: z.enum(FIELD_TYPES).describe("Field type"),
|
|
initialProps: z.object({}).passthrough().optional().describe("Optional overrides for the default field config"),
|
|
}),
|
|
{ readOnlyHint: false, destructiveHint: false },
|
|
withAuth(async ({ tableName, fieldName, label, type, initialProps }, _extra) => {
|
|
try {
|
|
const body = { tableName, fieldName, label, type };
|
|
if (initialProps && typeof initialProps === "object") body.initialProps = initialProps;
|
|
|
|
const { mcp } = await callSchemaEndpoint("/api/schema/create-field", body);
|
|
return mcp;
|
|
} catch (error) {
|
|
return handleToolError(error, "create_field", { tableName, fieldName, type });
|
|
}
|
|
})
|
|
);
|
|
}
|