Initial commit

This commit is contained in:
Jordan
2026-04-01 23:16:45 +01:00
commit 91cfdaee72
200 changed files with 25589 additions and 0 deletions

View File

@@ -0,0 +1,99 @@
import { z } from "zod";
import { withAuth, getSessionCredentials } from "../../auth/index.js";
import { SAAS_URL } from "../../config/index.js";
import { handleToolError, validateRequired, handleApiResponse } from "../helpers/errorHandler.js";
import { AcaiHttpClient, FormParamsBuilder } from "../helpers/acaiHttpClient.js";
import { withAuthParams } from "../helpers/authSchema.js";
export function registerCreateTableTool(server) {
server.tool(
"create_table",
"Create a new database table/schema in the system. This creates the table structure with basic configuration. After creation, you can use update_table_schema to add custom fields and modify the schema. Table types: 'multi' (multiple records like news, contacts), 'single' (single record like homepage), 'category' (category menu), 'separador' (menu separator/container). Table names are WITHOUT the 'cms_' prefix.",
withAuthParams({
menuName: z.string().describe("Display name for the menu (e.g., 'Noticias', 'Productos')"),
tableName: z.string().describe("Technical table name, lowercase with underscores (e.g., 'noticias', 'productos'). Will be auto-generated from menuName if not provided."),
type: z.enum(["multi", "single", "category", "separador"]).describe("Table type: 'multi' for multiple records, 'single' for single record, 'category' for category menu, 'separador' for menu separator"),
enlace: z.boolean().describe("Whether this table should include the 'enlace' field (true = generates general section URLs, false = no enlace). Ask the user before running this tool."),
seo_metas: z.boolean().optional().describe("Whether this table has SEO meta fields. Default: false"),
menuOrder: z.number().optional().describe("Order in the menu. If not provided, will be added at the end."),
}),
{ readOnlyHint: false, destructiveHint: false },
withAuth(async ({ menuName, tableName, type, enlace, seo_metas = false, menuOrder }, extra) => {
try {
// Validate required parameters
const validationError = validateRequired(
{ menuName, tableName, type, enlace },
['menuName', 'tableName', 'type', 'enlace'],
'create_table'
);
if (validationError) return validationError;
if (typeof enlace !== "boolean") {
return {
content: [{ type: "text", text: "Error: 'enlace' must be explicitly set to true or false before calling this tool." }],
isError: true,
};
}
// If menuOrder not provided, get max order from existing tables
let order = menuOrder;
if (!order) {
try {
const credentials = await getSessionCredentials(extra.sessionId);
const tablesResponse = await AcaiHttpClient.saasPostRequest(
{
action: "getSchemaTables",
type: "acai"
},
credentials.token
);
if (tablesResponse.data.result && tablesResponse.data.data) {
const orders = tablesResponse.data.data.map(t => t.menuOrder || 0);
order = Math.max(...orders, 0) + 1;
} else {
order = 1;
}
} catch (e) {
order = 1;
}
}
// Create table via Acai CMS admin using centralized HTTP client
const params = FormParamsBuilder.buildTableCreateParams(menuName, tableName, type, enlace, seo_metas, order);
const credentials = await getSessionCredentials(extra.sessionId);
const createResponse = await AcaiHttpClient.postAdminForm(
credentials.website,
params,
credentials.token
);
// Check for API errors
const apiError = handleApiResponse(createResponse.data, 'create_table');
if (apiError) return apiError;
// Log response for debugging (stderr to avoid corrupting MCP stream)
console.error("CMS Response:", createResponse.data);
return {
content: [{
type: "text",
text: JSON.stringify({
success: true,
message: "Table created successfully",
tableName: tableName,
menuName: menuName,
type: type,
menuOrder: order,
note: "Table created. You can now use get_table_schema to view it or update_table_schema to add custom fields."
}, null, 2)
}],
};
} catch (error) {
return handleToolError(error, 'create_table', { menuName, tableName, type });
}
})
);
}

