Class RunCommand
Handles compilation and execution of C# scripts in the Unity environment. Combines validation and execution into a single operation by delegating to RunCommandValidatorTool and RunCommandTool.
Inherited Members
Namespace: Unity.AI.MCP.Editor.Tools
Assembly: Unity.AI.MCP.Editor.dll
Syntax
public static class RunCommand
Fields
Description
Human-readable description of the Unity.RunCommand tool functionality and usage.
Declaration
public const string Description = "Compile and execute a C# script in the Unity Editor.\n\nThis tool first validates that the code can be compiled, then executes it if compilation succeeds.\nArgs: code (required), title (optional).\nReturns: compilation status, execution status, logs, and results.\n\nThis is a powerful tool that allows you to programmatically control virtually every aspect of the game, including physics, input, graphics, gameplay logic, project setting and package management.\n\n### The Golden Template\n```csharp\nusing UnityEngine;\nusing UnityEditor;\n\ninternal class CommandScript : IRunCommand\n{\n public void Execute(ExecutionResult result)\n {\n // 1. Your logic here\n GameObject cube = GameObject.CreatePrimitive(PrimitiveType.Cube);\n\n // 2. Register changes for Undo/Redo and tracking\n result.RegisterObjectCreation(cube);\n\n // 3. Log the result\n result.Log(\"Created {0}\", cube);\n }\n}\n```\n### Rules for Success\n1. **Class Name is Mandatory**: The class MUST be named `CommandScript`. Using any other name will cause a NullReferenceException or execution failure.\n2. **Use `internal` Accessibility**: Always use `internal class CommandScript`. Using `public` will cause an \"Inconsistent Accessibility\" compilation error.\n3. **Use the `result` Object**:\n - **Creation**: Use `result.RegisterObjectCreation(obj)` after creating objects.\n - **Modification**: Use `result.RegisterObjectModification(obj)` BEFORE changing properties.\n - **Deletion**: Use `result.DestroyObject(obj)` instead of `Object.DestroyImmediate`.\n - **Logging**:\n - `result.Log(\"Created {0}\", obj)` - Log with object references using `{0}`, `{1}`, etc.\n - `result.LogWarning(\"Warning message\")` - Log warnings\n - `result.LogError(\"Error message\")` - Log errors\n4. **Avoid Top-Level Statements**: Always wrap your code in the class structure above.\n\n"
Field Value
| Type | Description |
|---|---|
| string |
ToolName
The MCP tool name used to identify this tool in the registry.
Declaration
public const string ToolName = "Unity.RunCommand"
Field Value
| Type | Description |
|---|---|
| string |
Methods
GetOutputSchema()
Returns the output schema for this tool.
Declaration
[McpOutputSchema("Unity.RunCommand")]
public static object GetOutputSchema()
Returns
| Type | Description |
|---|---|
| object | The output schema object defining the structure of successful responses. |
HandleCommand(RunCommandParams)
Main handler for script compilation and execution.
Declaration
[McpTool("Unity.RunCommand", "Compile and execute a C# script in the Unity Editor.\n\nThis tool first validates that the code can be compiled, then executes it if compilation succeeds.\nArgs: code (required), title (optional).\nReturns: compilation status, execution status, logs, and results.\n\nThis is a powerful tool that allows you to programmatically control virtually every aspect of the game, including physics, input, graphics, gameplay logic, project setting and package management.\n\n### The Golden Template\n```csharp\nusing UnityEngine;\nusing UnityEditor;\n\ninternal class CommandScript : IRunCommand\n{\n public void Execute(ExecutionResult result)\n {\n // 1. Your logic here\n GameObject cube = GameObject.CreatePrimitive(PrimitiveType.Cube);\n\n // 2. Register changes for Undo/Redo and tracking\n result.RegisterObjectCreation(cube);\n\n // 3. Log the result\n result.Log(\"Created {0}\", cube);\n }\n}\n```\n### Rules for Success\n1. **Class Name is Mandatory**: The class MUST be named `CommandScript`. Using any other name will cause a NullReferenceException or execution failure.\n2. **Use `internal` Accessibility**: Always use `internal class CommandScript`. Using `public` will cause an \"Inconsistent Accessibility\" compilation error.\n3. **Use the `result` Object**:\n - **Creation**: Use `result.RegisterObjectCreation(obj)` after creating objects.\n - **Modification**: Use `result.RegisterObjectModification(obj)` BEFORE changing properties.\n - **Deletion**: Use `result.DestroyObject(obj)` instead of `Object.DestroyImmediate`.\n - **Logging**:\n - `result.Log(\"Created {0}\", obj)` - Log with object references using `{0}`, `{1}`, etc.\n - `result.LogWarning(\"Warning message\")` - Log warnings\n - `result.LogError(\"Error message\")` - Log errors\n4. **Avoid Top-Level Statements**: Always wrap your code in the class structure above.\n\n", null, null, Groups = new string[] { "core", "scripting" }, EnabledByDefault = true)]
public static Task<object> HandleCommand(RunCommandParams parameters)
Parameters
| Type | Name | Description |
|---|---|---|
| RunCommandParams | parameters | Parameters containing the script code and optional title. |
Returns
| Type | Description |
|---|---|
| Task<object> | A response object indicating success or failure with compilation and execution details. |