forked from IvanMurzak/Unity-MCP
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUnityMcpPlugin.cs
More file actions
275 lines (232 loc) · 10.5 KB
/
UnityMcpPlugin.cs
File metadata and controls
275 lines (232 loc) · 10.5 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
/*
┌──────────────────────────────────────────────────────────────────┐
│ Author: Ivan Murzak (https://github.com/IvanMurzak) │
│ Repository: GitHub (https://github.com/IvanMurzak/Unity-MCP) │
│ Copyright (c) 2025 Ivan Murzak │
│ Licensed under the Apache License, Version 2.0. │
│ See the LICENSE file in the project root for more information. │
└──────────────────────────────────────────────────────────────────┘
*/
#nullable enable
using System;
using System.Threading;
using System.Threading.Tasks;
using com.IvanMurzak.McpPlugin;
using com.IvanMurzak.McpPlugin.Common.Model;
using com.IvanMurzak.ReflectorNet;
using Microsoft.AspNetCore.SignalR.Client;
using Microsoft.Extensions.Logging;
using R3;
namespace com.IvanMurzak.Unity.MCP
{
using LogLevel = Runtime.Utils.LogLevel;
public partial class UnityMcpPlugin : IDisposable
{
public const string Version = "0.51.6";
private static int _singletonCount = 0;
public static bool HasAnyInstance => _singletonCount > 0;
protected static void IncrementSingletonCount() => Interlocked.Increment(ref _singletonCount);
protected static void DecrementSingletonCount() => Interlocked.Decrement(ref _singletonCount);
private static LogLevel _configuredLogLevel = LogLevel.Warning;
// Uses direct enum comparison: configured threshold <= requested level means enabled
public static bool IsLogEnabled(LogLevel level) => _configuredLogLevel <= level;
public static void ApplyLogLevel(LogLevel level) => _configuredLogLevel = level;
protected readonly CompositeDisposable _disposables = new();
protected readonly McpPluginSlot _plugin = new();
// Tracks only the latest plugin's ConnectionState subscription.
// Replaced (old one disposed) each time BuildMcpPlugin creates a new IMcpPlugin instance.
private IDisposable? _pluginConnectionSubscription;
public IMcpPlugin? McpPluginInstance => _plugin.Instance;
public bool HasMcpPluginInstance => _plugin.HasInstance;
public ILogger Logger => McpPluginInstance?.Logger ?? _logger;
public Reflector? Reflector => McpPluginInstance?.McpManager.Reflector;
public IToolManager? Tools => McpPluginInstance?.McpManager.ToolManager;
public IPromptManager? Prompts => McpPluginInstance?.McpManager.PromptManager;
public IResourceManager? Resources => McpPluginInstance?.McpManager.ResourceManager;
public UnityLogCollector? LogCollector { get; protected set; } = null;
// --- Connection state ---
protected readonly ReactiveProperty<HubConnectionState> _connectionState
= new(HubConnectionState.Disconnected);
public ReadOnlyReactiveProperty<HubConnectionState> ConnectionState => _connectionState;
public ReadOnlyReactiveProperty<bool> IsConnected => _connectionState
.Select(x => x == HubConnectionState.Connected)
.ToReadOnlyReactiveProperty(false);
// --- Constructor / Dispose ---
protected UnityMcpPlugin()
{
_disposables.Add(_connectionState);
}
public virtual void Dispose()
{
_pluginConnectionSubscription?.Dispose();
_pluginConnectionSubscription = null;
_disposables.Dispose();
// LogCollector and _connectionState are disposed by _disposables
LogCollector = null;
}
// --- Log collector ---
public void AddUnityLogCollector(ILogStorage logStorage)
{
if (logStorage == null)
throw new ArgumentNullException(nameof(logStorage));
if (LogCollector != null)
throw new InvalidOperationException($"{nameof(UnityLogCollector)} is already added.");
LogCollector = new UnityLogCollector(logStorage);
_disposables.Add(LogCollector);
}
public void AddUnityLogCollectorIfNeeded(Func<ILogStorage> logStorageProvider)
{
if (LogCollector != null)
return;
AddUnityLogCollector(logStorageProvider());
}
public void DisposeLogCollector()
{
LogCollector?.Save();
LogCollector?.Dispose();
LogCollector = null;
}
// --- Connection methods ---
public Task<bool> ConnectIfNeeded()
{
if (!unityConnectionConfig.KeepConnected)
return Task.FromResult(false);
return Connect();
}
public async Task<bool> Connect()
{
_logger.LogTrace("{method} called.", nameof(Connect));
try
{
var mcpPlugin = McpPluginInstance;
if (mcpPlugin == null)
{
_logger.LogError("{method}: McpPlugin instance is null.", nameof(Connect));
return false;
}
return await mcpPlugin.Connect();
}
finally
{
_logger.LogTrace("{method} completed.", nameof(Connect));
}
}
public async Task Disconnect()
{
_logger.LogTrace("{method} called.", nameof(Disconnect));
try
{
var mcpPlugin = McpPluginInstance;
if (mcpPlugin == null)
{
_logger.LogWarning("{method}: McpPlugin instance is null, nothing to disconnect, ignoring.",
nameof(Disconnect));
return;
}
try
{
_logger.LogDebug("{method}: Disconnecting McpPlugin instance.", nameof(Disconnect));
await mcpPlugin.Disconnect();
}
catch (Exception e)
{
_logger.LogError("{method}: Exception during disconnecting: {exception}",
nameof(Disconnect), e);
}
}
finally
{
_logger.LogTrace("{method} completed.", nameof(Disconnect));
}
}
public void DisconnectImmediate()
{
_logger.LogTrace("{method} called.", nameof(DisconnectImmediate));
try
{
var mcpPlugin = McpPluginInstance;
if (mcpPlugin == null)
{
_logger.LogWarning("{method}: McpPlugin instance is null, nothing to disconnect, ignoring.",
nameof(DisconnectImmediate));
return;
}
try
{
_logger.LogDebug("{method}: Disconnecting McpPlugin instance.", nameof(DisconnectImmediate));
mcpPlugin.DisconnectImmediate();
}
catch (Exception e)
{
_logger.LogError("{method}: Exception during disconnecting: {exception}",
nameof(DisconnectImmediate), e);
}
}
finally
{
_logger.LogTrace("{method} completed.", nameof(DisconnectImmediate));
}
}
public async Task NotifyToolRequestCompleted(RequestToolCompletedData request, CancellationToken cancellationToken = default)
{
var mcpPlugin = McpPluginInstance
?? throw new InvalidOperationException($"{nameof(McpPluginInstance)} is null");
while (mcpPlugin.ConnectionState.CurrentValue != HubConnectionState.Connected)
{
await Task.Delay(100, cancellationToken);
if (cancellationToken.IsCancellationRequested)
{
_logger.LogWarning("{method}: operation cancelled while waiting for connection.",
nameof(NotifyToolRequestCompleted));
return;
}
}
if (mcpPlugin.McpManager == null)
{
_logger.LogCritical("{method}: {instance} is null",
nameof(NotifyToolRequestCompleted), nameof(mcpPlugin.McpManager));
return;
}
if (mcpPlugin.McpManagerHub == null)
{
_logger.LogCritical("{method}: {instance} is null",
nameof(NotifyToolRequestCompleted), nameof(mcpPlugin.McpManagerHub));
return;
}
await mcpPlugin.McpManagerHub.NotifyToolRequestCompleted(request);
}
// --- Token / Port utilities ---
/// <summary>
/// Generates a cryptographically random URL-safe token.
/// Used by <see cref="UnityConnectionConfig.SetDefault"/> for initial token generation.
/// </summary>
public static string GenerateToken()
{
var bytes = new byte[32];
using var rng = System.Security.Cryptography.RandomNumberGenerator.Create();
rng.GetBytes(bytes);
return Convert.ToBase64String(bytes).TrimEnd('=').Replace('+', '-').Replace('/', '_');
}
/// <summary>
/// Generate a deterministic TCP port based on current directory.
/// Uses SHA256 hash for better distribution and less collisions.
/// Port range: 50000-59999 (less commonly used dynamic ports).
/// </summary>
public static int GeneratePortFromDirectory()
{
const int MinPort = 50000; // Higher range to avoid common dynamic ports
const int MaxPort = 59999;
const int PortRange = MaxPort - MinPort + 1;
var currentDir = Environment.CurrentDirectory.ToLowerInvariant();
using (var sha256 = System.Security.Cryptography.SHA256.Create())
{
var hashBytes = sha256.ComputeHash(System.Text.Encoding.UTF8.GetBytes(currentDir));
// Use first 4 bytes as an unsigned integer to avoid Math.Abs(int.MinValue) overflow
var hash = (uint)BitConverter.ToInt32(hashBytes, 0);
// Map to port range
var port = MinPort + (int)(hash % PortRange);
return port;
}
}
}
}