Files
agenticSystem/mcp-server/tools/libraries/add_global_library.js
Jordan Diaz 50c2076ebd libraries
2026-04-20 20:40:55 +00:00

104 lines
4.3 KiB
JavaScript

import { z } from "zod";
import { withAuth } from "../../auth/index.js";
import { withAuthParams } from "../helpers/authSchema.js";
import { handleToolError } from "../helpers/errorHandler.js";
import { pythonGet, pythonPost } from "../helpers/pythonServerClient.js";
import { getCurrentProjectInfo } from "../files/helpers.js";
// Tool: add_global_library
// Appends a URL to a section (top or bottom) of the project's global libraries.
// Idempotent: if the URL already exists in the target section (trim-compare),
// the list is left untouched and added:false is returned.
export function registerAddGlobalLibraryTool(server) {
server.tool(
"add_global_library",
`Add a URL to the project's global libraries. section='top' injects in <head> (CSS, fonts, critical JS); section='bottom' injects before </body> (most JS). Idempotent with dedupe — if the URL already exists in that section, returns added:false.`,
withAuthParams({
section: z.enum(["top", "bottom"]).describe("Where to inject: 'top' = <head>, 'bottom' = before </body>"),
url: z.string().min(1).describe("Absolute URL (https://…) or project-relative path (/js/foo.js)"),
}),
{ readOnlyHint: false, destructiveHint: false },
withAuth(async ({ section, url }, _extra) => {
try {
const { projectSlug } = getCurrentProjectInfo();
// 1. Read current state from Python.
const current = await pythonGet("/api/project/libraries", {
project: projectSlug,
});
if (!current?.success) {
return {
content: [{
type: "text",
text: JSON.stringify({
success: false,
error: current?.error || "Could not read current libraries",
}),
}],
isError: true,
};
}
const sectionList = Array.isArray(current[section]) ? current[section] : [];
const trimmedUrl = String(url).trim();
// 2. Dedupe: trim-compare URL against existing entries.
const exists = sectionList.some(
(entry) => String(entry?.url || "").trim() === trimmedUrl
);
if (exists) {
return {
content: [{
type: "text",
text: JSON.stringify({
success: true,
added: false,
reason: "already present",
section,
entries: sectionList,
}, null, 2),
}],
};
}
// 3. Append new entry. Backend normalizes to { num, url }.
const nextList = [...sectionList, { url: trimmedUrl }];
// 4. Persist via save endpoint.
const saveResult = await pythonPost("/api/project/libraries/save", {
project: projectSlug,
section,
libraries: nextList,
});
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,
added: true,
section,
entries: saveResult.libraries || [],
}, null, 2),
}],
};
} catch (error) {
return handleToolError(error, "add_global_library", { section, url });
}
})
);
}