| 1 | namespace AgentNotes.Core; |
| 2 | |
| 3 | /// <summary>CLI / env resolution for MCP 2.0 local TOML.</summary> |
| 4 | public static class AgentNotesBootstrap |
| 5 | { |
| 6 | public const string ConfigEnvVar = "AGENT_NOTES_CONFIG"; |
| 7 | |
| 8 | /// <summary>Exit code when config path is missing.</summary> |
| 9 | public const int ExitMissingConfig = 2; |
| 10 | |
| 11 | /// <summary>Exit code when config path is set but invalid.</summary> |
| 12 | public const int ExitInvalidConfig = 1; |
| 13 | |
| 14 | /// <summary>Last successfully loaded config path (<see cref="TryLoadSettings"/>).</summary> |
| 15 | public static string? LoadedConfigPath { get; private set; } |
| 16 | |
| 17 | public static bool IsStatusOnly(string[] args) => |
| 18 | args.Any(static a => a is "--status-only" or "--status_only"); |
| 19 | |
| 20 | public static string[] FilterStatusOnlyArgs(string[] args) => |
| 21 | args.Where(static a => a is not "--status-only" and not "--status_only").ToArray(); |
| 22 | |
| 23 | public static string? ResolveConfigPath(string[] args) |
| 24 | { |
| 25 | for (var i = 0; i < args.Length; i++) |
| 26 | { |
| 27 | var arg = args[i]; |
| 28 | if (arg is "--config" or "--config-file") |
| 29 | { |
| 30 | if (i + 1 >= args.Length) |
| 31 | throw new ArgumentException($"Missing path after {arg}."); |
| 32 | return args[i + 1].Trim(); |
| 33 | } |
| 34 | |
| 35 | const string prefix = "--config="; |
| 36 | if (arg.StartsWith(prefix, StringComparison.Ordinal)) |
| 37 | return arg[prefix.Length..].Trim(); |
| 38 | } |
| 39 | |
| 40 | var fromEnv = Environment.GetEnvironmentVariable(ConfigEnvVar); |
| 41 | return string.IsNullOrWhiteSpace(fromEnv) ? null : fromEnv.Trim(); |
| 42 | } |
| 43 | |
| 44 | /// <summary>Load settings for MCP host startup. Returns exit code 0 on success.</summary> |
| 45 | public static int TryLoadSettings(string[] args, out LocalSettings? settings, out string? errorMessage) |
| 46 | { |
| 47 | args = FilterStatusOnlyArgs(args); |
| 48 | settings = null; |
| 49 | errorMessage = null; |
| 50 | LoadedConfigPath = null; |
| 51 | string? configPath; |
| 52 | try |
| 53 | { |
| 54 | configPath = ResolveConfigPath(args); |
| 55 | } |
| 56 | catch (Exception ex) |
| 57 | { |
| 58 | errorMessage = ex.Message; |
| 59 | return ExitInvalidConfig; |
| 60 | } |
| 61 | |
| 62 | if (configPath is null) |
| 63 | { |
| 64 | errorMessage = |
| 65 | "agent-notes-mcp 2.0 requires --config <path.toml> in mcp.json (or AGENT_NOTES_CONFIG). " + |
| 66 | "Template: knowledge/work/local/agent-notes.workspace.example.toml in the agent-notes KB repository."; |
| 67 | return ExitMissingConfig; |
| 68 | } |
| 69 | |
| 70 | try |
| 71 | { |
| 72 | LoadedConfigPath = Path.GetFullPath(configPath); |
| 73 | settings = LocalSettingsLoader.Load(configPath); |
| 74 | return 0; |
| 75 | } |
| 76 | catch (Exception ex) |
| 77 | { |
| 78 | errorMessage = $"Failed to load config '{configPath}': {ex.Message}"; |
| 79 | return ExitInvalidConfig; |
| 80 | } |
| 81 | } |
| 82 | } |
| 83 | |