-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupdate-mcp-servers.py
More file actions
executable file
·164 lines (139 loc) · 5.23 KB
/
update-mcp-servers.py
File metadata and controls
executable file
·164 lines (139 loc) · 5.23 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
#!/usr/bin/env python3
# update-mcp-servers.py
# Installs Python MCP packages into the app bundle.
# Run once to set up MCP; re-run to add or upgrade packages.
#
# Usage: python3 update-mcp-servers.py [--identity=CERT]
import argparse
import os
import shutil
import subprocess
import sys
from pathlib import Path
# ── MCP Python packages ───────────────────────────────────────────────────────
# Add / uncomment one package at a time, then re-run this script to test.
MCP_PACKAGES = [
"mcp-proxy",
"mcp-server-time",
# "mcp-server-fetch",
"duckduckgo-mcp-server",
]
# ─────────────────────────────────────────────────────────────────────────────
RED = "\033[91m"
GREEN = "\033[92m"
RESET = "\033[0m"
def die(msg: str) -> None:
print(f"{RED}{msg}{RESET}", file=sys.stderr)
sys.exit(1)
def find_app_bundle(script_dir: Path) -> Path:
for candidate in sorted(script_dir.glob("*.app")):
if candidate.is_dir():
return candidate
die(f"No .app bundle found in {script_dir}")
def prepare(packages_dir: Path, python3: Path) -> None:
print()
print("==== Preparing MCP packages update ====")
print()
packages_dir.mkdir(parents=True, exist_ok=True)
if not python3.exists():
die(f"Bundled Python not found: {python3}")
print(f" Packages dir : {packages_dir}")
print(f" Python3 : {python3}")
print(f" Packages : {' '.join(MCP_PACKAGES)}")
print()
def install_packages(python3: Path, packages_dir: Path) -> None:
print("==== Installing Python MCP packages ====")
print()
version = subprocess.check_output([str(python3), "--version"], text=True).strip()
print(f" Python : {version}")
print(f" Target : {packages_dir}")
print(f" Packages : {' '.join(MCP_PACKAGES)}")
print()
result = subprocess.run([
str(python3), "-m", "pip", "install",
"--target", str(packages_dir),
"--no-compile",
"--upgrade",
*MCP_PACKAGES,
])
if result.returncode != 0:
die("pip install failed")
print()
print(" Stripping __pycache__, RECORD, and test dirs...")
for d in list(packages_dir.rglob("__pycache__")):
if d.is_dir():
shutil.rmtree(d, ignore_errors=True)
for f in packages_dir.rglob("*.dist-info/RECORD"):
f.unlink(missing_ok=True)
for name in ("tests", "test"):
for d in list(packages_dir.rglob(name)):
if d.is_dir():
shutil.rmtree(d, ignore_errors=True)
print(" Done.")
print()
def codesign(script_dir: Path, app_bundle: Path, identity: str) -> None:
print("==== Codesigning app bundle ====")
print()
script = script_dir / "codesign_applet.sh"
if not script.exists():
die(f"codesign_applet.sh not found: {script}")
result = subprocess.run([str(script), str(app_bundle), identity])
if result.returncode != 0:
die("Codesigning failed")
print()
def verify(python3: Path, packages_dir: Path) -> None:
print("==== Verifying installation ====")
print()
env = {**os.environ, "PYTHONPATH": str(packages_dir)}
failed = []
for module in ["mcp_proxy"]:
r = subprocess.run(
[str(python3), "-c", f"import {module}"],
env=env, capture_output=True,
)
if r.returncode == 0:
print(f" {module}: OK")
else:
print(f" {module}: FAILED", file=sys.stderr)
failed.append(module)
if failed:
die(f"Verification failed: {', '.join(failed)}")
print()
def print_summary(python3: Path, packages_dir: Path) -> None:
print("==== Update complete ====")
print()
env = {**os.environ, "PYTHONPATH": str(packages_dir)}
for pkg in MCP_PACKAGES:
r = subprocess.run(
[str(python3), "-m", "pip", "show", pkg],
env=env, capture_output=True, text=True,
)
for line in r.stdout.splitlines():
if line.startswith("Version:"):
ver = line.split(":", 1)[1].strip()
print(f" {GREEN}{pkg} {ver}{RESET} → {packages_dir}")
break
print()
print(" Next: launch AIChat.app — mcp-proxy starts automatically.")
print()
def main() -> None:
parser = argparse.ArgumentParser(
description="Install Python MCP packages into the app bundle.",
)
parser.add_argument(
"--identity", default="-",
metavar="CERT",
help="Signing identity for codesign (default: - for ad-hoc)",
)
args = parser.parse_args()
script_dir = Path(__file__).resolve().parent
app_bundle = find_app_bundle(script_dir)
packages_dir = app_bundle / "Contents" / "Library" / "Packages"
python3 = app_bundle / "Contents" / "Library" / "Python" / "bin" / "python3"
prepare(packages_dir, python3)
install_packages(python3, packages_dir)
codesign(script_dir, app_bundle, args.identity)
verify(python3, packages_dir)
print_summary(python3, packages_dir)
if __name__ == "__main__":
main()