Skip to content

Commit c43efe3

Browse files
dsarnoclaude
andauthored
Fix editor state always reporting stale when Unity is backgrounded (#789)
* Fix editor state always reporting stale when Unity is backgrounded EditorStateCache.GetSnapshot() returned a cached clone with the observed_at_unix_ms from the last OnUpdate tick. When Unity is backgrounded, OnUpdate is throttled, so the timestamp grows stale even though the data is current and Unity is responsive. Now stamps the clone at serve time so the server-side staleness check (>2s = stale) reflects when the snapshot was served, not when the update loop last ran. This fixes ready_for_tools being false for every backgrounded Unity editor on HTTP. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * Only re-stamp editor state when Unity is backgrounded Addresses CodeRabbit review feedback: the unconditional re-stamp defeated the staleness check for genuinely unresponsive focused editors. Now uses InternalEditorUtility.isApplicationActive to conditionally re-stamp only when Unity is backgrounded. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * Add /mcp-source skill and fix upstream main URL Add Claude Code skill for switching MCP package source in Unity projects. Fix mcp_source.py upstream main URL to include required #main branch suffix. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 22b2d85 commit c43efe3

3 files changed

Lines changed: 96 additions & 2 deletions

File tree

.claude/skills/mcp-source/SKILL.md

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
---
2+
name: mcp-source
3+
description: Switch MCP for Unity package source in connected Unity projects. Use /mcp-source [main|beta|branch|local] to swap between upstream releases, your remote branch, or local dev checkout.
4+
---
5+
6+
# Switch MCP for Unity Package Source
7+
8+
You are switching the `com.coplaydev.unity-mcp` package source in one or more Unity projects.
9+
10+
## allowed-tools
11+
12+
Bash, Read, Edit, ReadMcpResourceTool, ListMcpResourcesTool
13+
14+
## Instructions
15+
16+
### 1. Parse arguments
17+
18+
The user's argument is: `$ARGUMENTS`
19+
20+
Valid values: `main`, `beta`, `branch`, `local`, or empty.
21+
22+
If empty or not one of the four valid values, ask the user to choose:
23+
- **main** — upstream main branch (stable releases)
24+
- **beta** — upstream beta branch (pre-release)
25+
- **branch** — your current remote branch (for testing a PR)
26+
- **local** — local file reference to your checkout (for live dev iteration)
27+
28+
### 2. Detect repo context
29+
30+
Run these git commands from the current working directory to find the unity-mcp repo:
31+
32+
```bash
33+
git rev-parse --show-toplevel # → repo_root
34+
git rev-parse --abbrev-ref HEAD # → branch_name
35+
git remote get-url origin # → origin_url
36+
```
37+
38+
Convert SSH origins to HTTPS: if `origin_url` starts with `git@github.com:`, transform it to `https://github.com/{owner}/{repo}.git`.
39+
40+
### 3. Discover target Unity projects
41+
42+
Try two approaches to find `Packages/manifest.json` files to update:
43+
44+
**Approach A — MCP resources (preferred):**
45+
Read `mcpforunity://project/info` for each connected Unity instance (use `ListMcpResourcesTool` to find available instances). Extract `projectRoot` and use `{projectRoot}/Packages/manifest.json`.
46+
47+
**Approach B — filesystem fallback:**
48+
If no MCP instances are connected, search upward from the current working directory for `Packages/manifest.json` files using Bash:
49+
```bash
50+
find "$(pwd)" -maxdepth 3 -name "manifest.json" -path "*/Packages/manifest.json" 2>/dev/null
51+
```
52+
53+
If multiple manifests are found, update all of them (confirming with the user first).
54+
55+
### 4. Build the package URL
56+
57+
Based on the user's selection, construct the dependency value:
58+
59+
| Selection | URL |
60+
|-----------|-----|
61+
| `main` | `https://github.com/CoplayDev/unity-mcp.git?path=/MCPForUnity#main` |
62+
| `beta` | `https://github.com/CoplayDev/unity-mcp.git?path=/MCPForUnity#beta` |
63+
| `branch` | `{origin_https}?path=/MCPForUnity#{branch_name}` |
64+
| `local` | `file:{repo_root}/MCPForUnity` |
65+
66+
For `branch`: use the HTTPS-normalized origin URL and current git branch name.
67+
For `local`: use the absolute path to the repo root with `file:` prefix (no `//`), e.g. `file:/Users/davidsarno/unity-mcp/MCPForUnity`.
68+
69+
### 5. Update each manifest
70+
71+
For each discovered `manifest.json`:
72+
73+
1. Read the file with the Read tool
74+
2. Find the `"com.coplaydev.unity-mcp"` dependency line
75+
3. Use the Edit tool to replace the old value with the new URL
76+
4. Report what was changed: old value → new value, file path
77+
78+
### 6. Report results
79+
80+
After updating, tell the user:
81+
- Which manifests were updated
82+
- The old and new package source values
83+
- Remind them: "Unity will re-resolve the package automatically. If it doesn't, open Package Manager and click Refresh."

MCPForUnity/Editor/Services/EditorStateCache.cs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -514,7 +514,18 @@ public static JObject GetSnapshot()
514514
// Always return a fresh clone to prevent mutation bugs.
515515
// The main GC optimization comes from state-change detection (OnUpdate)
516516
// which prevents unnecessary _cached rebuilds, not from caching the clone.
517-
return (JObject)_cached.DeepClone();
517+
var clone = (JObject)_cached.DeepClone();
518+
519+
// When Unity is backgrounded, OnUpdate is throttled and the
520+
// cached timestamp grows stale even though the data is current.
521+
// Re-stamp only in that case so the server-side staleness check
522+
// still fires for genuinely unresponsive editors when focused.
523+
if (!InternalEditorUtility.isApplicationActive)
524+
{
525+
clone["observed_at_unix_ms"] = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
526+
}
527+
528+
return clone;
518529
}
519530
}
520531

mcp_source.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ def write_json(path: pathlib.Path, data: dict) -> None:
9292

9393

9494
def build_options(repo_root: pathlib.Path, branch: str, origin_https: str):
95-
upstream_main = "https://github.com/CoplayDev/unity-mcp.git?path=/MCPForUnity"
95+
upstream_main = "https://github.com/CoplayDev/unity-mcp.git?path=/MCPForUnity#main"
9696
upstream_beta = "https://github.com/CoplayDev/unity-mcp.git?path=/MCPForUnity#beta"
9797
# Ensure origin is https
9898
origin = origin_https

0 commit comments

Comments
 (0)