View File

@@ -0,0 +1,52 @@
import { z } from "zod";
import { withAuth, getSessionCredentials } from "../../auth/index.js";
import { handleToolError, validateRequired, handleApiResponse } from "../helpers/errorHandler.js";
import { AcaiHttpClient, FormParamsBuilder } from "../helpers/acaiHttpClient.js";
import { withAuthParams } from "../helpers/authSchema.js";
export function registerDeleteTableTool(server) {
server.tool(
"delete_table",
"⚠️ DANGEROUS: Delete a database table/module entirely. This removes the table definition and all its data. This operation is IRREVERSIBLE. Table names are WITHOUT the 'cms_' prefix.",
withAuthParams({
tableName: z.string().describe("Name of the table/module to delete (without 'cms_' prefix, e.g., 'equipo')"),
}),
{ readOnlyHint: false, destructiveHint: true },
withAuth(async ({ tableName }, extra) => {
try {
// Validate required parameters
const validationError = validateRequired({ tableName }, ['tableName'], 'delete_table');
if (validationError) return validationError;
// Build delete table parameters using centralized builder
const params = FormParamsBuilder.buildTableDeleteParams(tableName);
const credentials = await getSessionCredentials(extra.sessionId);
// Delete table via Acai CMS admin using centralized HTTP client
const response = await AcaiHttpClient.postAdminForm(
credentials.website,
params,
credentials.token
);
// Check for API errors
const apiError = handleApiResponse(response.data, 'delete_table');
if (apiError) return apiError;
return {
content: [{
type: "text",
text: JSON.stringify({
success: true,
message: `Table '${tableName}' deleted successfully`
}, null, 2)
}],
};
} catch (error) {
return handleToolError(error, 'delete_table', { tableName });
}
})
);
}

View File

