import { z } from "zod"; import { withAuth } from "../../auth/index.js"; import { withAuthParams } from "../helpers/authSchema.js"; import { handleToolError } from "../helpers/errorHandler.js"; import { pythonPost } from "../helpers/pythonServerClient.js"; import { getCurrentProjectInfo } from "../files/helpers.js"; // Tool: set_global_libraries // Replaces the entire list of libraries for a section. Destructive — overwrites // everything. Prefer add/remove for incremental edits; use this for bulk reorder // or full replacement. export function registerSetGlobalLibrariesTool(server) { server.tool( "set_global_libraries", `Replace the entire list of libraries for a section. Destructive — overwrites all existing entries. Prefer add/remove for incremental edits. Use for bulk reorder/replace.`, withAuthParams({ section: z.enum(["top", "bottom"]).describe("Target section: 'top' = , 'bottom' = before "), libraries: z.array(z.object({ url: z.string().min(1), })).describe("Full replacement list. Order is preserved."), }), { readOnlyHint: false, destructiveHint: true }, withAuth(async ({ section, libraries }, _extra) => { try { const { projectSlug } = getCurrentProjectInfo(); const saveResult = await pythonPost("/api/project/libraries/save", { project: projectSlug, section, libraries, }); if (!saveResult?.success) { return { content: [{ type: "text", text: JSON.stringify({ success: false, error: saveResult?.error || "Could not save libraries", }), }], isError: true, }; } return { content: [{ type: "text", text: JSON.stringify({ success: true, section: saveResult.section || section, entries: saveResult.libraries || [], }, null, 2), }], }; } catch (error) { return handleToolError(error, "set_global_libraries", { section, count: Array.isArray(libraries) ? libraries.length : 0 }); } }) ); }