add_node
Add a new node to a Godot scene by specifying its type, name, and parent, enabling scene structure modification for game development.
Instructions
Add a node to an existing scene
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| projectPath | Yes | Path to the Godot project directory | |
| scenePath | Yes | Path to the scene file (relative to project) | |
| parentNodePath | No | Path to the parent node (e.g., "root" or "root/Player") | root |
| nodeType | Yes | Type of node to add (e.g., Sprite2D, CollisionShape2D) | |
| nodeName | Yes | Name for the new node | |
| properties | No | Optional properties to set on the node |
Implementation Reference
- src/index.ts:1557-1648 (handler)The primary handler function for the 'add_node' tool. It normalizes input parameters, validates project and scene paths, prepares parameters, and invokes executeOperation('add_node') to perform the actual node addition via Godot's GDScript.private async handleAddNode(args: any) { // Normalize parameters to camelCase args = this.normalizeParameters(args); if (!args.projectPath || !args.scenePath || !args.nodeType || !args.nodeName) { return this.createErrorResponse( 'Missing required parameters', ['Provide projectPath, scenePath, nodeType, and nodeName'] ); } if (!this.validatePath(args.projectPath) || !this.validatePath(args.scenePath)) { return this.createErrorResponse( 'Invalid path', ['Provide valid paths without ".." or other potentially unsafe characters'] ); } try { // Check if the project directory exists and contains a project.godot file const projectFile = join(args.projectPath, 'project.godot'); if (!existsSync(projectFile)) { return this.createErrorResponse( `Not a valid Godot project: ${args.projectPath}`, [ 'Ensure the path points to a directory containing a project.godot file', 'Use list_projects to find valid Godot projects', ] ); } // Check if the scene file exists const scenePath = join(args.projectPath, args.scenePath); if (!existsSync(scenePath)) { return this.createErrorResponse( `Scene file does not exist: ${args.scenePath}`, [ 'Ensure the scene path is correct', 'Use create_scene to create a new scene first', ] ); } // Prepare parameters for the operation (already in camelCase) const params: any = { scenePath: args.scenePath, nodeType: args.nodeType, nodeName: args.nodeName, }; // Add optional parameters if (args.parentNodePath) { params.parentNodePath = args.parentNodePath; } if (args.properties) { params.properties = args.properties; } // Execute the operation const { stdout, stderr } = await this.executeOperation('add_node', params, args.projectPath); if (stderr && stderr.includes('Failed to')) { return this.createErrorResponse( `Failed to add node: ${stderr}`, [ 'Check if the node type is valid', 'Ensure the parent node path exists', 'Verify the scene file is valid', ] ); } return { content: [ { type: 'text', text: `Node '${args.nodeName}' of type '${args.nodeType}' added successfully to '${args.scenePath}'.\n\nOutput: ${stdout}`, }, ], }; } catch (error: any) { return this.createErrorResponse( `Failed to add node: ${error?.message || 'Unknown error'}`, [ 'Ensure Godot is installed correctly', 'Check if the GODOT_PATH environment variable is set correctly', 'Verify the project path is accessible', ] ); } }
- src/index.ts:784-817 (schema)The input schema definition for the 'add_node' tool, specifying required and optional parameters with descriptions and types.name: 'add_node', description: 'Add a node to an existing scene', inputSchema: { type: 'object', properties: { projectPath: { type: 'string', description: 'Path to the Godot project directory', }, scenePath: { type: 'string', description: 'Path to the scene file (relative to project)', }, parentNodePath: { type: 'string', description: 'Path to the parent node (e.g., "root" or "root/Player")', default: 'root', }, nodeType: { type: 'string', description: 'Type of node to add (e.g., Sprite2D, CollisionShape2D)', }, nodeName: { type: 'string', description: 'Name for the new node', }, properties: { type: 'object', description: 'Optional properties to set on the node', }, }, required: ['projectPath', 'scenePath', 'nodeType', 'nodeName'], }, },
- src/index.ts:951-951 (registration)The switch case in the CallToolRequestSchema handler that registers and dispatches 'add_node' tool calls to the handleAddNode function.return await this.handleAddNode(request.params.arguments);
- src/index.ts:474-542 (helper)The helper method executeOperation used by add_node handler to run the Godot GDScript (godot_operations.gd) with the 'add_node' operation and JSON parameters.private async executeOperation( operation: string, params: OperationParams, projectPath: string ): Promise<{ stdout: string; stderr: string }> { this.logDebug(`Executing operation: ${operation} in project: ${projectPath}`); this.logDebug(`Original operation params: ${JSON.stringify(params)}`); // Convert camelCase parameters to snake_case for Godot script const snakeCaseParams = this.convertCamelToSnakeCase(params); this.logDebug(`Converted snake_case params: ${JSON.stringify(snakeCaseParams)}`); // Ensure godotPath is set if (!this.godotPath) { await this.detectGodotPath(); if (!this.godotPath) { throw new Error('Could not find a valid Godot executable path'); } } try { // Serialize the snake_case parameters to a valid JSON string const paramsJson = JSON.stringify(snakeCaseParams); // Escape single quotes in the JSON string to prevent command injection const escapedParams = paramsJson.replace(/'/g, "'\\''"); // On Windows, cmd.exe does not strip single quotes, so we use // double quotes and escape them to ensure the JSON is parsed // correctly by Godot. const isWindows = process.platform === 'win32'; const quotedParams = isWindows ? `\"${paramsJson.replace(/\"/g, '\\"')}\"` : `'${escapedParams}'`; // Add debug arguments if debug mode is enabled const debugArgs = GODOT_DEBUG_MODE ? ['--debug-godot'] : []; // Construct the command with the operation and JSON parameters const cmd = [ `"${this.godotPath}"`, '--headless', '--path', `"${projectPath}"`, '--script', `"${this.operationsScriptPath}"`, operation, quotedParams, // Pass the JSON string as a single argument ...debugArgs, ].join(' '); this.logDebug(`Command: ${cmd}`); const { stdout, stderr } = await execAsync(cmd); return { stdout, stderr }; } catch (error: unknown) { // If execAsync throws, it still contains stdout/stderr if (error instanceof Error && 'stdout' in error && 'stderr' in error) { const execError = error as Error & { stdout: string; stderr: string }; return { stdout: execError.stdout, stderr: execError.stderr, }; } throw error; } }
- src/index.ts:136-136 (helper)Sets the path to the GDScript file (godot_operations.gd) that contains the actual implementation logic for the 'add_node' operation.this.operationsScriptPath = join(__dirname, 'scripts', 'godot_operations.gd');