| 1 | using Avalonia; |
| 2 | using Avalonia.Controls.ApplicationLifetimes; |
| 3 | using Avalonia.Markup.Xaml; |
| 4 | using CascadeIDE.Features.UiChrome; |
| 5 | using CascadeIDE.Lang; |
| 6 | using CascadeIDE.ViewModels; |
| 7 | using CascadeIDE.Views; |
| 8 | using ModelContextProtocol.Server; |
| 9 | |
| 10 | namespace CascadeIDE; |
| 11 | |
| 12 | public partial class App : Application |
| 13 | { |
| 14 | /// <summary>Запуск с MCP-сервером на stdio (агент/Cursor подключается к IDE по stdin/stdout).</summary> |
| 15 | public static bool RunMcpStdio { get; set; } |
| 16 | |
| 17 | /// <summary><c>cide://</c> из argv при cold start (ADR 0157).</summary> |
| 18 | public static string? PendingMagicLinkUri { get; set; } |
| 19 | |
| 20 | internal static IDisposable? MagicLinkPrimaryMutex { get; set; } |
| 21 | |
| 22 | public override void Initialize() |
| 23 | { |
| 24 | AvaloniaXamlLoader.Load(this); |
| 25 | } |
| 26 | |
| 27 | public override void OnFrameworkInitializationCompleted() |
| 28 | { |
| 29 | UiCulture.ApplyFromSettingsOrSystem(); |
| 30 | UiModeCatalog.Initialize(); |
| 31 | if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop) |
| 32 | { |
| 33 | var vm = new MainWindowViewModel(); |
| 34 | vm.IsMcpServerMode = RunMcpStdio; |
| 35 | desktop.MainWindow = new MainWindow { DataContext = vm }; |
| 36 | if (RunMcpStdio) |
| 37 | _ = RunMcpServerAsync(vm); |
| 38 | if (!string.IsNullOrWhiteSpace(PendingMagicLinkUri)) |
| 39 | { |
| 40 | vm.EnqueueMagicLink(PendingMagicLinkUri); |
| 41 | PendingMagicLinkUri = null; |
| 42 | } |
| 43 | |
| 44 | _ = vm.RefreshOllamaAsync(); |
| 45 | } |
| 46 | |
| 47 | base.OnFrameworkInitializationCompleted(); |
| 48 | } |
| 49 | |
| 50 | private static async Task RunMcpServerAsync(MainWindowViewModel vm) |
| 51 | { |
| 52 | try |
| 53 | { |
| 54 | var options = Services.IdeMcpServer.BuildOptions(vm.IdeMcp); |
| 55 | await using var server = McpServer.Create(new StdioServerTransport("CascadeIDE"), options); |
| 56 | await server.RunAsync(); |
| 57 | } |
| 58 | catch (Exception ex) |
| 59 | { |
| 60 | System.Diagnostics.Debug.WriteLine($"MCP server error: {ex.Message}"); |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | } |
| 65 | |