Skip to main content

execute_code

Auto-generated from the Python tool registry. Do not hand-edit outside <!-- examples:start --><!-- examples:end --> blocks — the generator (tools/generate_docs_reference.py) will overwrite them.

Group: scripting_ext  ·  Module: services.tools.execute_code

Maturity: stable (support status; independent of enabled state, readiness and permissions).

Description​

Execute arbitrary C# code inside the Unity Editor. The code runs as a method body with access to UnityEngine and UnityEditor namespaces; Object aliases UnityEngine.Object. Use 'return' to send data back. Compiled in-memory — no script files created. For an authorized test that must preserve the current Play state, proactively use this tool for runtime inspection, setup, state changes or game-specific method calls that dedicated tools cannot express, when it is exposed, enabled in Unity and execution consent is granted; the user need not name it again. Inspect the target/API first, guard Play state and target validity, keep safety_checks=true and execution bounded, and return concise before/after values. Preserve the session: no Play restart, scene reload, temporary script files or project refresh. Never bypass missing consent or a disabled tool. Verify outcomes; inspect uncertain mutations before retrying. Use bounded follow-up observations for asynchronous effects, not blocking waits. Code setup/direct method invocation does not verify the input/UI path; use interaction tools when that path is under test. For temporary resources or state, track ownership/lifetime and arrange exception-safe cleanup: restore borrowed state after use and release only owned resources once their consumers finish. Use lifecycle-appropriate APIs; preserve intended results and unrelated objects. Actions: execute (run code), get_history (list past executions), replay (re-run a history entry with its saved safety and compiler settings), clear_history. NOTE: safety_checks blocks known dangerous patterns but is not a full sandbox. Compiler options: 'auto' (Roslyn if available, else CodeDom), 'roslyn' (language support depends on the installed Microsoft.CodeAnalysis version), 'codedom' (language support depends on Unity's installed CSharpCodeProvider compiler).

Parameters​

NameTypeRequiredDescription
actionLiteral['execute', 'get_history', 'replay', 'clear_history']yesAction to perform.
codestr | None—C# code to execute (for 'execute' action). Must be a valid method body. Access UnityEngine and UnityEditor namespaces. Use 'return' to send data back.
safety_checksbool—Enable basic blocked-pattern checks (File.Delete, Process.Start, infinite loops, etc). For 'execute' only; replay uses the saved setting. Not a full sandbox — advanced bypass is possible. Default: true.
indexint | None—History entry index to replay (for 'replay' action).
limitint—Number of history entries to return (for 'get_history' action, 1-50). Default: 10.
compilerLiteral['auto', 'roslyn', 'codedom']—Compiler backend for 'execute' action. 'auto' uses Roslyn if Microsoft.CodeAnalysis is installed, else falls back to CodeDom. 'roslyn' forces Roslyn; supported language features depend on the installed Microsoft.CodeAnalysis version. 'codedom' forces legacy CSharpCodeProvider; language support depends on the Unity installation. Default: auto.
unity_instancestr | int | None—Target Unity instance for this call: Name@hash or hash prefix; an integer selects only that exact port over stdio and is rejected over HTTP. Read mcpforunity://instances for available targets. Omit to use the legacy active selection or the sole available instance.

Returns​

A dict containing the Unity response. The exact shape depends on the action.

Examples​

Run statements without returning a value:

{"action": "execute", "code": "Debug.Log(\"Hello from MCP\");"}

Return structured data, including Unity values. Vectors retain their numeric components inside nested results:

{"action": "execute", "code": "return new { position = new Vector3(1.234567f, 2f, 3f), count = 42 };"}

List recent executions, then pass an entry's index to replay:

{"action": "get_history", "limit": 5}
{"action": "replay", "index": 0}

Replay uses the selected entry's saved safety and compiler settings. Every execute or replay runs the code again; the compilation cache reuses compiled code, not the execution result.