-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathold_program.cs
More file actions
539 lines (461 loc) · 20.8 KB
/
old_program.cs
File metadata and controls
539 lines (461 loc) · 20.8 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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
using System.ComponentModel;
using System.Diagnostics;
using System.Text;
using System.Text.Json;
using Azure.AI.OpenAI;
using Azure.Identity;
using Microsoft.Agents.AI;
using Microsoft.Extensions.AI;
using ModelContextProtocol.Client;
using ModelContextProtocol.Protocol;
using OpenAI;
using static ModelContextProtocol.Protocol.ElicitRequestParams;
Console.WriteLine("╔════════════════════════════════════════════════════════════════╗");
Console.WriteLine("║ Windows Diagnostics MCP Chat Client v1.0 ║");
Console.WriteLine("║ Interactive Chat with System Diagnostics ║");
Console.WriteLine("╚════════════════════════════════════════════════════════════════╝");
Console.WriteLine();
var endpoint = new Uri("https://alonlecturedemo-resource.cognitiveservices.azure.com/");
var credential = new DefaultAzureCredential();
var deploymentName = "model-router";
var solutionRoot = Path.GetFullPath(Path.Combine(AppContext.BaseDirectory, "../../../.."));
var projectPath = Path.Combine(solutionRoot, "WinDiagMcpServer", "WinDiagMcpServer.csproj");
var dotnetExecutable = Path.Combine(
Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles),
"dotnet",
OperatingSystem.IsWindows() ? "dotnet.exe" : "dotnet");
Console.WriteLine("Building MCP Server...");
var buildProcess = Process.Start(new ProcessStartInfo
{
FileName = dotnetExecutable,
Arguments = $"build \"{projectPath}\"",
UseShellExecute = false,
CreateNoWindow = true
});
if (buildProcess != null)
{
await buildProcess.WaitForExitAsync();
if (buildProcess.ExitCode != 0)
{
Console.WriteLine("Failed to build MCP Server.");
return;
}
}
var serverExePath = Path.Combine(Path.GetDirectoryName(projectPath)!, "bin", "Debug", "net10.0-windows", "WinDiagMcpServer.exe");
if (!File.Exists(serverExePath))
{
Console.WriteLine($"Server executable not found at: {serverExePath}");
return;
}
Console.WriteLine("Starting MCP Server...");
var serverProcess = new Process
{
StartInfo = new ProcessStartInfo
{
FileName = serverExePath,
Arguments = "--urls=http://localhost:5000",
WorkingDirectory = Path.GetDirectoryName(projectPath) ?? solutionRoot,
UseShellExecute = true,
CreateNoWindow = false
}
};
serverProcess.Start();
AppDomain.CurrentDomain.ProcessExit += (_, _) => {
if (!serverProcess.HasExited)
{
serverProcess.Kill();
}
};
Console.WriteLine("Waiting for server to start...");
await Task.Delay(5000);
async ValueTask<ElicitResult> HandleElicitationAsync(ElicitRequestParams? requestParams, CancellationToken token)
{
await Task.CompletedTask;
if (requestParams?.RequestedSchema?.Properties is null || requestParams.RequestedSchema.Properties.Count == 0)
{
return new ElicitResult { Action = "reject" };
}
Console.WriteLine();
Console.WriteLine("══════════ HUMAN CONFIRMATION REQUIRED ══════════");
if (!string.IsNullOrWhiteSpace(requestParams.Message))
{
Console.WriteLine(requestParams.Message);
}
Console.WriteLine("Type 'cancel' anytime to abort the operation.");
var content = new Dictionary<string, JsonElement>();
foreach (var property in requestParams.RequestedSchema.Properties)
{
var value = PromptForValue(property.Key, property.Value, token);
if (value is null)
{
Console.WriteLine("[Elicitation] User cancelled input.");
return new ElicitResult { Action = "reject" };
}
content[property.Key] = value.Value;
}
Console.WriteLine("[Elicitation] Confirmation captured.");
return new ElicitResult { Action = "accept", Content = content };
JsonElement? PromptForValue(string propertyName, object schema, CancellationToken cancellationToken)
{
while (true)
{
cancellationToken.ThrowIfCancellationRequested();
switch (schema)
{
case StringSchema stringSchema:
Console.Write($"{stringSchema.Description ?? propertyName}: ");
var stringInput = Console.ReadLine();
if (IsCancelled(stringInput))
{
return null;
}
if (!string.IsNullOrWhiteSpace(stringInput))
{
return JsonSerializer.SerializeToElement(stringInput.Trim());
}
Console.WriteLine("A value is required. Type 'cancel' to abort.");
break;
case BooleanSchema booleanSchema:
Console.Write($"{booleanSchema.Description ?? propertyName} (y/n): ");
var boolInput = Console.ReadLine();
if (IsCancelled(boolInput))
{
return null;
}
if (TryParseBoolean(boolInput, out var boolResult))
{
return JsonSerializer.SerializeToElement(boolResult);
}
Console.WriteLine("Please respond with 'y' or 'n'.");
break;
case NumberSchema numberSchema:
Console.Write($"{numberSchema.Description ?? propertyName}: ");
var numberInput = Console.ReadLine();
if (IsCancelled(numberInput))
{
return null;
}
if (double.TryParse(numberInput, out var numericValue))
{
if (numberSchema.Type == "integer")
{
return JsonSerializer.SerializeToElement(Convert.ToInt32(numericValue));
}
return JsonSerializer.SerializeToElement(numericValue);
}
Console.WriteLine("Enter a numeric value (type 'cancel' to abort).");
break;
case TitledSingleSelectEnumSchema titledEnumSchema:
return PromptForEnum(propertyName, titledEnumSchema.OneOf.ToList(), cancellationToken);
case UntitledSingleSelectEnumSchema untitledEnumSchema:
var options = untitledEnumSchema.Enum
.Select(value => new EnumSchemaOption { Const = value, Title = value })
.ToList();
return PromptForEnum(propertyName, options, cancellationToken);
default:
Console.WriteLine($"Unsupported schema type for '{propertyName}'. Rejecting.");
return null;
}
}
}
JsonElement? PromptForEnum(string propertyName, IList<EnumSchemaOption> options, CancellationToken cancellationToken)
{
if (options.Count == 0)
{
Console.WriteLine($"No selectable options supplied for '{propertyName}'.");
return null;
}
while (true)
{
cancellationToken.ThrowIfCancellationRequested();
Console.WriteLine($"{propertyName} - choose one of the following:");
for (var i = 0; i < options.Count; i++)
{
var option = options[i];
Console.WriteLine($" {i + 1}. {option.Title}");
}
Console.Write("Selection (number or value, 'cancel' to abort): ");
var selection = Console.ReadLine();
if (IsCancelled(selection))
{
return null;
}
if (int.TryParse(selection, out var choiceIndex) && choiceIndex > 0 && choiceIndex <= options.Count)
{
return JsonSerializer.SerializeToElement(options[choiceIndex - 1].Const);
}
var optionMatch = options.FirstOrDefault(opt => string.Equals(opt.Const, selection, StringComparison.OrdinalIgnoreCase));
if (optionMatch is not null)
{
return JsonSerializer.SerializeToElement(optionMatch.Const);
}
Console.WriteLine("Invalid selection. Please try again.");
}
}
static bool IsCancelled(string? input) => string.Equals(input?.Trim(), "cancel", StringComparison.OrdinalIgnoreCase);
static bool TryParseBoolean(string? input, out bool value)
{
if (bool.TryParse(input, out value))
{
return true;
}
if (string.Equals(input, "y", StringComparison.OrdinalIgnoreCase) || string.Equals(input, "yes", StringComparison.OrdinalIgnoreCase))
{
value = true;
return true;
}
if (string.Equals(input, "n", StringComparison.OrdinalIgnoreCase) || string.Equals(input, "no", StringComparison.OrdinalIgnoreCase))
{
value = false;
return true;
}
value = false;
return false;
}
}
var clientOptions = new McpClientOptions
{
Capabilities = new ClientCapabilities
{
Elicitation = new ElicitationCapability
{
Form = new FormElicitationCapability()
}
},
Handlers = new McpClientHandlers
{
ElicitationHandler = HandleElicitationAsync
}
};
var mcpClient = await McpClient.CreateAsync(
new HttpClientTransport(new HttpClientTransportOptions
{
Endpoint = new Uri("http://localhost:5000/mcp?apiKey=secure-mcp-key")
}),
clientOptions);
Console.WriteLine("Fetching tools...");
var mcpTools = await mcpClient.ListToolsAsync();
var allTools = mcpTools.Cast<AITool>().ToList();
// Define the internal tool for reading resources
async Task<string> ReadMcpResource(
[Description("The URI of the resource to read (e.g. eventlog://snapshot/abc123?limit=10&offset=0)")] string resourceUri)
{
try
{
Console.WriteLine($"[Internal Tool] Reading resource: {resourceUri}");
var uri = new Uri(resourceUri);
var result = await mcpClient.ReadResourceAsync(uri);
// Combine all contents
var contentList = new List<string>();
foreach (var content in result.Contents)
{
if (content is TextResourceContents textContent)
{
contentList.Add(textContent.Text);
Console.WriteLine($"[Internal Tool] Successfully read {textContent.Text.Length} bytes");
}
else
{
contentList.Add($"[Binary Content: {content.MimeType}]");
}
}
return string.Join("\n", contentList);
}
catch (Exception ex)
{
var errorMsg = $"Error reading resource '{resourceUri}': {ex.Message}";
Console.WriteLine($"[Internal Tool] {errorMsg}");
if (ex.InnerException != null)
{
Console.WriteLine($"[Internal Tool] Inner exception: {ex.InnerException.Message}");
}
return errorMsg;
}
}
var readResourceFunction = AIFunctionFactory.Create(
ReadMcpResource,
"read_resource",
"Reads the content of an MCP resource. Resources support pagination via query parameters (e.g., ?limit=10&offset=0).");
allTools.Add(readResourceFunction);
// Define the internal tool for retrieving MCP prompts
async Task<string> GetMcpPromptContentAsync(
[Description("The name of the MCP prompt to retrieve")] string promptName,
[Description("Optional JSON object of arguments for the prompt")] string? argumentsJson = null)
{
try
{
Console.WriteLine($"[Internal Tool] Getting MCP prompt: {promptName}");
var prompts = await mcpClient.ListPromptsAsync();
var prompt = prompts.FirstOrDefault(p => p.Name == promptName);
if (prompt is null)
{
return $"Prompt '{promptName}' not found on MCP server.";
}
IDictionary<string, object?> argsDict;
if (string.IsNullOrWhiteSpace(argumentsJson))
{
argsDict = new Dictionary<string, object?>();
}
else
{
// Simple JSON -> dictionary parsing
argsDict = JsonSerializer
.Deserialize<Dictionary<string, object?>>(argumentsJson)
?? new Dictionary<string, object?>();
}
var promptResult = await prompt.GetAsync(argsDict);
// Build a formatted string from the prompt messages
var sb = new StringBuilder();
sb.AppendLine($"[MCP PROMPT: {promptName}]");
sb.AppendLine();
foreach (var msg in promptResult.Messages)
{
sb.AppendLine($"[{msg.Role}]");
// Serialize the ContentBlock to get its content
var contentJson = JsonSerializer.Serialize(msg.Content);
var contentObj = JsonSerializer.Deserialize<JsonElement>(contentJson);
if (contentObj.TryGetProperty("text", out var textElement))
{
sb.AppendLine(textElement.GetString());
}
else
{
sb.AppendLine(msg.Content.ToString() ?? "[No content]");
}
sb.AppendLine();
}
var resultText = sb.ToString();
Console.WriteLine($"[Internal Tool] Retrieved prompt '{promptName}' with {promptResult.Messages.Count} message(s)");
return resultText;
}
catch (Exception ex)
{
var errorMsg = $"Error retrieving prompt '{promptName}': {ex.Message}";
Console.WriteLine($"[Internal Tool] {errorMsg}");
if (ex.InnerException is not null)
{
Console.WriteLine($"[Internal Tool] Inner exception: {ex.InnerException.Message}");
}
return errorMsg;
}
}
var getPromptFunction = AIFunctionFactory.Create(
GetMcpPromptContentAsync,
"get_prompt",
"Retrieves and expands a named MCP prompt from the diagnostics MCP server.");
allTools.Add(getPromptFunction);
// Fetch available prompts and build the prompt list for instructions
var availablePromptsList = "";
if (mcpClient.ServerCapabilities.Prompts is not null)
{
Console.WriteLine("Fetching prompts...");
var mcpPrompts = await mcpClient.ListPromptsAsync();
Console.WriteLine($"Found {mcpPrompts.Count} prompts");
if (mcpPrompts.Any())
{
var promptDescriptions = mcpPrompts.Select(p =>
$" - {p.Name}: {p.Description ?? "No description"}");
availablePromptsList = "\n\nAVAILABLE MCP PROMPTS:\n" +
string.Join("\n", promptDescriptions);
}
}
// Create AI Agent
AIAgent agent = new AzureOpenAIClient(endpoint, credential)
.GetChatClient(deploymentName)
.CreateAIAgent(
instructions: $@"You are a helpful system diagnostics assistant.
You have access to Windows diagnostics tools via MCP.
MCP PROMPTS - IMPORTANT WORKFLOW:
- The MCP server provides named prompts that contain step-by-step workflows for complex diagnostics.
- When the user asks for diagnostics like 'health check', 'diagnose system', 'check for issues', etc.,
YOU MUST FIRST call the 'get_prompt' tool to get the appropriate workflow.
- Available prompts:{availablePromptsList}
HOW TO USE PROMPTS:
1. When user asks for diagnostics, identify the matching prompt name from the list above
2. Call get_prompt with the prompt name (and any required parameters in argumentsJson)
3. The prompt will return a detailed workflow - FOLLOW IT STEP BY STEP
4. Execute each step in the workflow using the appropriate tools
5. Present results to the user as you complete each major step
PROMPT MATCHING EXAMPLES:
- User says 'health check' or 'diagnose system' → use 'DiagnoseSystemHealth' prompt
- User says 'check cpu' or 'high cpu' → use 'ExplainHighCpu' prompt
- User says 'security check' or 'security scan' → use 'DetectSecurityAnomalies' prompt
- User asks about 'application errors' or 'recent errors' → use 'AnalyzeRecentApplicationErrors' prompt
PROMPT PARAMETERS:
- If a prompt requires parameters (like hoursBack), pass them as JSON in argumentsJson
- Example: get_prompt('DiagnoseSystemHealth', '{{""hoursBack"":24}}')
- Default values are usually fine if user doesn't specify
EVENT LOG QUERIES:
When creating event log snapshots, use these XPath query patterns:
- All recent errors: ""*[System/Level=2]""
- All recent warnings: ""*[System/Level=3]""
- Last N hours: ""*[System/TimeCreated[@SystemTime >= '2025-01-01T00:00:00.000Z']]""
- Specific event ID: ""*[System/EventID=1000]""
- All events: ""*""
RESOURCE READING STRATEGY:
1. When you receive a resourceUri from a tool (e.g., eventlog://snapshot/abc123), you MUST use the 'read_resource' tool to read it.
2. Event log resources support pagination. ALWAYS start with a small page:
- First read: resourceUri?limit=10&offset=0
- Check the 'Pagination' section in the response for TotalCount
- If TotalCount > 50, read incrementally and summarize patterns
- DO NOT try to read all events at once if there are many
3. The response includes a 'Pagination' object with:
- TotalCount: total events in snapshot
- ReturnedCount: events in current page
- HasMore: whether more pages exist
- NextOffset: offset for next page (if HasMore=true)
4. For large snapshots (>100 events):
- Analyze first page for patterns
- Only read more pages if specific information is needed
- Summarize findings rather than listing every event
5. Parse the Events array from the response JSON and extract relevant information like:
- TimeCreated
- ProviderName
- LevelDisplayName (Error, Warning, Information)
- Message
6. If you get an error reading a resource, report the exact error message to the user.
RESPONSE FORMAT:
When presenting event log data:
- Show the timestamp, level, and message for each event
- Group similar events if there are patterns
- Summarize total counts by severity level
Be concise and focus on answering the user's specific question.
Maintain context from previous messages in the conversation.",
name: "WinDiagAgent",
tools: allTools);
// Create a new agent thread with history management
var thread = agent.GetNewThread();
Console.WriteLine("Agent ready. Type 'exit' to quit.");
Console.WriteLine();
while (true)
{
Console.Write("User: ");
var input = Console.ReadLine();
if (string.IsNullOrWhiteSpace(input))
{
continue;
}
if (input.Trim().ToLower() == "exit")
{
break;
}
try
{
// Run agent with thread - framework handles history automatically
var response = await agent.RunAsync(input, thread);
Console.WriteLine($"Agent: {response.Text}");
}
catch (Exception ex)
{
Console.WriteLine($"Error: {ex.Message}");
// If token limit exceeded, provide guidance
if (ex.Message.Contains("context_length_exceeded") ||
ex.Message.Contains("tokens") ||
ex.Message.Contains("maximum context length"))
{
Console.WriteLine("[System] Token limit reached. The agent should use pagination to read resources in smaller chunks.");
Console.WriteLine("[System] Try rephrasing your question to be more specific.");
}
}
Console.WriteLine();
}