Skip to content
Merged
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
51 changes: 49 additions & 2 deletions MCPForUnity/Editor/Tools/BatchExecute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,14 +91,28 @@ public static async Task<object> HandleCommand(JObject @params)
try
{
var result = await CommandRegistry.InvokeCommandAsync(toolName, commandParams).ConfigureAwait(true);
invocationSuccessCount++;
bool callSucceeded = DetermineCallSucceeded(result);
if (callSucceeded)
{
invocationSuccessCount++;
}
else
{
invocationFailureCount++;
anyCommandFailed = true;
}

commandResults.Add(new
{
tool = toolName,
callSucceeded = true,
callSucceeded,
result
});

if (!callSucceeded && failFast)
{
break;
}
}
catch (Exception ex)
{
Expand Down Expand Up @@ -134,6 +148,39 @@ public static async Task<object> HandleCommand(JObject @params)
: 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)
Expand Down
39 changes: 34 additions & 5 deletions MCPForUnity/Editor/Tools/GameObjects/GameObjectCreate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,11 @@ internal static object Handle(JObject @params)

// --- Try Instantiating Prefab First ---
string originalPrefabPath = prefabPath;
if (!string.IsNullOrEmpty(prefabPath))
if (!saveAsPrefab && !string.IsNullOrEmpty(prefabPath))
{
if (!prefabPath.Contains("/") && !prefabPath.EndsWith(".prefab", StringComparison.OrdinalIgnoreCase))
string extension = System.IO.Path.GetExtension(prefabPath);

if (!prefabPath.Contains("/") && (string.IsNullOrEmpty(extension) || extension.Equals(".prefab", StringComparison.OrdinalIgnoreCase)))
{
string prefabNameOnly = prefabPath;
McpLog.Info($"[ManageGameObject.Create] Searching for prefab named: '{prefabNameOnly}'");
Expand All @@ -51,11 +53,38 @@ internal static object Handle(JObject @params)
McpLog.Info($"[ManageGameObject.Create] Found unique prefab at path: '{prefabPath}'");
}
}
else if (!prefabPath.EndsWith(".prefab", StringComparison.OrdinalIgnoreCase))
else if (prefabPath.Contains("/") && string.IsNullOrEmpty(extension))
{
McpLog.Warn($"[ManageGameObject.Create] Provided prefabPath '{prefabPath}' does not end with .prefab. Assuming it's missing and appending.");
McpLog.Warn($"[ManageGameObject.Create] Provided prefabPath '{prefabPath}' has no extension. Assuming it's a prefab and appending .prefab.");
prefabPath += ".prefab";
}
else if (!prefabPath.Contains("/") && !string.IsNullOrEmpty(extension) && !extension.Equals(".prefab", StringComparison.OrdinalIgnoreCase))
{
string fileName = prefabPath;
string fileNameWithoutExtension = System.IO.Path.GetFileNameWithoutExtension(fileName);
McpLog.Info($"[ManageGameObject.Create] Searching for asset file named: '{fileName}'");

string[] guids = AssetDatabase.FindAssets(fileNameWithoutExtension);
var matches = guids
.Select(g => AssetDatabase.GUIDToAssetPath(g))
.Where(p => p.EndsWith("/" + fileName, StringComparison.OrdinalIgnoreCase) || p.Equals(fileName, StringComparison.OrdinalIgnoreCase))
.ToArray();

if (matches.Length == 0)
{
return new ErrorResponse($"Asset file '{fileName}' not found anywhere in the project.");
}
else if (matches.Length > 1)
{
string foundPaths = string.Join(", ", matches);
return new ErrorResponse($"Multiple assets found matching file name '{fileName}': {foundPaths}. Please provide a more specific path.");
}
else
{
prefabPath = matches[0];
McpLog.Info($"[ManageGameObject.Create] Found unique asset at path: '{prefabPath}'");
}
}

GameObject prefabAsset = AssetDatabase.LoadAssetAtPath<GameObject>(prefabPath);
if (prefabAsset != null)
Expand Down Expand Up @@ -83,7 +112,7 @@ internal static object Handle(JObject @params)
}
else
{
McpLog.Warn($"[ManageGameObject.Create] Prefab asset not found at path: '{prefabPath}'. Will proceed to create new object if specified.");
return new ErrorResponse($"Asset not found or not a GameObject at path: '{prefabPath}'.");
}
}

Expand Down