|
| 1 | +using CalendarMcp.Core.Models; |
| 2 | + |
| 3 | +namespace CalendarMcp.Core.Services; |
| 4 | + |
| 5 | +/// <summary> |
| 6 | +/// A single capability exposed by an account (e.g. <c>calendar</c>, <c>email</c>, <c>contacts</c>). |
| 7 | +/// </summary> |
| 8 | +public sealed record AccountCapability(string Name, bool ReadOnly); |
| 9 | + |
| 10 | +/// <summary> |
| 11 | +/// Single source of truth for what an account can do, derived from its provider type |
| 12 | +/// (and, for the JSON provider, its configured file paths). Used by <c>list_accounts</c> |
| 13 | +/// to advertise capabilities and by the calendar tools to skip accounts that have no |
| 14 | +/// calendar capability (e.g. email-only IMAP accounts) instead of attempting a read that |
| 15 | +/// would throw <see cref="System.NotSupportedException"/> and surface a spurious warning. |
| 16 | +/// </summary> |
| 17 | +public static class AccountCapabilities |
| 18 | +{ |
| 19 | + public const string Calendar = "calendar"; |
| 20 | + public const string Email = "email"; |
| 21 | + public const string Contacts = "contacts"; |
| 22 | + |
| 23 | + /// <summary> |
| 24 | + /// Determines the capabilities for an account based on its provider type and configuration. |
| 25 | + /// </summary> |
| 26 | + public static IReadOnlyList<AccountCapability> GetCapabilities(AccountInfo account) |
| 27 | + { |
| 28 | + var provider = account.Provider.ToLowerInvariant(); |
| 29 | + |
| 30 | + return provider switch |
| 31 | + { |
| 32 | + "microsoft365" or "m365" => |
| 33 | + [ |
| 34 | + new AccountCapability(Calendar, false), |
| 35 | + new AccountCapability(Email, false), |
| 36 | + new AccountCapability(Contacts, false) |
| 37 | + ], |
| 38 | + "google" or "gmail" or "google workspace" => |
| 39 | + [ |
| 40 | + new AccountCapability(Calendar, false), |
| 41 | + new AccountCapability(Email, false), |
| 42 | + new AccountCapability(Contacts, false) |
| 43 | + ], |
| 44 | + "outlook.com" or "outlook" or "hotmail" => |
| 45 | + [ |
| 46 | + new AccountCapability(Calendar, false), |
| 47 | + new AccountCapability(Email, false), |
| 48 | + new AccountCapability(Contacts, false) |
| 49 | + ], |
| 50 | + "ics" or "icalendar" => |
| 51 | + [ |
| 52 | + new AccountCapability(Calendar, true) |
| 53 | + ], |
| 54 | + "imap" or "imap-smtp" => |
| 55 | + [ |
| 56 | + new AccountCapability(Email, false) |
| 57 | + ], |
| 58 | + "json" or "json-calendar" => GetJsonCapabilities(account), |
| 59 | + _ => |
| 60 | + [ |
| 61 | + new AccountCapability(Calendar, false) |
| 62 | + ] |
| 63 | + }; |
| 64 | + } |
| 65 | + |
| 66 | + /// <summary> |
| 67 | + /// Returns <c>true</c> when the account advertises a calendar capability. False for |
| 68 | + /// email-only accounts (e.g. IMAP), letting calendar tools skip them during fan-out. |
| 69 | + /// </summary> |
| 70 | + public static bool HasCalendar(AccountInfo account) => |
| 71 | + GetCapabilities(account).Any(c => c.Name == Calendar); |
| 72 | + |
| 73 | + /// <summary> |
| 74 | + /// JSON accounts have optional email and contacts support depending on configured file paths. |
| 75 | + /// </summary> |
| 76 | + private static IReadOnlyList<AccountCapability> GetJsonCapabilities(AccountInfo account) |
| 77 | + { |
| 78 | + var config = account.ProviderConfig; |
| 79 | + var capabilities = new List<AccountCapability> |
| 80 | + { |
| 81 | + new(Calendar, true) |
| 82 | + }; |
| 83 | + |
| 84 | + if (config.ContainsKey("emailsFilePath") && !string.IsNullOrEmpty(config["emailsFilePath"]) |
| 85 | + || config.ContainsKey("emailsOneDrivePath") && !string.IsNullOrEmpty(config["emailsOneDrivePath"])) |
| 86 | + { |
| 87 | + capabilities.Add(new AccountCapability(Email, true)); |
| 88 | + } |
| 89 | + |
| 90 | + if (config.ContainsKey("contactsFilePath") && !string.IsNullOrEmpty(config["contactsFilePath"]) |
| 91 | + || config.ContainsKey("contactsOneDrivePath") && !string.IsNullOrEmpty(config["contactsOneDrivePath"])) |
| 92 | + { |
| 93 | + capabilities.Add(new AccountCapability(Contacts, true)); |
| 94 | + } |
| 95 | + |
| 96 | + return capabilities; |
| 97 | + } |
| 98 | +} |
0 commit comments