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
1213const 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