-
Notifications
You must be signed in to change notification settings - Fork 1k
Expand file tree
/
Copy pathBatchExecute.cs
More file actions
323 lines (286 loc) · 12 KB
/
BatchExecute.cs
File metadata and controls
323 lines (286 loc) · 12 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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using MCPForUnity.Editor.Constants;
using MCPForUnity.Editor.Helpers;
using MCPForUnity.Editor.Services;
using Newtonsoft.Json.Linq;
using UnityEditor;
namespace MCPForUnity.Editor.Tools
{
/// <summary>
/// Executes multiple MCP commands within a single Unity-side handler. Commands are executed sequentially
/// on the main thread to preserve determinism and Unity API safety.
/// </summary>
[McpForUnityTool("batch_execute", AutoRegister = false)]
public static class BatchExecute
{
/// <summary>Default limit when no EditorPrefs override is set.</summary>
internal const int DefaultMaxCommandsPerBatch = 25;
/// <summary>Hard ceiling to prevent extreme editor freezes regardless of user setting.</summary>
internal const int AbsoluteMaxCommandsPerBatch = 100;
/// <summary>
/// Returns the user-configured max commands per batch, clamped between 1 and <see cref="AbsoluteMaxCommandsPerBatch"/>.
/// </summary>
internal static int GetMaxCommandsPerBatch()
{
int configured = EditorPrefs.GetInt(EditorPrefKeys.BatchExecuteMaxCommands, DefaultMaxCommandsPerBatch);
return Math.Clamp(configured, 1, AbsoluteMaxCommandsPerBatch);
}
public static async Task<object> HandleCommand(JObject @params)
{
if (@params == null)
{
return new ErrorResponse("'commands' payload is required.");
}
var commandsToken = @params["commands"] as JArray;
if (commandsToken == null || commandsToken.Count == 0)
{
return new ErrorResponse("Provide at least one command entry in 'commands'.");
}
int maxCommands = GetMaxCommandsPerBatch();
if (commandsToken.Count > maxCommands)
{
return new ErrorResponse(
$"A maximum of {maxCommands} commands are allowed per batch (configurable in MCP Tools window, hard max {AbsoluteMaxCommandsPerBatch}).");
}
// --- Async gateway path ---
bool isAsync = @params.Value<bool?>("async") ?? false;
if (isAsync)
{
return HandleAsyncSubmit(@params, commandsToken);
}
// --- Legacy synchronous path (unchanged) ---
bool failFast = @params.Value<bool?>("failFast") ?? false;
bool parallelRequested = @params.Value<bool?>("parallel") ?? false;
int? maxParallel = @params.Value<int?>("maxParallelism");
if (parallelRequested)
{
McpLog.Warn("batch_execute parallel mode requested, but commands will run sequentially on the main thread for safety.");
}
var commandResults = new List<object>(commandsToken.Count);
int invocationSuccessCount = 0;
int invocationFailureCount = 0;
bool anyCommandFailed = false;
foreach (var token in commandsToken)
{
if (token is not JObject commandObj)
{
invocationFailureCount++;
anyCommandFailed = true;
commandResults.Add(new
{
tool = (string)null,
callSucceeded = false,
error = "Command entries must be JSON objects."
});
if (failFast)
{
break;
}
continue;
}
string toolName = commandObj["tool"]?.ToString();
var rawParams = commandObj["params"] as JObject ?? new JObject();
var commandParams = NormalizeParameterKeys(rawParams);
if (string.IsNullOrWhiteSpace(toolName))
{
invocationFailureCount++;
anyCommandFailed = true;
commandResults.Add(new
{
tool = toolName,
callSucceeded = false,
error = "Each command must include a non-empty 'tool' field."
});
if (failFast)
{
break;
}
continue;
}
// Block disabled tools (mirrors TransportCommandDispatcher check)
var toolMeta = MCPServiceLocator.ToolDiscovery.GetToolMetadata(toolName);
if (toolMeta != null && !MCPServiceLocator.ToolDiscovery.IsToolEnabled(toolName))
{
invocationFailureCount++;
anyCommandFailed = true;
commandResults.Add(new
{
tool = toolName,
callSucceeded = false,
result = new ErrorResponse($"Tool '{toolName}' is disabled in the Unity Editor.")
});
if (failFast) break;
continue;
}
try
{
var result = await CommandRegistry.InvokeCommandAsync(toolName, commandParams).ConfigureAwait(true);
bool callSucceeded = DetermineCallSucceeded(result);
if (callSucceeded)
{
invocationSuccessCount++;
}
else
{
invocationFailureCount++;
anyCommandFailed = true;
}
commandResults.Add(new
{
tool = toolName,
callSucceeded,
result
});
if (!callSucceeded && failFast)
{
break;
}
}
catch (Exception ex)
{
invocationFailureCount++;
anyCommandFailed = true;
commandResults.Add(new
{
tool = toolName,
callSucceeded = false,
error = ex.Message
});
if (failFast)
{
break;
}
}
}
bool overallSuccess = !anyCommandFailed;
var data = new
{
results = commandResults,
callSuccessCount = invocationSuccessCount,
callFailureCount = invocationFailureCount,
parallelRequested,
parallelApplied = false,
maxParallelism = maxParallel
};
return overallSuccess
? new SuccessResponse("Batch execution completed.", data)
: new ErrorResponse("One or more commands failed.", data);
}
private static bool DetermineCallSucceeded(object result)
{
if (result == null)
{
return true;
}
if (result is IMcpResponse response)
{
return response.Success;
}
if (result is JObject obj)
{
var successToken = obj["success"];
if (successToken != null && successToken.Type == JTokenType.Boolean)
{
return successToken.Value<bool>();
}
}
if (result is JToken token)
{
var successToken = token["success"];
if (successToken != null && successToken.Type == JTokenType.Boolean)
{
return successToken.Value<bool>();
}
}
return true;
}
private static JObject NormalizeParameterKeys(JObject source)
{
if (source == null)
{
return new JObject();
}
var normalized = new JObject();
foreach (var property in source.Properties())
{
string normalizedName = ToCamelCase(property.Name);
normalized[normalizedName] = property.Value;
}
return normalized;
}
private static string ToCamelCase(string key) => StringCaseUtility.ToCamelCase(key);
/// <summary>
/// Handle async batch submission. Queues commands via CommandGateway and returns
/// a ticket (for non-instant batches) or results inline (for instant batches).
/// </summary>
private static object HandleAsyncSubmit(JObject @params, JArray commandsToken)
{
bool atomic = @params.Value<bool?>("atomic") ?? false;
string agent = @params.Value<string>("agent") ?? "anonymous";
string label = @params.Value<string>("label") ?? "";
var commands = new List<BatchCommand>();
foreach (var token in commandsToken)
{
if (token is not JObject cmdObj) continue;
string toolName = cmdObj["tool"]?.ToString();
if (string.IsNullOrWhiteSpace(toolName)) continue;
var rawParams = cmdObj["params"] as JObject ?? new JObject();
var cmdParams = NormalizeParameterKeys(rawParams);
var toolTier = CommandRegistry.GetToolTier(toolName);
var effectiveTier = CommandClassifier.Classify(toolName, toolTier, cmdParams);
commands.Add(new BatchCommand { Tool = toolName, Params = cmdParams, Tier = effectiveTier, CausesDomainReload = CommandClassifier.CausesDomainReload(toolName, cmdParams) });
}
if (commands.Count == 0)
{
return new ErrorResponse("No valid commands in async batch.");
}
var job = CommandGatewayState.Queue.Submit(agent, label, atomic, commands);
if (job.Tier == ExecutionTier.Instant)
{
// Execute inline, return results directly
foreach (var cmd in commands)
{
try
{
var result = CommandRegistry.InvokeCommandAsync(cmd.Tool, cmd.Params)
.ConfigureAwait(true).GetAwaiter().GetResult();
job.Results.Add(result);
}
catch (Exception ex)
{
job.Results.Add(new ErrorResponse(ex.Message));
if (atomic)
{
job.Status = JobStatus.Failed;
job.Error = ex.Message;
job.CompletedAt = DateTime.UtcNow;
return new ErrorResponse($"Instant batch failed at command '{cmd.Tool}': {ex.Message}",
new { ticket = job.Ticket, results = job.Results });
}
}
}
job.Status = JobStatus.Done;
job.CompletedAt = DateTime.UtcNow;
return new SuccessResponse("Batch completed (instant).",
new { ticket = job.Ticket, results = job.Results });
}
// Non-instant: return ticket for polling
return new PendingResponse(
$"Batch queued as {job.Ticket}. Poll with poll_job.",
pollIntervalSeconds: 2.0,
data: new
{
ticket = job.Ticket,
status = job.Status.ToString().ToLowerInvariant(),
position = CommandGatewayState.Queue.GetAheadOf(job.Ticket).Count,
tier = job.Tier.ToString().ToLowerInvariant(),
agent,
label,
atomic
});
}
}
}