Initial commit
This commit is contained in:
73
mcp-server/tools/records/addModuleToRecord.js
Normal file
73
mcp-server/tools/records/addModuleToRecord.js
Normal file
@@ -0,0 +1,73 @@
|
||||
import { z } from "zod";
|
||||
import { withAuth, getSessionCredentials } from "../../auth/index.js";
|
||||
import { handleApiResponse, handleToolError, validateRequired } from "../helpers/errorHandler.js";
|
||||
import { AcaiHttpClient } from "../helpers/acaiHttpClient.js";
|
||||
import { withAuthParams } from "../helpers/authSchema.js";
|
||||
|
||||
export function registerAddModuleToRecordTool(server) {
|
||||
server.tool(
|
||||
"add_module_to_record",
|
||||
`Adds a builder module to a specific record at the desired position. Returns the generated sectionId — use it directly with set_module_config_vars without needing to call list_page_modules.
|
||||
|
||||
Required params:
|
||||
- tableName (string) without 'cms_' prefix
|
||||
- recordNum (number) record primary key ('num' field, never 'id')
|
||||
- moduleId (string) module identifier
|
||||
Optional:
|
||||
- modulePosition (number) insertion index (0-based, default 0)
|
||||
|
||||
Response includes: sectionId, moduleId, position, totalModules`,
|
||||
withAuthParams({
|
||||
tableName: z.string().describe("Table name without cms_ prefix, e.g. 'apartados'"),
|
||||
recordNum: z.union([z.string(), z.number()]).describe("Record num (ID) where the module will be inserted"),
|
||||
moduleId: z.string().describe("Module ID to insert"),
|
||||
modulePosition: z.number().optional().describe("Position in the builder array (0-based). Default 0.")
|
||||
}),
|
||||
{ readOnlyHint: false, destructiveHint: false },
|
||||
withAuth(async ({ tableName, recordNum, moduleId, modulePosition }, extra) => {
|
||||
try {
|
||||
const validationError = validateRequired({ tableName, recordNum, moduleId }, ['tableName', 'recordNum', 'moduleId'], 'add_module_to_record');
|
||||
if (validationError) return validationError;
|
||||
|
||||
const sessionId = extra.sessionId;
|
||||
const credentials = await getSessionCredentials(sessionId);
|
||||
const payload = {
|
||||
tableName,
|
||||
recordNum,
|
||||
moduleId,
|
||||
modulePosition: modulePosition ?? 0
|
||||
};
|
||||
|
||||
// Same endpoint pattern as create_or_update_record: cmsApi subaction
|
||||
const response = await AcaiHttpClient.addModuleToRecord(
|
||||
credentials,
|
||||
credentials.token,
|
||||
credentials.tokenHash,
|
||||
payload
|
||||
);
|
||||
|
||||
const apiError = handleApiResponse(response.data, 'add_module_to_record');
|
||||
if (apiError) return apiError;
|
||||
|
||||
const result = response.data || {}
|
||||
return {
|
||||
content: [{
|
||||
type: "text",
|
||||
text: JSON.stringify({
|
||||
success: true,
|
||||
action: 'add_module_to_record',
|
||||
tableName,
|
||||
recordNum,
|
||||
moduleId,
|
||||
sectionId: result.sectionId,
|
||||
position: result.position ?? (modulePosition ?? 0),
|
||||
totalModules: result.totalModules,
|
||||
}, null, 2)
|
||||
}]
|
||||
};
|
||||
} catch (error) {
|
||||
return handleToolError(error, 'add_module_to_record', { tableName, recordNum, moduleId });
|
||||
}
|
||||
})
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user