Initial commit
This commit is contained in:
207
mcp-server/tools/tables/fields.js
Normal file
207
mcp-server/tools/tables/fields.js
Normal 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 });
|
||||
}
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user