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