-
-
Notifications
You must be signed in to change notification settings - Fork 159
Expand file tree
/
Copy pathwrapper.py
More file actions
886 lines (761 loc) · 34.1 KB
/
wrapper.py
File metadata and controls
886 lines (761 loc) · 34.1 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
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
"""Agent wrapper - runs the real interactive CLI with auto-trigger on @mentions.
Usage:
python wrapper.py claude
python wrapper.py codex
python wrapper.py gemini
python wrapper.py kimi
python wrapper.py qwen
Cross-platform:
- Windows: injects keystrokes via Win32 WriteConsoleInput (wrapper_windows.py)
- Mac/Linux: injects keystrokes via tmux send-keys (wrapper_unix.py)
How it works:
1. Starts the agent CLI in an interactive terminal.
2. Watches the queue file in the background for @mentions from the chat room.
3. When triggered, injects "mcp read #channel - you were mentioned, take appropriate action".
4. The agent picks up the prompt as if the user typed it.
"""
import json
import os
import shutil
import sys
import threading
import time
from pathlib import Path
ROOT = Path(__file__).parent
SERVER_NAME = "agentchattr"
# ---------------------------------------------------------------------------
# Per-instance provider config
# ---------------------------------------------------------------------------
def _write_json_mcp_settings(config_file: Path, url: str, transport: str = "http",
*, token: str = "") -> Path:
"""Write/merge a settings-style JSON file with nested mcpServers config.
Preserves existing servers in the file — only updates the agentchattr entry.
Gemini CLI 0.32+ expects:
- "httpUrl" key (not "url") for streamable-http transport
- "url" key for SSE transport
- "trust": true to skip per-call approval prompts
"""
config_file.parent.mkdir(parents=True, exist_ok=True)
existing: dict = {}
if config_file.exists():
try:
existing = json.loads(config_file.read_text("utf-8"))
except Exception:
pass
servers = existing.get("mcpServers", {})
# Gemini CLI uses "httpUrl" for streamable-http, "url" for SSE
if transport in ("http", "streamable-http"):
entry: dict = {"type": "http", "httpUrl": url, "trust": True}
else:
entry = {"type": transport, "url": url, "trust": True}
if token:
entry["headers"] = {"Authorization": f"Bearer {token}"}
servers[SERVER_NAME] = entry
existing["mcpServers"] = servers
# Enable folder trust so ~/.gemini/trustedFolders.json is respected
security = existing.get("security", {})
folder_trust = security.get("folderTrust", {})
folder_trust["enabled"] = True
security["folderTrust"] = folder_trust
existing["security"] = security
config_file.write_text(json.dumps(existing, indent=2) + "\n", "utf-8")
return config_file
def _read_project_mcp_servers(project_dir: Path) -> dict:
"""Read existing MCP servers from the project's .mcp.json."""
mcp_file = project_dir / ".mcp.json"
if mcp_file.exists():
try:
data = json.loads(mcp_file.read_text("utf-8"))
servers = data.get("mcpServers", {})
# Remove agentchattr — we'll add our own authenticated version
servers.pop(SERVER_NAME, None)
return servers
except Exception:
pass
return {}
def _write_claude_mcp_config(
config_file: Path,
url: str,
*,
token: str = "",
project_servers: dict | None = None,
) -> Path:
"""Write a Claude Code --mcp-config file with bearer auth.
Includes all project MCP servers (unity-mcp etc.) so --strict-mcp-config
can be used without losing other servers."""
config_file.parent.mkdir(parents=True, exist_ok=True)
# Start with other project servers (e.g. unity-mcp)
servers = dict(project_servers or {})
# Add agentchattr with bearer token for direct server auth
entry: dict = {"type": "http", "url": url}
if token:
entry["headers"] = {"Authorization": f"Bearer {token}"}
servers[SERVER_NAME] = entry
payload = {"mcpServers": servers}
config_file.write_text(json.dumps(payload, indent=2) + "\n", "utf-8")
return config_file
# ---------------------------------------------------------------------------
# Built-in provider defaults (applied when agent config has no mcp_inject)
# ---------------------------------------------------------------------------
_BUILTIN_DEFAULTS: dict[str, dict] = {
"claude": {
"mcp_inject": "flag",
"mcp_flag": "--mcp-config",
"mcp_transport": "http",
"mcp_merge_project": True, # include unity-mcp etc.
},
"gemini": {
"mcp_inject": "env",
"mcp_env_var": "GEMINI_CLI_SYSTEM_SETTINGS_PATH",
"mcp_transport": "http", # streamable-http; SSE has blocking issues in Gemini 0.32.x
"mcp_merge_project": True,
},
"codex": {
"mcp_inject": "proxy_flag",
"mcp_proxy_flag_template": '-c mcp_servers.{server}.url="{url}"',
# mcp_merge_project disabled — Codex reads .mcp.json natively,
# and duplicate detection is name-based only (e.g. unityMCP vs unity-mcp)
},
"kimi": {
"mcp_inject": "flag",
"mcp_flag": "--mcp-config-file",
"mcp_transport": "http",
"mcp_merge_project": True,
},
"kilo": {
"mcp_inject": "env_content",
"mcp_env_var": "KILO_CONFIG_CONTENT",
"mcp_transport": "http",
},
}
_VALID_INJECT_MODES = {"settings_file", "env", "flag", "proxy_flag", "env_content"}
def _resolve_mcp_inject(agent: str, agent_cfg: dict) -> dict:
"""Resolve MCP injection config: explicit agent_cfg > built-in defaults > None."""
inject_mode = agent_cfg.get("mcp_inject")
if inject_mode:
return dict(agent_cfg)
if agent in _BUILTIN_DEFAULTS:
merged = dict(_BUILTIN_DEFAULTS[agent])
merged.update({k: v for k, v in agent_cfg.items() if k.startswith("mcp_")})
return merged
return {}
def _get_server_url(mcp_cfg: dict, transport: str) -> str:
"""Build the MCP server URL for the given transport."""
if transport == "sse":
port = mcp_cfg.get("sse_port", 8201)
return f"http://127.0.0.1:{port}/sse"
port = mcp_cfg.get("http_port", 8200)
return f"http://127.0.0.1:{port}/mcp"
def _apply_mcp_inject(
inject_cfg: dict,
instance_name: str,
data_dir: Path,
proxy_url: str | None,
*,
token: str = "",
mcp_cfg: dict | None = None,
project_dir: Path | None = None,
) -> tuple[list[str], dict[str, str], Path | None]:
"""Apply MCP config injection based on the resolved inject config.
Returns (extra_launch_args, inject_env, settings_path_or_None).
settings_path is stored so re-registration can rewrite it.
"""
mode = inject_cfg.get("mcp_inject")
if not mode:
return [], {}, None
launch_args: list[str] = []
inject_env: dict[str, str] = {}
settings_path: Path | None = None
config_dir = data_dir / "provider-config"
transport = inject_cfg.get("mcp_transport", "http")
server_url = _get_server_url(mcp_cfg or {}, transport)
if mode == "settings_file":
# Write a settings JSON file at a user-specified path (e.g. .qwen/settings.json)
raw_path = inject_cfg.get("mcp_settings_path", "")
if not raw_path:
raise ValueError(f"mcp_inject = 'settings_file' requires mcp_settings_path")
target = Path(raw_path)
if not target.is_absolute():
base = Path(project_dir) if project_dir else Path.cwd()
target = base / target
settings_path = _write_json_mcp_settings(target, server_url,
transport=transport, token=token)
# Optionally set an env var pointing to the settings file
env_var = inject_cfg.get("mcp_env_var")
if env_var:
inject_env[env_var] = str(settings_path)
elif mode == "env":
# Write a settings file in provider-config dir, expose via env var
env_var = inject_cfg.get("mcp_env_var")
if not env_var:
raise ValueError(f"mcp_inject = 'env' requires mcp_env_var")
settings_path = _write_json_mcp_settings(
config_dir / f"{instance_name}-settings.json",
server_url, transport=transport, token=token,
)
# Merge project .mcp.json servers into the settings file
merge_project = inject_cfg.get("mcp_merge_project", False)
if merge_project and project_dir and settings_path:
project_servers = _read_project_mcp_servers(project_dir)
if project_servers:
try:
data = json.loads(settings_path.read_text("utf-8"))
servers = data.get("mcpServers", {})
for name, cfg in project_servers.items():
if name not in servers:
# Normalize for Gemini: url → httpUrl for HTTP transport
entry = dict(cfg)
srv_type = entry.get("type", "http")
if srv_type in ("http", "streamable-http") and "url" in entry and "httpUrl" not in entry:
entry["httpUrl"] = entry.pop("url")
entry.setdefault("trust", True)
servers[name] = entry
data["mcpServers"] = servers
settings_path.write_text(json.dumps(data, indent=2) + "\n", "utf-8")
except Exception:
pass
inject_env[env_var] = str(settings_path)
elif mode == "flag":
# Write a config file, pass it as a CLI flag
flag = inject_cfg.get("mcp_flag", "--mcp-config")
merge_project = inject_cfg.get("mcp_merge_project", False)
project_servers = _read_project_mcp_servers(project_dir) if (merge_project and project_dir) else {}
settings_path = _write_claude_mcp_config(
config_dir / f"{instance_name}-mcp.json",
server_url, token=token, project_servers=project_servers,
)
launch_args = [flag, str(settings_path)]
elif mode == "env_content":
# Build JSON config content and set it as an env var directly (no file written).
# Used by Kilo CLI which reads KILO_CONFIG_CONTENT at startup.
env_var = inject_cfg.get("mcp_env_var")
if not env_var:
raise ValueError("mcp_inject = 'env_content' requires mcp_env_var")
entry: dict = {"type": "remote", "url": server_url, "enabled": True}
if token:
entry["headers"] = {"Authorization": f"Bearer {token}"}
payload = {"mcp": {SERVER_NAME: entry}}
inject_env[env_var] = json.dumps(payload)
elif mode == "proxy_flag":
# Pass the proxy URL as CLI flags (e.g. codex -c ...)
template = inject_cfg.get("mcp_proxy_flag_template",
'-c mcp_servers.{server}.url="{url}"')
expanded = template.format(server=SERVER_NAME, url=proxy_url or "")
launch_args = expanded.split()
return launch_args, inject_env, settings_path
def _ensure_gemini_folder_trusted(project_dir: Path) -> None:
"""Add project_dir as TRUST_FOLDER in ~/.gemini/trustedFolders.json.
Gemini CLI blocks ALL MCPs (including system-settings ones) for untrusted
folders. A more-specific TRUST_FOLDER entry overrides any parent-level
DO_NOT_TRUST rule, so we always write the exact cwd we're launching in.
Respects GEMINI_CLI_TRUSTED_FOLDERS_PATH env override if set.
"""
trusted_path_env = os.environ.get("GEMINI_CLI_TRUSTED_FOLDERS_PATH", "")
if trusted_path_env:
trusted_file = Path(trusted_path_env)
else:
trusted_file = Path.home() / ".gemini" / "trustedFolders.json"
try:
data: dict = {}
if trusted_file.exists():
try:
data = json.loads(trusted_file.read_text("utf-8"))
except Exception:
data = {}
folder_key = str(project_dir)
if data.get(folder_key) == "TRUST_FOLDER":
return # already trusted — nothing to do
data[folder_key] = "TRUST_FOLDER"
trusted_file.parent.mkdir(parents=True, exist_ok=True)
trusted_file.write_text(json.dumps(data, indent=2) + "\n", "utf-8")
print(f" Trusted folder for Gemini MCPs: {folder_key}")
except Exception as exc:
print(f" Warning: could not update Gemini trusted folders: {exc}")
def _build_provider_launch(
agent: str,
agent_cfg: dict,
instance_name: str,
data_dir: Path,
proxy_url: str | None,
extra_args: list[str],
env: dict[str, str],
*,
token: str = "",
mcp_cfg: dict | None = None,
project_dir: Path | None = None,
) -> tuple[list[str], dict[str, str], dict[str, str], Path | None]:
"""Return provider-specific launch args/env/inject_env/settings_path.
inject_env: env vars that must propagate INTO the agent process. On
Mac/Linux these are prefixed onto the tmux command via ``env VAR=val``
because subprocess.run(env=...) only affects the tmux client binary.
On Windows they are simply merged into the Popen env dict.
"""
inject_cfg = _resolve_mcp_inject(agent, agent_cfg)
mcp_args, inject_env, settings_path = _apply_mcp_inject(
inject_cfg, instance_name, data_dir, proxy_url,
token=token, mcp_cfg=mcp_cfg, project_dir=project_dir,
)
launch_args = [*mcp_args, *extra_args]
launch_env = dict(env)
return launch_args, launch_env, inject_env, settings_path
def _register_instance(server_port: int, base: str, label: str | None = None) -> dict:
import urllib.request
reg_body = json.dumps({"base": base, "label": label}).encode()
reg_req = urllib.request.Request(
f"http://127.0.0.1:{server_port}/api/register",
method="POST",
data=reg_body,
headers={"Content-Type": "application/json"},
)
with urllib.request.urlopen(reg_req, timeout=5) as reg_resp:
return json.loads(reg_resp.read())
def _auth_headers(token: str, *, include_json: bool = False) -> dict[str, str]:
headers = {"Authorization": f"Bearer {token}"}
if include_json:
headers["Content-Type"] = "application/json"
return headers
# ---------------------------------------------------------------------------
# Queue watcher
# ---------------------------------------------------------------------------
def _notify_recovery(data_dir: Path, agent_name: str):
"""Write a flag file that the server picks up and broadcasts as a system message."""
try:
flag = data_dir / f"{agent_name}_recovered"
flag.write_text(agent_name, "utf-8")
except Exception:
pass
_IDENTITY_HINT = (
" (If this is a multi-instance session, reclaim your previous identity from "
"your context window, NOT from the chat history before responding. If you "
"didn't have one, tell the user to give you a name by clicking your status "
"pill at the top.)"
)
def _fetch_role(server_port: int, agent_name: str) -> str:
"""Fetch this agent's role from the server status endpoint."""
try:
import urllib.request
req = urllib.request.Request(f"http://127.0.0.1:{server_port}/api/roles")
with urllib.request.urlopen(req, timeout=3) as resp:
roles = json.loads(resp.read())
return roles.get(agent_name, "")
except Exception:
return ""
def _fetch_active_rules(server_port: int, token: str = "") -> dict | None:
"""Fetch active rules from the server."""
try:
import urllib.request
headers = {"Authorization": f"Bearer {token}"} if token else {}
req = urllib.request.Request(f"http://127.0.0.1:{server_port}/api/rules/active", headers=headers)
with urllib.request.urlopen(req, timeout=3) as resp:
return json.loads(resp.read())
except Exception:
return None
def _report_rule_sync(server_port: int, agent_name: str, epoch: int, token: str = ""):
"""Report that this agent has seen rules at the given epoch."""
try:
import urllib.request
body = json.dumps({"epoch": epoch}).encode()
headers = {"Content-Type": "application/json"}
if token:
headers["Authorization"] = f"Bearer {token}"
req = urllib.request.Request(
f"http://127.0.0.1:{server_port}/api/rules/agent_sync/{agent_name}",
method="POST",
data=body,
headers=headers,
)
urllib.request.urlopen(req, timeout=3)
except Exception:
pass
def _queue_watcher(get_identity_fn, inject_fn, *, is_multi_instance: bool = False, trigger_flag=None,
server_port: int = 8300, agent_name: str = "", get_token_fn=None,
refresh_interval: int = 10):
"""Poll queue file and inject an MCP read task when triggered."""
first_mention = True
last_rules_epoch = 0 # 0 = unknown/cold start — will inject on first trigger
trigger_count = 0
while True:
try:
_, queue_file = get_identity_fn()
if queue_file.exists() and queue_file.stat().st_size > 0:
with open(queue_file, "r", encoding="utf-8") as f:
lines = f.readlines()
queue_file.write_text("", "utf-8")
has_trigger = False
channel = "general"
for line in lines:
line = line.strip()
if not line:
continue
try:
data = json.loads(line)
except json.JSONDecodeError:
continue
has_trigger = True
if isinstance(data, dict) and "channel" in data:
channel = data["channel"]
if has_trigger:
# Signal activity BEFORE injecting — covers the thinking phase
if trigger_flag is not None:
trigger_flag[0] = True
time.sleep(0.5)
# Check if this is a job/activity-scoped trigger
job_id = None
custom_prompt = ""
for line in lines:
line = line.strip()
if not line:
continue
try:
data = json.loads(line)
if isinstance(data, dict) and "job_id" in data:
job_id = data["job_id"]
if isinstance(data, dict):
raw_prompt = data.get("prompt", "")
if isinstance(raw_prompt, str) and raw_prompt.strip():
custom_prompt = raw_prompt.strip()
except json.JSONDecodeError:
pass
if custom_prompt:
prompt = custom_prompt
elif job_id:
prompt = f"mcp read job_id={job_id} - you were mentioned in a job thread, take appropriate action"
else:
prompt = f"mcp read #{channel} - you were mentioned, take appropriate action"
# Use current identity (may have changed via rename)
current_name, _ = get_identity_fn()
# Append role if set — check both current name and base name
role = _fetch_role(server_port, current_name)
if not role and current_name != agent_name:
role = _fetch_role(server_port, agent_name)
if role:
prompt += f"\n\nROLE: {role}"
# Smart rules injection: first trigger, epoch change, or periodic refresh
_token = get_token_fn() if get_token_fn else ""
rules_data = _fetch_active_rules(server_port, _token)
trigger_count += 1
if rules_data:
# Use server-side refresh_interval (live from settings UI)
ri = rules_data.get("refresh_interval", refresh_interval)
need_inject = (
last_rules_epoch == 0
or rules_data["epoch"] != last_rules_epoch
or (ri > 0 and trigger_count % ri == 0)
)
if need_inject:
if rules_data["rules"]:
rules_text = "; ".join(rules_data["rules"])
prompt += f"\n\nRULES:\n{rules_text}"
last_rules_epoch = rules_data["epoch"]
_report_rule_sync(server_port, current_name, rules_data["epoch"], _token)
if first_mention and is_multi_instance:
prompt += _IDENTITY_HINT
first_mention = False
# Flatten to single line — multi-line text triggers paste
# detection in CLIs (Claude Code shows "[Pasted text +N]")
# which can break injection of long session prompts
inject_fn(prompt.replace("\n", " "))
except Exception:
pass
time.sleep(1)
# ---------------------------------------------------------------------------
# Main
# ---------------------------------------------------------------------------
def main():
import argparse
import urllib.error
import urllib.request
from config_loader import load_config
config = load_config(ROOT)
agent_names = list(config.get("agents", {}).keys())
parser = argparse.ArgumentParser(description="Agent wrapper with chat auto-trigger")
parser.add_argument("agent", choices=agent_names, help=f"Agent to wrap ({', '.join(agent_names)})")
parser.add_argument("--no-restart", action="store_true", help="Do not restart on exit")
parser.add_argument("--label", type=str, default=None, help="Custom display label")
args, extra = parser.parse_known_args()
agent = args.agent
agent_cfg = config.get("agents", {}).get(agent, {})
cwd = agent_cfg.get("cwd", ".")
command = agent_cfg.get("command", agent)
data_dir = ROOT / config.get("server", {}).get("data_dir", "./data")
data_dir.mkdir(parents=True, exist_ok=True)
server_port = config.get("server", {}).get("port", 8300)
mcp_cfg = config.get("mcp", {})
try:
registration = _register_instance(server_port, agent, args.label)
except Exception as exc:
print(f" Registration failed ({exc}).")
print(" Wrapper cannot continue without a registered identity.")
sys.exit(1)
assigned_name = registration["name"]
assigned_token = registration["token"]
print(f" Registered as: {assigned_name} (slot {registration.get('slot', '?')})")
proxy = None
proxy_url = None
# Resolve MCP injection mode to determine if a proxy is needed.
# Direct-connect modes (settings_file, env, flag) don't need a proxy.
# proxy_flag mode needs a proxy. No mcp_inject = proxy fallback.
inject_cfg = _resolve_mcp_inject(agent, agent_cfg)
inject_mode = inject_cfg.get("mcp_inject", "")
if inject_mode and inject_mode not in _VALID_INJECT_MODES:
print(f" Error: unknown mcp_inject mode '{inject_mode}' for agent '{agent}'.")
print(f" Valid modes: {', '.join(sorted(_VALID_INJECT_MODES))}")
sys.exit(1)
needs_proxy = inject_mode in ("proxy_flag", "") or not inject_mode
if needs_proxy:
from mcp_proxy import McpIdentityProxy
transport = inject_cfg.get("mcp_transport", "http")
if transport == "sse":
upstream_base = f"http://127.0.0.1:{mcp_cfg.get('sse_port', 8201)}"
proxy_path = "/sse"
else:
upstream_base = f"http://127.0.0.1:{mcp_cfg.get('http_port', 8200)}"
proxy_path = "/mcp"
proxy = McpIdentityProxy(
upstream_base=upstream_base,
upstream_path=proxy_path,
agent_name=assigned_name,
instance_token=assigned_token,
)
if proxy.start() is False:
print(" Failed to start MCP proxy.")
sys.exit(1)
proxy_url = f"{proxy.url}{proxy_path}"
_identity_lock = threading.Lock()
_identity = {
"name": assigned_name,
"queue": data_dir / f"{assigned_name}_queue.jsonl",
"token": assigned_token,
}
def get_identity():
with _identity_lock:
return _identity["name"], _identity["queue"]
def get_token():
with _identity_lock:
return _identity["token"]
# Rewrite MCP config when token/name changes (e.g. after 409 re-register).
# Most CLIs won't re-read mid-session, but the file is correct for next restart.
def _rewrite_mcp_config(instance_name: str, new_token: str):
if not inject_mode or needs_proxy:
return # proxy-based agents don't have config files to rewrite
try:
_apply_mcp_inject(
inject_cfg, instance_name, data_dir, proxy_url,
token=new_token, mcp_cfg=mcp_cfg,
project_dir=(ROOT / cwd).resolve(),
)
except Exception:
pass
def set_runtime_identity(new_name: str | None = None, new_token: str | None = None):
with _identity_lock:
old_name = _identity["name"]
old_token = _identity["token"]
changed = False
if new_name and new_name != old_name:
_identity["name"] = new_name
_identity["queue"] = data_dir / f"{new_name}_queue.jsonl"
changed = True
if new_token and new_token != old_token:
_identity["token"] = new_token
changed = True
current_name = _identity["name"]
current_token = _identity["token"]
if changed and proxy is not None:
proxy.agent_name = current_name
proxy.token = current_token
if changed:
if new_name and new_name != old_name:
print(f" Identity updated: {old_name} -> {new_name}")
if new_token and new_token != old_token:
print(f" Session refreshed for @{current_name}")
_rewrite_mcp_config(current_name, current_token)
return changed
queue_file = _identity["queue"]
if queue_file.exists():
queue_file.write_text("", "utf-8")
strip_vars = {"CLAUDECODE"} | set(agent_cfg.get("strip_env", []))
env = {k: v for k, v in os.environ.items() if k not in strip_vars}
resolved = shutil.which(command)
if not resolved:
print(f" Error: '{command}' not found on PATH.")
print(" Install it first, then try again.")
sys.exit(1)
command = resolved
project_dir = (ROOT / cwd).resolve()
# Gemini: ensure the project directory is trusted so MCPs are allowed.
# Gemini blocks ALL MCPs for untrusted folders — even system-settings ones.
if agent == "gemini" or inject_cfg.get("mcp_inject") == "env":
_ensure_gemini_folder_trusted(project_dir)
launch_args, env, inject_env, mcp_settings_path = _build_provider_launch(
agent=agent,
agent_cfg=agent_cfg,
instance_name=assigned_name,
data_dir=data_dir,
proxy_url=proxy_url,
extra_args=extra,
env=env,
token=assigned_token,
mcp_cfg=mcp_cfg,
project_dir=project_dir,
)
print(f" === {assigned_name.capitalize()} Chat Wrapper ===")
if not needs_proxy:
print(f" MCP: direct connect ({inject_mode}) with bearer auth")
if mcp_settings_path:
print(f" Config: {mcp_settings_path}")
elif proxy_url:
print(f" Local MCP proxy: {proxy_url}")
print(f" @{assigned_name} mentions auto-inject MCP reads")
print(f" Starting {command} in {cwd}...\n")
def _heartbeat():
while True:
current_name, _ = get_identity()
current_token = get_token()
url = f"http://127.0.0.1:{server_port}/api/heartbeat/{current_name}"
try:
req = urllib.request.Request(
url,
method="POST",
data=b"",
headers=_auth_headers(current_token),
)
with urllib.request.urlopen(req, timeout=5) as resp:
resp_data = json.loads(resp.read())
server_name = resp_data.get("name", current_name)
if server_name != current_name:
set_runtime_identity(server_name)
except urllib.error.HTTPError as exc:
if exc.code == 409:
try:
replacement = _register_instance(server_port, agent, args.label)
set_runtime_identity(replacement["name"], replacement["token"])
_notify_recovery(data_dir, replacement["name"])
except Exception:
pass
time.sleep(5)
continue
except Exception:
time.sleep(5)
continue
time.sleep(5)
threading.Thread(target=_heartbeat, daemon=True).start()
_watcher_inject_fn = None
_watcher_thread = None
_is_multi_instance = registration.get("slot", 1) > 1
_trigger_flag = [False] # shared: queue watcher sets True, activity checker reads
_refresh_interval = 10 # default; overridden per-trigger by server settings
def start_watcher(inject_fn):
nonlocal _watcher_inject_fn, _watcher_thread
_watcher_inject_fn = inject_fn
_watcher_thread = threading.Thread(
target=_queue_watcher,
args=(get_identity, inject_fn),
kwargs={"is_multi_instance": _is_multi_instance, "trigger_flag": _trigger_flag,
"server_port": server_port, "agent_name": assigned_name,
"get_token_fn": get_token, "refresh_interval": _refresh_interval},
daemon=True,
)
_watcher_thread.start()
def _watcher_monitor():
nonlocal _watcher_thread
while True:
time.sleep(5)
if _watcher_thread and not _watcher_thread.is_alive() and _watcher_inject_fn:
_watcher_thread = threading.Thread(
target=_queue_watcher,
args=(get_identity, _watcher_inject_fn),
kwargs={"is_multi_instance": _is_multi_instance, "trigger_flag": _trigger_flag,
"server_port": server_port, "agent_name": assigned_name,
"get_token_fn": get_token, "refresh_interval": _refresh_interval},
daemon=True,
)
_watcher_thread.start()
current_name, _ = get_identity()
_notify_recovery(data_dir, current_name)
threading.Thread(target=_watcher_monitor, daemon=True).start()
_activity_checker = None
def _set_activity_checker(checker):
nonlocal _activity_checker
_activity_checker = checker
def _activity_monitor():
last_active = None
last_report_time = 0
REPORT_INTERVAL = 3 # re-send state every 3s while active (keeps server lease fresh)
while True:
time.sleep(1)
if not _activity_checker:
continue
try:
active = _activity_checker()
now = time.time()
# Send on state change, periodically while active (refresh lease),
# or periodically while idle (keep presence alive)
IDLE_REPORT_INTERVAL = 8 # keep-alive while idle
should_send = (
active != last_active
or (active and now - last_report_time >= REPORT_INTERVAL)
or (not active and now - last_report_time >= IDLE_REPORT_INTERVAL)
)
if should_send:
current_name, _ = get_identity()
current_token = get_token()
url = f"http://127.0.0.1:{server_port}/api/heartbeat/{current_name}"
body = json.dumps({"active": active}).encode()
req = urllib.request.Request(
url,
method="POST",
data=body,
headers=_auth_headers(current_token, include_json=True),
)
resp = urllib.request.urlopen(req, timeout=5)
resp_code = resp.getcode()
last_active = active
last_report_time = now
except Exception:
pass
threading.Thread(target=_activity_monitor, daemon=True).start()
_agent_pid = [None]
if sys.platform == "win32":
from wrapper_windows import get_activity_checker, run_agent
_set_activity_checker(get_activity_checker(_agent_pid, agent_name=assigned_name, trigger_flag=_trigger_flag))
else:
from wrapper_unix import get_activity_checker, run_agent
unix_session_name = f"agentchattr-{assigned_name}"
_set_activity_checker(get_activity_checker(unix_session_name, trigger_flag=_trigger_flag))
run_kwargs = dict(
command=command,
extra_args=launch_args,
cwd=cwd,
env=env,
queue_file=queue_file,
agent=agent,
no_restart=args.no_restart,
start_watcher=start_watcher,
strip_env=list(strip_vars),
pid_holder=_agent_pid,
inject_env=inject_env,
inject_delay=agent_cfg.get("inject_delay", 0.3),
)
if sys.platform != "win32":
run_kwargs["session_name"] = unix_session_name
try:
run_agent(**run_kwargs)
finally:
try:
current_name, _ = get_identity()
current_token = get_token()
dereg_req = urllib.request.Request(
f"http://127.0.0.1:{server_port}/api/deregister/{current_name}",
method="POST",
data=b"",
headers=_auth_headers(current_token),
)
urllib.request.urlopen(dereg_req, timeout=5)
print(f" Deregistered {current_name}")
except Exception:
pass
if proxy is not None:
proxy.stop()
print(" Wrapper stopped.")
if __name__ == "__main__":
main()