Files
agenticSystem/mcp-server/tools/modules/delete.js
2026-04-12 10:16:52 +00:00

54 lines
2.4 KiB
JavaScript

import { z } from "zod";
import { withAuth, getSessionCredentials, getApiClient } from "../../auth/index.js";
import { handleToolError, validateRequired, handleApiResponse } from "../helpers/errorHandler.js";
import { withAuthParams } from "../helpers/authSchema.js";
import { AcaiHttpClient } from "../helpers/acaiHttpClient.js";
export function registerDeleteModuleTool(server) {
server.tool(
"delete_module",
"Elimina un módulo del proyecto. Borra la carpeta completa del módulo (template/estandar/modulos/{moduleId}/). OBLIGATORIO: llama a check_module_usage ANTES. Si el módulo está en uso (inUse=true), DENIEGA el borrado e informa al usuario de las páginas donde se usa. NO intentes quitar el módulo de las páginas por tu cuenta — solo el usuario puede decidir eso.",
withAuthParams({
moduleId: z.string().describe("ID del módulo a eliminar (nombre de la carpeta)"),
}),
{ readOnlyHint: false, destructiveHint: true },
withAuth(async ({ moduleId }, extra) => {
try {
const validationError = validateRequired({ moduleId }, ['moduleId'], 'delete_module');
if (validationError) return validationError;
const credentials = await getSessionCredentials(extra.sessionId);
const payload = {
action_ws: "deleteModule",
fileName: moduleId,
token: credentials.token,
tokenHash: credentials.tokenHash
};
const response = await AcaiHttpClient.postViewerFunctions(
await getApiClient(extra.sessionId),
payload
);
// Check for API errors (ej: módulo en uso)
const apiError = handleApiResponse(response.data, 'delete_module');
if (apiError) return apiError;
return {
content: [{
type: "text",
text: JSON.stringify({
success: true,
moduleId,
message: `Módulo "${moduleId}" eliminado correctamente.`
}, null, 2)
}],
};
} catch (error) {
return handleToolError(error, 'delete_module', { moduleId });
}
})
);
}