|
| 1 | +using System.ComponentModel; |
| 2 | +using System.Text.Json; |
| 3 | +using System.Threading.Tasks; |
| 4 | +using ModelContextProtocol.Server; |
| 5 | + |
| 6 | +namespace CodingWithCalvin.MCPServer.Server.Tools; |
| 7 | + |
| 8 | +[McpServerToolType] |
| 9 | +public class DiagnosticsTools |
| 10 | +{ |
| 11 | + private readonly RpcClient _rpcClient; |
| 12 | + private readonly JsonSerializerOptions _jsonOptions; |
| 13 | + |
| 14 | + public DiagnosticsTools(RpcClient rpcClient) |
| 15 | + { |
| 16 | + _rpcClient = rpcClient; |
| 17 | + _jsonOptions = new JsonSerializerOptions { WriteIndented = true }; |
| 18 | + } |
| 19 | + |
| 20 | + [McpServerTool(Name = "errors_list", ReadOnly = true)] |
| 21 | + [Description("Get errors, warnings, and messages from the Error List. Returns diagnostics with file, line, description, and severity. Filter by severity to focus on specific issues.")] |
| 22 | + public async Task<string> GetErrorListAsync( |
| 23 | + [Description("Filter by severity: \"Error\", \"Warning\", \"Message\", or null for all. Case-insensitive.")] |
| 24 | + string? severity = null, |
| 25 | + [Description("Maximum number of items to return. Defaults to 100.")] |
| 26 | + int maxResults = 100) |
| 27 | + { |
| 28 | + var result = await _rpcClient.GetErrorListAsync(severity, maxResults); |
| 29 | + |
| 30 | + // Always return the JSON result (includes debug info if TotalCount is 0) |
| 31 | + return JsonSerializer.Serialize(result, _jsonOptions); |
| 32 | + } |
| 33 | + |
| 34 | + [McpServerTool(Name = "output_read", ReadOnly = true)] |
| 35 | + [Description("Read content from an Output window pane. Specify pane by GUID or well-known name (\"Build\", \"Debug\", \"General\"). Note: Some panes may not support reading due to VS API limitations.")] |
| 36 | + public async Task<string> ReadOutputPaneAsync( |
| 37 | + [Description("Output pane identifier: GUID string or well-known name (\"Build\", \"Debug\", \"General\").")] |
| 38 | + string paneIdentifier) |
| 39 | + { |
| 40 | + var result = await _rpcClient.ReadOutputPaneAsync(paneIdentifier); |
| 41 | + |
| 42 | + if (string.IsNullOrEmpty(result.Content)) |
| 43 | + { |
| 44 | + return $"Output pane '{paneIdentifier}' is empty or does not support reading"; |
| 45 | + } |
| 46 | + |
| 47 | + return JsonSerializer.Serialize(result, _jsonOptions); |
| 48 | + } |
| 49 | + |
| 50 | + [McpServerTool(Name = "output_write", Destructive = false, Idempotent = false)] |
| 51 | + [Description("Write a message to an Output window pane. Custom panes are auto-created. System panes (Build, Debug) must already exist. Message is appended to existing content.")] |
| 52 | + public async Task<string> WriteOutputPaneAsync( |
| 53 | + [Description("Output pane identifier: GUID string or name. Custom GUIDs/names will create new panes if needed.")] |
| 54 | + string paneIdentifier, |
| 55 | + [Description("Message to write. Appended to existing content.")] |
| 56 | + string message, |
| 57 | + [Description("Whether to activate (bring to front) the Output window. Defaults to false.")] |
| 58 | + bool activate = false) |
| 59 | + { |
| 60 | + var success = await _rpcClient.WriteOutputPaneAsync(paneIdentifier, message, activate); |
| 61 | + return success |
| 62 | + ? $"Message written to output pane: {paneIdentifier}" |
| 63 | + : $"Failed to write to output pane: {paneIdentifier}"; |
| 64 | + } |
| 65 | + |
| 66 | + [McpServerTool(Name = "output_list_panes", ReadOnly = true)] |
| 67 | + [Description("List available Output window panes. Returns well-known panes (Build, Debug, General) with their names and GUIDs.")] |
| 68 | + public async Task<string> GetOutputPanesAsync() |
| 69 | + { |
| 70 | + var panes = await _rpcClient.GetOutputPanesAsync(); |
| 71 | + |
| 72 | + if (panes.Count == 0) |
| 73 | + { |
| 74 | + return "No output panes available"; |
| 75 | + } |
| 76 | + |
| 77 | + return JsonSerializer.Serialize(panes, _jsonOptions); |
| 78 | + } |
| 79 | +} |
0 commit comments