import { z } from "zod"; import { withAuth } from "../../auth/index.js"; import { withAuthParams } from "../helpers/authSchema.js"; import { handleToolError } from "../helpers/errorHandler.js"; import { pythonGet } from "../helpers/pythonServerClient.js"; import { getCurrentProjectInfo } from "../files/helpers.js"; // Tool: get_layout_field // Reads a global layout field (style or javascript) from the project's layout.json // via the Python endpoint. Read-only — delegates to the backend which parses // layout.json and returns the raw content string plus layoutExists flag. export function registerGetLayoutFieldTool(server) { server.tool( "get_layout_field", `Get the content of a global layout field of the project. Supported: 'style' (global CSS injected in all pages), 'javascript' (global JS), 'header' (HTML/Twig source of the site header), 'footer' (HTML/Twig source of the site footer). Use this before set_layout_field to see the current content.`, withAuthParams({ field: z.enum(["style", "javascript", "header", "footer"]).describe("Which layout field: 'style', 'javascript', 'header' or 'footer'"), }), { readOnlyHint: true, destructiveHint: false }, withAuth(async ({ field }, _extra) => { try { const { projectSlug } = getCurrentProjectInfo(); const result = await pythonGet("/api/project/layout-field", { project: projectSlug, field, }); if (!result?.success) { return { content: [{ type: "text", text: JSON.stringify({ success: false, error: result?.error || "Could not read layout field", }), }], isError: true, }; } return { content: [{ type: "text", text: JSON.stringify({ success: true, field: result.field || field, content: result.content || "", layoutExists: !!result.layoutExists, }, null, 2), }], }; } catch (error) { return handleToolError(error, "get_layout_field", { field }); } }) ); }