-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
115 lines (93 loc) · 4.06 KB
/
Program.cs
File metadata and controls
115 lines (93 loc) · 4.06 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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using ModelContextProtocol.Server;
using SoundTouchMCP.Models;
using SoundTouchMCP.Services;
var builder = Host.CreateApplicationBuilder(args);
var appSettingsPath = ResolveAppSettingsPath(args, builder.Environment.ContentRootPath);
// Configure logging to stderr
builder.Logging.AddConsole(consoleLogOptions =>
{
consoleLogOptions.LogToStandardErrorThreshold = LogLevel.Trace;
});
// Add configuration
builder.Configuration.AddJsonFile(appSettingsPath, optional: false, reloadOnChange: true);
// Configure and validate SoundTouch settings
builder.Services
.AddOptions<SoundTouchConfiguration>()
.Bind(builder.Configuration.GetSection("SoundTouch"))
.ValidateOnStart();
builder.Services.AddSingleton<IValidateOptions<SoundTouchConfiguration>, SoundTouchConfigurationValidator>();
// Register HttpClient for SoundTouchClient
builder.Services.AddHttpClient<ISoundTouchClient, SoundTouchClient>();
builder.Services.AddHttpClient("SoundTouchDiscoveryClient", (serviceProvider, client) =>
{
var config = serviceProvider.GetRequiredService<IOptions<SoundTouchConfiguration>>().Value;
var timeoutMs = Math.Clamp(config.Discovery.ProbeTimeoutMs, 500, 30_000);
client.Timeout = TimeSpan.FromMilliseconds(timeoutMs);
})
.SetHandlerLifetime(TimeSpan.FromMinutes(5));
// Register device discovery service
builder.Services.AddSingleton<IDeviceDiscoveryService, SoundTouchMCP.Services.DeviceDiscoveryService>();
builder.Services.AddSingleton<IDeviceStoreService, DeviceStoreService>();
// Add MCP Server
builder.Services
.AddMcpServer()
.WithStdioServerTransport()
.WithToolsFromAssembly();
await builder.Build().RunAsync();
static string ResolveAppSettingsPath(string[] args, string contentRootPath)
{
var cliPath = TryGetConfigPathFromArgs(args);
if (!string.IsNullOrWhiteSpace(cliPath))
return EnsureExistingPath(cliPath, "--config");
var envAppSettingsPath = Environment.GetEnvironmentVariable("SOUNDTOUCH_APPSETTINGS_PATH");
if (!string.IsNullOrWhiteSpace(envAppSettingsPath))
return EnsureExistingPath(envAppSettingsPath, "SOUNDTOUCH_APPSETTINGS_PATH");
var envConfigDir = Environment.GetEnvironmentVariable("SOUNDTOUCH_CONFIG_DIR");
if (!string.IsNullOrWhiteSpace(envConfigDir))
{
var fromDir = Path.Combine(envConfigDir, "appsettings.json");
return EnsureExistingPath(fromDir, "SOUNDTOUCH_CONFIG_DIR");
}
var besideExecutable = Path.Combine(AppContext.BaseDirectory, "appsettings.json");
if (File.Exists(besideExecutable))
return Path.GetFullPath(besideExecutable);
var contentRootCandidate = Path.Combine(contentRootPath, "appsettings.json");
if (File.Exists(contentRootCandidate))
return Path.GetFullPath(contentRootCandidate);
throw new InvalidOperationException(
"Could not locate appsettings.json. Provide --config, set SOUNDTOUCH_APPSETTINGS_PATH, " +
"or set SOUNDTOUCH_CONFIG_DIR.");
}
static string? TryGetConfigPathFromArgs(string[] args)
{
for (var index = 0; index < args.Length; index++)
{
var arg = args[index];
if (arg.Equals("--config", StringComparison.OrdinalIgnoreCase))
{
if (index + 1 >= args.Length)
throw new ArgumentException("Missing value for --config.");
return args[index + 1];
}
if (arg.StartsWith("--config=", StringComparison.OrdinalIgnoreCase))
{
var value = arg[("--config=".Length)..];
if (string.IsNullOrWhiteSpace(value))
throw new ArgumentException("Missing value for --config.");
return value;
}
}
return null;
}
static string EnsureExistingPath(string path, string source)
{
var resolved = Path.GetFullPath(path);
if (!File.Exists(resolved))
throw new FileNotFoundException($"Configuration path from {source} does not exist: {resolved}");
return resolved;
}