import { z } from "zod"; import { withAuth, getSessionCredentials } from "../../auth/index.js"; import { handleToolError, validateRequired } from "../helpers/errorHandler.js"; import { AcaiHttpClient } from "../helpers/acaiHttpClient.js"; import { withAuthParams } from "../helpers/authSchema.js"; export function registerGetRecordTool(server) { server.tool( "get_record", `Get a single record by its 'num' (primary key) with full details including uploads and relations. Table names: use WITHOUT 'cms_' prefix for tables that have a schema in cms/data/schema/. For tables WITHOUT schema (system tables, custom tables), pass the EXACT full table name including 'cms_' prefix. Examples: - "productos" (has schema) → fetches from cms_productos automatically - "cms_uploads" (no schema) → fetches with exact name, no prefix added`, withAuthParams({ tableName: z.string().describe("Table name. Without 'cms_' prefix if it has schema, or exact name with 'cms_' prefix if no schema."), recordNum: z.string().describe("Record 'num' (primary key)"), loadUploads: z.boolean().optional().default(true).describe("Load upload field data (default: true)"), loadRelations: z.boolean().optional().default(true).describe("Resolve foreign key relations (default: true)"), }), { readOnlyHint: true, destructiveHint: false }, withAuth(async ({ tableName, recordNum, loadUploads = true, loadRelations = true }, extra) => { try { const validationError = validateRequired( { tableName, recordNum }, ['tableName', 'recordNum'], 'get_record' ); if (validationError) return validationError; const credentials = await getSessionCredentials(extra.sessionId); // Detect if table name has cms_ prefix (no schema table) const noSchema = tableName.startsWith("cms_"); const payload = { tableName, where: noSchema ? `num=${recordNum}` : `num=${recordNum}`, limit: 1, options: { uploads: loadUploads, relations: loadRelations, relationsDepth: 2, }, }; // Tables without schema need special options if (noSchema) { payload.options.prefix = ""; payload.options.ignoreSchema = true; } const response = await AcaiHttpClient.postCmsApi( credentials, "get", payload, credentials.token, credentials.tokenHash ); const records = response.data?.data || []; if (records.length === 0) { return { content: [{ type: "text", text: JSON.stringify({ success: false, error: `Record num=${recordNum} not found in table '${tableName}'`, hint: noSchema ? "Table queried with exact name (no schema mode)." : "If the table has no schema, try with the full name including 'cms_' prefix." }, null, 2) }], }; } return { content: [{ type: "text", text: JSON.stringify({ success: true, tableName, record: records[0], }, null, 2) }], }; } catch (error) { return handleToolError(error, 'get_record', { tableName, recordNum }); } }) ); }