fix: update get_budgets to match actual monarchmoney API response structure
All checks were successful
Build and Push Monarch MCP Docker Image / build (push) Successful in 17s
All checks were successful
Build and Push Monarch MCP Docker Image / build (push) Successful in 17s
The monarchmoney library's get_budgets() returns budgetData.monthlyAmountsByCategory and categoryGroups, not a simple 'budgets' array. Updated parsing to correctly extract budget data by category with monthly planned/actual/remaining amounts.
This commit is contained in:
@@ -112,19 +112,53 @@ async def get_budgets(reason: Optional[str] = None) -> str:
|
|||||||
client = await get_authenticated_client()
|
client = await get_authenticated_client()
|
||||||
budgets = await client.get_budgets()
|
budgets = await client.get_budgets()
|
||||||
|
|
||||||
|
# Build a category lookup from categoryGroups
|
||||||
|
category_lookup = {}
|
||||||
|
for group in budgets.get("categoryGroups", []):
|
||||||
|
for cat in group.get("categories", []):
|
||||||
|
category_lookup[cat.get("id")] = {
|
||||||
|
"name": cat.get("name"),
|
||||||
|
"group": group.get("name"),
|
||||||
|
"variability": cat.get("budgetVariability"),
|
||||||
|
}
|
||||||
|
|
||||||
|
# Process monthly amounts by category
|
||||||
budget_list = []
|
budget_list = []
|
||||||
for b in budgets.get("budgets", []):
|
for item in budgets.get("budgetData", {}).get("monthlyAmountsByCategory", []):
|
||||||
|
cat_id = (item.get("category") or {}).get("id")
|
||||||
|
cat_info = category_lookup.get(cat_id, {})
|
||||||
|
|
||||||
|
for monthly in item.get("monthlyAmounts", []):
|
||||||
budget_list.append(
|
budget_list.append(
|
||||||
{
|
{
|
||||||
"name": b.get("name"),
|
"month": monthly.get("month"),
|
||||||
"amount": b.get("amount"),
|
"category": cat_info.get("name"),
|
||||||
"spent": b.get("spent"),
|
"group": cat_info.get("group"),
|
||||||
"remaining": b.get("remaining"),
|
"planned": monthly.get("plannedCashFlowAmount"),
|
||||||
"category": (b.get("category") or {}).get("name"),
|
"actual": monthly.get("actualAmount"),
|
||||||
|
"remaining": monthly.get("remainingAmount"),
|
||||||
|
"rollover": monthly.get("previousMonthRolloverAmount"),
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
return serialize_json(budget_list)
|
# Also include monthly totals summary
|
||||||
|
totals = []
|
||||||
|
for total in budgets.get("budgetData", {}).get("totalsByMonth", []):
|
||||||
|
totals.append(
|
||||||
|
{
|
||||||
|
"month": total.get("month"),
|
||||||
|
"income_planned": (total.get("totalIncome") or {}).get("plannedAmount"),
|
||||||
|
"income_actual": (total.get("totalIncome") or {}).get("actualAmount"),
|
||||||
|
"expenses_planned": (total.get("totalExpenses") or {}).get(
|
||||||
|
"plannedAmount"
|
||||||
|
),
|
||||||
|
"expenses_actual": (total.get("totalExpenses") or {}).get(
|
||||||
|
"actualAmount"
|
||||||
|
),
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
return serialize_json({"budgets": budget_list, "totals": totals})
|
||||||
|
|
||||||
|
|
||||||
def validate_account_id(account_id: str) -> int:
|
def validate_account_id(account_id: str) -> int:
|
||||||
|
|||||||
Reference in New Issue
Block a user