-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathProgram.cs
More file actions
253 lines (214 loc) · 9.25 KB
/
Copy pathProgram.cs
File metadata and controls
253 lines (214 loc) · 9.25 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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
using Microsoft.Extensions.Configuration;
using System.Text;
using System.Text.Json;
using System.Text.Json.Serialization;
namespace MCPSqlServer;
/// <summary>
/// MCP SQL Server addon for Windsurf IDE
/// This server acts as an MCP (Model Context Protocol) server that interacts with SQL Server
/// to provide schema information, query execution, and other database operations.
/// </summary>
public class Program
{
private static readonly JsonSerializerOptions _jsonOptions = new()
{
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
Converters = { new JsonStringEnumConverter(JsonNamingPolicy.CamelCase) },
PropertyNameCaseInsensitive = true,
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull,
WriteIndented = false
};
public static async Task Main(string[] args)
{
Console.Error.WriteLine("MCP SQL Server addon starting...");
StreamWriter? writeLogWriter = null;
try
{
InitializeConsoleEncoding();
// Load configuration and get connection string
var (connectionString, debugMode, logPath, allowWorkspaceOverride) = LoadConfiguration(args);
Console.Error.WriteLine($"Log path: {logPath}");
Console.Error.WriteLine($"Allow workspace override: {allowWorkspaceOverride}");
var (readLogWriter, writeLogWriterTemp) = InitializeLogFiles(logPath);
writeLogWriter = writeLogWriterTemp;
// Create JSON-RPC handler with logging
JsonRpcHandler handler = new(_jsonOptions, connectionString, writeLogWriter, debugMode, allowWorkspaceOverride);
Console.Error.WriteLine("JSON-RPC handler created");
// Process requests
await ProcessRequestsAsync(handler, readLogWriter, writeLogWriter);
}
catch (Exception ex)
{
await HandleFatalError(ex, writeLogWriter);
}
finally
{
writeLogWriter?.Dispose();
}
}
private static void InitializeConsoleEncoding()
{
Console.InputEncoding = Encoding.UTF8;
Console.OutputEncoding = Encoding.UTF8;
Console.Error.WriteLine("Encodings set to UTF8");
}
private static (StreamWriter readLogWriter, StreamWriter writeLogWriter) InitializeLogFiles(string basePath)
{
try
{
// Ensure the log directory exists
Directory.CreateDirectory(basePath);
string readLogPath = Path.Combine(basePath, "read.log");
string writeLogPath = Path.Combine(basePath, "write.log");
// Create log files with UTF8 encoding and append mode
StreamWriter readLogWriter = new(readLogPath, append: true, encoding: Encoding.UTF8);
StreamWriter writeLogWriter = new(writeLogPath, append: true, encoding: Encoding.UTF8);
readLogWriter.AutoFlush = true;
writeLogWriter.AutoFlush = true;
Console.Error.WriteLine("Log files initialized");
return (readLogWriter, writeLogWriter);
}
catch (Exception ex)
{
Console.Error.WriteLine($"Warning: Failed to initialize log files at '{basePath}' ({ex.Message}). Falling back to AppContext.BaseDirectory.");
// Fallback to binary directory
string fallbackPath = AppContext.BaseDirectory;
Directory.CreateDirectory(fallbackPath);
string readLogPath = Path.Combine(fallbackPath, "read.log");
string writeLogPath = Path.Combine(fallbackPath, "write.log");
StreamWriter readLogWriter = new(readLogPath, append: true, encoding: Encoding.UTF8);
StreamWriter writeLogWriter = new(writeLogPath, append: true, encoding: Encoding.UTF8);
readLogWriter.AutoFlush = true;
writeLogWriter.AutoFlush = true;
Console.Error.WriteLine("Log files initialized in AppContext.BaseDirectory");
return (readLogWriter, writeLogWriter);
}
}
private static (string connectionString, bool debugMode, string logPath, bool allowWorkspaceOverride) LoadConfiguration(string[] args)
{
string? configPath = null;
// 1. Check if a path to appsettings.json is passed as an argument
if (args.Length > 0 && !string.IsNullOrWhiteSpace(args[0]))
{
string argPath = args[0];
if (File.Exists(argPath))
{
configPath = argPath;
}
else if (Directory.Exists(argPath))
{
string combined = Path.Combine(argPath, "appsettings.json");
if (File.Exists(combined))
{
configPath = combined;
}
}
if (configPath != null)
{
Console.Error.WriteLine($"Using configuration file from command-line argument: {configPath}");
}
else
{
Console.Error.WriteLine($"Warning: Configuration file/directory specified in argument not found: {argPath}");
}
}
// 2. If not passed or not found, try to locate it dynamically
if (configPath == null)
{
configPath = FindAppSettingsPath();
}
if (configPath == null)
{
throw new FileNotFoundException("Could not find 'appsettings.json' in either the workspace directory or the application binary directory.");
}
// Load configuration
string directory = Path.GetDirectoryName(configPath) ?? AppContext.BaseDirectory;
string filename = Path.GetFileName(configPath);
IConfigurationRoot configuration = new ConfigurationBuilder()
.SetBasePath(directory)
.AddJsonFile(filename, optional: false)
.Build();
Console.Error.WriteLine($"Configuration loaded from: {configPath}");
// Get connection string from configuration
string? connectionString = configuration.GetConnectionString("DefaultConnection");
if (string.IsNullOrEmpty(connectionString))
{
throw new InvalidOperationException($"Connection string 'DefaultConnection' not found in config file: {configPath}");
}
Console.Error.WriteLine("Connection string found");
// Get debug mode setting
var debugMode = configuration["DebugMode"]?.ToLowerInvariant() is "true";
Console.Error.WriteLine($"Debug mode: {debugMode}");
// Get log path setting
string? logPath = configuration["LogPath"];
if (string.IsNullOrEmpty(logPath))
{
// Fallback to default path if not specified
logPath = AppContext.BaseDirectory;
Console.Error.WriteLine("LogPath not found in configuration, using default path");
}
// Get allow workspace override setting
var allowWorkspaceOverride = configuration["AllowWorkspaceOverride"]?.ToLowerInvariant() is not "false";
return (connectionString, debugMode, logPath, allowWorkspaceOverride);
}
private static string? FindAppSettingsPath()
{
string currentDir = Directory.GetCurrentDirectory();
// 1. Check current directory directly (workspace root)
string directPath = Path.Combine(currentDir, "appsettings.json");
if (File.Exists(directPath))
{
return directPath;
}
// 2. Fallback to AppContext.BaseDirectory directly (where the binary lives)
string fallbackPath = Path.Combine(AppContext.BaseDirectory, "appsettings.json");
if (File.Exists(fallbackPath))
{
return fallbackPath;
}
return null;
}
private static async Task ProcessRequestsAsync(JsonRpcHandler handler, StreamWriter readLogWriter, StreamWriter writeLogWriter)
{
// Read from stdin and process requests
using StreamReader streamReader = new(Console.OpenStandardInput(), Console.InputEncoding);
Console.Error.WriteLine("Ready to process requests");
while (true)
{
string? line = await streamReader.ReadLineAsync();
if (line == null)
{
// End of input
break;
}
// Log the read input
await readLogWriter.WriteLineAsync($"[{DateTime.UtcNow:yyyy-MM-dd HH:mm:ss.fff}] {line}");
try
{
await handler.ProcessRequestAsync(line);
}
catch (Exception ex)
{
await LogErrorAsync(ex, writeLogWriter);
}
}
}
private static async Task LogErrorAsync(Exception ex, StreamWriter? writeLogWriter)
{
Console.Error.WriteLine($"Error processing request: {ex}");
if (writeLogWriter != null)
{
await writeLogWriter.WriteLineAsync($"[{DateTime.UtcNow:yyyy-MM-dd HH:mm:ss.fff}] Error: {ex}");
}
}
private static async Task HandleFatalError(Exception ex, StreamWriter? writeLogWriter)
{
Console.Error.WriteLine($"Fatal error: {ex}");
if (writeLogWriter != null)
{
await writeLogWriter.WriteLineAsync($"[{DateTime.UtcNow:yyyy-MM-dd HH:mm:ss.fff}] Fatal error: {ex}");
await writeLogWriter.DisposeAsync();
}
Environment.Exit(1);
}
}