List Accounts
ynab_list_accountsRetrieve a list of all accounts in your YNAB budget, including optional closed accounts, to easily find account IDs for creating transactions.
Instructions
Lists all accounts in a budget. Useful for finding account IDs when creating transactions.
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| budgetId | No | The ID of the budget (optional, defaults to YNAB_BUDGET_ID environment variable) | |
| includeClosedAccounts | No | Include closed accounts in the list (default: false) |
Implementation Reference
- src/tools/ListAccountsTool.ts:25-69 (handler)The execute function that handles the 'ynab_list_accounts' tool logic. Calls ynab API accounts.getAccounts, filters by closed/deleted, formats output with balance/1000.
export async function execute(input: ListAccountsInput, api: ynab.API) { try { const budgetId = getBudgetId(input.budgetId); const includeClosedAccounts = input.includeClosedAccounts ?? false; console.error(`Listing accounts for budget ${budgetId}`); const response = await api.accounts.getAccounts(budgetId); // Filter and format accounts const accounts = response.data.accounts .filter((account) => !account.deleted && (includeClosedAccounts || !account.closed)) .map((account) => ({ id: account.id, name: account.name, type: account.type, on_budget: account.on_budget, closed: account.closed, balance: (account.balance / 1000).toFixed(2), cleared_balance: (account.cleared_balance / 1000).toFixed(2), uncleared_balance: (account.uncleared_balance / 1000).toFixed(2), transfer_payee_id: account.transfer_payee_id, })); return { content: [{ type: "text" as const, text: JSON.stringify({ accounts, account_count: accounts.length, }, null, 2), }], }; } catch (error) { console.error("Error listing accounts:", error); return { content: [{ type: "text" as const, text: JSON.stringify({ success: false, error: getErrorMessage(error), }, null, 2), }], }; } } - src/tools/ListAccountsTool.ts:7-15 (schema)Input schema using zod: optional budgetId and optional includeClosedAccounts boolean.
export const inputSchema = { budgetId: z.string().optional().describe("The ID of the budget (optional, defaults to YNAB_BUDGET_ID environment variable)"), includeClosedAccounts: z.boolean().optional().describe("Include closed accounts in the list (default: false)"), }; interface ListAccountsInput { budgetId?: string; includeClosedAccounts?: boolean; } - src/tools/ListAccountsTool.ts:17-23 (helper)Helper function getBudgetId that resolves budget ID from input or YNAB_BUDGET_ID env var.
function getBudgetId(inputBudgetId?: string): string { const budgetId = inputBudgetId || process.env.YNAB_BUDGET_ID || ""; if (!budgetId) { throw new Error("No budget ID provided. Please provide a budget ID or set the YNAB_BUDGET_ID environment variable."); } return budgetId; } - src/index.ts:105-109 (registration)Registration of the tool using server.registerTool with name, description, inputSchema, and execute callback.
server.registerTool(ListAccountsTool.name, { title: "List Accounts", description: ListAccountsTool.description, inputSchema: ListAccountsTool.inputSchema, }, async (input) => ListAccountsTool.execute(input, api)); - src/index.ts:19-19 (registration)Import of the ListAccountsTool module in the main index.ts file.
import * as ListAccountsTool from "./tools/ListAccountsTool.js";