Repository navigation
Expand file tree
/
Copy pathapp.py
More file actions
472 lines (427 loc) · 21.8 KB
/
Copy pathapp.py
File metadata and controls
472 lines (427 loc) · 21.8 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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
#!/usr/bin/env python3
import atexit
import html
import json
import os
import queue
import re
import shutil
import signal
import socketserver
import subprocess
import sys
import threading
import time
from http import HTTPStatus
from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer
from pathlib import Path
from urllib.parse import parse_qs, unquote, urlparse
import hub as hub_module
import importer
import inspector
import integrations
import oauth
import secrets_store
import sessions
import updater as updater_module
from config_store import (STATE_DIR, config_path, load_config, load_profiles, load_settings, reveal_command, save_servers,
save_settings, update_profiles, validate_servers)
from version import VERSION
ROOT = Path(__file__).resolve().parent
STARTED_AT = time.time()
CALLBACK_TEXT = {
"en": {"ok": "Signed in", "fail": "Sign-in failed", "connected": "“{server}” is connected.", "back": "Back to MCP Hub"},
"de": {"ok": "Angemeldet", "fail": "Anmeldung fehlgeschlagen", "connected": "„{server}“ ist verbunden.", "back": "Zurück zum MCP Hub"},
}
hub = hub_module.Hub(load_config, load_settings, STATE_DIR, load_profiles)
STATIC = {"/app.css": "text/css; charset=utf-8", "/i18n.js": "text/javascript; charset=utf-8"}
MCP_PATH = re.compile(r"^/(?:p/([^/]+)/)?mcp(?:/(compact))?$")
updater = updater_module.Updater(ROOT, STATE_DIR, VERSION)
def sync_hub_in_background():
threading.Thread(target=hub.sync, daemon=True).start()
def bridge_info():
python = sys.executable if sys.platform == "win32" else shutil.which("python3") or sys.executable
return {"python": python, "script": str(ROOT / "bridge.py")}
def restart():
time.sleep(0.5)
inspector.disconnect_all()
hub.close()
sys.stdout.flush()
os.execv(sys.executable, [sys.executable, *sys.argv])
class Server(ThreadingHTTPServer):
# SO_REUSEADDR on Windows lets a second hub bind a port that is already in use.
allow_reuse_address = sys.platform != "win32"
def server_bind(self):
# HTTPServer.server_bind resolves the host name via getfqdn, which stalls for ~30 s on some macOS setups.
socketserver.TCPServer.server_bind(self)
self.server_name, self.server_port = self.server_address[:2]
class Handler(BaseHTTPRequestHandler):
def log_message(self, fmt, *args):
pass
def send(self, payload, status=HTTPStatus.OK, content_type="application/json", headers=None):
body = payload if isinstance(payload, bytes) else json.dumps(payload).encode()
self.send_response(status)
for key, value in (headers or {}).items():
self.send_header(key, value)
self.send_header("Content-Type", content_type)
self.send_header("Content-Length", str(len(body)))
self.send_header("Cache-Control", "no-store")
self.end_headers()
self.wfile.write(body)
def body(self):
length = int(self.headers.get("Content-Length") or 0)
return json.loads(self.rfile.read(length) or b"{}")
def query(self, name, default, cast=str):
try:
return cast(parse_qs(urlparse(self.path).query).get(name, [default])[0])
except ValueError:
return cast(default)
def rejected(self, write=False):
port = self.server.server_address[1]
hosts = (f"127.0.0.1:{port}", f"localhost:{port}", f"[::1]:{port}")
if self.headers.get("Host") not in hosts:
return HTTPStatus.FORBIDDEN, "Host not allowed"
origin = self.headers.get("Origin")
if origin and origin not in tuple(f"http://{host}" for host in hosts):
return HTTPStatus.FORBIDDEN, "Origin not allowed"
if write and self.path.startswith("/api/") and self.headers.get_content_type() != "application/json":
return HTTPStatus.UNSUPPORTED_MEDIA_TYPE, "Content-Type must be application/json"
return None
def do_GET(self):
if rejection := self.rejected():
return self.send({"error": rejection[1]}, rejection[0])
return self.get()
def do_PUT(self):
if rejection := self.rejected(write=True):
return self.send({"error": rejection[1]}, rejection[0])
try:
return self.put()
except json.JSONDecodeError as e:
return self.send({"error": f"Invalid JSON: {e}"}, HTTPStatus.BAD_REQUEST)
def do_POST(self):
if rejection := self.rejected(write=True):
return self.send({"error": rejection[1]}, rejection[0])
try:
return self.post()
except json.JSONDecodeError as e:
return self.send({"error": f"Invalid JSON: {e}"}, HTTPStatus.BAD_REQUEST)
def do_DELETE(self):
if rejection := self.rejected():
return self.send({"error": rejection[1]}, rejection[0])
return self.delete()
def state(self):
return {
"version": VERSION,
"configPath": str(config_path()),
"config": load_config(),
"settings": load_settings(),
"profiles": load_profiles(),
"bridge": bridge_info(),
"platform": sys.platform,
"startedAt": STARTED_AT,
}
def get(self):
path = self.path.split("?")[0]
if path in ("/", "/index.html"):
return self.send((ROOT / "index.html").read_bytes(), content_type="text/html; charset=utf-8")
if path in STATIC:
return self.send((ROOT / path.lstrip("/")).read_bytes(), content_type=STATIC[path])
if self.path == "/api/state":
return self.send(self.state())
if self.path == "/api/hub":
return self.send(hub.status())
if self.path == "/api/live":
return self.send(hub.live_clients())
if self.path == "/api/clients":
return self.send(hub.clients.snapshot())
if self.path == "/api/integrations":
return self.send(integrations.status(self.server.server_address[1]))
if self.path == "/api/compact":
full = json.dumps(hub.handle("tools/list", {})["tools"], ensure_ascii=False)
small = json.dumps(hub.handle("tools/list", {}, mode="compact")["tools"], ensure_ascii=False)
return self.send({"fullChars": len(full), "compactChars": len(small)})
if self.path == "/api/update":
enabled = load_settings()["checkUpdates"]
if enabled and updater.stale():
updater.check_in_background()
return self.send({**updater.status(), "enabled": enabled})
if self.path == "/api/usage":
return self.send(hub.usage.snapshot())
if self.path.startswith("/api/hub/tools"):
tools = hub.tools(parse_qs(urlparse(self.path).query).get("name", [""])[0])
if tools is None:
return self.send({"error": "Server is not running in the hub"}, HTTPStatus.NOT_FOUND)
return self.send(tools)
if self.path.startswith("/api/hub/review"):
review = hub.review(self.query("name", ""))
if review is None:
return self.send({"error": "Server is not running in the hub"}, HTTPStatus.NOT_FOUND)
return self.send(review)
if self.path.startswith("/api/stats"):
days = self.query("days", "14", int)
return self.send(hub.calls.stats(max(1, min(days, 90))))
if self.path.startswith("/api/calls"):
limit = self.query("limit", "300", int)
return self.send(hub.calls.recent(min(limit, 1000)))
if match := MCP_PATH.match(self.path):
if "text/event-stream" not in self.headers.get("Accept", ""):
self.send_response(HTTPStatus.METHOD_NOT_ALLOWED)
self.send_header("Allow", "POST")
self.send_header("Content-Length", "0")
self.end_headers()
return
if not hub.clients.valid(self.headers.get("Mcp-Session-Id")):
return self.send({"error": "Unknown session"}, HTTPStatus.NOT_FOUND)
return self.event_stream(unquote(match.group(1)) if match.group(1) else None)
url = urlparse(self.path)
if url.path == "/oauth/callback":
return self.oauth_callback(parse_qs(url.query))
if url.path == "/api/inspect/log":
return self.send(inspector.log(self.query("name", ""), self.query("since", "0", float)))
self.send({"error": "not found"}, HTTPStatus.NOT_FOUND)
def put(self):
data = self.body()
if self.path == "/api/servers":
servers = data.get("mcpServers")
if error := validate_servers(servers):
return self.send({"error": error}, HTTPStatus.BAD_REQUEST)
save_servers(servers)
sync_hub_in_background()
return self.send(self.state())
if self.path == "/api/settings":
save_settings(data)
sync_hub_in_background()
return self.send(self.state())
self.send({"error": "not found"}, HTTPStatus.NOT_FOUND)
def post(self):
if match := MCP_PATH.match(self.path):
return self.mcp(unquote(match.group(1)) if match.group(1) else None, match.group(2))
if self.path.startswith("/api/integrations/"):
data, port = self.body(), self.server.server_address[1]
action = self.path.removeprefix("/api/integrations/")
try:
if action == "connect":
url = f"http://127.0.0.1:{port}/mcp" + ("/compact" if data.get("compact") else "")
integrations.connect(data.get("id", ""), url, port, bridge_info())
elif action == "disconnect":
integrations.disconnect(data.get("id", ""), port)
elif action == "remove":
integrations.remove(data.get("id", ""), data.get("names") or [])
elif action == "export":
return self.send(integrations.export(data.get("id", ""), port))
else:
return self.send({"error": "not found"}, HTTPStatus.NOT_FOUND)
except integrations.IntegrationError as e:
return self.send({"error": str(e)}, HTTPStatus.CONFLICT)
except OSError as e:
return self.send({"error": f"Could not write the client configuration: {e}"}, HTTPStatus.CONFLICT)
return self.send(integrations.status(port))
if self.path == "/api/reveal":
try:
subprocess.Popen(reveal_command(config_path()), stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
except OSError as e:
return self.send({"error": f"Could not open the folder: {e}"}, HTTPStatus.CONFLICT)
return self.send({})
if self.path == "/api/hub/restart":
name = self.body().get("name", "")
if name not in hub.upstreams:
return self.send({"error": f"Unknown server: {name}"}, HTTPStatus.NOT_FOUND)
hub.reconnect(name)
return self.send({})
if self.path == "/api/hub/approve":
data = self.body()
if not isinstance(data.get("items"), dict):
return self.send({"error": "items must map each entry to the hash shown in the review"},
HTTPStatus.BAD_REQUEST)
approved = hub.approve(data.get("name", ""), data["items"])
if approved is None:
return self.send({"error": "Server is not running in the hub"}, HTTPStatus.NOT_FOUND)
return self.send({"approved": approved, "hub": hub.status()})
if self.path == "/api/restart":
threading.Thread(target=restart, daemon=True).start()
return self.send({"ok": True})
if self.path == "/api/update/check":
return self.send({**updater.check(), "enabled": load_settings()["checkUpdates"]})
if self.path == "/api/update/apply":
if updater.applying:
return self.send({"error": "An update is already running"}, HTTPStatus.CONFLICT)
try:
version = updater.apply()
except updater_module.UpdateError as e:
return self.send({"error": str(e)}, HTTPStatus.CONFLICT)
threading.Thread(target=restart, daemon=True).start()
return self.send({"ok": True, "version": version})
if self.path == "/api/profiles":
data = self.body()
if error := update_profiles(data.get("action"), data.get("name"), data.get("newName"), sync_hub_in_background,
access=data.get("access")):
return self.send({"error": error}, HTTPStatus.BAD_REQUEST)
return self.send(self.state())
if self.path in ("/api/oauth/start", "/api/oauth/logout"):
name = self.body().get("name", "")
entry = load_config()["mcpServers"].get(name)
if not entry or not entry.get("url"):
return self.send({"error": f"Not a remote server: {name}"}, HTTPStatus.NOT_FOUND)
if self.path.endswith("logout"):
oauth.logout(name)
hub.reconnect(name)
return self.send({})
try:
settings = {k: secrets_store.resolve_value(v) for k, v in (entry.get("oauth") or {}).items()}
url = oauth.start(name, secrets_store.resolve_value(entry["url"]), self.redirect_uri(), hub.challenge(name), settings)
except (oauth.OAuthError, secrets_store.SecretError) as e:
return self.send({"error": str(e)}, HTTPStatus.BAD_GATEWAY)
return self.send({"url": url})
if self.path == "/api/import/parse":
try:
result = importer.parse(self.body().get("text", ""), self.server.server_address[1])
except importer.ImportError_ as e:
return self.send({"error": str(e)}, HTTPStatus.BAD_REQUEST)
result["existing"] = sorted(set(result["servers"]) & set(load_config()["mcpServers"]))
return self.send(result)
if self.path == "/api/secrets":
data = self.body()
try:
return self.send({"ref": secrets_store.store_keychain(data.get("name", ""), data.get("value", ""))})
except secrets_store.SecretError as e:
return self.send({"error": str(e)}, HTTPStatus.BAD_REQUEST)
if self.path.startswith("/api/inspect/"):
return self.inspect(self.path.removeprefix("/api/inspect/"))
self.send({"error": "not found"}, HTTPStatus.NOT_FOUND)
def redirect_uri(self):
return f"http://127.0.0.1:{self.server.server_address[1]}/oauth/callback"
def oauth_callback(self, query):
error = query.get("error_description", query.get("error", [""]))[0]
server = None
if not error:
try:
server = oauth.finish(query.get("state", [""])[0], query.get("code", [""])[0])
hub.reconnect(server)
except (oauth.OAuthError, secrets_store.SecretError) as e:
error = str(e)
language = load_settings()["language"]
if language not in CALLBACK_TEXT:
language = "de" if self.headers.get("Accept-Language", "").lower().startswith("de") else "en"
words = CALLBACK_TEXT[language]
title = words["fail"] if error else words["ok"]
text = error or words["connected"].format(server=server)
message = json.dumps({"type": "mcp-hub-oauth", "server": server, "error": error or None}).replace("<", "\\u003c")
page = f"""<!doctype html><html lang="{language}"><meta charset="utf-8"><title>{title} · MCP Hub</title>
<style>:root{{--bg:#f6f6f4;--ink:#1c1c1a;--muted:#6b6b66;--tone:{"#c0362c" if error else "#1f8a4c"}}}@media (prefers-color-scheme:dark){{:root{{--bg:#141413;--ink:#ecebe6;--muted:#9a998f;--tone:{"#ff7366" if error else "#4cc27f"}}}}}body{{margin:0;min-height:100vh;display:grid;place-items:center;font:14px/1.45 -apple-system,system-ui,sans-serif;background:var(--bg);color:var(--ink)}}main{{max-width:420px;padding:24px;text-align:center;display:grid;gap:14px}}h1{{font-size:16px;margin:0;color:var(--tone)}}p{{margin:0;color:var(--muted)}}a{{color:var(--ink);text-underline-offset:3px}}</style>
<main><h1>{title}</h1><p>{html.escape(text)}</p><a href="/">{words["back"]}</a></main>
<script>if (window.opener) {{ window.opener.postMessage({message}, location.origin); {"" if error else "setTimeout(() => window.close(), 1200);"} }}</script></html>"""
self.send(page.encode(), HTTPStatus.BAD_REQUEST if error else HTTPStatus.OK, "text/html; charset=utf-8")
def event_stream(self, profile=None):
self.send_response(HTTPStatus.OK)
self.send_header("Content-Type", "text/event-stream")
self.send_header("Cache-Control", "no-store")
self.end_headers()
session_id = self.headers.get("Mcp-Session-Id")
client = hub.clients.lookup(session_id, self.headers.get("User-Agent", ""))
listener = hub.subscribe(f"{client} · {profile}" if profile else client)
hub.clients.stream(session_id, 1)
try:
while True:
try:
message = listener.get(timeout=25)
self.wfile.write(f"event: message\ndata: {json.dumps(message)}\n\n".encode())
except queue.Empty:
self.wfile.write(b": ping\n\n")
self.wfile.flush()
except (BrokenPipeError, ConnectionResetError):
pass
finally:
hub.unsubscribe(listener)
hub.clients.stream(session_id, -1)
def mcp(self, profile=None, mode=None):
try:
message = self.body()
except json.JSONDecodeError:
return self.send({"jsonrpc": "2.0", "id": None, "error": {"code": -32700, "message": "Parse error"}},
HTTPStatus.BAD_REQUEST)
if not isinstance(message, dict) or "method" not in message or "id" not in message:
self.send_response(HTTPStatus.ACCEPTED)
self.send_header("Content-Length", "0")
self.end_headers()
return
reply = {"jsonrpc": "2.0", "id": message["id"]}
user_agent = self.headers.get("User-Agent", "")
headers = {}
if message["method"] != "initialize" and not hub.clients.valid(self.headers.get("Mcp-Session-Id")):
return self.send({"jsonrpc": "2.0", "id": message["id"],
"error": {"code": -32001, "message": "Unknown session, please initialize again"}},
HTTPStatus.NOT_FOUND)
if message["method"] == "initialize":
client_info = (message.get("params") or {}).get("clientInfo")
headers["Mcp-Session-Id"] = hub.clients.register(client_info, user_agent, profile, mode)
client = hub.clients.lookup(headers["Mcp-Session-Id"], user_agent)
else:
session_id = self.headers.get("Mcp-Session-Id")
hub.clients.touch(session_id, message["method"] in ("tools/call", "prompts/get", "resources/read"))
client = hub.clients.lookup(session_id, user_agent)
if profile:
client = f"{client} · {profile}"
try:
reply["result"] = hub.handle(message["method"], message.get("params"), client, profile, mode)
except hub_module.JsonRpcError as e:
reply["error"] = {"code": e.code, "message": str(e)}
except Exception as e:
reply["error"] = {"code": -32603, "message": f"Internal error: {e}"}
self.send(reply, headers=headers)
def delete(self):
if MCP_PATH.match(self.path):
hub.clients.end(self.headers.get("Mcp-Session-Id"))
self.send_response(HTTPStatus.NO_CONTENT)
self.end_headers()
return
self.send({"error": "not found"}, HTTPStatus.NOT_FOUND)
def inspect(self, action):
data = self.body()
name = data.get("name", "")
try:
if action == "connect":
entry = load_config()["mcpServers"].get(name)
if not entry:
return self.send({"error": f"Unknown server: {name}"}, HTTPStatus.NOT_FOUND)
return self.send(inspector.connect(name, entry, load_settings()["passEnvironment"]))
if action == "call":
return self.send({"result": inspector.call(name, data["method"], data.get("params"))})
if action == "disconnect":
inspector.disconnect(name)
return self.send({})
except sessions.RpcError as e:
return self.send({"error": str(e)}, HTTPStatus.BAD_GATEWAY)
except OSError as e:
return self.send({"error": f"Start failed: {e}"}, HTTPStatus.BAD_GATEWAY)
self.send({"error": "not found"}, HTTPStatus.NOT_FOUND)
def option(name, default=None):
if name in sys.argv[1:-1]:
return sys.argv[sys.argv.index(name) + 1]
return default
def bind(port, attempts=20):
for attempt in range(attempts):
try:
return Server(("127.0.0.1", port), Handler)
except OSError:
if attempt == attempts - 1:
raise
time.sleep(0.5)
def main():
if log := option("--log"):
Path(log).parent.mkdir(parents=True, exist_ok=True)
sys.stdout = sys.stderr = open(log, "a", buffering=1, encoding="utf-8")
port = int(option("--port", os.environ.get("PORT", 4747)))
STATE_DIR.mkdir(mode=0o700, parents=True, exist_ok=True)
server = bind(port)
atexit.register(inspector.disconnect_all)
atexit.register(hub.close)
sync_hub_in_background()
hub.supervise_in_background()
signal.signal(signal.SIGTERM, lambda *_: sys.exit(0))
print(f"MCP Hub v{VERSION} running at http://127.0.0.1:{port}", flush=True)
try:
server.serve_forever()
except KeyboardInterrupt:
sys.exit(0)
if __name__ == "__main__":
main()