## Runs a snippet of GDScript in the editor and returns what it returns. ## ## The snippet becomes the body of a `run()` method on a throwaway RefCounted, ## compiled with GDScript.new() and called from here. That gives it the same ## access the editor has, which is the point — and the danger: it can change ## anything, and a loop that does not end hangs the editor, because a running ## GDScript function cannot be interrupted. So the tool imposes no timeout; ## the description asks for short, terminating snippets instead. class_name ClaudeGodotToolEval extends ClaudeGodotTool const MAX_RESULT_CHARS := 50_000 func definition() -> Dictionary: return { "name": "eval", "description": "Run a snippet of GDScript inside the editor and return the value it `return`s. The snippet is the body of a function: write statements, and finish with `return ` for the result — a `print` goes to the Output panel, not to this result. Useful for checking an engine API, a node, a resource or a computed value without leaving the chat. It runs on the editor's main thread with full access, so it can change project state, and a loop that never ends hangs the editor; keep it short and terminating. Use the file and settings tools for changes you mean to keep.", "input_schema": { "type": "object", "properties": { "code": {"type": "string", "description": "GDScript statements, indentation included, ending in `return `. No `extends`, `class_name` or top-level `func` — the snippet is already inside a function. Example: 'var tree = Engine.get_main_loop()\\nreturn tree.get_node_count()'"} }, "required": ["code"] } } func execute(input: Dictionary) -> Dictionary: var code: String = str(input.get("code", "")) if code.strip_edges() == "": return {"is_error": true, "content": "Missing 'code'."} var script := GDScript.new() script.source_code = _wrap(code) var err := script.reload() # can_instantiate() is deliberately NOT checked. In Godot 4.7 it returns # false for a GDScript built at runtime (GDScript.new() + source_code + # reload()) even when reload() succeeded with no error and new() then works # and run() executes — so relying on it aborted every snippet, including # `return 1 + 1`, and blamed the user's code for it. The real check is # whether new() returns something, just below. if err != OK: return { "is_error": true, "content": "The snippet did not compile (reload error %d: %s). Godot names the line in the Output panel and the editor log — read_log with which='editor' shows it. The usual causes are a syntax error, wrong indentation, or `extends`/`class_name`/`func` at the top: the snippet is already the body of a function." % [err, error_string(err)], } var instance = script.new() if instance == null: return {"is_error": true, "content": "The snippet compiled but could not be instantiated."} var result = instance.run() return {"content": _render(result)} ## Indents every line one tab so it sits inside run(). Multi-line string ## literals would carry that tab into their contents, which is the one thing ## this changes about the snippet. static func _wrap(code: String) -> String: var body := "" for line in code.split("\n"): body += "\t" + line + "\n" if body.strip_edges() == "": body = "\tpass\n" return "extends RefCounted\n\nfunc run():\n" + body static func _render(value) -> String: if value == null: return "(the snippet ran but returned null — use `return` to produce a value)" var text: String if value is Dictionary or value is Array: text = JSON.stringify(value, " ") else: text = str(value) if text.length() > MAX_RESULT_CHARS: text = text.substr(0, MAX_RESULT_CHARS) + "\n… (truncated at %d characters)" % MAX_RESULT_CHARS return text