Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
350 changes: 350 additions & 0 deletions MCPForUnity/Editor/Tools/ExecuteCode.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,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;
}
}
}
2 changes: 2 additions & 0 deletions MCPForUnity/Editor/Tools/ExecuteCode.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading