add_node
Add a new node as a child to an existing node in Godot .tscn scene files to expand scene trees and organize game elements.
Instructions
Add a new node as a child of an existing node in a .tscn scene file.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| scene | Yes | Path to the .tscn file | |
| parent | Yes | Parent node path (use root node name for root, or node path like "Path/To/Parent") | |
| type | Yes | Godot node type (e.g. Sprite2D, Camera2D) | |
| name | Yes | Name for the new node | |
| properties | No | Optional properties as key-value pairs | |
| expectedHash | No | Expected content hash for stale-edit prevention |
Implementation Reference
- src/tools/scene-edit-tools.ts:122-189 (handler)The handler function for the add_node tool, which validates the parent node, checks for duplicates, and updates the scene hierarchy.
handler: async (ctx) => { const { scene: scenePath, parent, type, name, properties, expectedHash } = ctx.args; validatePath(scenePath); return withFileLock(scenePath, async () => { try { const { source, scene } = await readAndParse(scenePath); const hashErr = hashCheck(source, expectedHash); if (hashErr) return makeTextResponse({ error: hashErr, data: null }); // Validate parent exists const parentNode = scene.nodes.get(parent); if (!parentNode) { return makeTextResponse({ error: `Parent node not found: ${parent}`, data: null, }); } // Compute parent attr for TSCN format const parentAttr = parentNode.parent === null ? "." : parentNode.nodePath; const newNodePath = parentNode.parent === null ? name : parentNode.nodePath + "/" + name; // Check for duplicate if (scene.nodes.has(newNodePath)) { return makeTextResponse({ error: `Node already exists: ${newNodePath}`, data: null, }); } // Build properties const nodeProps: Record<string, unknown> = {}; if (properties) { for (const [key, value] of Object.entries(properties)) { nodeProps[key] = convertJsonToTscnValue(value); } } // Create NodeInfo const newNode: NodeInfo = { name, type, scenePath: scenePath, nodePath: newNodePath, parent: parentAttr, children: [], script: null, groups: [], properties: nodeProps, isInstanceOf: null, rawSpan: null, }; scene.nodes.set(newNodePath, newNode); parentNode.children.push(name); await writeAndEmit(scenePath, scene, eventBus, ctx.signal); return makeTextResponse({ data: { added: newNodePath, parent: parentAttr, type, }, metadata: { source: "index" }, }); - Schema definition for the add_node tool inputs including scene path, parent node, node type, name, properties, and optional expectedHash.
schema: { scene: z.string().describe("Path to the .tscn file"), parent: z .string() .describe( 'Parent node path (use root node name for root, or node path like "Path/To/Parent")', ), type: z.string().describe("Godot node type (e.g. Sprite2D, Camera2D)"), name: z.string().describe("Name for the new node"), properties: z .record(z.unknown()) .optional() .describe("Optional properties as key-value pairs"), expectedHash: z .string() .optional() .describe("Expected content hash for stale-edit prevention"), }, - src/tools/scene-edit-tools.ts:93-103 (registration)Registration of the add_node tool within the tool list.
{ name: "add_node", description: { base: "Add a new node as a child of an existing node in a .tscn scene file.", slots: { editing: "Provide contentHash from your last read to prevent conflicts with concurrent edits.", planning: "Consider the scene hierarchy carefully before adding nodes. Use get_scene_tree first.", }, },