Sanselll/unity-mcp
If you are the rightful owner of unity-mcp and would like to certify it and/or have it hosted online, please leave a comment on the right or send an email to dayong@mcphub.com.
The Unity MCP Server is a Unity Editor plugin that implements a Model Context Protocol (MCP) server, enabling direct communication between Claude Code and Unity Editor.
Unity MCP Server
A Unity Editor plugin that implements a Model Context Protocol (MCP) server, enabling direct communication between Claude Code and Unity Editor. The system runs a pure C# HTTP server within Unity Editor itself, eliminating the need for external dependencies.
🚀 Features
- Direct HTTP Communication: No bridge scripts or external processes needed
- Dynamic Tool System: 10+ built-in tools with JSON-defined custom tools and hot-reloading
- C# Code Execution: Execute arbitrary C# code directly in Unity Editor
- Real-time Unity Control: Play/pause, scene manipulation, log access
- Compilation Safety: Detects Unity compilation state and responds appropriately
- Auto-start: Server automatically starts when Unity loads
- Visual Status Monitor: Built-in GUI for server management and monitoring
🏗️ Architecture
The system consists of three main layers:
- HTTP MCP Server (
MCPServer.cs) - Handles MCP JSON-RPC requests on port 9876 - Dynamic Tool System (
DynamicToolManager.cs) - Manages and executes Unity tools defined in JSON files - C# Code Execution (
UnityDynamicCompiler.cs) - Compiles and executes C# code queries dynamically
Claude Code ←→ HTTP (Port 9876) ←→ Unity MCP Server ←→ Unity Editor API
📦 Installation
Prerequisites
- Unity 2022.3 or higher
- Claude Code CLI installed
Method 1: Unity Package Manager (Recommended)
- Open Unity Package Manager (
Window > Package Manager) - Click the
+button and selectAdd package from git URL - Enter:
https://github.com/Sanselll/unity-mcp.git?path=UnityPackage/com.mcp.unity - Click
Add
Method 2: Local Installation
-
Clone this repository:
git clone https://github.com/Sanselll/unity-mcp.git cd unity-mcp -
Copy the Unity package to your project:
cp -r UnityPackage/com.mcp.unity /path/to/your/unity/project/Packages/ -
Unity will automatically import and compile the package
⚙️ Configuration
1. Unity Setup
The MCP server starts automatically when Unity loads. You can manage it through:
- Menu:
Tools > Unity MCP > Server Status - Status Window: Monitor connections, request counts, and server state
2. Claude Code Configuration
Configure Claude Code to connect to the Unity MCP server:
claude mcp add --transport http unity http://localhost:9876
3. Verify Connection
Test the connection:
# Check if server is responding
curl -X POST -H "Content-Type: application/json" \
-d '{"jsonrpc":"2.0","method":"server/status","id":1}' \
http://localhost:9876
🛠️ Usage
Basic C# Code Execution
In Claude Code, use the execute_query tool to run C# code in Unity:
// Create a cube
var cube = GameObject.CreatePrimitive(PrimitiveType.Cube);
cube.name = "MyCube";
cube.transform.position = new Vector3(0, 1, 0);
return $"Created cube at position {cube.transform.position}";
Built-in Tools
The system includes several pre-built tools:
execute_query- Execute arbitrary C# code in Unity Editorget_logs- Retrieve Unity console logs (errors, warnings, info)clear_logs- Clear Unity consolelist_scene_objects- List GameObjects in current scenelist_scenes- List all scenes in projectcreate_gameobject- Create GameObjects with primitivesfocus_unity- Bring Unity window to foregroundrecompile- Trigger script recompilationplay_mode- Control Unity play/pause/stopfind_object- Find GameObjects by name patternremove_tool- Remove custom tools
Example Workflows
Scene Analysis
Ask Claude: "What objects are in my current scene?"
Error Investigation
Ask Claude: "Show me the recent errors from Unity console"
Quick GameObject Creation
Ask Claude: "Create 5 cubes in a line along the X axis"
Play Mode Testing
Ask Claude: "Start play mode and create a sphere at the player position"
🔧 Advanced Usage
Manual Tool Extension
Developers can extend the system by manually creating JSON tool files in the Tools/Custom/ directory. See the 📝 Tool Development section for details on the JSON format and hot-reloading capabilities.
API Endpoints
The server exposes several MCP endpoints:
initialize- Server initializationtools/list- Get available toolstools/call- Execute a toolserver/status- Get server statusresources/list- List available resourcesresources/read- Read resource content
🧪 Development & Testing
Testing
Test the installation by:
- Installing the package in Unity
- Configuring Claude Code with the server URL
- Using the
execute_querytool to run simple C# code
Debugging
- Server Status: Use
Tools > Unity MCP > Server Statusto monitor the server - Console Logs: Check Unity console for detailed error messages
- Port Conflicts: Server runs on port 9876 (configurable in MCPServer.cs)
- Compilation Detection: Server pauses during Unity recompilation
Assembly Configuration
The package uses a custom assembly definition (UnityMCP.Editor.asmdef) with:
autoReferenced: false- Reduces recompilation triggers- Editor-only scope
- Dependencies on
Newtonsoft.Json
📝 Tool Development
Tool Structure
Tools are JSON files with this structure:
{
"name": "tool_name",
"description": "Tool description",
"queryTemplate": "C# code with {{parameter}} placeholders",
"inputSchema": {
"type": "object",
"properties": {
"parameter": {
"type": "string|number|boolean",
"description": "Parameter description",
"default": "default_value"
}
},
"required": ["required_param"]
}
}
Template System
- Use
{{parameterName}}placeholders in queryTemplate - Supports parameter substitution with type conversion
- Default values from schema are used for missing parameters
- Full Unity API access in templates
Hot Reloading
Tools are automatically reloaded when JSON files change, enabling rapid development without Unity restarts.
🛡️ Security & Safety
Code Execution Safety
- Tools are validated before creation
- Dangerous namespaces (
System.IO.File,System.Diagnostics.Process) are blocked - All code runs within Unity's compilation context
- No external file system access by default
Compilation Safety
- Server detects Unity compilation state
- Returns appropriate errors during recompilation
- Prevents crashes during domain reloads
🐛 Troubleshooting
Common Issues
Server won't start
- Check if port 9876 is available
- Look for Unity console errors
- Try restarting Unity
Connection timeout
- Verify server is running:
Tools > Unity MCP > Server Status - Check firewall settings
- Test with curl command
Tools not loading
- Check JSON syntax in tool files
- Verify file paths and permissions
- Look for validation errors in Unity console
Compilation errors
- Server automatically pauses during recompilation
- Wait for compilation to complete
- Check for syntax errors in custom tools
Performance Notes
- Server runs on Unity's main thread for API safety
- HTTP requests are queued and processed during Update cycle
- Large result sets may impact performance
🤝 Contributing
We welcome contributions! Please:
- Fork the repository
- Create a feature branch
- Ensure code quality and documentation
- Submit a pull request
Development Setup
- Clone the repository
- Open in Unity 2022.3+
- Install dependencies via Package Manager
- Test with a Unity project to verify functionality
📄 License
This project is licensed under the MIT License - see the file for details.
🙏 Acknowledgments
- Built for Claude Code integration
- Uses Model Context Protocol specification
- Unity Editor APIs and reflection system
- Community feedback and contributions
📚 Additional Resources
Made with ❤️ for Unity developers using Claude Code