-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathTransportCommandDispatcher.cs
More file actions
633 lines (559 loc) · 25 KB
/
Copy pathTransportCommandDispatcher.cs
File metadata and controls
633 lines (559 loc) · 25 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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using MCPForUnity.Editor.Helpers;
using MCPForUnity.Editor.Models;
using MCPForUnity.Editor.Services;
using MCPForUnity.Editor.Tools;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using UnityEditor;
namespace MCPForUnity.Editor.Services.Transport
{
/// <summary>
/// Centralised command execution pipeline shared by all transport implementations.
/// Guarantees that MCP commands are executed on the Unity main thread while preserving
/// the legacy response format expected by the server.
/// </summary>
[InitializeOnLoad]
internal static class TransportCommandDispatcher
{
private static SynchronizationContext _mainThreadContext;
private static int _mainThreadId;
private static int _processingFlag;
private sealed class PendingCommand
{
public PendingCommand(
string commandJson,
TaskCompletionSource<string> completionSource,
CancellationToken cancellationToken,
CancellationTokenRegistration registration)
{
CommandJson = commandJson;
CompletionSource = completionSource;
CancellationToken = cancellationToken;
CancellationRegistration = registration;
QueuedAt = DateTime.UtcNow;
}
public string CommandJson { get; }
public TaskCompletionSource<string> CompletionSource { get; }
public CancellationToken CancellationToken { get; }
public CancellationTokenRegistration CancellationRegistration { get; }
public bool IsExecuting { get; set; }
public DateTime QueuedAt { get; }
public void Dispose()
{
CancellationRegistration.Dispose();
}
public void TrySetResult(string payload)
{
CompletionSource.TrySetResult(payload);
}
public void TrySetCanceled()
{
CompletionSource.TrySetCanceled(CancellationToken);
}
}
private static readonly Dictionary<string, PendingCommand> Pending = new();
/// <summary>
/// Maps command JSON content hash → pending ID for deduplication.
/// When a duplicate command arrives while an identical one is still pending,
/// the duplicate shares the original's TaskCompletionSource instead of queueing again.
/// </summary>
private static readonly Dictionary<string, string> ContentHashToPendingId = new();
private static readonly object PendingLock = new();
/// <summary>
/// Maximum age of a pending command that is still eligible for dedup matching.
/// If the original command hangs (stuck gateway job, lost transport, etc.) waiters
/// must not pile up indefinitely on a dead TaskCompletionSource — after this
/// window, new identical commands create a fresh pending entry instead.
/// </summary>
private static readonly TimeSpan DedupTtl = TimeSpan.FromSeconds(60);
private static bool updateHooked;
private static bool initialised;
static TransportCommandDispatcher()
{
// Ensure this runs on the Unity main thread at editor load.
_mainThreadContext = SynchronizationContext.Current;
_mainThreadId = Thread.CurrentThread.ManagedThreadId;
EnsureInitialised();
// Always keep the update hook installed so commands arriving from background
// websocket tasks don't depend on a background-thread event subscription.
if (!updateHooked)
{
updateHooked = true;
EditorApplication.update += ProcessQueue;
}
}
/// <summary>
/// Schedule a command for execution on the Unity main thread and await its JSON response.
/// </summary>
public static Task<string> ExecuteCommandJsonAsync(string commandJson, CancellationToken cancellationToken)
{
if (commandJson is null)
{
throw new ArgumentNullException(nameof(commandJson));
}
EnsureInitialised();
// --- Deduplication: if an identical command is already pending, share its result ---
var contentHash = ComputeContentHash(commandJson);
lock (PendingLock)
{
if (contentHash != null
&& ContentHashToPendingId.TryGetValue(contentHash, out var existingId)
&& Pending.TryGetValue(existingId, out var existingPending)
&& !existingPending.CancellationToken.IsCancellationRequested
&& (DateTime.UtcNow - existingPending.QueuedAt) < DedupTtl)
{
McpLog.Info($"[Dispatcher] Dedup: identical command already pending (id={existingId}). Sharing result.");
// Propagate the NEW caller's cancellation into a linked waiter so each dedup
// waiter can bail independently. Without this, a cancelled caller still blocks
// on the original command's TCS — which is the root cause of stall-under-load.
if (cancellationToken.CanBeCanceled)
{
var waiterTcs = new TaskCompletionSource<string>(TaskCreationOptions.RunContinuationsAsynchronously);
var waiterReg = cancellationToken.Register(() => waiterTcs.TrySetCanceled(cancellationToken));
existingPending.CompletionSource.Task.ContinueWith(t =>
{
waiterReg.Dispose();
if (t.IsCanceled) waiterTcs.TrySetCanceled();
else if (t.IsFaulted) waiterTcs.TrySetException(t.Exception!.InnerExceptions);
else waiterTcs.TrySetResult(t.Result);
}, TaskScheduler.Default);
return waiterTcs.Task;
}
return existingPending.CompletionSource.Task;
}
// Stale dedup entry (TTL exceeded or cancelled original): drop the mapping
// so the code below creates a fresh PendingCommand instead of piling onto
// a dead TCS.
if (contentHash != null
&& ContentHashToPendingId.TryGetValue(contentHash, out var staleId)
&& Pending.TryGetValue(staleId, out var stalePending)
&& (stalePending.CancellationToken.IsCancellationRequested
|| (DateTime.UtcNow - stalePending.QueuedAt) >= DedupTtl))
{
McpLog.Warn($"[Dispatcher] Dedup entry stale for id={staleId} (age={(DateTime.UtcNow - stalePending.QueuedAt).TotalSeconds:F0}s). Releasing hash for fresh dispatch.");
ContentHashToPendingId.Remove(contentHash);
}
}
var id = Guid.NewGuid().ToString("N");
var tcs = new TaskCompletionSource<string>(TaskCreationOptions.RunContinuationsAsynchronously);
var registration = cancellationToken.CanBeCanceled
? cancellationToken.Register(() => CancelPending(id, cancellationToken))
: default;
var pending = new PendingCommand(commandJson, tcs, cancellationToken, registration);
lock (PendingLock)
{
Pending[id] = pending;
if (contentHash != null)
ContentHashToPendingId[contentHash] = id;
}
// Proactively wake up the main thread execution loop. This improves responsiveness
// in scenarios where EditorApplication.update is throttled or temporarily not firing
// (e.g., Unity unfocused, compiling, or during domain reload transitions).
RequestMainThreadPump();
return tcs.Task;
}
internal static Task<T> RunOnMainThreadAsync<T>(Func<T> func, CancellationToken cancellationToken)
{
if (func is null)
{
throw new ArgumentNullException(nameof(func));
}
var tcs = new TaskCompletionSource<T>(TaskCreationOptions.RunContinuationsAsynchronously);
var registration = cancellationToken.CanBeCanceled
? cancellationToken.Register(() => tcs.TrySetCanceled(cancellationToken))
: default;
void Invoke()
{
try
{
if (tcs.Task.IsCompleted)
{
return;
}
var result = func();
tcs.TrySetResult(result);
}
catch (Exception ex)
{
tcs.TrySetException(ex);
}
finally
{
registration.Dispose();
}
}
// Best-effort nudge: if we're posting from a background thread (e.g., websocket receive),
// encourage Unity to run a loop iteration so the posted callback can execute even when unfocused.
try { EditorApplication.QueuePlayerLoopUpdate(); } catch { }
if (_mainThreadContext != null && Thread.CurrentThread.ManagedThreadId != _mainThreadId)
{
_mainThreadContext.Post(_ => Invoke(), null);
return tcs.Task;
}
Invoke();
return tcs.Task;
}
private static void RequestMainThreadPump()
{
void Pump()
{
try
{
// Hint Unity to run a loop iteration soon.
EditorApplication.QueuePlayerLoopUpdate();
}
catch
{
// Best-effort only.
}
ProcessQueue();
}
if (_mainThreadContext != null && Thread.CurrentThread.ManagedThreadId != _mainThreadId)
{
_mainThreadContext.Post(_ => Pump(), null);
return;
}
Pump();
}
private static void EnsureInitialised()
{
if (initialised)
{
return;
}
CommandRegistry.Initialize();
initialised = true;
}
private static void HookUpdate()
{
// Deprecated: we keep the update hook installed permanently (see static ctor).
if (updateHooked) return;
updateHooked = true;
EditorApplication.update += ProcessQueue;
}
private static void UnhookUpdateIfIdle()
{
// Intentionally no-op: keep update hook installed so background commands always process.
// This avoids "must focus Unity to re-establish contact" edge cases.
return;
}
private static void ProcessQueue()
{
if (Interlocked.Exchange(ref _processingFlag, 1) == 1)
{
return;
}
try
{
List<(string id, PendingCommand pending)> ready;
lock (PendingLock)
{
// Early exit inside lock to prevent per-frame List allocations (GitHub issue #577)
if (Pending.Count == 0)
{
return;
}
ready = new List<(string, PendingCommand)>(Pending.Count);
foreach (var kvp in Pending)
{
if (kvp.Value.IsExecuting)
{
continue;
}
kvp.Value.IsExecuting = true;
ready.Add((kvp.Key, kvp.Value));
}
if (ready.Count == 0)
{
UnhookUpdateIfIdle();
return;
}
}
foreach (var (id, pending) in ready)
{
ProcessCommand(id, pending);
}
}
finally
{
Interlocked.Exchange(ref _processingFlag, 0);
}
}
private static void ProcessCommand(string id, PendingCommand pending)
{
if (pending.CancellationToken.IsCancellationRequested)
{
RemovePending(id, pending);
pending.TrySetCanceled();
return;
}
string commandText = pending.CommandJson?.Trim();
if (string.IsNullOrEmpty(commandText))
{
pending.TrySetResult(SerializeError("Empty command received"));
RemovePending(id, pending);
return;
}
if (string.Equals(commandText, "ping", StringComparison.OrdinalIgnoreCase))
{
var pingResponse = new
{
status = "success",
result = new { message = "pong" }
};
pending.TrySetResult(JsonConvert.SerializeObject(pingResponse));
RemovePending(id, pending);
return;
}
if (!IsValidJson(commandText))
{
var invalidJsonResponse = new
{
status = "error",
error = "Invalid JSON format",
receivedText = commandText.Length > 50 ? commandText[..50] + "..." : commandText
};
pending.TrySetResult(JsonConvert.SerializeObject(invalidJsonResponse));
RemovePending(id, pending);
return;
}
try
{
var command = JsonConvert.DeserializeObject<Command>(commandText);
if (command == null)
{
pending.TrySetResult(SerializeError("Command deserialized to null", "Unknown", commandText));
RemovePending(id, pending);
return;
}
if (string.IsNullOrWhiteSpace(command.type))
{
pending.TrySetResult(SerializeError("Command type cannot be empty"));
RemovePending(id, pending);
return;
}
if (string.Equals(command.type, "ping", StringComparison.OrdinalIgnoreCase))
{
var pingResponse = new
{
status = "success",
result = new { message = "pong" }
};
pending.TrySetResult(JsonConvert.SerializeObject(pingResponse));
RemovePending(id, pending);
return;
}
var parameters = command.@params ?? new JObject();
// Block execution of disabled resources
var resourceMeta = MCPServiceLocator.ResourceDiscovery.GetResourceMetadata(command.type);
if (resourceMeta != null && !MCPServiceLocator.ResourceDiscovery.IsResourceEnabled(command.type))
{
pending.TrySetResult(SerializeError(
$"Resource '{command.type}' is disabled in the Unity Editor."));
RemovePending(id, pending);
return;
}
// Block execution of disabled tools
var toolMeta = MCPServiceLocator.ToolDiscovery.GetToolMetadata(command.type);
if (toolMeta != null && !MCPServiceLocator.ToolDiscovery.IsToolEnabled(command.type))
{
pending.TrySetResult(SerializeError(
$"Tool '{command.type}' is disabled in the Unity Editor."));
RemovePending(id, pending);
return;
}
var logType = resourceMeta != null ? "resource" : toolMeta != null ? "tool" : "unknown";
// --- Tier-aware dispatch ---
var declaredTier = CommandRegistry.GetToolTier(command.type);
var effectiveTier = CommandClassifier.Classify(command.type, declaredTier, parameters);
if (effectiveTier != ExecutionTier.Instant)
{
// Route Smooth/Heavy through the gateway queue for tier-aware scheduling,
// heavy exclusivity, domain-reload guards, and CancellationToken support.
var job = CommandGatewayState.Queue.SubmitSingle(command.type, parameters, "transport");
var gatewaySw = McpLogRecord.IsEnabled ? System.Diagnostics.Stopwatch.StartNew() : null;
async void AwaitGateway()
{
try
{
var gatewayResult = await CommandGatewayState.AwaitJob(job);
gatewaySw?.Stop();
if (gatewayResult is IMcpResponse mcpResp && !mcpResp.Success)
{
McpLogRecord.Log(command.type, parameters, logType, "ERROR", gatewaySw?.ElapsedMilliseconds ?? 0, (gatewayResult as ErrorResponse)?.Error);
var errResponse = new { status = "error", result = gatewayResult, _queue = new { ticket = job.Ticket, tier = job.Tier.ToString().ToLowerInvariant(), queued = true } };
pending.TrySetResult(JsonConvert.SerializeObject(errResponse));
}
else
{
McpLogRecord.Log(command.type, parameters, logType, "SUCCESS", gatewaySw?.ElapsedMilliseconds ?? 0, null);
var okResponse = new { status = "success", result = gatewayResult, _queue = new { ticket = job.Ticket, tier = job.Tier.ToString().ToLowerInvariant(), queued = true } };
pending.TrySetResult(JsonConvert.SerializeObject(okResponse));
}
}
catch (Exception ex)
{
gatewaySw?.Stop();
McpLogRecord.Log(command.type, parameters, logType, "ERROR", gatewaySw?.ElapsedMilliseconds ?? 0, ex.Message);
pending.TrySetResult(SerializeError(ex.Message, command.type, ex.StackTrace));
}
finally
{
EditorApplication.delayCall += () => RemovePending(id, pending);
}
}
AwaitGateway();
return;
}
// --- Instant tier: execute directly (existing path) ---
var sw = McpLogRecord.IsEnabled ? System.Diagnostics.Stopwatch.StartNew() : null;
var result = CommandRegistry.ExecuteCommand(command.type, parameters, pending.CompletionSource);
if (result == null)
{
// Async command – cleanup after completion on next editor frame to preserve order.
var capturedType = command.type;
var capturedParams = parameters;
var capturedLogType = logType;
pending.CompletionSource.Task.ContinueWith(t =>
{
sw?.Stop();
var logStatus = "SUCCESS";
string logError = null;
if (t.IsFaulted)
{
logStatus = "ERROR";
logError = t.Exception?.InnerException?.Message;
}
else if (t.IsCompletedSuccessfully && t.Result != null)
{
try
{
var resultObj = JObject.Parse(t.Result);
if (string.Equals(resultObj.Value<string>("status"), "error", StringComparison.OrdinalIgnoreCase))
{
logStatus = "ERROR";
logError = resultObj.Value<string>("error");
}
}
catch { }
}
McpLogRecord.Log(capturedType, capturedParams, capturedLogType,
logStatus, sw?.ElapsedMilliseconds ?? 0, logError);
EditorApplication.delayCall += () => RemovePending(id, pending);
}, TaskScheduler.Default);
return;
}
sw?.Stop();
string syncLogStatus = "SUCCESS";
string syncLogError = null;
if (result is ErrorResponse errResp)
{
syncLogStatus = "ERROR";
syncLogError = errResp.Error;
}
McpLogRecord.Log(command.type, parameters, logType, syncLogStatus, sw?.ElapsedMilliseconds ?? 0, syncLogError);
var directResponse = new { status = "success", result };
pending.TrySetResult(JsonConvert.SerializeObject(directResponse));
RemovePending(id, pending);
}
catch (Exception ex)
{
McpLog.Error($"Error processing command: {ex.Message}\n{ex.StackTrace}");
pending.TrySetResult(SerializeError(ex.Message, "Unknown (error during processing)", ex.StackTrace));
RemovePending(id, pending);
}
}
private static void CancelPending(string id, CancellationToken token)
{
PendingCommand pending = null;
lock (PendingLock)
{
if (Pending.Remove(id, out pending))
{
CleanContentHash(id);
UnhookUpdateIfIdle();
}
}
pending?.TrySetCanceled();
pending?.Dispose();
}
private static void RemovePending(string id, PendingCommand pending)
{
lock (PendingLock)
{
Pending.Remove(id);
CleanContentHash(id);
UnhookUpdateIfIdle();
}
pending.Dispose();
}
/// <summary>
/// Remove the content hash entry that points to the given pending ID.
/// Must be called under PendingLock.
/// </summary>
private static void CleanContentHash(string pendingId)
{
string hashToRemove = null;
foreach (var kvp in ContentHashToPendingId)
{
if (kvp.Value == pendingId)
{
hashToRemove = kvp.Key;
break;
}
}
if (hashToRemove != null)
ContentHashToPendingId.Remove(hashToRemove);
}
/// <summary>
/// Compute a stable content hash for command deduplication.
/// Returns null for non-JSON commands (e.g., "ping") which are cheap enough to not need dedup.
/// </summary>
private static string ComputeContentHash(string commandJson)
{
if (string.IsNullOrWhiteSpace(commandJson)) return null;
var trimmed = commandJson.Trim();
if (!trimmed.StartsWith("{")) return null; // Skip non-JSON (ping, etc.)
// Use the raw JSON string as the hash key. Retries from the same client produce
// byte-identical JSON, so this is both fast and correct for the dedup use case.
// For very large payloads, a proper hash could be used, but MCP commands are small.
return trimmed;
}
private static string SerializeError(string message, string commandType = null, string stackTrace = null)
{
var errorResponse = new
{
status = "error",
error = message,
command = commandType ?? "Unknown",
stackTrace
};
return JsonConvert.SerializeObject(errorResponse);
}
private static bool IsValidJson(string text)
{
if (string.IsNullOrWhiteSpace(text))
{
return false;
}
text = text.Trim();
if ((text.StartsWith("{") && text.EndsWith("}")) || (text.StartsWith("[") && text.EndsWith("]")))
{
try
{
JToken.Parse(text);
return true;
}
catch
{
return false;
}
}
return false;
}
}
}