Files
agenticSystem/mcp-server/tools/auth/index.js
2026-07-18 10:03:12 +00:00

48 lines
2.3 KiB
JavaScript

import { sessionCredentials } from "../../auth/credentials.js";
import { refreshSessionCredentials } from "../../auth/sessionRefresh.js";
import { withAuthParams } from "../helpers/authSchema.js";
import { resolveCurrentProjectDir } from "../files/helpers.js";
import { getCurrentSessionId } from "../../utils/sessionContext.js";
export function registerAuthTools(server) {
server.tool(
"refresh_acai_token",
`Refresh the Acai JWT token when it has expired (403 "Token no válido" errors). Delegates to the Python server which detects expiration, renews the token against the webservice if needed, persists the updated .acai file and returns fresh credentials. Use this tool when any other tool fails with a 403 token error.`,
withAuthParams({}),
{ readOnlyHint: false, destructiveHint: false },
async (_args, extra) => {
try {
const projectDir = resolveCurrentProjectDir();
if (!projectDir) {
return {
content: [{ type: "text", text: JSON.stringify({ success: false, error: "Project dir no disponible en esta sesion" }) }],
isError: true,
};
}
const mcpSessionId = getCurrentSessionId() || extra?.sessionId || "_default";
const previousToken = sessionCredentials.get(mcpSessionId)?.token || null;
const freshCreds = await refreshSessionCredentials(mcpSessionId, { force: true });
const rotated = Boolean(previousToken && previousToken !== freshCreds.token);
return {
content: [{
type: "text",
text: JSON.stringify({
success: true,
message: rotated ? "Token refreshed (rotated by Python)" : "Token refreshed successfully",
domain: freshCreds.website,
rotated: !!rotated,
}, null, 2),
}],
};
} catch (error) {
return {
content: [{ type: "text", text: JSON.stringify({ success: false, error: error.message }) }],
isError: true,
};
}
}
);
}