fix: remove try-except blocks from MCP tools to allow auth-retry decorator to work
All checks were successful
Build and Push Monarch MCP Docker Image / build (push) Successful in 16s

This commit is contained in:
Ben
2025-12-24 21:48:34 +00:00
parent 1e8d484389
commit e462c31907

View File

@@ -41,26 +41,22 @@ def serialize_json(data: Any) -> str:
@retry_on_auth_error() @retry_on_auth_error()
async def get_accounts(reason: Optional[str] = None) -> str: async def get_accounts(reason: Optional[str] = None) -> str:
"""Get all financial accounts from Monarch Money.""" """Get all financial accounts from Monarch Money."""
try: client = await get_authenticated_client()
client = await get_authenticated_client() accounts = await client.get_accounts()
accounts = await client.get_accounts()
account_list = [] account_list = []
for account in accounts.get("accounts", []): for account in accounts.get("accounts", []):
account_info = { account_info = {
"id": account.get("id"), "id": account.get("id"),
"name": account.get("displayName") or account.get("name"), "name": account.get("displayName") or account.get("name"),
"type": (account.get("type") or {}).get("name"), "type": (account.get("type") or {}).get("name"),
"balance": account.get("currentBalance"), "balance": account.get("currentBalance"),
"institution": (account.get("institution") or {}).get("name"), "institution": (account.get("institution") or {}).get("name"),
"is_active": not account.get("deactivatedAt"), "is_active": not account.get("deactivatedAt"),
} }
account_list.append(account_info) account_list.append(account_info)
return serialize_json(account_list) return serialize_json(account_list)
except Exception as e:
logger.error(f"Failed to get accounts: {e}")
return f"Error: {str(e)}"
@mcp.tool() @mcp.tool()
@@ -76,94 +72,76 @@ async def get_transactions(
Get transactions from Monarch Money. Get transactions from Monarch Money.
Dates should be in YYYY-MM-DD format. Dates should be in YYYY-MM-DD format.
""" """
try: client = await get_authenticated_client()
client = await get_authenticated_client() filters = {}
filters = {} if start_date:
if start_date: filters["start_date"] = start_date
filters["start_date"] = start_date if end_date:
if end_date: filters["end_date"] = end_date
filters["end_date"] = end_date if account_id:
if account_id: filters["account_id"] = account_id
filters["account_id"] = account_id
transactions = await client.get_transactions( transactions = await client.get_transactions(limit=limit, offset=offset, **filters)
limit=limit, offset=offset, **filters
results = transactions.get("allTransactions", {}).get("results", [])
formatted = []
for txn in results:
formatted.append(
{
"id": txn.get("id"),
"date": txn.get("date"),
"amount": txn.get("amount"),
"description": txn.get("description"),
"category": (txn.get("category") or {}).get("name"),
"account": (txn.get("account") or {}).get("displayName"),
"merchant": (txn.get("merchant") or {}).get("name"),
"is_pending": txn.get("isPending", False),
}
) )
results = transactions.get("allTransactions", {}).get("results", []) return serialize_json(formatted)
formatted = []
for txn in results:
formatted.append(
{
"id": txn.get("id"),
"date": txn.get("date"),
"amount": txn.get("amount"),
"description": txn.get("description"),
"category": (txn.get("category") or {}).get("name"),
"account": (txn.get("account") or {}).get("displayName"),
"merchant": (txn.get("merchant") or {}).get("name"),
"is_pending": txn.get("isPending", False),
}
)
return serialize_json(formatted)
except Exception as e:
logger.error(f"Failed to get transactions: {e}")
return f"Error: {str(e)}"
@mcp.tool() @mcp.tool()
@retry_on_auth_error() @retry_on_auth_error()
async def get_budgets(reason: Optional[str] = None) -> str: async def get_budgets(reason: Optional[str] = None) -> str:
"""Get current budget information.""" """Get current budget information."""
try: client = await get_authenticated_client()
client = await get_authenticated_client() budgets = await client.get_budgets()
budgets = await client.get_budgets()
budget_list = [] budget_list = []
for b in budgets.get("budgets", []): for b in budgets.get("budgets", []):
budget_list.append( budget_list.append(
{ {
"name": b.get("name"), "name": b.get("name"),
"amount": b.get("amount"), "amount": b.get("amount"),
"spent": b.get("spent"), "spent": b.get("spent"),
"remaining": b.get("remaining"), "remaining": b.get("remaining"),
"category": (b.get("category") or {}).get("name"), "category": (b.get("category") or {}).get("name"),
} }
) )
return serialize_json(budget_list) return serialize_json(budget_list)
except Exception as e:
logger.error(f"Failed to get budgets: {e}")
return f"Error: {str(e)}"
@mcp.tool() @mcp.tool()
@retry_on_auth_error() @retry_on_auth_error()
async def get_account_holdings(account_id: str, reason: Optional[str] = None) -> str: async def get_account_holdings(account_id: str, reason: Optional[str] = None) -> str:
"""Get investment holdings for a specific account.""" """Get investment holdings for a specific account."""
try: client = await get_authenticated_client()
client = await get_authenticated_client() # The library expects an int for account_id
# The library expects an int for account_id holdings = await client.get_account_holdings(int(account_id))
holdings = await client.get_account_holdings(int(account_id)) return serialize_json(holdings)
return serialize_json(holdings)
except Exception as e:
logger.error(f"Failed to get holdings: {e}")
return f"Error: {str(e)}"
@mcp.tool() @mcp.tool()
@retry_on_auth_error() @retry_on_auth_error()
async def refresh_accounts(reason: Optional[str] = None) -> str: async def refresh_accounts(reason: Optional[str] = None) -> str:
"""Request a refresh of account data from financial institutions.""" """Request a refresh of account data from financial institutions."""
try: client = await get_authenticated_client()
client = await get_authenticated_client() # Request refresh for all accounts (empty list often means all in this library)
# Request refresh for all accounts (empty list often means all in this library) result = await client.request_accounts_refresh([])
result = await client.request_accounts_refresh([]) return serialize_json(result)
return serialize_json(result)
except Exception as e:
logger.error(f"Failed to refresh accounts: {e}")
return f"Error: {str(e)}"
# --- Health Check Endpoint --- # --- Health Check Endpoint ---