-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathProgram.cs
More file actions
149 lines (128 loc) · 6.35 KB
/
Program.cs
File metadata and controls
149 lines (128 loc) · 6.35 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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
using SharpTools.Tools.Interfaces;
using SharpTools.Tools.Mcp.Tools;
using SharpTools.Tools.Extensions;
using Serilog;
using System.CommandLine;
using System.CommandLine.Parsing;
using ModelContextProtocol.Protocol;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.DependencyInjection;
namespace SharpTools.StdioServer;
public static class Program {
public const string ApplicationName = "SharpToolsMcpStdioServer";
public const string ApplicationVersion = "0.0.1";
public const string LogOutputTemplate = "[{Timestamp:HH:mm:ss} {Level:u3}] [{SourceContext}] {Message:lj}{NewLine}{Exception}";
public static async Task<int> Main(string[] args) {
_ = typeof(SolutionTools);
_ = typeof(AnalysisTools);
_ = typeof(ModificationTools);
var logDirOption = new Option<string?>(
name: "--log-directory",
description: "Optional path to a log directory. If not specified, logs only go to console.");
var logLevelOption = new Option<Serilog.Events.LogEventLevel>(
name: "--log-level",
description: "Minimum log level for console and file.",
getDefaultValue: () => Serilog.Events.LogEventLevel.Information);
var loadSolutionOption = new Option<string?>(
name: "--load-solution",
description: "Path to a solution file (.sln) to load immediately on startup.");
var rootCommand = new RootCommand("SharpTools MCP StdIO Server")
{
logDirOption,
logLevelOption,
loadSolutionOption
};
ParseResult? parseResult = null;
rootCommand.SetHandler((invocationContext) => {
parseResult = invocationContext.ParseResult;
});
await rootCommand.InvokeAsync(args);
if (parseResult == null) {
Console.Error.WriteLine("Failed to parse command line arguments.");
return 1;
}
string? logDirPath = parseResult.GetValueForOption(logDirOption);
Serilog.Events.LogEventLevel minimumLogLevel = parseResult.GetValueForOption(logLevelOption);
string? solutionPath = parseResult.GetValueForOption(loadSolutionOption);
var loggerConfiguration = new LoggerConfiguration()
.MinimumLevel.Is(minimumLogLevel)
.MinimumLevel.Override("Microsoft", Serilog.Events.LogEventLevel.Warning)
.MinimumLevel.Override("Microsoft.Hosting.Lifetime", Serilog.Events.LogEventLevel.Information)
.MinimumLevel.Override("Microsoft.CodeAnalysis", Serilog.Events.LogEventLevel.Information)
.MinimumLevel.Override("ModelContextProtocol", Serilog.Events.LogEventLevel.Warning)
.Enrich.FromLogContext()
.WriteTo.Async(a => a.Console(
outputTemplate: LogOutputTemplate,
standardErrorFromLevel: Serilog.Events.LogEventLevel.Verbose,
restrictedToMinimumLevel: minimumLogLevel));
if (!string.IsNullOrWhiteSpace(logDirPath)) {
if (string.IsNullOrWhiteSpace(logDirPath)) {
Console.Error.WriteLine("Log directory is not valid.");
return 1;
}
if (!Directory.Exists(logDirPath)) {
Console.Error.WriteLine($"Log directory does not exist. Creating: {logDirPath}");
try {
Directory.CreateDirectory(logDirPath);
} catch (Exception ex) {
Console.Error.WriteLine($"Failed to create log directory: {ex.Message}");
return 1;
}
}
string logFilePath = Path.Combine(logDirPath, $"{ApplicationName}-.log");
loggerConfiguration.WriteTo.Async(a => a.File(
logFilePath,
rollingInterval: RollingInterval.Day,
outputTemplate: LogOutputTemplate,
fileSizeLimitBytes: 10 * 1024 * 1024,
rollOnFileSizeLimit: true,
retainedFileCountLimit: 7,
restrictedToMinimumLevel: minimumLogLevel));
Console.Error.WriteLine($"Logging to file: {Path.GetFullPath(logDirPath)} with minimum level {minimumLogLevel}");
}
Log.Logger = loggerConfiguration.CreateBootstrapLogger();
var builder = Host.CreateApplicationBuilder(args);
builder.Logging.ClearProviders();
builder.Logging.AddSerilog();
builder.Services.WithSharpToolsServices();
builder.Services
.AddMcpServer(options => {
options.ServerInfo = new Implementation {
Name = ApplicationName,
Version = ApplicationVersion,
};
})
.WithStdioServerTransport()
.WithSharpTools();
try {
Log.Information("Starting {AppName} v{AppVersion}", ApplicationName, ApplicationVersion);
var host = builder.Build();
if (!string.IsNullOrEmpty(solutionPath)) {
try {
var solutionManager = host.Services.GetRequiredService<ISolutionManager>();
var editorConfigProvider = host.Services.GetRequiredService<IEditorConfigProvider>();
Log.Information("Loading solution: {SolutionPath}", solutionPath);
await solutionManager.LoadSolutionAsync(solutionPath, CancellationToken.None);
var solutionDir = Path.GetDirectoryName(solutionPath);
if (!string.IsNullOrEmpty(solutionDir)) {
await editorConfigProvider.InitializeAsync(solutionDir, CancellationToken.None);
Log.Information("Solution loaded successfully: {SolutionPath}", solutionPath);
} else {
Log.Warning("Could not determine directory for solution path: {SolutionPath}", solutionPath);
}
} catch (Exception ex) {
Log.Error(ex, "Error loading solution: {SolutionPath}", solutionPath);
}
}
await host.RunAsync();
return 0;
} catch (Exception ex) {
Log.Fatal(ex, "{AppName} terminated unexpectedly.", ApplicationName);
return 1;
} finally {
Log.Information("{AppName} shutting down.", ApplicationName);
await Log.CloseAndFlushAsync();
}
}
}