using System.ComponentModel; using System.Text.Json; using Microsoft.EntityFrameworkCore; using ModelContextProtocol.Server; using MoneyMap.Data; using MoneyMap.Services; namespace MoneyMap.Mcp.Tools; [McpServerToolType] public static class DashboardTools { private static readonly JsonSerializerOptions JsonOptions = new() { WriteIndented = true }; [McpServerTool(Name = "get_dashboard"), Description("Get dashboard overview: top spending categories, recent transactions, and aggregate stats.")] public static async Task GetDashboard( [Description("Number of top categories to show (default 8)")] int? topCategoriesCount = null, [Description("Number of recent transactions to show (default 20)")] int? recentTransactionsCount = null, IDashboardService dashboardService = default!) { var data = await dashboardService.GetDashboardDataAsync( topCategoriesCount ?? 8, recentTransactionsCount ?? 20); return JsonSerializer.Serialize(data, JsonOptions); } [McpServerTool(Name = "get_monthly_trend"), Description("Get month-over-month spending totals for trend analysis.")] public static async Task GetMonthlyTrend( [Description("Number of months to include (default 6)")] int? months = null, [Description("Filter to a specific category")] string? category = null, MoneyMapContext db = default!) { var monthCount = months ?? 6; var endDate = DateTime.Today; var startDate = new DateTime(endDate.Year, endDate.Month, 1).AddMonths(-(monthCount - 1)); var q = db.Transactions .Where(t => t.Date >= startDate && t.Date <= endDate) .Where(t => t.Amount < 0) .Where(t => t.TransferToAccountId == null) .ExcludeTransfers(); if (!string.IsNullOrWhiteSpace(category)) q = q.Where(t => t.Category == category); var monthly = await q .GroupBy(t => new { t.Date.Year, t.Date.Month }) .Select(g => new { Year = g.Key.Year, Month = g.Key.Month, Total = g.Sum(t => Math.Abs(t.Amount)), Count = g.Count() }) .OrderBy(x => x.Year).ThenBy(x => x.Month) .ToListAsync(); var result = monthly.Select(m => new { Period = $"{m.Year}-{m.Month:D2}", m.Total, m.Count }).ToList(); return JsonSerializer.Serialize(new { Category = category ?? "All Spending", Months = result }, JsonOptions); } }