@@ -0,0 +1,207 @@
import { z } from "zod";
import fsPromises from "node:fs/promises";
import path from "node:path";
import { fileURLToPath } from "node:url";
import { withAuth, getSessionCredentials } from "../../auth/index.js";
import { handleToolError, validateRequired, handleApiResponse } from "../helpers/errorHandler.js";
import { AcaiHttpClient, FormParamsBuilder } from "../helpers/acaiHttpClient.js";
import { withAuthParams } from "../helpers/authSchema.js";
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
export function registerEditTableFieldTool(server) {
server.tool(
"edit_table_field",
`Create or edit fields in a database table. Use this for ALL field operations — do NOT use update_table_schema.
Tables WITHOUT 'cms_' prefix. Field types: textfield, textbox, wysiwyg, codigo, checkbox, date, list, multitext, upload, separator, none.
For 'list': set optionsType to 'text', 'table', or 'query' with corresponding option params.
TIP: Don't set isRequired=true on upload fields.`,
withAuthParams({
tableName: z.string().describe("Name of the table (without 'cms_' prefix)"),
fields: z.array(z.object({
fieldname: z.string().describe("Current field name (for editing) or new field name (for creating)"),
newFieldname: z.string().optional().describe("New field name if renaming the field. Leave empty if not renaming."),
label: z.string().optional().describe("Field label shown in the UI"),
type: z.enum(["textfield", "textbox", "wysiwyg", "codigo", "checkbox", "date", "list", "multitext", "upload", "separator", "none"]).optional().describe("Field type"),
order: z.number().optional().describe("Display order in the form"),
defaultValue: z.string().optional().describe("Default value for the field"),
description: z.string().optional().describe("Field description/help text"),
isRequired: z.union([z.number(), z.boolean()]).optional().describe("Whether field is required (0/1 or false/true)"),
isUnique: z.union([z.number(), z.boolean()]).optional().describe("Whether field must be unique (0/1 or false/true)"),
// List field options
listType: z.enum(["pulldown", "radios", "pulldownMulti", "checkboxes"]).optional().describe("For 'list' type: how to display options"),
optionsType: z.enum(["text", "table", "query"]).optional().describe("For 'list' type: source of options"),
optionsText: z.string().optional().describe("For optionsType='text': newline-separated options (use 'value|Label' format)"),
optionsTablename: z.string().optional().describe("For optionsType='table': source table name"),
optionsValueField: z.string().optional().describe("For optionsType='table': field to use as value"),
optionsLabelField: z.string().optional().describe("For optionsType='table': field to display as label"),
optionsQuery: z.string().optional().describe("For optionsType='query': SQL query to get options"),
// Validation
minLength: z.number().optional().describe("Minimum length for text fields"),
maxLength: z.number().optional().describe("Maximum length for text fields"),
// Upload field options
allowedExtensions: z.string().optional().describe("For 'upload' type: comma-separated file extensions"),
maxUploads: z.number().optional().describe("For 'upload' type: maximum number of files"),
createThumbnails: z.union([z.number(), z.boolean()]).optional().describe("For 'upload' type: create thumbnails (0/1)"),
maxThumbnailWidth: z.number().optional().describe("For 'upload' type: thumbnail width"),
maxThumbnailHeight: z.number().optional().describe("For 'upload' type: thumbnail height"),
// Advanced options
isSystemField: z.union([z.number(), z.boolean()]).optional().describe("System field, cannot be edited by users (0/1)"),
adminOnly: z.union([z.number(), z.boolean()]).optional().describe("Only admin can modify (0/1)"),
fieldWidth: z.number().optional().describe("Field width in pixels"),
fieldHeight: z.number().optional().describe("Field height in pixels (for textbox, wysiwyg, codigo)"),
}).passthrough()).describe("Array of field configurations. Each field can include any properties from fieldData.json."),
}),
{ readOnlyHint: false, destructiveHint: false },
withAuth(async ({ tableName, fields }, extra) => {
const startTime = Date.now();
console.error(`[Tool] edit_table_field - START: tableName=${tableName}, fieldCount=${fields.length}, sessionId=${extra.sessionId}`);
try {
// Validate required parameters
const validationError = validateRequired(
{ tableName, fields },
['tableName', 'fields'],
'edit_table_field'
);
if (validationError) {
console.error(`[Tool] edit_table_field - VALIDATION ERROR: ${validationError.content[0].text}`);
return validationError;
}
// Load fieldData.json as template (from server root directory)
const fieldDataPath = path.join(__dirname, '..', '..', 'fieldData.json');
let fieldDataTemplate;
try {
const fieldDataRaw = await fsPromises.readFile(fieldDataPath, 'utf-8');
fieldDataTemplate = JSON.parse(fieldDataRaw);
} catch (error) {
return {
content: [{ type: "text", text: `Error loading fieldData.json template: ${error.message}. Make sure fieldData.json exists in the server directory.` }],
isError: true,
};
}
// Build multipleFields array
const multipleFields = fields.map(fieldConfig => {
const { fieldname, newFieldname, ...restConfig } = fieldConfig;
// Build the complete field data by merging template with provided config
const fieldData = {
...fieldDataTemplate,
...restConfig,
fieldname: fieldname,
newFieldname: newFieldname || fieldname,
};
// Convert boolean values to 0/1 for compatibility
Object.keys(fieldData).forEach(key => {
if (typeof fieldData[key] === 'boolean') {
fieldData[key] = fieldData[key] ? 1 : 0;
}
});
return fieldData;
});
// Create URLSearchParams with root parameters using centralized builder
const params = FormParamsBuilder.buildFieldEditParams(`${tableName}`, multipleFields);
const credentials = await getSessionCredentials(extra.sessionId);
// Send to Acai CMS admin.php using centralized HTTP client
const response = await AcaiHttpClient.postAdminForm(
credentials.website,
params,
credentials.token
);
// Check for error response
if (response.data && typeof response.data === 'string' && response.data.trim().length > 0) {
return {
content: [{
type: "text",
text: JSON.stringify({
success: false,
message: "Field operation completed with message",
serverResponse: response.data,
tableName: tableName,
fieldsCount: fields.length
}, null, 2)
}],
};
}
const elapsedTime = Date.now() - startTime;
console.error(`[Tool] edit_table_field - SUCCESS: completed in ${elapsedTime}ms, fieldsCount=${fields.length}`);
return {
content: [{
type: "text",
text: JSON.stringify({
success: true,
message: fields.length === 1
? `Field '${fields[0].fieldname}' processed successfully`
: `${fields.length} fields processed successfully`,
tableName: tableName,
fieldsProcessed: fields.map(f => f.newFieldname || f.fieldname),
debugResponse: response.data
}, null, 2)
}],
};
} catch (error) {
const elapsedTime = Date.now() - startTime;
console.error(`[Tool] edit_table_field - ERROR after ${elapsedTime}ms: ${error.message}`);
return handleToolError(error, 'edit_table_field', { tableName, fieldCount: fields.length });
}
})
);
}
export function registerDeleteTableFieldTool(server) {
server.tool(
"delete_table_field",
"Delete a field from a database table structure. WARNING: This will delete all data in this column. Table names are WITHOUT the 'cms_' prefix.",
withAuthParams({
tableName: z.string().describe("Name of the table (without 'cms_' prefix)"),
fieldname: z.string().describe("Name of the field to delete"),
}),
{ readOnlyHint: false, destructiveHint: true },
withAuth(async ({ tableName, fieldname }, extra) => {
try {
// Build delete field parameters using centralized builder
const params = FormParamsBuilder.buildFieldDeleteParams(`cms_${tableName}`, fieldname);
const credentials = await getSessionCredentials(extra.sessionId);
// Delete field via Acai CMS admin using centralized HTTP client
const response = await AcaiHttpClient.postAdminForm(
credentials.website,
params,
credentials.token
);
return {
content: [{
type: "text",
text: JSON.stringify({
success: true,
message: `Field '${fieldname}' deleted from table '${tableName}'`,
tableName: tableName
}, null, 2)
}],
};
} catch (error) {
return handleToolError(error, 'delete_table_field', { tableName, fieldname });
}
})
);
}

