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: delete_table // Borra el schema (ini.php) de una tabla. Si dropData=true tambien hace DROP TABLE // en MySQL — destruye los datos de forma IRREVERSIBLE. Si la tabla tiene registros // y dropData=false el backend devuelve error con recordCount (intencional: no se // debe borrar el schema dejando datos huerfanos en MySQL sin confirmacion explicita). export function registerDeleteTableTool(server) { server.tool( "delete_table", `Delete a table from the project. IRREVERSIBLE when dropData=true. Behaviour: - dropData=false (default): deletes the schema (.ini.php). If the MySQL table has records, the backend refuses and returns recordCount so you can warn the user. - dropData=true: DROP TABLE in MySQL + delete schema. Data is permanently destroyed. - dryRun=true: does not delete anything, only reports recordCount. Use as a pre-flight check before asking the user for confirmation. Table names are WITHOUT the 'cms_' prefix.`, withAuthParams({ tableName: z.string().describe("Table name without 'cms_' prefix"), dropData: z.boolean().optional().describe("If true, DROP the MySQL table. Default false."), dryRun: z.boolean().optional().describe("If true, only report recordCount without deleting. Default false."), }), { readOnlyHint: false, destructiveHint: true }, withAuth(async ({ tableName, dropData, dryRun }, _extra) => { try { const body = { tableName, confirm: true, dropData: dropData === true, dryRun: dryRun === true, }; const { mcp } = await callSchemaEndpoint("/api/schema/delete-table", body); return mcp; } catch (error) { return handleToolError(error, "delete_table", { tableName, dropData: dropData === true, dryRun: dryRun === true }); } }) ); }