import { pythonPost } from "../helpers/pythonServerClient.js"; import { getCurrentProjectInfo } from "../files/helpers.js"; /** * Llama a un endpoint /api/schema/* del server Python. * * Todas las schema-tools comparten el mismo contrato: * - Resolver projectSlug desde la sesion actual. * - POST al endpoint con { project, ...body }. * - Mapear respuesta a formato MCP conservando warnings/schema/etc. * * No validamos payloads aqui: la responsabilidad es del caller (zod) y * del backend Python (validaciones fuertes + proxy al PHP). * * @param {string} endpoint - Path relativo, ej: "/api/schema/create-table" * @param {object} body - Campos especificos de la tool (sin `project`) * @returns {Promise<{mcp: object}>} Respuesta lista para devolver desde la tool */ export async function callSchemaEndpoint(endpoint, body) { const { projectSlug } = getCurrentProjectInfo(); const result = await pythonPost(endpoint, { project: projectSlug, ...body }); // success=false -> error con los campos utiles del backend (error, // warnings, recordCount, dataCount, ...) preservados para el LLM. if (!result || result.success === false) { const { success: _s, ...rest } = result || {}; return { mcp: { content: [{ type: "text", text: JSON.stringify({ success: false, error: (result && result.error) || "Schema endpoint returned no success", ...rest, }, null, 2), }], isError: true, }, }; } // success=true -> devolvemos el payload completo (incluye warnings, // schema, recordCount, etc. cuando el backend los adjunta). return { mcp: { content: [{ type: "text", text: JSON.stringify(result, null, 2), }], }, }; }