Skip to content

Commit 7a0b984

Browse files
committed
fix: use the proper create prefab field
1 parent 0d1b088 commit 7a0b984

File tree

2 files changed

+51
-41
lines changed

2 files changed

+51
-41
lines changed

Editor/Tools/CreatePrefabTool.cs

Lines changed: 9 additions & 9 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
}
@@ -80,7 +80,7 @@ public override JObject Execute(JObject parameters)
8080
AssetDatabase.Refresh();
8181

8282
// Log the action
83-
McpLogger.LogInfo($"Created prefab '{prefab.name}' at path '{prefabPath}' from script '{scriptName}'");
83+
McpLogger.LogInfo($"Created prefab '{prefab.name}' at path '{prefabPath}' from script '{componentName}'");
8484

8585
// Create the response
8686
return new JObject
@@ -92,22 +92,22 @@ public override JObject Execute(JObject parameters)
9292
};
9393
}
9494

95-
private Component AddComponent(GameObject gameObject, string scriptName)
95+
private Component AddComponent(GameObject gameObject, string componentName)
9696
{
9797
// Find the script type
98-
Type scriptType = Type.GetType($"{scriptName}, Assembly-CSharp");
98+
Type scriptType = Type.GetType($"{componentName}, Assembly-CSharp");
9999
if (scriptType == null)
100100
{
101101
// Try with just the class name
102-
scriptType = Type.GetType(scriptName);
102+
scriptType = Type.GetType(componentName);
103103
}
104104

105105
if (scriptType == null)
106106
{
107107
// Try to find the type using AppDomain
108108
foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies())
109109
{
110-
scriptType = assembly.GetType(scriptName);
110+
scriptType = assembly.GetType(componentName);
111111
if (scriptType != null)
112112
break;
113113
}
Lines changed: 42 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,45 @@
1-
import { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js';
2-
import { McpUnity } from '../unity/mcpUnity.js';
3-
import { McpUnityError, ErrorType } from '../utils/errors.js';
4-
import * as z from 'zod';
5-
import { Logger } from '../utils/logger.js';
1+
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
2+
import { McpUnity } from "../unity/mcpUnity.js";
3+
import { McpUnityError, ErrorType } from "../utils/errors.js";
4+
import * as z from "zod";
5+
import { Logger } from "../utils/logger.js";
66

77
// Constants for the tool
8-
const toolName = 'create_prefab';
9-
const toolDescription = 'Creates a prefab with optional MonoBehaviour script and serialized field values';
8+
const toolName = "create_prefab";
9+
const toolDescription =
10+
"Creates a prefab with optional MonoBehaviour script and serialized field values";
1011

1112
// Parameter schema for the tool
1213
const paramsSchema = z.object({
13-
componentName: z.string().optional().describe('The name of the MonoBehaviour Component to add to the prefab (optional)'),
14-
prefabName: z.string().describe('The name of the prefab to create'),
15-
fieldValues: z.record(z.any()).optional().describe('Optional JSON object of serialized field values to apply to the prefab')
14+
componentName: z
15+
.string()
16+
.optional()
17+
.describe(
18+
"The name of the MonoBehaviour Component to add to the prefab (optional)"
19+
),
20+
prefabName: z.string().describe("The name of the prefab to create"),
21+
fieldValues: z
22+
.record(z.any())
23+
.optional()
24+
.describe(
25+
"Optional JSON object of serialized field values to apply to the prefab"
26+
),
1627
});
1728

1829
/**
1930
* Creates and registers the CreatePrefab tool with the MCP server
20-
*
31+
*
2132
* @param server The MCP server to register the tool with
2233
* @param mcpUnity The McpUnity instance to communicate with Unity
2334
* @param logger The logger instance for diagnostic information
2435
*/
25-
export function registerCreatePrefabTool(server: McpServer, mcpUnity: McpUnity, logger: Logger) {
36+
export function registerCreatePrefabTool(
37+
server: McpServer,
38+
mcpUnity: McpUnity,
39+
logger: Logger
40+
) {
2641
logger.info(`Registering tool: ${toolName}`);
27-
42+
2843
server.tool(
2944
toolName,
3045
toolDescription,
@@ -45,47 +60,42 @@ export function registerCreatePrefabTool(server: McpServer, mcpUnity: McpUnity,
4560

4661
/**
4762
* Handler function for the CreatePrefab tool
48-
*
63+
*
4964
* @param mcpUnity The McpUnity instance to communicate with Unity
5065
* @param params The validated parameters for the tool
5166
* @returns A promise that resolves to the tool execution result
5267
* @throws McpUnityError if validation fails or the request to Unity fails
5368
*/
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-
69+
async function toolHandler(mcpUnity: McpUnity, params: any) {
6270
if (!params.prefabName) {
6371
throw new McpUnityError(
6472
ErrorType.VALIDATION,
6573
"'prefabName' must be provided"
6674
);
6775
}
68-
76+
6977
const response = await mcpUnity.sendRequest({
7078
method: toolName,
71-
params
79+
params,
7280
});
73-
81+
7482
if (!response.success) {
7583
throw new McpUnityError(
7684
ErrorType.TOOL_EXECUTION,
7785
response.message || `Failed to create prefab`
7886
);
7987
}
80-
88+
8189
return {
82-
content: [{
83-
type: response.type,
84-
text: response.message || `Successfully created prefab`
85-
}],
90+
content: [
91+
{
92+
type: response.type,
93+
text: response.message || `Successfully created prefab`,
94+
},
95+
],
8696
// Include the prefab path in the result for programmatic access
8797
data: {
88-
prefabPath: response.prefabPath
89-
}
98+
prefabPath: response.prefabPath,
99+
},
90100
};
91101
}

0 commit comments

Comments
 (0)