-
Notifications
You must be signed in to change notification settings - Fork 1k
Expand file tree
/
Copy pathExecuteCode.cs
More file actions
350 lines (302 loc) · 12.7 KB
/
ExecuteCode.cs
File metadata and controls
350 lines (302 loc) · 12.7 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
using System;
using System.CodeDom.Compiler;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using MCPForUnity.Editor.Helpers;
using Microsoft.CSharp;
using Newtonsoft.Json.Linq;
using UnityEngine;
namespace MCPForUnity.Editor.Tools
{
[McpForUnityTool("execute_code", AutoRegister = false)]
public static class ExecuteCode
{
private const int MaxCodeLength = 50000;
private const int MaxHistoryEntries = 50;
private const int MaxHistoryCodePreview = 500;
private const int WrapperLineOffset = 10;
private const string WrapperClassName = "MCPDynamicCode";
private const string WrapperMethodName = "Execute";
private const string ActionExecute = "execute";
private const string ActionGetHistory = "get_history";
private const string ActionClearHistory = "clear_history";
private const string ActionReplay = "replay";
private static readonly List<HistoryEntry> _history = new List<HistoryEntry>();
private static string[] _cachedAssemblyPaths;
private static readonly HashSet<string> _blockedPatterns = new HashSet<string>(StringComparer.OrdinalIgnoreCase)
{
"System.IO.File.Delete",
"System.IO.Directory.Delete",
"FileUtil.DeleteFileOrDirectory",
"AssetDatabase.DeleteAsset",
"AssetDatabase.MoveAssetToTrash",
"EditorApplication.Exit",
"Process.Start",
"Process.Kill",
"while(true)",
"while (true)",
"for(;;)",
"for (;;)",
};
public static object HandleCommand(JObject @params)
{
if (@params == null)
return new ErrorResponse("Parameters cannot be null.");
var p = new ToolParams(@params);
var actionResult = p.GetRequired("action");
if (!actionResult.IsSuccess)
return new ErrorResponse(actionResult.ErrorMessage);
string action = actionResult.Value.ToLowerInvariant();
switch (action)
{
case ActionExecute:
return HandleExecute(@params);
case ActionGetHistory:
return HandleGetHistory(@params);
case ActionClearHistory:
return HandleClearHistory();
case ActionReplay:
return HandleReplay(@params);
default:
return new ErrorResponse(
$"Unknown action: '{action}'. Valid actions: {ActionExecute}, {ActionGetHistory}, {ActionClearHistory}, {ActionReplay}");
}
}
private static object HandleExecute(JObject @params)
{
string code = @params["code"]?.ToString();
if (string.IsNullOrWhiteSpace(code))
return new ErrorResponse("Required parameter 'code' is missing or empty.");
if (code.Length > MaxCodeLength)
return new ErrorResponse($"Code exceeds maximum length of {MaxCodeLength} characters.");
bool safetyChecks = @params["safety_checks"]?.Value<bool>() ?? true;
if (safetyChecks)
{
var violation = CheckBlockedPatterns(code);
if (violation != null)
return new ErrorResponse($"Blocked pattern detected: {violation}");
}
try
{
var startTime = DateTime.UtcNow;
var result = CompileAndExecute(code);
var elapsed = (DateTime.UtcNow - startTime).TotalMilliseconds;
AddToHistory(code, result, elapsed, safetyChecks);
return result;
}
catch (Exception e)
{
McpLog.Error($"[ExecuteCode] Execution failed: {e}");
var errorResult = new ErrorResponse($"Execution failed: {e.Message}");
AddToHistory(code, errorResult, 0, safetyChecks);
return errorResult;
}
}
private static object HandleGetHistory(JObject @params)
{
int limit = @params["limit"]?.Value<int>() ?? 10;
limit = Math.Clamp(limit, 1, MaxHistoryEntries);
if (_history.Count == 0)
return new SuccessResponse("No execution history.", new { total = 0, entries = new object[0] });
var entries = _history.Skip(Math.Max(0, _history.Count - limit)).ToList();
return new SuccessResponse($"Returning {entries.Count} of {_history.Count} history entries.", new
{
total = _history.Count,
entries = entries.Select((e, i) => new
{
index = _history.Count - entries.Count + i,
codePreview = e.code.Length > MaxHistoryCodePreview
? e.code.Substring(0, MaxHistoryCodePreview) + "..."
: e.code,
e.success,
e.resultPreview,
e.elapsedMs,
e.timestamp,
e.safetyChecksEnabled,
}).ToList(),
});
}
private static object HandleClearHistory()
{
int count = _history.Count;
_history.Clear();
return new SuccessResponse($"Cleared {count} history entries.");
}
private static object HandleReplay(JObject @params)
{
if (_history.Count == 0)
return new ErrorResponse("No execution history to replay.");
int? index = @params["index"]?.Value<int>();
if (index == null || index < 0 || index >= _history.Count)
return new ErrorResponse($"Invalid history index. Valid range: 0-{_history.Count - 1}");
var entry = _history[index.Value];
var replayParams = JObject.FromObject(new
{
action = ActionExecute,
code = entry.code,
safety_checks = entry.safetyChecksEnabled,
});
return HandleExecute(replayParams);
}
private static object CompileAndExecute(string code)
{
string wrappedSource = WrapUserCode(code);
using (var provider = new CSharpCodeProvider())
{
var parameters = new CompilerParameters
{
GenerateInMemory = true,
GenerateExecutable = false,
TreatWarningsAsErrors = false,
};
AddReferences(parameters);
var results = provider.CompileAssemblyFromSource(parameters, wrappedSource);
if (results.Errors.HasErrors)
{
var errors = new List<string>();
foreach (CompilerError error in results.Errors)
{
if (!error.IsWarning)
{
int userLine = Math.Max(1, error.Line - WrapperLineOffset);
errors.Add($"Line {userLine}: {error.ErrorText}");
}
}
return new ErrorResponse("Compilation failed", new { errors });
}
var assembly = results.CompiledAssembly;
var type = assembly.GetType(WrapperClassName);
if (type == null)
return new ErrorResponse("Internal error: failed to find compiled type.");
var method = type.GetMethod(WrapperMethodName, BindingFlags.Public | BindingFlags.Static);
if (method == null)
return new ErrorResponse("Internal error: failed to find Execute method.");
object result = null;
Exception executionError = null;
try
{
result = method.Invoke(null, null);
}
catch (TargetInvocationException tie)
{
executionError = tie.InnerException ?? tie;
}
catch (Exception e)
{
executionError = e;
}
if (executionError != null)
return new ErrorResponse($"Runtime error: {executionError.Message}",
new { exceptionType = executionError.GetType().Name, stackTrace = executionError.StackTrace });
if (result != null)
return new SuccessResponse("Code executed successfully.", new { result = SerializeResult(result) });
return new SuccessResponse("Code executed successfully.");
}
}
private static string WrapUserCode(string code)
{
var sb = new StringBuilder();
sb.AppendLine("using System;");
sb.AppendLine("using System.Collections.Generic;");
sb.AppendLine("using System.Linq;");
sb.AppendLine("using System.Reflection;");
sb.AppendLine("using UnityEngine;");
sb.AppendLine("using UnityEditor;");
sb.AppendLine($"public static class {WrapperClassName}");
sb.AppendLine("{");
sb.AppendLine($" public static object {WrapperMethodName}()");
sb.AppendLine(" {");
sb.AppendLine(code);
sb.AppendLine(" }");
sb.AppendLine("}");
return sb.ToString();
}
private static void AddReferences(CompilerParameters parameters)
{
if (_cachedAssemblyPaths == null)
_cachedAssemblyPaths = ResolveAssemblyPaths();
foreach (var path in _cachedAssemblyPaths)
parameters.ReferencedAssemblies.Add(path);
}
private static string[] ResolveAssemblyPaths()
{
var paths = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies())
{
try
{
if (assembly.IsDynamic) continue;
var location = assembly.Location;
if (string.IsNullOrEmpty(location)) continue;
if (!System.IO.File.Exists(location)) continue;
paths.Add(location);
}
catch (NotSupportedException)
{
// Some assemblies don't support Location property
}
}
var result = new string[paths.Count];
paths.CopyTo(result);
return result;
}
private static string CheckBlockedPatterns(string code)
{
foreach (var pattern in _blockedPatterns)
{
if (code.IndexOf(pattern, StringComparison.OrdinalIgnoreCase) >= 0)
return $"Code contains blocked pattern: '{pattern}'. Disable safety checks with safety_checks=false if this is intentional.";
}
return null;
}
private static void AddToHistory(string code, object result, double elapsedMs, bool safetyChecks)
{
string preview;
if (result is SuccessResponse sr)
preview = sr.Data?.ToString() ?? sr.Message;
else if (result is ErrorResponse er)
preview = er.Error;
else
preview = result?.ToString() ?? "null";
if (preview != null && preview.Length > 200)
preview = preview.Substring(0, 200) + "...";
_history.Add(new HistoryEntry
{
code = code,
success = result is SuccessResponse,
resultPreview = preview,
elapsedMs = Math.Round(elapsedMs, 1),
timestamp = DateTime.UtcNow.ToString("yyyy-MM-ddTHH:mm:ssZ"),
safetyChecksEnabled = safetyChecks,
});
while (_history.Count > MaxHistoryEntries)
_history.RemoveAt(0);
}
private static object SerializeResult(object result)
{
if (result == null) return null;
var type = result.GetType();
if (type.IsPrimitive || result is string || result is decimal)
return result;
try
{
return JToken.FromObject(result);
}
catch
{
return result.ToString();
}
}
private class HistoryEntry
{
public string code;
public bool success;
public string resultPreview;
public double elapsedMs;
public string timestamp;
public bool safetyChecksEnabled;
}
}
}