## Runs a shell command outside the editor. ## ## Not SSH: SSH needs curve25519 key exchange, ed25519 signatures and ## chacha20-poly1305 or AES-GCM, and Godot exposes none of those (Crypto ## gives RSA/HMAC, AESContext only ECB and CBC), so a real SSH client can't ## be written in pure GDScript. Instead this talks HTTP to a tiny exec ## bridge you run next to the shell you want — see shell_bridge.py in this ## folder. On desktop it skips the bridge and uses OS.execute directly; on ## Android/Quest OS.execute is unavailable, which is exactly the case the ## bridge exists for (run it in Termux, reachable on 127.0.0.1). ## ## The request blocks the editor while it runs, so the timeout is short and ## capped — this is for `git status`, not for `npm install`. class_name ClaudeGodotToolRemote extends ClaudeGodotTool const DEFAULT_BRIDGE := "127.0.0.1:8799" const DEFAULT_TIMEOUT := 15.0 const MAX_TIMEOUT := 60.0 func definition() -> Dictionary: return { "name": "remote", "description": "Run a shell command on the machine hosting the editor, returning its stdout, stderr and exit code. On desktop it runs locally; on Android/Quest it goes to a shell bridge (default %s) that the user starts in Termux. Keep commands short — the editor is blocked while one runs." % DEFAULT_BRIDGE, "input_schema": { "type": "object", "properties": { "command": {"type": "string", "description": "The command line to run, e.g. 'git status --short'."}, "cwd": {"type": "string", "description": "Working directory for the command. Optional."}, "timeout": {"type": "number", "description": "Seconds to wait, default %d, max %d." % [int(DEFAULT_TIMEOUT), int(MAX_TIMEOUT)]} }, "required": ["command"] } } func execute(input: Dictionary) -> Dictionary: var command: String = input.get("command", "") if command.strip_edges() == "": return {"is_error": true, "content": "Missing 'command'."} var timeout: float = clampf(float(input.get("timeout", DEFAULT_TIMEOUT)), 1.0, MAX_TIMEOUT) var cwd: String = input.get("cwd", "") if OS.has_feature("android") or OS.has_feature("web"): return _via_bridge(command, cwd, timeout) return _via_os_execute(command, cwd) func _via_os_execute(command: String, cwd: String) -> Dictionary: var full := command if cwd == "" else "cd %s && %s" % [cwd, command] var output: Array = [] var code := OS.execute("sh", ["-lc", full], output, true, false) if code == -1: return {"is_error": true, "content": "Could not start a shell on this platform."} var text := "\n".join(output).strip_edges() return { "content": "exit %d\n%s" % [code, text if text != "" else "(no output)"], "is_error": code != 0, } ## Blocking HTTP/1.1 POST to the bridge. HTTPRequest is async and tool ## execution is synchronous, so this speaks the protocol over a raw socket ## and polls until the body arrives or the deadline passes. func _via_bridge(command: String, cwd: String, timeout: float) -> Dictionary: var address := ClaudeGodotPreferences.get_value("shell_bridge", DEFAULT_BRIDGE) var parts := address.split(":") if parts.size() != 2: return {"is_error": true, "content": "Malformed shell_bridge '%s' — expected host:port." % address} var host: String = parts[0] var port := int(parts[1]) var peer := StreamPeerTCP.new() if peer.connect_to_host(host, port) != OK: return {"is_error": true, "content": _bridge_hint(address)} var deadline := Time.get_ticks_msec() + int(timeout * 1000.0) while peer.get_status() == StreamPeerTCP.STATUS_CONNECTING: peer.poll() if Time.get_ticks_msec() > deadline: return {"is_error": true, "content": _bridge_hint(address)} if peer.get_status() != StreamPeerTCP.STATUS_CONNECTED: return {"is_error": true, "content": _bridge_hint(address)} var body := JSON.stringify({"command": command, "cwd": cwd, "timeout": timeout}) var body_bytes := body.to_utf8_buffer() var request := "POST /exec HTTP/1.1\r\nHost: %s\r\nContent-Type: application/json\r\nContent-Length: %d\r\nConnection: close\r\n\r\n" % [address, body_bytes.size()] peer.put_data(request.to_utf8_buffer()) peer.put_data(body_bytes) var received := PackedByteArray() while Time.get_ticks_msec() < deadline: peer.poll() var available := peer.get_available_bytes() if available > 0: var chunk := peer.get_data(available) if chunk[0] == OK: received.append_array(chunk[1]) elif peer.get_status() != StreamPeerTCP.STATUS_CONNECTED and received.size() > 0: break peer.disconnect_from_host() if received.is_empty(): return {"is_error": true, "content": "Shell bridge at %s did not answer within %ds." % [address, int(timeout)]} var text := received.get_string_from_utf8() var split := text.find("\r\n\r\n") var payload := text.substr(split + 4) if split != -1 else text var json := JSON.new() if json.parse(payload) != OK: return {"is_error": true, "content": "Shell bridge returned something that isn't JSON: %s" % payload} var data = json.get_data() if not (data is Dictionary): return {"is_error": true, "content": "Shell bridge returned an unexpected payload: %s" % payload} var exit_code := int(data.get("exit_code", -1)) var out := str(data.get("stdout", "")).strip_edges() var err := str(data.get("stderr", "")).strip_edges() var report := "exit %d" % exit_code if out != "": report += "\n" + out if err != "": report += "\nstderr:\n" + err if out == "" and err == "": report += "\n(no output)" return {"content": report, "is_error": exit_code != 0} func _bridge_hint(address: String) -> String: return "No shell bridge listening on %s. Start it where you want commands to run (in Termux: `python addons/claude_godot/agent/tools/shell_bridge.py`), or point the shell_bridge setting in user://claude_godot/preferences.csv at another host:port." % address