View File

@@ -0,0 +1,20 @@
// TODO: adaptar create, delete, fields, list, schema para Docker local
// import { registerListTablesTool } from './list.js';
// import { registerGetTableSchemaTool, registerUpdateTableSchemaTool } from './schema.js';
// import { registerGetTableTemplatesTool } from './templates.js';
// import { registerCreateTableTool } from './create.js';
// import { registerDeleteTableTool } from './delete.js';
// import { registerEditTableFieldTool, registerDeleteTableFieldTool } from './fields.js';
export function registerTableTools(server) {
// registerListTablesTool(server);
// registerGetTableSchemaTool(server);
// registerUpdateTableSchemaTool(server);
// registerGetTableTemplatesTool(server);
// registerCreateTableTool(server);
// registerDeleteTableTool(server);
// registerEditTableFieldTool(server);
// registerDeleteTableFieldTool(server);
}

View File

@@ -0,0 +1,74 @@
import { z } from "zod";
import { withAuth, getSessionCredentials } from "../../auth/index.js";
import { handleToolError, handleApiResponse } from "../helpers/errorHandler.js";
import { AcaiHttpClient } from "../helpers/acaiHttpClient.js";
import { withAuthParams } from "../helpers/authSchema.js";
export function registerListTablesTool(server) {
server.tool(
"list_tables",
"List all database tables/schemas and General Sections (tables with 'enlace' field) in the system. Table names returned here are WITHOUT the 'cms_' prefix — use them as-is in all other tool calls. The primary key for all tables is 'num', never 'id'.",
withAuthParams({
withoutEnlace: z.boolean().default(true).describe("If true, include all tables, not only the ones that are general sections with 'enlace' field"),
}),
{ readOnlyHint: true, destructiveHint: false },
withAuth(async ({ withoutEnlace }, extra) => {
try {
console.error(`[list_tables] Tool called with sessionId: ${extra.sessionId}`);
console.error(`[list_tables] Getting credentials for session...`);
const creds = await getSessionCredentials(extra.sessionId);
console.error(`[list_tables] Credentials: website=${creds.website}, hasToken=${!!creds.token}, profileName=${creds.profileName}`);
if (!creds.token) {
console.error(`[list_tables] ERROR: No token found for session ${extra.sessionId}!`);
return {
content: [{
type: "text",
text: JSON.stringify({
error: "No authentication token found for this session. Please login first using login_client tool.",
sessionId: extra.sessionId,
profileName: creds.profileName
}, null, 2)
}],
isError: true
};
}
const response = await AcaiHttpClient.saasPostRequest(
{
action: 'getSchemaTables',
type: 'menu'
},
creds.token
);
if (!response.data.success) {
return {
content: [{ type: "text", text: "Error getting tables: " + JSON.stringify(response.data) }],
isError: true,
};
}
// Filter tables based on withoutEnlace parameter
const tables = response.data.data.filter(schema =>
withoutEnlace ? true : !!schema.enlace
).map(table => ({
name: table.menuName,
tableName: table.tableName,
order: table.menuOrder,
enlace: table.enlace,
hasBuilder: !!table.builder
}));
return {
content: [{ type: "text", text: JSON.stringify(tables, null, 2) }],
};
} catch (error) {
return handleToolError(error, 'list_tables');
}
})
);
}

