fix: improve input validation, error logging, and env var handling
All checks were successful
Build and Push Monarch MCP Docker Image / build (push) Successful in 30s
All checks were successful
Build and Push Monarch MCP Docker Image / build (push) Successful in 30s
- Add validate_account_id() for get_account_holdings input validation - Fix double logging bug in retry_on_auth_error decorator - Remove emojis from log messages for cleaner log parsing - Make PORT and LOG_LEVEL environment variables functional - Delete redundant requirements.txt (pyproject.toml is authoritative) - Clarify MONARCH_PORT is for Docker Compose only in .env.example
This commit is contained in:
@@ -4,6 +4,7 @@ Monarch Money MCP Server - Custom SSE Implementation.
|
||||
|
||||
import logging
|
||||
import json
|
||||
import os
|
||||
from typing import Optional, Any
|
||||
|
||||
from dotenv import load_dotenv
|
||||
@@ -17,9 +18,11 @@ from monarch_mcp_custom.auth import get_authenticated_client, retry_on_auth_erro
|
||||
# Load environment variables
|
||||
load_dotenv()
|
||||
|
||||
# Configure logging
|
||||
# Configure logging with LOG_LEVEL from environment
|
||||
log_level = os.getenv("LOG_LEVEL", "INFO").upper()
|
||||
logging.basicConfig(
|
||||
level=logging.INFO, format="%(asctime)s - %(name)s - %(levelname)s - %(message)s"
|
||||
level=getattr(logging, log_level, logging.INFO),
|
||||
format="%(asctime)s - %(name)s - %(levelname)s - %(message)s",
|
||||
)
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
@@ -124,13 +127,23 @@ async def get_budgets(reason: Optional[str] = None) -> str:
|
||||
return serialize_json(budget_list)
|
||||
|
||||
|
||||
def validate_account_id(account_id: str) -> int:
|
||||
"""Validate and convert account_id to integer."""
|
||||
if not account_id or not account_id.strip():
|
||||
raise ValueError("account_id must be provided and cannot be empty")
|
||||
try:
|
||||
return int(account_id.strip())
|
||||
except (ValueError, TypeError):
|
||||
raise ValueError("account_id must be a valid integer")
|
||||
|
||||
|
||||
@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."""
|
||||
validated_id = validate_account_id(account_id)
|
||||
client = await get_authenticated_client()
|
||||
# The library expects an int for account_id
|
||||
holdings = await client.get_account_holdings(int(account_id))
|
||||
holdings = await client.get_account_holdings(validated_id)
|
||||
return serialize_json(holdings)
|
||||
|
||||
|
||||
@@ -176,4 +189,5 @@ app = create_app()
|
||||
if __name__ == "__main__":
|
||||
import uvicorn
|
||||
|
||||
uvicorn.run(app, host="0.0.0.0", port=8000)
|
||||
port = int(os.getenv("PORT", "8000"))
|
||||
uvicorn.run(app, host="0.0.0.0", port=port)
|
||||
|
||||
Reference in New Issue
Block a user