|
| 1 | +using EmoTracker.Core; |
| 2 | +using Microsoft.AspNetCore.Builder; |
| 3 | +using Microsoft.AspNetCore.Hosting; |
| 4 | +using Microsoft.Extensions.DependencyInjection; |
| 5 | +using Microsoft.Extensions.Logging; |
| 6 | +using ModelContextProtocol; |
| 7 | +using Newtonsoft.Json.Linq; |
| 8 | +using Serilog; |
| 9 | +using System; |
| 10 | +using System.Threading; |
| 11 | +using System.Threading.Tasks; |
| 12 | + |
| 13 | +namespace EmoTracker.Extensions.McpServer |
| 14 | +{ |
| 15 | + public class McpServerExtension : ObservableObject, Extension |
| 16 | + { |
| 17 | + const int DefaultPort = 27125; |
| 18 | + |
| 19 | + public string Name => "MCP Dev Server"; |
| 20 | + public string UID => "emotracker_mcp_server"; |
| 21 | + public int Priority => -500; |
| 22 | + |
| 23 | + private bool mbActive; |
| 24 | + public bool Active |
| 25 | + { |
| 26 | + get => mbActive; |
| 27 | + private set => SetProperty(ref mbActive, value); |
| 28 | + } |
| 29 | + |
| 30 | + private string mStatusText = "Stopped"; |
| 31 | + public string StatusText |
| 32 | + { |
| 33 | + get => mStatusText; |
| 34 | + private set => SetProperty(ref mStatusText, value); |
| 35 | + } |
| 36 | + |
| 37 | + private WebApplication mApp; |
| 38 | + private McpStatusIndicator mStatusBarControl; |
| 39 | + |
| 40 | + public object StatusBarControl |
| 41 | + { |
| 42 | + get |
| 43 | + { |
| 44 | + if (!UserDirectory.IsDevMode) |
| 45 | + return null; |
| 46 | + |
| 47 | + if (mStatusBarControl == null) |
| 48 | + mStatusBarControl = new McpStatusIndicator { DataContext = this }; |
| 49 | + return mStatusBarControl; |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + public void Start() |
| 54 | + { |
| 55 | + if (!UserDirectory.IsDevMode) |
| 56 | + return; |
| 57 | + |
| 58 | + _ = StartServerAsync(); |
| 59 | + } |
| 60 | + |
| 61 | + public void Stop() |
| 62 | + { |
| 63 | + if (mApp != null) |
| 64 | + { |
| 65 | + try |
| 66 | + { |
| 67 | + mApp.StopAsync().GetAwaiter().GetResult(); |
| 68 | + mApp.DisposeAsync().AsTask().GetAwaiter().GetResult(); |
| 69 | + } |
| 70 | + catch (Exception ex) |
| 71 | + { |
| 72 | + Log.Warning(ex, "[MCP] Error stopping server"); |
| 73 | + } |
| 74 | + mApp = null; |
| 75 | + Active = false; |
| 76 | + StatusText = "Stopped"; |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + private async Task StartServerAsync() |
| 81 | + { |
| 82 | + try |
| 83 | + { |
| 84 | + int port = DefaultPort; |
| 85 | + string portEnv = Environment.GetEnvironmentVariable("EMOTRACKER_MCP_PORT"); |
| 86 | + if (!string.IsNullOrEmpty(portEnv) && int.TryParse(portEnv, out int envPort)) |
| 87 | + port = envPort; |
| 88 | + |
| 89 | + var builder = WebApplication.CreateBuilder(); |
| 90 | + builder.WebHost.UseUrls($"http://localhost:{port}"); |
| 91 | + |
| 92 | + builder.Logging.ClearProviders(); |
| 93 | + |
| 94 | + builder.Services.AddMcpServer(options => |
| 95 | + { |
| 96 | + options.ServerInfo = new() |
| 97 | + { |
| 98 | + Name = "EmoTracker Dev Server", |
| 99 | + Version = ApplicationVersion.Current.ToString() |
| 100 | + }; |
| 101 | + }) |
| 102 | + .WithTools<Tools.PackDataTools>() |
| 103 | + .WithTools<Tools.ApplicationControlTools>() |
| 104 | + .WithTools<Tools.LuaTools>() |
| 105 | + .WithTools<Tools.ScreenshotTools>() |
| 106 | + .WithTools<Tools.UiAutomationTools>() |
| 107 | + .WithTools<Tools.SettingsTools>() |
| 108 | + .WithTools<Tools.LocationTools>() |
| 109 | + .WithTools<Tools.SaveLoadTools>() |
| 110 | + .WithTools<Tools.WindowTools>() |
| 111 | + .WithTools<Tools.NoteTools>() |
| 112 | + .WithTools<Tools.ExtensionTools>() |
| 113 | + .WithTools<Tools.PackageTools>() |
| 114 | + .WithHttpTransport(); |
| 115 | + |
| 116 | + mApp = builder.Build(); |
| 117 | + mApp.MapMcp(); |
| 118 | + |
| 119 | + await mApp.StartAsync(); |
| 120 | + |
| 121 | + Active = true; |
| 122 | + StatusText = $"Listening on port {port}"; |
| 123 | + Log.Information("[MCP] Server listening on port {Port}", port); |
| 124 | + } |
| 125 | + catch (Exception ex) |
| 126 | + { |
| 127 | + Active = false; |
| 128 | + StatusText = $"Error: {ex.Message}"; |
| 129 | + Log.Error(ex, "[MCP] Failed to start server"); |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + public void OnPackageUnloaded() { } |
| 134 | + public void OnPackageLoaded() { } |
| 135 | + |
| 136 | + public JToken SerializeToJson() => null; |
| 137 | + public bool DeserializeFromJson(JToken token) => true; |
| 138 | + } |
| 139 | +} |
0 commit comments