51 lines
2.2 KiB
JavaScript
51 lines
2.2 KiB
JavaScript
import { z } from "zod";
|
|
import { withAuth, getSessionCredentials, getApiClient } from "../../auth/index.js";
|
|
import { handleToolError, handleApiResponse } from "../helpers/errorHandler.js";
|
|
import { AcaiHttpClient } from "../helpers/acaiHttpClient.js";
|
|
import { withAuthParams } from "../helpers/authSchema.js";
|
|
import { parseIniMeta } from "./iniParser.js";
|
|
|
|
export function registerListTablesTool(server) {
|
|
server.tool(
|
|
"list_tables",
|
|
"List all database tables in the project. Table names are WITHOUT 'cms_' prefix — use them as-is in all other tool calls. Primary key is 'num', never 'id'.",
|
|
withAuthParams({}),
|
|
{ readOnlyHint: true, destructiveHint: false },
|
|
withAuth(async (params, extra) => {
|
|
try {
|
|
const credentials = await getSessionCredentials(extra.sessionId);
|
|
const payload = {
|
|
action_ws: "getTableSchemas",
|
|
token: credentials.token,
|
|
tokenHash: credentials.tokenHash,
|
|
};
|
|
const response = await AcaiHttpClient.postViewerFunctions(
|
|
await getApiClient(extra.sessionId),
|
|
payload
|
|
);
|
|
const apiError = handleApiResponse(response.data, 'list_tables');
|
|
if (apiError) return apiError;
|
|
|
|
const schemas = response.data.schemas || {};
|
|
const tables = Object.entries(schemas).map(([filename, iniContent]) => {
|
|
const tableName = filename.replace('.ini.php', '');
|
|
const meta = parseIniMeta(iniContent);
|
|
return {
|
|
tableName,
|
|
menuName: meta.menuName || tableName,
|
|
menuType: meta.menuType || 'multi',
|
|
enlace: meta.enlace || null,
|
|
hasBuilder: iniContent.includes('[builder]'),
|
|
};
|
|
});
|
|
|
|
return {
|
|
content: [{ type: "text", text: JSON.stringify(tables, null, 2) }],
|
|
};
|
|
} catch (error) {
|
|
return handleToolError(error, 'list_tables');
|
|
}
|
|
})
|
|
);
|
|
}
|