-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathopeneta_mcp_services.py
More file actions
913 lines (838 loc) · 28.7 KB
/
Copy pathopeneta_mcp_services.py
File metadata and controls
913 lines (838 loc) · 28.7 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
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
#!/usr/bin/env python3
"""Manage local OpenETA MCP services for OpenETA perception and manipulation."""
from __future__ import annotations
import argparse
import asyncio
import json
import os
import signal
import subprocess
import sys
import time
import urllib.error
import urllib.request
from dataclasses import dataclass
from pathlib import Path
from typing import Any, Iterable
REPO_ROOT = Path(__file__).resolve().parents[1]
DEFAULT_STATE_DIR = Path("outputs/mcp_services")
DEFAULT_HOST = "127.0.0.1"
DEFAULT_SAM3_PORT = 8773
DEFAULT_ANYGRASP_PORT = 8774
DEFAULT_ANYPLACE_PORT = 8775
DEFAULT_CONTACT_GRASPNET_PORT = 8776
DEFAULT_MOLMOPOINT_PORT = 8777
DEFAULT_GRASPGENX_PORT = 8778
DEFAULT_UNIDEPTH_V2_PORT = 8779
DEFAULT_UNIDEPTH_V2_MODEL_ID = "lpiccinelli/unidepth-v2-vitl14"
DEFAULT_MOLMOPOINT_MODEL_ID = "allenai/MolmoPoint-8B"
DEFAULT_MOLMOPOINT_MODEL_REVISION = "188130f961c8e0888a34e11121a1423c461a01ba"
STOP_TIMEOUT_S = 5.0
START_TIMEOUT_S = 8.0
@dataclass(frozen=True)
class ServiceConfig:
name: str
python: str
host: str
port: int
state_dir: Path
command: list[str]
env: dict[str, str]
health_server_name: str | None = None
@property
def pid_file(self) -> Path:
return self.state_dir / f"{self.name}.pid"
@property
def log_file(self) -> Path:
return self.state_dir / f"{self.name}.log"
@property
def health_url(self) -> str:
return f"http://{self.host}:{self.port}/"
@property
def sse_url(self) -> str:
return f"http://{self.host}:{self.port}/sse"
@property
def expected_health_server(self) -> str:
return self.health_server_name or self.name
def build_parser() -> argparse.ArgumentParser:
parser = argparse.ArgumentParser(description="Manage OpenETA MCP services.")
parser.add_argument(
"command",
choices=("start", "stop", "restart", "status", "health", "smoke"),
)
parser.add_argument(
"target",
choices=(
"sam3",
"anygrasp",
"anyplace",
"contact_graspnet",
"molmopoint",
"graspgenx",
"unidepth_v2",
"all",
),
)
parser.add_argument("--host", default=DEFAULT_HOST)
parser.add_argument("--sam3-port", type=int, default=DEFAULT_SAM3_PORT)
parser.add_argument("--anygrasp-port", type=int, default=DEFAULT_ANYGRASP_PORT)
parser.add_argument("--anyplace-port", type=int, default=DEFAULT_ANYPLACE_PORT)
parser.add_argument(
"--contact-graspnet-port",
type=int,
default=DEFAULT_CONTACT_GRASPNET_PORT,
)
parser.add_argument("--molmopoint-port", type=int, default=DEFAULT_MOLMOPOINT_PORT)
parser.add_argument("--graspgenx-port", type=int, default=DEFAULT_GRASPGENX_PORT)
parser.add_argument("--unidepth-v2-port", type=int, default=DEFAULT_UNIDEPTH_V2_PORT)
parser.add_argument("--state-dir", type=Path, default=DEFAULT_STATE_DIR)
parser.add_argument("--sam3-python")
parser.add_argument("--anygrasp-python")
parser.add_argument("--anyplace-python")
parser.add_argument("--contact-graspnet-python")
parser.add_argument("--molmopoint-python")
parser.add_argument("--graspgenx-python")
parser.add_argument("--unidepth-v2-python")
parser.add_argument("--anygrasp-sdk-root")
parser.add_argument("--anygrasp-checkpoint-path")
parser.add_argument("--anyplace-root")
parser.add_argument("--anyplace-config-path")
parser.add_argument("--contact-graspnet-root")
parser.add_argument("--contact-graspnet-checkpoint-dir")
parser.add_argument("--sam3-hf-home")
parser.add_argument("--sam3-cache-dir")
parser.add_argument("--molmopoint-hf-home")
parser.add_argument("--molmopoint-model-id")
parser.add_argument("--molmopoint-model-revision")
parser.add_argument("--graspgenx-root")
parser.add_argument("--graspgenx-checkpoint-root")
parser.add_argument("--graspgenx-gripper-descriptions-root")
parser.add_argument("--unidepth-v2-model-id")
parser.add_argument("--unidepth-v2-device")
parser.add_argument("--unidepth-v2-resolution-level", type=int)
parser.add_argument("--dry-run", action="store_true")
parser.add_argument("--force", action="store_true")
parser.add_argument("--json", action="store_true", dest="json_output")
return parser
def main(argv: list[str] | None = None) -> int:
parser = build_parser()
args = parser.parse_args(argv)
try:
configs = _build_configs(args)
if args.command in {"start", "restart"}:
_validate_start_requirements(configs)
if args.command == "start":
results = {config.name: _start_service(config, dry_run=args.dry_run) for config in configs}
elif args.command == "stop":
results = {config.name: _stop_service(config, force=args.force) for config in configs}
elif args.command == "restart":
results = {}
for config in configs:
if args.dry_run:
results[config.name] = _restart_dry_run(config)
else:
stop_result = _stop_service(config, force=args.force, missing_ok=True)
start_result = _start_service(config, dry_run=False)
results[config.name] = {
"stop": stop_result,
"start": start_result,
"ok": bool(stop_result.get("ok")) and bool(start_result.get("ok")),
}
elif args.command == "status":
results = {config.name: _status_service(config) for config in configs}
elif args.command == "health":
results = {config.name: _health_service(config) for config in configs}
elif args.command == "smoke":
results = {config.name: _smoke_service(config) for config in configs}
else: # pragma: no cover - argparse prevents this.
parser.error(f"unknown command: {args.command}")
except ConfigError as exc:
print(str(exc), file=sys.stderr)
return 2
_print_results(results, json_output=args.json_output)
if args.command == "status":
return 0
return 0 if _results_ok(results) else 1
def _build_configs(args: argparse.Namespace) -> list[ServiceConfig]:
targets = (
(
"sam3",
"anygrasp",
"anyplace",
"contact_graspnet",
"molmopoint",
"graspgenx",
"unidepth_v2",
)
if args.target == "all"
else (args.target,)
)
return [_build_config(target, args) for target in targets]
def _build_config(name: str, args: argparse.Namespace) -> ServiceConfig:
state_dir = _resolve_state_dir(args.state_dir)
env = os.environ.copy()
if name == "sam3":
python = args.sam3_python or os.environ.get("OPENETA_SAM3_PYTHON") or sys.executable
if args.sam3_hf_home:
env["HF_HOME"] = args.sam3_hf_home
if args.sam3_cache_dir:
env["HF_HUB_CACHE"] = args.sam3_cache_dir
command = [
python,
str(REPO_ROOT / "tools" / "sam3_mcp_server.py"),
"--transport",
"sse",
"--host",
args.host,
"--port",
str(args.sam3_port),
]
return ServiceConfig(
name=name,
python=python,
host=args.host,
port=args.sam3_port,
state_dir=state_dir,
command=command,
env=env,
)
if name == "anygrasp":
python = (
args.anygrasp_python or os.environ.get("OPENETA_ANYGRASP_PYTHON") or sys.executable
)
sdk_root = args.anygrasp_sdk_root or os.environ.get("OPENETA_ANYGRASP_SDK_ROOT")
checkpoint_path = args.anygrasp_checkpoint_path or os.environ.get(
"OPENETA_ANYGRASP_CHECKPOINT_PATH"
)
command = [
python,
str(REPO_ROOT / "tools" / "anygrasp_mcp_server.py"),
"--transport",
"sse",
"--host",
args.host,
"--port",
str(args.anygrasp_port),
]
if sdk_root:
command.extend(["--sdk-root", sdk_root])
if checkpoint_path:
command.extend(["--checkpoint-path", checkpoint_path])
return ServiceConfig(
name=name,
python=python,
host=args.host,
port=args.anygrasp_port,
state_dir=state_dir,
command=command,
env=env,
)
if name == "anyplace":
python = (
args.anyplace_python
or os.environ.get("OPENETA_ANYPLACE_PYTHON")
or sys.executable
)
anyplace_root = args.anyplace_root or os.environ.get("OPENETA_ANYPLACE_ROOT")
config_path = args.anyplace_config_path or os.environ.get(
"OPENETA_ANYPLACE_CONFIG_PATH"
)
command = [
python,
str(REPO_ROOT / "tools" / "anyplace_mcp_server.py"),
"--transport",
"sse",
"--host",
args.host,
"--port",
str(args.anyplace_port),
]
if anyplace_root:
command.extend(["--anyplace-root", anyplace_root])
if config_path:
command.extend(["--config-path", config_path])
return ServiceConfig(
name=name,
python=python,
host=args.host,
port=args.anyplace_port,
state_dir=state_dir,
command=command,
env=env,
)
if name == "molmopoint":
python = (
args.molmopoint_python
or os.environ.get("OPENETA_MOLMOPOINT_PYTHON")
or sys.executable
)
hf_home = args.molmopoint_hf_home or os.environ.get("OPENETA_MOLMOPOINT_HF_HOME")
model_id = (
args.molmopoint_model_id
or os.environ.get("OPENETA_MOLMOPOINT_MODEL_ID")
or DEFAULT_MOLMOPOINT_MODEL_ID
)
model_revision = (
args.molmopoint_model_revision
or os.environ.get("OPENETA_MOLMOPOINT_MODEL_REVISION")
or DEFAULT_MOLMOPOINT_MODEL_REVISION
)
command = [
python,
str(REPO_ROOT / "tools" / "molmopoint_mcp_server.py"),
"--transport",
"sse",
"--host",
args.host,
"--port",
str(args.molmopoint_port),
"--model-id",
model_id,
"--model-revision",
model_revision,
]
if hf_home:
command.extend(["--hf-home", hf_home])
env["HF_HOME"] = hf_home
return ServiceConfig(
name=name,
python=python,
host=args.host,
port=args.molmopoint_port,
state_dir=state_dir,
command=command,
env=env,
)
if name == "graspgenx":
python = (
args.graspgenx_python
or os.environ.get("OPENETA_GRASPGENX_PYTHON")
or sys.executable
)
backend_root = args.graspgenx_root or os.environ.get(
"OPENETA_GRASPGENX_ROOT"
)
checkpoint_root = args.graspgenx_checkpoint_root or os.environ.get(
"OPENETA_GRASPGENX_CHECKPOINT_ROOT"
)
gripper_root = (
args.graspgenx_gripper_descriptions_root
or os.environ.get("OPENETA_GRASPGENX_GRIPPER_DESCRIPTIONS_ROOT")
)
command = [
python,
str(REPO_ROOT / "tools" / "graspgenx_mcp_server.py"),
"--transport",
"sse",
"--host",
args.host,
"--port",
str(args.graspgenx_port),
]
if backend_root:
command.extend(["--graspgenx-root", backend_root])
if checkpoint_root:
command.extend(["--checkpoint-root", checkpoint_root])
if gripper_root:
command.extend(["--gripper-descriptions-root", gripper_root])
return ServiceConfig(
name=name,
python=python,
host=args.host,
port=args.graspgenx_port,
state_dir=state_dir,
command=command,
env=env,
health_server_name="openeta-graspgenx",
)
if name == "unidepth_v2":
python = (
args.unidepth_v2_python
or os.environ.get("OPENETA_UNIDEPTH_V2_PYTHON")
or sys.executable
)
model_id = (
args.unidepth_v2_model_id
or os.environ.get("OPENETA_UNIDEPTH_V2_MODEL_ID")
or DEFAULT_UNIDEPTH_V2_MODEL_ID
)
device = (
args.unidepth_v2_device
or os.environ.get("OPENETA_UNIDEPTH_V2_DEVICE")
or "auto"
)
resolution_level = (
args.unidepth_v2_resolution_level
if args.unidepth_v2_resolution_level is not None
else int(os.environ.get("OPENETA_UNIDEPTH_V2_RESOLUTION_LEVEL", "4"))
)
command = [
python,
str(REPO_ROOT / "tools" / "unidepth_v2_mcp_server.py"),
"--transport",
"sse",
"--host",
args.host,
"--port",
str(args.unidepth_v2_port),
"--model-id",
model_id,
"--device",
device,
"--resolution-level",
str(resolution_level),
]
return ServiceConfig(
name=name,
python=python,
host=args.host,
port=args.unidepth_v2_port,
state_dir=state_dir,
command=command,
env=env,
health_server_name="unidepth-v2",
)
python = (
args.contact_graspnet_python
or os.environ.get("OPENETA_CONTACT_GRASPNET_PYTHON")
or sys.executable
)
backend_root = args.contact_graspnet_root or os.environ.get(
"OPENETA_CONTACT_GRASPNET_ROOT"
)
checkpoint_dir = args.contact_graspnet_checkpoint_dir or os.environ.get(
"OPENETA_CONTACT_GRASPNET_CHECKPOINT_DIR"
)
command = [
python,
str(REPO_ROOT / "tools" / "contact_graspnet_mcp_server.py"),
"--transport",
"sse",
"--host",
args.host,
"--port",
str(args.contact_graspnet_port),
]
if backend_root:
command.extend(["--contact-graspnet-root", backend_root])
if checkpoint_dir:
command.extend(["--checkpoint-dir", checkpoint_dir])
return ServiceConfig(
name=name,
python=python,
host=args.host,
port=args.contact_graspnet_port,
state_dir=state_dir,
command=command,
env=env,
)
def _validate_start_requirements(configs: Iterable[ServiceConfig]) -> None:
for config in configs:
if config.name == "anygrasp":
if "--sdk-root" not in config.command:
raise ConfigError(
"AnyGrasp sdk root is required: pass --anygrasp-sdk-root or set "
"OPENETA_ANYGRASP_SDK_ROOT."
)
if "--checkpoint-path" not in config.command:
raise ConfigError(
"AnyGrasp checkpoint path is required: pass --anygrasp-checkpoint-path or set "
"OPENETA_ANYGRASP_CHECKPOINT_PATH."
)
if config.name == "anyplace":
if "--anyplace-root" not in config.command:
raise ConfigError(
"AnyPlace root is required: pass --anyplace-root or set "
"OPENETA_ANYPLACE_ROOT."
)
if "--config-path" not in config.command:
raise ConfigError(
"AnyPlace config path is required: pass --anyplace-config-path or set "
"OPENETA_ANYPLACE_CONFIG_PATH."
)
if config.name == "contact_graspnet":
if "--contact-graspnet-root" not in config.command:
raise ConfigError(
"Contact-GraspNet root is required: pass --contact-graspnet-root "
"or set OPENETA_CONTACT_GRASPNET_ROOT."
)
if "--checkpoint-dir" not in config.command:
raise ConfigError(
"Contact-GraspNet checkpoint directory is required: pass "
"--contact-graspnet-checkpoint-dir or set "
"OPENETA_CONTACT_GRASPNET_CHECKPOINT_DIR."
)
if config.name == "graspgenx":
if "--graspgenx-root" not in config.command:
raise ConfigError(
"GraspGenX root is required: pass --graspgenx-root or set "
"OPENETA_GRASPGENX_ROOT."
)
if "--checkpoint-root" not in config.command:
raise ConfigError(
"GraspGenX checkpoint root is required: pass "
"--graspgenx-checkpoint-root or set "
"OPENETA_GRASPGENX_CHECKPOINT_ROOT."
)
if "--gripper-descriptions-root" not in config.command:
raise ConfigError(
"GraspGenX gripper descriptions root is required: pass "
"--graspgenx-gripper-descriptions-root or set "
"OPENETA_GRASPGENX_GRIPPER_DESCRIPTIONS_ROOT."
)
def _start_service(config: ServiceConfig, *, dry_run: bool) -> dict[str, Any]:
pid = _read_pid(config.pid_file)
if pid is not None and _pid_alive(pid):
if not _pid_matches_command(pid, config):
return {
"ok": False,
"service": config.name,
"action": "start",
"reason": "pid_mismatch",
"pid": pid,
"url": config.sse_url,
"log": str(config.log_file),
}
return {
"ok": False,
"service": config.name,
"action": "start",
"reason": "already_running",
"pid": pid,
"url": config.sse_url,
"log": str(config.log_file),
}
if dry_run:
return {
"ok": True,
"service": config.name,
"action": "start",
"dry_run": True,
"command": _shell_join(config.command),
"url": config.sse_url,
"log": str(config.log_file),
}
config.state_dir.mkdir(parents=True, exist_ok=True)
try:
with config.log_file.open("ab") as log:
process = subprocess.Popen(
config.command,
cwd=REPO_ROOT,
env=config.env,
stdout=log,
stderr=subprocess.STDOUT,
stdin=subprocess.DEVNULL,
start_new_session=True,
)
except OSError as exc:
return {
"ok": False,
"service": config.name,
"action": "start",
"reason": "launch_failed",
"error": str(exc),
"url": config.sse_url,
"log": str(config.log_file),
}
config.pid_file.write_text(f"{process.pid}\n", encoding="utf-8")
ready = _wait_for_service_ready(config, process)
if ready is not None:
return ready
return {
"ok": True,
"service": config.name,
"action": "start",
"pid": process.pid,
"url": config.sse_url,
"log": str(config.log_file),
}
def _restart_dry_run(config: ServiceConfig) -> dict[str, Any]:
return {
"ok": True,
"service": config.name,
"action": "restart",
"dry_run": True,
"stop": "would_stop_matching_service_if_running",
"command": _shell_join(config.command),
"url": config.sse_url,
"log": str(config.log_file),
}
def _stop_service(
config: ServiceConfig,
*,
force: bool,
missing_ok: bool = False,
) -> dict[str, Any]:
pid = _read_pid(config.pid_file)
if pid is None:
return {
"ok": missing_ok,
"service": config.name,
"action": "stop",
"reason": "pid_missing",
}
if not _pid_alive(pid):
_unlink_if_exists(config.pid_file)
return {
"ok": True,
"service": config.name,
"action": "stop",
"reason": "not_running",
"pid": pid,
}
if not _pid_matches_command(pid, config):
return {
"ok": False,
"service": config.name,
"action": "stop",
"reason": "pid_mismatch",
"pid": pid,
}
sig = signal.SIGKILL if force else signal.SIGTERM
try:
os.kill(pid, sig)
except ProcessLookupError:
_unlink_if_exists(config.pid_file)
return {
"ok": True,
"service": config.name,
"action": "stop",
"reason": "not_running",
"pid": pid,
}
except PermissionError:
return {
"ok": False,
"service": config.name,
"action": "stop",
"reason": "permission_denied",
"pid": pid,
}
if force:
_unlink_if_exists(config.pid_file)
return {
"ok": True,
"service": config.name,
"action": "stop",
"pid": pid,
"signal": "SIGKILL",
}
deadline = time.monotonic() + STOP_TIMEOUT_S
while time.monotonic() < deadline:
if not _pid_alive(pid):
_unlink_if_exists(config.pid_file)
return {
"ok": True,
"service": config.name,
"action": "stop",
"pid": pid,
"signal": "SIGTERM",
}
time.sleep(0.1)
return {
"ok": False,
"service": config.name,
"action": "stop",
"pid": pid,
"signal": "SIGTERM",
"reason": "still_running",
}
def _status_service(config: ServiceConfig) -> dict[str, Any]:
pid = _read_pid(config.pid_file)
running = bool(pid is not None and _pid_alive(pid) and _pid_matches_command(pid, config))
health_ok = _http_health_check(
config.health_url,
expected_server=config.expected_health_server,
)
return {
"ok": running and health_ok,
"service": config.name,
"running": running,
"pid": pid if running else None,
"health": "ok" if health_ok else "failed",
"url": config.sse_url,
"health_url": config.health_url,
"log": str(config.log_file),
}
def _health_service(config: ServiceConfig) -> dict[str, Any]:
health_ok = _http_health_check(
config.health_url,
expected_server=config.expected_health_server,
)
return {
"ok": health_ok,
"service": config.name,
"health": "ok" if health_ok else "failed",
"health_url": config.health_url,
}
def _smoke_service(config: ServiceConfig) -> dict[str, Any]:
try:
tools = _mcp_list_tools(config.sse_url)
except Exception as exc: # noqa: BLE001 - command output should show failure reason.
return {
"ok": False,
"service": config.name,
"smoke": "failed",
"url": config.sse_url,
"reason": str(exc),
}
expected_tools = {
"anyplace": "predict_placement",
"contact_graspnet": "predict_grasps",
"molmopoint": "point_image",
"graspgenx": "predict_grasps",
"unidepth_v2": "estimate_depth",
}
expected_tool = expected_tools.get(config.name)
if expected_tool is not None and expected_tool not in tools:
return {
"ok": False,
"service": config.name,
"smoke": "failed",
"url": config.sse_url,
"reason": f"missing_tool:{expected_tool}",
"tools": tools,
}
return {
"ok": True,
"service": config.name,
"smoke": "ok",
"url": config.sse_url,
"tools": tools,
}
def _mcp_list_tools(url: str) -> list[str]:
return asyncio.run(_mcp_list_tools_async(url))
async def _mcp_list_tools_async(url: str) -> list[str]:
from mcp import ClientSession
from mcp.client.sse import sse_client
async with sse_client(url) as streams:
async with ClientSession(streams[0], streams[1]) as session:
await session.initialize()
result = await session.list_tools()
return [tool.name for tool in result.tools]
def _wait_for_service_ready(
config: ServiceConfig,
process: subprocess.Popen[bytes],
) -> dict[str, Any] | None:
deadline = time.monotonic() + START_TIMEOUT_S
while time.monotonic() < deadline:
exit_code = process.poll()
if exit_code is not None:
_unlink_if_exists(config.pid_file)
return {
"ok": False,
"service": config.name,
"action": "start",
"reason": "exited_early",
"exit_code": exit_code,
"pid": process.pid,
"url": config.sse_url,
"log": str(config.log_file),
}
if _http_health_check(
config.health_url,
expected_server=config.expected_health_server,
):
return None
time.sleep(0.1)
return {
"ok": False,
"service": config.name,
"action": "start",
"reason": "health_timeout",
"pid": process.pid,
"url": config.sse_url,
"log": str(config.log_file),
}
def _http_health_check(url: str, expected_server: str | None = None) -> bool:
try:
with urllib.request.urlopen(url, timeout=2.0) as response:
if not 200 <= response.status < 300:
return False
if expected_server is None:
return True
payload = json.loads(response.read().decode("utf-8"))
return payload.get("ok") is True and payload.get("server") == expected_server
except (OSError, urllib.error.URLError, ValueError, json.JSONDecodeError):
return False
def _read_pid(path: Path) -> int | None:
try:
text = path.read_text(encoding="utf-8").strip()
except OSError:
return None
if not text:
return None
try:
pid = int(text)
except ValueError:
return None
if pid <= 0:
return None
return pid
def _pid_alive(pid: int) -> bool:
if pid <= 0:
return False
try:
os.kill(pid, 0)
return True
except ProcessLookupError:
return False
except PermissionError:
return True
def _pid_matches_command(pid: int, config: ServiceConfig) -> bool:
if pid <= 0:
return False
cmdline_path = Path("/proc") / str(pid) / "cmdline"
try:
raw = cmdline_path.read_bytes()
except OSError:
return False
cmdline = raw.replace(b"\x00", b" ").decode("utf-8", errors="replace")
return str(config.command[1]) in cmdline
def _resolve_state_dir(path: Path) -> Path:
if path.is_absolute():
return path
return REPO_ROOT / path
def _unlink_if_exists(path: Path) -> None:
try:
path.unlink()
except FileNotFoundError:
pass
def _print_results(results: dict[str, Any], *, json_output: bool) -> None:
if json_output:
print(json.dumps(results, indent=2, sort_keys=True))
return
for name, result in results.items():
if "command" in result:
print(f"{name}: dry-run")
print(f" command: {result['command']}")
print(f" url: {result['url']}")
continue
fields = [f"{name}:"]
if "action" in result:
fields.append(str(result["action"]))
fields.append("ok" if result.get("ok") else "failed")
print(" ".join(fields))
for key in ("pid", "running", "health", "smoke", "url", "health_url", "log", "reason"):
if key in result:
print(f" {key}: {result[key]}")
if "tools" in result:
print(f" tools: {', '.join(result['tools'])}")
def _results_ok(results: dict[str, Any]) -> bool:
for result in results.values():
if isinstance(result, dict) and "ok" in result:
if not result["ok"]:
return False
continue
if isinstance(result, dict):
nested = [value for value in result.values() if isinstance(value, dict)]
if nested and not all(value.get("ok") for value in nested):
return False
return True
def _shell_join(parts: Iterable[str]) -> str:
import shlex
return " ".join(shlex.quote(str(part)) for part in parts)
class ConfigError(Exception):
"""CLI configuration cannot satisfy a requested service action."""
if __name__ == "__main__":
raise SystemExit(main())