Skip to content
Closed
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
┌──────────────────────────────────────────────────────────────────┐
│ 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.ComponentModel;
using com.IvanMurzak.ReflectorNet.Model;

namespace AIGD
Comment thread
tianlang0704 marked this conversation as resolved.
{
public class ScriptExecuteResponse
{
[Description("The serialized return value of the executed script. Null if the script returns void.")]
public SerializedMember? Result { get; set; }

[Description("Human-readable message describing the execution result.")]
public string? Message { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
using System.Linq;
using System.Reflection;
using System.Text;
using AIGD;
using com.IvanMurzak.McpPlugin;
using com.IvanMurzak.McpPlugin.Common.Model;
using com.IvanMurzak.ReflectorNet.Model;
using com.IvanMurzak.ReflectorNet.Utils;
using com.IvanMurzak.Unity.MCP.Utils;
Expand Down Expand Up @@ -52,7 +54,7 @@ public static partial class Tool_Script
"while body-only mode (isMethodBody=true) auto-generates the boilerplate so you only " +
"provide the method body. Unity objects (GameObject, Component, etc.) can be passed as " +
"parameters using their Ref types (GameObjectRef, ComponentRef, etc.) or directly by type.")]
public static SerializedMember? Execute
public static ResponseCallValueTool<ScriptExecuteResponse> Execute
(
[Description("C# code to compile and execute. " +
"In full code mode (default, isMethodBody=false): must define a complete class with a static method. " +
Expand Down Expand Up @@ -131,17 +133,41 @@ public static SerializedMember? Execute
throw new Exception(error);
}

var reflector = UnityMcpPluginEditor.Instance.Reflector ?? throw new Exception("Reflector is not available.");
var jsonSerializer = reflector.JsonSerializer;

// Void return - use plain success message
if (result is null)
return null;
{
return ResponseCallValueTool<ScriptExecuteResponse>
.Success(new ScriptExecuteResponse
{
Result = null,
Message = "Script executed successfully. No return value."
});
}

// Non-null result - use SuccessStructured to preserve JSON structure
if (result is SerializedMember serializedResult)
return serializedResult;

var reflector = UnityMcpPluginEditor.Instance.Reflector ?? throw new Exception("Reflector is not available.");
{
return ResponseCallValueTool<ScriptExecuteResponse>
.SuccessStructured(jsonSerializer.SerializeToNode(new ScriptExecuteResponse
{
Result = serializedResult,
Message = "Script executed successfully."
}));
}

return reflector.Serialize(
var serialized = reflector.Serialize(
obj: result,
logger: logger);

return ResponseCallValueTool<ScriptExecuteResponse>
.SuccessStructured(jsonSerializer.SerializeToNode(new ScriptExecuteResponse
{
Result = serialized,
Message = "Script executed successfully."
}));
Comment on lines 160 to +170
});
}

Expand Down