Skip to content

Commit 4efa92e

Browse files
Merge pull request #77 from Snowblaze/am/create-prefab-tool-field-fix
fix: use the proper create prefab field
2 parents 0d1b088 + 5ea6840 commit 4efa92e

File tree

2 files changed

+16
-20
lines changed

2 files changed

+16
-20
lines changed

Editor/Tools/CreatePrefabTool.cs

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public CreatePrefabTool()
2525
public override JObject Execute(JObject parameters)
2626
{
2727
// Extract parameters
28-
string scriptName = parameters["scriptName"]?.ToObject<string>();
28+
string componentName = parameters["componentName"]?.ToObject<string>();
2929
string prefabName = parameters["prefabName"]?.ToObject<string>();
3030
JObject fieldValues = parameters["fieldValues"]?.ToObject<JObject>();
3131

@@ -42,20 +42,20 @@ public override JObject Execute(JObject parameters)
4242
GameObject tempObject = new GameObject(prefabName);
4343

4444
// Add component if provided
45-
if (!string.IsNullOrEmpty(scriptName))
45+
if (!string.IsNullOrEmpty(componentName))
4646
{
4747
try
4848
{
4949
// Add component
50-
Component component = AddComponent(tempObject, scriptName);
50+
Component component = AddComponent(tempObject, componentName);
5151

5252
// Apply field values if provided and component exists
5353
ApplyFieldValues(fieldValues, component);
5454
}
5555
catch (Exception ex)
5656
{
5757
return McpUnitySocketHandler.CreateErrorResponse(
58-
$"Failed to add component '{scriptName}' to GameObject",
58+
$"Failed to add component '{componentName}' to GameObject",
5959
"component_error"
6060
);
6161
}
@@ -71,7 +71,8 @@ public override JObject Execute(JObject parameters)
7171
}
7272

7373
// Create the prefab
74-
GameObject prefab = PrefabUtility.SaveAsPrefabAsset(tempObject, prefabPath);
74+
bool success = false;
75+
PrefabUtility.SaveAsPrefabAsset(tempObject, prefabPath, out success);
7576

7677
// Clean up temporary object
7778
UnityEngine.Object.DestroyImmediate(tempObject);
@@ -80,34 +81,36 @@ public override JObject Execute(JObject parameters)
8081
AssetDatabase.Refresh();
8182

8283
// Log the action
83-
McpLogger.LogInfo($"Created prefab '{prefab.name}' at path '{prefabPath}' from script '{scriptName}'");
84+
McpLogger.LogInfo($"Created prefab '{prefabName}' at path '{prefabPath}' from script '{componentName}'");
85+
86+
string message = success ? $"Successfully created prefab '{prefabName}' at path '{prefabPath}'" : $"Failed to create prefab '{prefabName}' at path '{prefabPath}'";
8487

8588
// Create the response
8689
return new JObject
8790
{
88-
["success"] = true,
91+
["success"] = success,
8992
["type"] = "text",
90-
["message"] = $"Successfully created prefab '{prefab.name}' at path '{prefabPath}'",
93+
["message"] = message,
9194
["prefabPath"] = prefabPath
9295
};
9396
}
9497

95-
private Component AddComponent(GameObject gameObject, string scriptName)
98+
private Component AddComponent(GameObject gameObject, string componentName)
9699
{
97100
// Find the script type
98-
Type scriptType = Type.GetType($"{scriptName}, Assembly-CSharp");
101+
Type scriptType = Type.GetType($"{componentName}, Assembly-CSharp");
99102
if (scriptType == null)
100103
{
101104
// Try with just the class name
102-
scriptType = Type.GetType(scriptName);
105+
scriptType = Type.GetType(componentName);
103106
}
104107

105108
if (scriptType == null)
106109
{
107110
// Try to find the type using AppDomain
108111
foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies())
109112
{
110-
scriptType = assembly.GetType(scriptName);
113+
scriptType = assembly.GetType(componentName);
111114
if (scriptType != null)
112115
break;
113116
}

Server~/src/tools/createPrefabTool.ts

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -51,14 +51,7 @@ export function registerCreatePrefabTool(server: McpServer, mcpUnity: McpUnity,
5151
* @returns A promise that resolves to the tool execution result
5252
* @throws McpUnityError if validation fails or the request to Unity fails
5353
*/
54-
async function toolHandler(mcpUnity: McpUnity, params: any) {
55-
if (!params.scriptName) {
56-
throw new McpUnityError(
57-
ErrorType.VALIDATION,
58-
"'scriptName' must be provided"
59-
);
60-
}
61-
54+
async function toolHandler(mcpUnity: McpUnity, params: any) {
6255
if (!params.prefabName) {
6356
throw new McpUnityError(
6457
ErrorType.VALIDATION,

0 commit comments

Comments
 (0)