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