import type { Logger } from "../logger.js";
import type { ListAccountsInput } from "../schemas.js";
import { listAccountMetadata, loadServerConfig } from "../config.js";
import { ToolError } from "../errors.js";
import { successResponse, errorResponse, type ToolCallResult } from "./response.js";
/**
* Handle account listing with optional filtering.
*/
export function handleListAccounts(
input: ListAccountsInput,
env: NodeJS.ProcessEnv,
_logger: Logger,
): ToolCallResult {
try {
const { accounts, policy } = loadServerConfig(env);
const targetId = input.account_id ? input.account_id.toLowerCase() : undefined;
const list = listAccountMetadata(accounts).filter((account) => {
if (!targetId) {
return true;
}
return account.account_id === targetId;
});
if (targetId && list.length === 0) {
throw new ToolError("CONFIG_MISSING", `Account not configured: ${targetId}`);
}
const summary = `Found ${list.length} configured SMTP account(s).`;
return successResponse({
summary,
data: { accounts: list },
_meta: {
send_enabled: policy.sendEnabled,
limits: {
max_recipients: policy.maxRecipients,
max_message_bytes: policy.maxMessageBytes,
max_attachments: policy.maxAttachments,
max_attachment_bytes: policy.maxAttachmentBytes,
},
},
});
} catch (error) {
const message = error instanceof Error ? error.message : "Failed to list accounts.";
return errorResponse(message);
}
}