import { z } from "zod"; import { withAuth, getSessionCredentials } from "../../auth/index.js"; import { handleToolError, handleApiResponse } from "../helpers/errorHandler.js"; import { AcaiHttpClient } from "../helpers/acaiHttpClient.js"; import { withAuthParams } from "../helpers/authSchema.js"; export function registerListTablesTool(server) { server.tool( "list_tables", "List all database tables/schemas and General Sections (tables with 'enlace' field) in the system. Table names returned here are WITHOUT the 'cms_' prefix — use them as-is in all other tool calls. The primary key for all tables is 'num', never 'id'.", withAuthParams({ withoutEnlace: z.boolean().default(true).describe("If true, include all tables, not only the ones that are general sections with 'enlace' field"), }), { readOnlyHint: true, destructiveHint: false }, withAuth(async ({ withoutEnlace }, extra) => { try { console.error(`[list_tables] Tool called with sessionId: ${extra.sessionId}`); console.error(`[list_tables] Getting credentials for session...`); const creds = await getSessionCredentials(extra.sessionId); console.error(`[list_tables] Credentials: website=${creds.website}, hasToken=${!!creds.token}, profileName=${creds.profileName}`); if (!creds.token) { console.error(`[list_tables] ERROR: No token found for session ${extra.sessionId}!`); return { content: [{ type: "text", text: JSON.stringify({ error: "No authentication token found for this session. Please login first using login_client tool.", sessionId: extra.sessionId, profileName: creds.profileName }, null, 2) }], isError: true }; } const response = await AcaiHttpClient.saasPostRequest( { action: 'getSchemaTables', type: 'menu' }, creds.token ); if (!response.data.success) { return { content: [{ type: "text", text: "Error getting tables: " + JSON.stringify(response.data) }], isError: true, }; } // Filter tables based on withoutEnlace parameter const tables = response.data.data.filter(schema => withoutEnlace ? true : !!schema.enlace ).map(table => ({ name: table.menuName, tableName: table.tableName, order: table.menuOrder, enlace: table.enlace, hasBuilder: !!table.builder })); return { content: [{ type: "text", text: JSON.stringify(tables, null, 2) }], }; } catch (error) { return handleToolError(error, 'list_tables'); } }) ); }