Skip to main content
Glama

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
NameRequiredDescriptionDefault
sceneYesPath to the .tscn file
parentYesParent node path (use root node name for root, or node path like "Path/To/Parent")
typeYesGodot node type (e.g. Sprite2D, Camera2D)
nameYesName for the new node
propertiesNoOptional properties as key-value pairs
expectedHashNoExpected content hash for stale-edit prevention

Implementation Reference

  • 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"),
    },
  • 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.",
        },
      },

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/woohq/godette-mcp'

If you have feedback or need assistance with the MCP directory API, please join our Discord server