forked from CoplayDev/unity-mcp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmcp_source.py
More file actions
executable file
·173 lines (136 loc) · 5.32 KB
/
mcp_source.py
File metadata and controls
executable file
·173 lines (136 loc) · 5.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#!/usr/bin/env python3
"""
Generic helper to switch the MCP for Unity package source in a Unity project's
Packages/manifest.json. This is useful for switching between upstream and local repos while working on the MCP.
Usage:
python mcp_source.py [--manifest /abs/path/to/manifest.json] [--repo /abs/path/to/unity-mcp] [--choice 1|2|3]
Choices:
1) Upstream main (CoplayDev/unity-mcp)
2) Your remote current branch (derived from `origin` and current branch)
3) Local repo workspace (file: URL to MCPForUnity in your checkout)
"""
from __future__ import annotations
import argparse
import json
import pathlib
import subprocess
import sys
PKG_NAME = "com.coplaydev.unity-mcp"
BRIDGE_SUBPATH = "MCPForUnity"
def run_git(repo: pathlib.Path, *args: str) -> str:
result = subprocess.run([
"git", "-C", str(repo), *args
], capture_output=True, text=True)
if result.returncode != 0:
raise RuntimeError(result.stderr.strip()
or f"git {' '.join(args)} failed")
return result.stdout.strip()
def normalize_origin_to_https(url: str) -> str:
"""Map common SSH origin forms to https for Unity's git URL scheme."""
if url.startswith("git@github.com:"):
owner_repo = url.split(":", 1)[1]
if owner_repo.endswith(".git"):
owner_repo = owner_repo[:-4]
return f"https://github.com/{owner_repo}.git"
# already https or file: etc.
return url
def detect_repo_root(explicit: str | None) -> pathlib.Path:
if explicit:
return pathlib.Path(explicit).resolve()
# Prefer the git toplevel from the script's directory
here = pathlib.Path(__file__).resolve().parent
try:
top = run_git(here, "rev-parse", "--show-toplevel")
return pathlib.Path(top)
except Exception:
return here
def detect_branch(repo: pathlib.Path) -> str:
return run_git(repo, "rev-parse", "--abbrev-ref", "HEAD")
def detect_origin(repo: pathlib.Path) -> str:
url = run_git(repo, "remote", "get-url", "origin")
return normalize_origin_to_https(url)
def find_manifest(explicit: str | None) -> pathlib.Path:
if explicit:
return pathlib.Path(explicit).resolve()
# Walk up from CWD looking for Packages/manifest.json
cur = pathlib.Path.cwd().resolve()
for parent in [cur, *cur.parents]:
candidate = parent / "Packages" / "manifest.json"
if candidate.exists():
return candidate
raise FileNotFoundError(
"Could not find Packages/manifest.json from current directory. Use --manifest to specify a path.")
def read_json(path: pathlib.Path) -> dict:
with path.open("r", encoding="utf-8") as f:
return json.load(f)
def write_json(path: pathlib.Path, data: dict) -> None:
with path.open("w", encoding="utf-8") as f:
json.dump(data, f, indent=2)
f.write("\n")
def build_options(repo_root: pathlib.Path, branch: str, origin_https: str):
upstream = "https://github.com/CoplayDev/unity-mcp.git?path=/MCPForUnity"
# Ensure origin is https
origin = origin_https
# If origin is a local file path or non-https, try to coerce to https github if possible
if origin.startswith("file:"):
# Not meaningful for remote option; keep upstream
origin_remote = upstream
else:
origin_remote = origin
return [
("[1] Upstream main", upstream),
(f"[2] Remote {branch}",
f"{origin_remote}?path=/{BRIDGE_SUBPATH}#{branch}"),
(f"[3] Local {branch}",
f"file:{(repo_root / BRIDGE_SUBPATH).as_posix()}"),
]
def parse_args() -> argparse.Namespace:
p = argparse.ArgumentParser(
description="Switch MCP for Unity package source")
p.add_argument("--manifest", help="Path to Packages/manifest.json")
p.add_argument(
"--repo", help="Path to unity-mcp repo root (for local file option)")
p.add_argument(
"--choice", choices=["1", "2", "3"], help="Pick option non-interactively")
return p.parse_args()
def main() -> None:
args = parse_args()
try:
repo_root = detect_repo_root(args.repo)
branch = detect_branch(repo_root)
origin = detect_origin(repo_root)
except Exception as e:
print(f"Error: {e}", file=sys.stderr)
sys.exit(1)
options = build_options(repo_root, branch, origin)
try:
manifest_path = find_manifest(args.manifest)
except Exception as e:
print(f"Error: {e}", file=sys.stderr)
sys.exit(1)
print("Select MCP package source by number:")
for label, _ in options:
print(label)
if args.choice:
choice = args.choice
else:
choice = input("Enter 1-3: ").strip()
if choice not in {"1", "2", "3"}:
print("Invalid selection.", file=sys.stderr)
sys.exit(1)
idx = int(choice) - 1
_, chosen = options[idx]
data = read_json(manifest_path)
deps = data.get("dependencies", {})
if PKG_NAME not in deps:
print(
f"Error: '{PKG_NAME}' not found in manifest dependencies.", file=sys.stderr)
sys.exit(1)
print(f"\nUpdating {PKG_NAME} → {chosen}")
deps[PKG_NAME] = chosen
data["dependencies"] = deps
write_json(manifest_path, data)
print(f"Done. Wrote to: {manifest_path}")
print("Tip: In Unity, open Package Manager and Refresh to re-resolve packages.")
if __name__ == "__main__":
main()