View File

@@ -0,0 +1,184 @@
import { z } from "zod";
import { withAuth, getApiClient, getSessionCredentials, getCommonParams } from "../../auth/index.js";
import { normalizeSchemaForSave, mergeTableSchemas } from "../../utils/fieldHelpers.js";
import { handleToolError, validateRequired, handleApiResponse } from "../helpers/errorHandler.js";
import { AcaiHttpClient } from "../helpers/acaiHttpClient.js";
import { withAuthParams } from "../helpers/authSchema.js";
export function registerGetTableSchemaTool(server) {
server.tool(
"get_table_schema",
"Get the schema of a database table. Tables WITHOUT 'cms_' prefix. Primary key is 'num', NEVER 'id'. Use minimal=true for just field names + types (saves tokens).",
withAuthParams({
tableName: z.string().describe("Name of the table to get schema for (without 'cms_' prefix)"),
minimal: z.boolean().optional().describe("If true, returns only field names and types (compact). Default: false (full schema with all metadata)."),
}),
{ readOnlyHint: true, destructiveHint: false },
withAuth(async ({ tableName, minimal }, extra) => {
try {
// Validate required parameters
const validationError = validateRequired({ tableName }, ['tableName'], 'get_table_schema');
if (validationError) return validationError;
const credentials = await getSessionCredentials(extra.sessionId);
const response = await AcaiHttpClient.saasPostRequest(
{
id: tableName
},
credentials.token
);
if (!response.data.success) {
return {
content: [{ type: "text", text: "Error getting schema: " + JSON.stringify(response.data) }],
isError: true,
};
}
// Find the specific table
const table = response.data.data;
if (!table) {
return {
content: [{ type: "text", text: `Table '${tableName}' not found` }],
isError: true,
};
}
// Minimal mode: return only field names, types, and key metadata
if (minimal) {
const minimalSchema = {};
for (const [key, value] of Object.entries(table)) {
if (value && typeof value === 'object' && value.type) {
const field = { type: value.type };
if (value.label) field.label = value.label;
if (value.optionsType) field.optionsType = value.optionsType;
if (value.optionsTablename) field.optionsTablename = value.optionsTablename;
if (value.isRequired) field.isRequired = value.isRequired;
minimalSchema[key] = field;
} else if (typeof value !== 'object') {
// Keep top-level scalar metadata (menuName, menuType, enlace, etc.)
minimalSchema[key] = value;
}
}
return {
content: [{ type: "text", text: JSON.stringify(minimalSchema, null, 2) }],
};
}
return {
content: [{ type: "text", text: JSON.stringify(table, null, 2) }],
};
} catch (error) {
return handleToolError(error, 'get_table_schema', { tableName });
}
})
);
}
export function registerUpdateTableSchemaTool(server) {
server.tool(
"update_table_schema",
`Update table-level metadata (menuName, menuOrder, enlace, seo_metas). NOT for field operations — use edit_table_field instead.
Tables WITHOUT 'cms_' prefix. 2-step process: saves to SAAS server, then triggers website schema update.`,
withAuthParams({
tableName: z.string().describe("Name of the table to update"),
schema: z.object({}).passthrough().describe("Schema object with fields objects ( like reference schema table ) to add or update. By default, this is merged with the existing schema."),
overwrite: z.boolean().optional().describe("If true, replaces the ENTIRE schema with the provided one (deleting missing fields). If false (default), merges with existing schema."),
}),
{ readOnlyHint: false, destructiveHint: false },
withAuth(async ({ tableName, schema, overwrite = false }, extra) => {
try {
// Validate required parameters
const validationError = validateRequired({ tableName, schema }, ['tableName', 'schema'], 'update_table_schema');
if (validationError) return validationError;
let schemaToSave;
const credentials = await getSessionCredentials(extra.sessionId);
if (overwrite) {
// If overwrite is true, use the provided schema directly
schemaToSave = { ...schema };
} else {
// Step 1: Fetch current schema to preserve existing fields
const getResponse = await AcaiHttpClient.saasPostRequest(
{
id: tableName
},
credentials.token
);
if (!getResponse.data.success) {
return {
content: [{ type: "text", text: "Error fetching current schema: " + JSON.stringify(getResponse.data) }],
isError: true,
};
}
const currentTable = getResponse.data.data;
if (!currentTable) {
return {
content: [{ type: "text", text: `Table '${tableName}' not found. Please create it first using create_table.` }],
isError: true,
};
}
// Step 2: Merge new schema into existing schema
schemaToSave = mergeTableSchemas(currentTable, schema);
}
normalizeSchemaForSave(schemaToSave);
// Remove tableName from schema (as done in frontend)
delete schemaToSave.tableName;
// Step 3: Save merged schema to SAAS server (PUT request)
const saasResponse = await AcaiHttpClient.saasPutRequest(
{
action: "saveSchema",
schema: schemaToSave,
id: tableName,
},
credentials.token
);
// SAAS returns {success: true} not {result: true}
if (!saasResponse.data.success && !saasResponse.data.result) {
return {
content: [{ type: "text", text: "Error saving schema to SAAS: " + JSON.stringify(saasResponse.data) }],
isError: true,
};
}
// Step 4: Trigger schema update on website
const client = await getApiClient(extra.sessionId);
const updateResponse = await client.post("/cms/lib/viewer_functions.php", await getCommonParams(extra.sessionId, {
action_ws: "updateAllSchemas",
tokenHash: credentials.tokenHash
}));
// Check for website update errors
let updateError = handleApiResponse(updateResponse.data, 'update_table_schema');
if (updateError) return updateError;
return {
content: [{
type: "text",
text: JSON.stringify({
success: true,
message: overwrite ? "Schema overwritten successfully" : "Schema updated successfully (merged with existing fields)",
mergedFields: Object.keys(schemaToSave),
saasResponse: saasResponse.data,
webResponse: updateResponse.data
}, null, 2)
}],
};
} catch (error) {
return handleToolError(error, 'update_table_schema', { tableName, overwrite });
}
})
);
}