Forge
csharp5e452b8e
1using System.Collections.Frozen;
2using System.Text.Json;
3using DotnetDebugMcp;
4using ModelContextProtocol.Protocol;
5using ModelContextProtocol.Server;
6
7// MCP-сервер для отладки .NET: DAP + интеграция с Visual Studio (DTE).
8// Сейчас: хранение брейкпоинтов (set/list/clear); DAP launch + continue/next.
9
10var toolsList = ToolCatalog.Build();
11
12var options = new McpServerOptions
13{
14 ServerInfo = new Implementation { Name = "DotnetDebugMcp", Version = "0.3.0" },
15 ProtocolVersion = "2024-11-05",
16 Capabilities = new ServerCapabilities { Tools = new ToolsCapability { ListChanged = false } },
17 Handlers = new McpServerHandlers
18 {
19 ListToolsHandler = (_, _) => ValueTask.FromResult(new ListToolsResult { Tools = toolsList }),
20
21 CallToolHandler = async (request, cancellationToken) =>
22 {
23 var name = request.Params?.Name ?? "";
24 var args = request.Params?.Arguments is IReadOnlyDictionary<string, JsonElement> a
25 ? a
26 : FrozenDictionary<string, JsonElement>.Empty;
27 try
28 {
29 cancellationToken.ThrowIfCancellationRequested();
30 string text = name switch
31 {
32 "debug_ping" => $"OK {DateTime.UtcNow:O} — DotnetDebugMcp. Tools: {string.Join(", ", toolsList.Select(t => t.Name))}.",
33 "debug_set_breakpoints" => BreakpointToolHandlers.HandleSetBreakpoints(args),
34 "debug_list_breakpoints" => BreakpointToolHandlers.HandleListBreakpoints(args),
35 "debug_clear_breakpoints" => BreakpointToolHandlers.HandleClearBreakpoints(args),
36 "debug_launch" => await DebugLaunchToolHandlers.HandleDebugLaunch(args),
37 "debug_attach" => await DebugLaunchToolHandlers.HandleDebugAttach(args),
38 "debug_continue" => await DebugControlToolHandlers.HandleDebugContinue(args),
39 "debug_step_over" => await DebugControlToolHandlers.HandleDebugStepOver(args),
40 "debug_step_into" => await DebugControlToolHandlers.HandleDebugStepInto(args),
41 "debug_step_out" => await DebugControlToolHandlers.HandleDebugStepOut(args),
42 "debug_stop" => await DebugControlToolHandlers.HandleDebugStop(args),
43 "debug_stack_trace" => await DebugControlToolHandlers.HandleDebugStackTrace(args),
44 "debug_variables" => await DebugControlToolHandlers.HandleDebugVariables(args),
45 "debug_variable_children" => await DebugControlToolHandlers.HandleDebugVariableChildren(args),
46 _ => throw new ArgumentException($"Unknown tool: {name}.")
47 };
48 return new CallToolResult { Content = [new TextContentBlock { Text = text }] };
49 }
50 catch (ArgumentException ex)
51 {
52 return new CallToolResult { Content = [new TextContentBlock { Text = $"Error: {ex.Message}" }], IsError = true };
53 }
54 catch (OperationCanceledException)
55 {
56 // Хост MCP отменил вызов (таймаут, закрытие сессии) — не смешиваем с сбоями DAP/netcoredbg.
57 var tag = string.IsNullOrEmpty(name) ? "(unknown)" : name;
58 return new CallToolResult { Content = [new TextContentBlock { Text = $"# Aborted: {tag}" }] };
59 }
60 catch (Exception ex)
61 {
62 return new CallToolResult { Content = [new TextContentBlock { Text = "Error: " + DapHelpers.FormatException(ex) }], IsError = true };
63 }
64 }
65 }
66};
67
68var transport = new StdioServerTransport("DotnetDebugMcp");
69await using var server = McpServer.Create(transport, options);
70await server.RunAsync();
71return 0;
72
View only · write via MCP/CIDE