Tool: compile
Relevant source files
The following files were used as context for generating this wiki page:
- Packages/src/Editor/Api/McpTools/Compile/CompileTool.cs
- Packages/src/Editor/Api/McpTools/UseCases/Tools/CompileUseCase.cs
- Packages/src/Editor/Api/McpTools/Compile/SKILL.md
- Packages/src/TOOL_REFERENCE.md
- README.md
- Packages/src/ARCHITECTURE.md
- Packages/src/Editor/Compilation/RoslynCompiler.cs
- Packages/src/TypeScriptServer~/src/tools/dynamic-unity-command-tool.ts
The compile tool is a fundamental component of the uLoopMCP system, designed to enable an AI agent to trigger Unity project compilation programmatically. Its primary purpose is to form a "self-hosted development loop" where an AI can write or modify code, execute compilation, analyze the results for errors or warnings, and then iterate on fixes until the project compiles successfully. This tool is central to achieving autonomous development and bug-fixing workflows within a Unity project.
The tool performs an AssetDatabase.Refresh() before initiating the compilation, ensuring that any file changes are recognized by Unity. It can detect errors and warnings that standard linters might miss, providing critical feedback for the AI agent.
Sources: README.md, Packages/src/TOOL_REFERENCE.md:12-13
Architecture and Execution Flow
The compile tool follows the standard uLoopMCP architectural pattern, where a request from an MCP client (like Claude Code or Cursor) is relayed through a TypeScript server to the Unity Editor.
The overall system architecture involves three main components: the MCP Client, the TypeScript Server, and the Unity Editor. The TypeScript server acts as an intermediary, receiving MCP protocol commands and translating them into TCP/JSON-RPC requests for the Unity Editor.
Sources: Packages/src/ARCHITECTURE.md:13-18
The sequence diagram below illustrates the typical flow for a compile command.
This diagram shows the end-to-end execution path for the compile tool.
Sources: Packages/src/ARCHITECTURE.md:32-37, Packages/src/Editor/Api/McpTools/Compile/CompileTool.cs:19-25, Packages/src/TypeScriptServer~/src/tools/dynamic-unity-command-tool.ts:114-124
C# Implementation
On the Unity side, the compile tool's implementation is split between a Tool class and a UseCase class, following a Domain-Driven Design (DDD) integrated pattern.
Sources: Packages/src/Editor/Api/McpTools/Compile/CompileTool.cs:7-8
CompileTool Class
The CompileTool class is the direct entry point for the compile command within the Unity Editor. It is a lightweight class responsible for receiving the request and delegating the core logic to the CompileUseCase.
- It inherits from
AbstractUnityTool<CompileSchema, CompileResponse>, ensuring type-safe handling of parameters and responses. - The
[McpTool]attribute automatically registers the class with the uLoopMCP system, making it discoverable by the TypeScript server. - Its primary function is to instantiate and call the
ExecuteAsyncmethod of theCompileUseCase.
Sources: Packages/src/Editor/Api/McpTools/Compile/CompileTool.cs:10-25
The class relationships are shown below.
This diagram illustrates how CompileTool uses its schema and response types and delegates its execution logic to CompileUseCase.
Sources: Packages/src/Editor/Api/McpTools/Compile/CompileTool.cs:10-25
CompileUseCase Class
The CompileUseCase class encapsulates the business logic for the compilation process. It is responsible for interacting with Unity's compilation pipeline and constructing the CompileResponse. The ExecuteAsync method within this class handles the actual compilation trigger and result processing.
Sources: Packages/src/Editor/Api/McpTools/UseCases/Tools/CompileUseCase.cs
API Specification
The compile tool exposes a simple API with one parameter and a detailed response object.
Sources: Packages/src/TOOL_REFERENCE.md:11-34
Parameters
| Parameter | Type | Description | Default |
|---|---|---|---|
ForceRecompile | boolean | If true, forces a full recompilation of the project. | false |
Sources: Packages/src/TOOL_REFERENCE.md:14-15
Response
| Field | Type | Description |
|---|---|---|
Success | boolean | true if compilation succeeded without errors. |
ErrorCount | number | The total number of compilation errors. |
WarningCount | number | The total number of compilation warnings. |
CompletedAt | string | The ISO timestamp when the compilation finished. |
Errors | array | An array of error objects if ErrorCount > 0. |
Warnings | array | An array of warning objects if WarningCount > 0. |
Message | string | An optional message providing additional information. |
Sources: Packages/src/TOOL_REFERENCE.md:17-34
Skill Integration
The compile tool is exposed to LLM tools through the "Skills" system. A SKILL.md file defines the command, its purpose, and how it should be used by an AI. This allows tools like Claude Code to automatically discover and invoke /uloop-compile.
The skill definition emphasizes its use for fixing compile errors and as a verification step in an autonomous development loop.
Sources: Packages/src/Editor/Api/McpTools/Compile/SKILL.md, README.md:214-222
Related Compilation Mechanisms
While compile handles full project compilation, uLoopMCP also provides a more granular compilation mechanism through the execute-dynamic-code tool. This tool uses an embedded Roslyn compiler to compile and run C# code snippets dynamically.
It features a CompileOnly parameter which allows for validating a code snippet without executing it. This is useful for checking the syntax of a newly generated script before creating a .cs file and triggering a full project recompile with the main compile tool.
Sources: Packages/src/Editor/Api/McpTools/ExecuteDynamicCode/ExecuteDynamicCodeSchema.cs:46-50, Packages/src/Editor/Compilation/RoslynCompiler.cs:253-290
Conclusion
The compile tool is a cornerstone of the uLoopMCP feature set. By providing a simple yet powerful interface to the Unity compilation pipeline, it empowers AI agents to autonomously manage a core part of the software development lifecycle. Its integration into the broader toolset and Skills system makes it a critical enabler for complex, automated tasks like refactoring, bug fixing, and feature implementation.
Sources: README.md:27-33, 80-84