Go to file
dsarno 8eb684ddc2
fix: comprehensive performance optimizations, claude code config, and stability improvements (issue #577) (#595)
* fix: reduce per-frame GC allocations causing editor hitches (issue #577)

Eliminate memory allocations that occurred every frame, which triggered
garbage collection spikes (~28ms) approximately every second.

Changes:
- EditorStateCache: Skip BuildSnapshot() entirely when state unchanged
  (check BEFORE building). Increased poll interval from 0.25s to 1.0s.
  Cache DeepClone() results to avoid allocations on GetSnapshot().

- TransportCommandDispatcher: Early exit before lock/list allocation
  when Pending.Count == 0, eliminating per-frame allocations when idle.

- StdioBridgeHost: Same early exit pattern for commandQueue.

- MCPForUnityEditorWindow: Throttle OnEditorUpdate to 2-second intervals
  instead of every frame, preventing expensive socket checks 60+/sec.

Fixes GitHub issue #577: High performance impact even when MCP server is off

* fix: prevent multiple domain reloads when calling refresh_unity (issue #577)

Root Cause:
- send_command() had a hardcoded retry loop (min 6 attempts) on connection errors
- Each retry resent the refresh_unity command, causing Unity to reload 6 times
- retry_on_reload=False only controlled reload-state retries, not connection retries

The Fix:
1. Unity C# (MCPForUnity/Editor):
   - Added --reinstall flag to uvx commands in dev mode
   - Ensures local development changes are picked up by uvx/Claude Code
   - Applies to all client configurators (Claude Code, Codex, etc.)

2. Python Server (Server/src):
   - Added max_attempts parameter to send_command()
   - Pass max_attempts=0 when retry_on_reload=False
   - Fixed type handling in refresh_unity.py (handle MCPResponse objects)
   - Added timeout to connection error recovery conditions
   - Recovery logic now returns success instead of error to prevent client retries

Changes:
- MCPForUnity/Editor: Added --reinstall to dev mode uvx commands
- Server/refresh_unity.py: Fixed type handling, improved error recovery
- Server/unity_connection.py: Added max_attempts param, disable retries when retry_on_reload=False

Result: refresh_unity with compile=request now triggers only 1 domain reload instead of 6

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>

* fix: UI and server stability improvements

Unity Editor (C#):
- Fix "Resuming..." stuck state when manually clicking End Session
  Clear ResumeStdioAfterReload and ResumeHttpAfterReload flags in
  OnConnectionToggleClicked and EndOrphanedSessionAsync to prevent
  UI from getting stuck showing "Resuming..." with disabled button
- Remove unsupported --reinstall flag from all uvx command builders
  uvx does not support --reinstall and shows warning when used
  Use --no-cache --refresh instead for dev mode cache busting

Python Server:
- Add "aborted" to connection error patterns in refresh_unity
  Handle WinError 10053 (connection aborted) gracefully during
  Unity domain reload, treating it as expected behavior
- Add WindowsSafeRotatingFileHandler to suppress log rotation errors
  Windows file locking prevents log rotation when file is open by
  another process; catch PermissionError to avoid noisy stack traces
- Fix packaging: add py-modules = ["main"] to pyproject.toml
  setuptools.packages.find only discovers packages (directories with
  __init__.py), must explicitly list standalone module files

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>

* docs: improve refresh_unity connection loss handling documentation

Add detailed comments and logging to clarify why connection loss during
compile is treated as success (expected domain reload behavior, not failure).
This addresses PR feedback about potentially masking real connection errors.

The logic is intentional and correct:
- Connection loss only treated as success when compile='request'
- Domain reload causing disconnect is expected Unity behavior
- Subsequent wait_for_ready loop validates Unity becomes ready
- Prevents multiple domain reload loops (issue #577)

Added logging for observability:
- Info log when expected disconnect detected
- Warning log for non-recoverable errors

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>

* fix: add missing logger import in refresh_unity

Missing logger import causes NameError at runtime when connection
loss handling paths are triggered (lines 82 and 91).

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>

* fix: address code review feedback on thread safety and semantics

Addresses four issues raised in code review:

1. EditorStateCache.GetSnapshot() - Remove shared cached clone
   - Revert to always returning fresh DeepClone() to prevent mutation bugs
   - Main GC optimization remains: state-change detection prevents
     unnecessary _cached rebuilds (the expensive operation)
   - Removed _cachedClone and _cachedCloneSequence fields

2. refresh_unity.py - Fix blocking reason terminology mismatch
   - Changed "asset_refresh" to "asset_import" to match activityPhase
     values from EditorStateCache.cs
   - Ensures asset import is correctly detected as blocking state

3. TransportCommandDispatcher - Fix unsynchronized Count access
   - Moved Pending.Count check inside PendingLock
   - Prevents data races and InvalidOperationException from concurrent
     dictionary access

4. StdioBridgeHost - Fix unsynchronized Count access
   - Moved commandQueue.Count check inside lockObj
   - Ensures all collection access is properly serialized

All changes maintain the GC allocation optimizations while fixing
thread safety violations and semantic contract changes.

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>

* fix: address code review feedback on thread safety and timeout handling

- refresh_unity.py: Track readiness explicitly and return failure on timeout
  instead of silently returning success when wait loop exits without confirming
  ready_for_tools=true

- McpClientConfiguratorBase.cs: Add thread safety guard for Configure() call
  in CheckStatusWithProjectDir(). Changed default attemptAutoRewrite to false
  and added runtime check to prevent calling Configure() from background threads.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Sonnet 4.5 <noreply@anthropic.com>
2026-01-20 18:11:25 -08:00
.claude v9 pre-release pruning (#528) 2026-01-07 18:51:51 -04:00
.github Fix ULF detection in Claude licensing (#569) 2026-01-15 13:34:50 -08:00
CustomTools/RoslynRuntimeCompilation [FEATURE] Custom Tool Fix and Add inspection window for all the tools (#414) 2025-12-07 19:38:32 -05:00
MCPForUnity fix: comprehensive performance optimizations, claude code config, and stability improvements (issue #577) (#595) 2026-01-20 18:11:25 -08:00
Server fix: comprehensive performance optimizations, claude code config, and stability improvements (issue #577) (#595) 2026-01-20 18:11:25 -08:00
TestProjects fix: Add special handling for UIDocument serialization to prevent infinite loops (#586) 2026-01-19 08:21:36 -08:00
docs feat: add test coverage tracking with pytest-cov (#512) 2026-01-08 17:32:44 -04:00
scripts Improved ci prompt testing suite (#270) 2025-09-07 15:47:56 -07:00
tools Update URL for v9 (#537) 2026-01-08 17:30:58 -04:00
.dockerignore HTTP Server, uvx, C# only custom tools (#375) 2025-11-24 23:21:06 -04:00
.gitignore feat: add test coverage tracking with pytest-cov (#512) 2026-01-08 17:32:44 -04:00
LICENSE chore: update package metadata and license to CoplayDev organization 2025-08-08 15:54:43 -04:00
README-zh.md v9 pre-release pruning (#528) 2026-01-07 18:51:51 -04:00
README.md Fix blog post URL in README 2026-01-20 18:02:17 -08:00
claude_skill_unity.zip Claude Skill Example Upload (#380) 2025-12-02 00:43:22 -05:00
deploy-dev.bat [FEATURE] Camera Capture (#449) 2025-12-09 19:00:30 -05:00
docker-compose.yml HTTP Server, uvx, C# only custom tools (#375) 2025-11-24 23:21:06 -04:00
mcp_source.py HTTP Server, uvx, C# only custom tools (#375) 2025-11-24 23:21:06 -04:00
prune_tool_results.py Rename plugin folder to MCPForUnity (#303) 2025-10-03 20:23:28 -04:00
restore-dev.bat HTTP Server, uvx, C# only custom tools (#375) 2025-11-24 23:21:06 -04:00
test_unity_socket_framing.py Autoformat (#297) 2025-09-30 16:25:33 -04:00

README.md

MCP for Unity
English 简体中文

Proudly sponsored and maintained by Coplay -- the best AI assistant for Unity.

Discord Unity Asset Store python

Create your Unity apps with LLMs! MCP for Unity bridges AI assistants (Claude, Cursor, VS Code, etc.) with your Unity Editor via the Model Context Protocol. Give your LLM tools to manage assets, control scenes, edit scripts, and automate tasks.

MCP for Unity building a scene

Quick Start

Prerequisites

1. Install the Unity Package

In Unity: Window > Package Manager > + > Add package from git URL...

[!TIP]

https://github.com/CoplayDev/unity-mcp.git?path=/MCPForUnity

Need a stable/fixed version? Use a tagged URL (requires uninstall to update):

https://github.com/CoplayDev/unity-mcp.git?path=/MCPForUnity#v9.0.3
Other install options (Asset Store, OpenUPM)

Unity Asset Store:

  1. Visit MCP for Unity on the Asset Store
  2. Click Add to My Assets, then import via Window > Package Manager

OpenUPM:

openupm add com.coplaydev.unity-mcp

2. Start the Server & Connect

  1. In Unity: Window > MCP for Unity
  2. Click Start Server (launches HTTP server on localhost:8080)
  3. Select your MCP Client from the dropdown and click Configure
  4. Look for 🟢 "Connected ✓"
  5. Connect your client: Some clients (Cursor, Windsurf, Antigravity) require enabling an MCP toggle in settings, while others (Claude Desktop, Claude Code) auto-connect after configuration.

That's it! Try a prompt like: "Create a red, blue and yellow cube" or "Build a simple player controller"


Features & Tools

Key Features

  • Natural Language Control — Instruct your LLM to perform Unity tasks
  • Powerful Tools — Manage assets, scenes, materials, scripts, and editor functions
  • Automation — Automate repetitive Unity workflows
  • Extensible — Works with various MCP Clients

Available Tools

manage_assetmanage_editormanage_gameobjectmanage_componentsmanage_materialmanage_prefabsmanage_scenemanage_scriptmanage_scriptable_objectmanage_shadermanage_vfxbatch_executefind_gameobjectsfind_in_fileread_consolerefresh_unityrun_testsget_test_jobexecute_menu_itemapply_text_editsscript_apply_editsvalidate_scriptcreate_scriptdelete_scriptget_sha

Available Resources

custom_toolsunity_instancesmenu_itemsget_testsgameobjectgameobject_componentseditor_stateeditor_selectioneditor_prefab_stageproject_infoproject_tagsproject_layers

Performance Tip: Use batch_execute for multiple operations — it's 10-100x faster than individual calls!

Manual Configuration

If auto-setup doesn't work, add this to your MCP client's config file:

HTTP (default — works with Claude Desktop, Cursor, Windsurf):

{
  "mcpServers": {
    "unityMCP": {
      "url": "http://localhost:8080/mcp"
    }
  }
}

VS Code:

{
  "servers": {
    "unityMCP": {
      "type": "http",
      "url": "http://localhost:8080/mcp"
    }
  }
}
Stdio configuration (legacy)

macOS/Linux:

{
  "mcpServers": {
    "unityMCP": {
      "command": "uv",
      "args": ["run", "--directory", "/Users/YOUR_USERNAME/Library/AppSupport/UnityMCP/UnityMcpServer/src", "server.py", "--transport", "stdio"]
    }
  }
}

Windows:

{
  "mcpServers": {
    "unityMCP": {
      "command": "C:/Users/YOUR_USERNAME/AppData/Local/Microsoft/WinGet/Links/uv.exe",
      "args": ["run", "--directory", "C:/Users/YOUR_USERNAME/AppData/Local/UnityMCP/UnityMcpServer/src", "server.py", "--transport", "stdio"]
    }
  }
}
Multiple Unity Instances

MCP for Unity supports multiple Unity Editor instances. To target a specific one:

  1. Ask your LLM to check the unity_instances resource
  2. Use set_active_instance with the Name@hash (e.g., MyProject@abc123)
  3. All subsequent tools route to that instance
Roslyn Script Validation (Advanced)

For Strict validation that catches undefined namespaces, types, and methods:

  1. Install NuGetForUnity
  2. Window > NuGet Package Manager → Install Microsoft.CodeAnalysis v4.14.0
  3. Also install SQLitePCLRaw.core and SQLitePCLRaw.bundle_e_sqlite3
  4. Add USE_ROSLYN to Player Settings > Scripting Define Symbols
  5. Restart Unity
Manual DLL installation (if NuGetForUnity isn't available)
  1. Download Microsoft.CodeAnalysis.CSharp.dll and dependencies from NuGet
  2. Place DLLs in Assets/Plugins/ folder
  3. Ensure .NET compatibility settings are correct
  4. Add USE_ROSLYN to Scripting Define Symbols
  5. Restart Unity
Troubleshooting
  • Unity Bridge Not Connecting: Check Window > MCP for Unity status, restart Unity
  • Server Not Starting: Verify uv --version works, check the terminal for errors
  • Client Not Connecting: Ensure the HTTP server is running and the URL matches your config

Detailed setup guides:

Still stuck? Open an Issue or Join Discord

Contributing

See README-DEV.md for development setup. For custom tools, see CUSTOM_TOOLS.md.

  1. Fork → Create issue → Branch (feature/your-idea) → Make changes → PR
Telemetry & Privacy

Anonymous, privacy-focused telemetry (no code, no project names, no personal data). Opt out with DISABLE_TELEMETRY=true. See TELEMETRY.md.


License: MIT — See LICENSE | Need help? Discord | Issues


Star History

Star History Chart

Unity AI Tools by Coplay

Coplay offers 3 AI tools for Unity:

  • MCP for Unity is available freely under the MIT license.
  • Coplay is a premium Unity AI assistant that sits within Unity and is more than the MCP for Unity.
  • Coplay MCP a free-for-now MCP for Coplay tools.

(These tools have different tech stacks. See this blog post comparing Coplay to MCP for Unity.)

Coplay

Disclaimer

This project is a free and open-source tool for the Unity Editor, and is not affiliated with Unity Technologies.