Skip to content

Commit ec57b2a

Browse files
committed
Add createPrefabTool MCP server tool wrapper
Implemented the MCP server side TypeScript tool wrapper createPrefabTool.ts for the CreatePrefabTool, including parameter validation, error handling, and communication with Unity Editor.
1 parent 1c8edd8 commit ec57b2a

File tree

1 file changed

+91
-0
lines changed

1 file changed

+91
-0
lines changed
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
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';
6+
7+
// Constants for the tool
8+
const toolName = 'create_prefab';
9+
const toolDescription = 'Creates a prefab with optional MonoBehaviour script and serialized field values';
10+
11+
// Parameter schema for the tool
12+
const paramsSchema = z.object({
13+
scriptName: z.string().optional().describe('The name of the MonoBehaviour script class (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')
16+
});
17+
18+
/**
19+
* Creates and registers the CreatePrefab tool with the MCP server
20+
*
21+
* @param server The MCP server to register the tool with
22+
* @param mcpUnity The McpUnity instance to communicate with Unity
23+
* @param logger The logger instance for diagnostic information
24+
*/
25+
export function registerCreatePrefabTool(server: McpServer, mcpUnity: McpUnity, logger: Logger) {
26+
logger.info(`Registering tool: ${toolName}`);
27+
28+
server.tool(
29+
toolName,
30+
toolDescription,
31+
paramsSchema.shape,
32+
async (params: any) => {
33+
try {
34+
logger.info(`Executing tool: ${toolName}`, params);
35+
const result = await toolHandler(mcpUnity, params);
36+
logger.info(`Tool execution successful: ${toolName}`);
37+
return result;
38+
} catch (error) {
39+
logger.error(`Tool execution failed: ${toolName}`, error);
40+
throw error;
41+
}
42+
}
43+
);
44+
}
45+
46+
/**
47+
* Handler function for the CreatePrefab tool
48+
*
49+
* @param mcpUnity The McpUnity instance to communicate with Unity
50+
* @param params The validated parameters for the tool
51+
* @returns A promise that resolves to the tool execution result
52+
* @throws McpUnityError if validation fails or the request to Unity fails
53+
*/
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+
62+
if (!params.prefabName) {
63+
throw new McpUnityError(
64+
ErrorType.VALIDATION,
65+
"'prefabName' must be provided"
66+
);
67+
}
68+
69+
const response = await mcpUnity.sendRequest({
70+
method: toolName,
71+
params
72+
});
73+
74+
if (!response.success) {
75+
throw new McpUnityError(
76+
ErrorType.TOOL_EXECUTION,
77+
response.message || `Failed to create prefab`
78+
);
79+
}
80+
81+
return {
82+
content: [{
83+
type: response.type,
84+
text: response.message || `Successfully created prefab`
85+
}],
86+
// Include the prefab path in the result for programmatic access
87+
data: {
88+
prefabPath: response.prefabPath
89+
}
90+
};
91+
}

0 commit comments

Comments
 (0)