forked from CrackingShells/Hatch
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path__main__.py
More file actions
1064 lines (949 loc) · 36.1 KB
/
__main__.py
File metadata and controls
1064 lines (949 loc) · 36.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
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
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""Entry point for Hatch CLI.
This module provides the main entry point for the Hatch package manager CLI.
It handles argument parsing and routes commands to appropriate handler modules.
Architecture:
This module implements the routing layer of the CLI architecture:
1. Parses command-line arguments using argparse
2. Initializes shared managers (HatchEnvironmentManager, MCPHostConfigurationManager)
3. Attaches managers to the args namespace for handler access
4. Routes commands to appropriate handler modules
Command Structure:
hatch create <name> - Create package template (cli_system)
hatch validate <path> - Validate package (cli_system)
hatch env <subcommand> - Environment management (cli_env)
hatch package <subcommand> - Package management (cli_package)
hatch mcp <subcommand> - MCP host configuration (cli_mcp)
Entry Points:
- python -m hatch.cli: Module execution via __main__.py
- hatch: Console script defined in pyproject.toml
Handler Signature:
All handlers follow: (args: Namespace) -> int
- args.env_manager: HatchEnvironmentManager instance
- args.mcp_manager: MCPHostConfigurationManager instance
- Returns: Exit code (0 for success, non-zero for errors)
Example:
$ hatch --version
$ hatch env list
$ hatch mcp configure claude-desktop my-server --command python
"""
import argparse
import logging
import sys
from pathlib import Path
from hatch.cli.cli_utils import get_hatch_version, Color, _colors_enabled
class HatchArgumentParser(argparse.ArgumentParser):
"""Custom ArgumentParser with formatted error messages.
Overrides the error() method to format argparse errors with
[ERROR] prefix and bright red color (when colors enabled).
Reference: R13 §4.2.1 (13-error_message_formatting_v0.md)
Output format:
[ERROR] <message>
Example:
>>> parser = HatchArgumentParser(description="Test CLI")
>>> parser.parse_args(['--invalid'])
[ERROR] unrecognized arguments: --invalid
"""
def error(self, message: str) -> None:
"""Override to format errors with [ERROR] prefix and color.
Args:
message: Error message from argparse
Note:
Preserves exit code 2 (argparse convention).
"""
if _colors_enabled():
self.exit(2, f"{Color.RED.value}[ERROR]{Color.RESET.value} {message}\n")
else:
self.exit(2, f"[ERROR] {message}\n")
def _setup_create_command(subparsers):
"""Set up 'hatch create' command parser."""
create_parser = subparsers.add_parser(
"create", help="Create a new package template"
)
create_parser.add_argument("name", help="Package name")
create_parser.add_argument(
"--dir", "-d", default=".", help="Target directory (default: current directory)"
)
create_parser.add_argument(
"--description", "-D", default="", help="Package description"
)
create_parser.add_argument(
"--dry-run", action="store_true", help="Preview changes without execution"
)
def _setup_validate_command(subparsers):
"""Set up 'hatch validate' command parser."""
validate_parser = subparsers.add_parser("validate", help="Validate a package")
validate_parser.add_argument("package_dir", help="Path to package directory")
def _setup_env_commands(subparsers):
"""Set up 'hatch env' command parsers."""
env_subparsers = subparsers.add_parser(
"env", help="Environment management commands"
).add_subparsers(dest="env_command", help="Environment command to execute")
# Create environment command
env_create_parser = env_subparsers.add_parser(
"create", help="Create a new environment"
)
env_create_parser.add_argument("name", help="Environment name")
env_create_parser.add_argument(
"--description", "-D", default="", help="Environment description"
)
env_create_parser.add_argument(
"--python-version", help="Python version for the environment (e.g., 3.11, 3.12)"
)
env_create_parser.add_argument(
"--no-python",
action="store_true",
help="Don't create a Python environment using conda/mamba",
)
env_create_parser.add_argument(
"--no-hatch-mcp-server",
action="store_true",
help="Don't install hatch_mcp_server wrapper in the new environment",
)
env_create_parser.add_argument(
"--hatch_mcp_server_tag",
help="Git tag/branch reference for hatch_mcp_server wrapper installation (e.g., 'dev', 'v0.1.0')",
)
env_create_parser.add_argument(
"--dry-run", action="store_true", help="Preview changes without execution"
)
# Remove environment command
env_remove_parser = env_subparsers.add_parser(
"remove", help="Remove an environment"
)
env_remove_parser.add_argument("name", help="Environment name")
env_remove_parser.add_argument(
"--dry-run", action="store_true", help="Preview changes without execution"
)
env_remove_parser.add_argument(
"--auto-approve", action="store_true", help="Skip confirmation prompt"
)
# List environments command - now with subcommands per R10
env_list_parser = env_subparsers.add_parser(
"list", help="List environments, hosts, or servers"
)
env_list_subparsers = env_list_parser.add_subparsers(
dest="list_command", help="List command to execute"
)
# Default list behavior (no subcommand) - handled by checking list_command is None
env_list_parser.add_argument(
"--pattern",
help="Filter environments by name using regex pattern",
)
env_list_parser.add_argument(
"--json", action="store_true", help="Output in JSON format"
)
# env list hosts subcommand per R10 §3.3
env_list_hosts_parser = env_list_subparsers.add_parser(
"hosts", help="List environment/host/server deployments"
)
env_list_hosts_parser.add_argument(
"--env",
"-e",
help="Filter by environment name using regex pattern",
)
env_list_hosts_parser.add_argument(
"--server",
help="Filter by server name using regex pattern",
)
env_list_hosts_parser.add_argument(
"--json", action="store_true", help="Output in JSON format"
)
# env list servers subcommand per R10 §3.4
env_list_servers_parser = env_list_subparsers.add_parser(
"servers", help="List environment/server/host deployments"
)
env_list_servers_parser.add_argument(
"--env",
"-e",
help="Filter by environment name using regex pattern",
)
env_list_servers_parser.add_argument(
"--host",
help="Filter by host name using regex pattern (use '-' for undeployed)",
)
env_list_servers_parser.add_argument(
"--json", action="store_true", help="Output in JSON format"
)
# Set current environment command
env_use_parser = env_subparsers.add_parser(
"use", help="Set the current environment"
)
env_use_parser.add_argument("name", help="Environment name")
env_use_parser.add_argument(
"--dry-run", action="store_true", help="Preview changes without execution"
)
# Show current environment command
env_subparsers.add_parser("current", help="Show the current environment")
# Show environment details command
env_show_parser = env_subparsers.add_parser(
"show", help="Show detailed environment configuration"
)
env_show_parser.add_argument("name", help="Environment name to show")
# Python environment management commands
env_python_subparsers = env_subparsers.add_parser(
"python", help="Manage Python environments"
).add_subparsers(
dest="python_command", help="Python environment command to execute"
)
# Initialize Python environment
python_init_parser = env_python_subparsers.add_parser(
"init", help="Initialize Python environment"
)
python_init_parser.add_argument(
"--hatch_env",
default=None,
help="Hatch environment name in which the Python environment is located (default: current environment)",
)
python_init_parser.add_argument(
"--python-version", help="Python version (e.g., 3.11, 3.12)"
)
python_init_parser.add_argument(
"--force", action="store_true", help="Force recreation if exists"
)
python_init_parser.add_argument(
"--no-hatch-mcp-server",
action="store_true",
help="Don't install hatch_mcp_server wrapper in the Python environment",
)
python_init_parser.add_argument(
"--hatch_mcp_server_tag",
help="Git tag/branch reference for hatch_mcp_server wrapper installation (e.g., 'dev', 'v0.1.0')",
)
python_init_parser.add_argument(
"--dry-run", action="store_true", help="Preview changes without execution"
)
# Show Python environment info
python_info_parser = env_python_subparsers.add_parser(
"info", help="Show Python environment information"
)
python_info_parser.add_argument(
"--hatch_env",
default=None,
help="Hatch environment name in which the Python environment is located (default: current environment)",
)
python_info_parser.add_argument(
"--detailed", action="store_true", help="Show detailed diagnostics"
)
# Hatch MCP server wrapper management
hatch_mcp_parser = env_python_subparsers.add_parser(
"add-hatch-mcp", help="Add hatch_mcp_server wrapper to the environment"
)
hatch_mcp_parser.add_argument(
"--hatch_env",
default=None,
help="Hatch environment name. It must possess a valid Python environment. (default: current environment)",
)
hatch_mcp_parser.add_argument(
"--tag",
default=None,
help="Git tag/branch reference for wrapper installation (e.g., 'dev', 'v0.1.0')",
)
hatch_mcp_parser.add_argument(
"--dry-run", action="store_true", help="Preview changes without execution"
)
# Remove Python environment
python_remove_parser = env_python_subparsers.add_parser(
"remove", help="Remove Python environment"
)
python_remove_parser.add_argument(
"--hatch_env",
default=None,
help="Hatch environment name in which the Python environment is located (default: current environment)",
)
python_remove_parser.add_argument(
"--force", action="store_true", help="Force removal without confirmation"
)
python_remove_parser.add_argument(
"--dry-run", action="store_true", help="Preview changes without execution"
)
# Launch Python shell
python_shell_parser = env_python_subparsers.add_parser(
"shell", help="Launch Python shell in environment"
)
python_shell_parser.add_argument(
"--hatch_env",
default=None,
help="Hatch environment name in which the Python environment is located (default: current environment)",
)
python_shell_parser.add_argument(
"--cmd", help="Command to run in the shell (optional)"
)
def _setup_package_commands(subparsers):
"""Set up 'hatch package' command parsers."""
pkg_subparsers = subparsers.add_parser(
"package", help="Package management commands"
).add_subparsers(dest="pkg_command", help="Package command to execute")
# Add package command
pkg_add_parser = pkg_subparsers.add_parser(
"add", help="Add a package to the current environment"
)
pkg_add_parser.add_argument(
"package_path_or_name", help="Path to package directory or name of the package"
)
pkg_add_parser.add_argument(
"--env",
"-e",
default=None,
help="Environment name (default: current environment)",
)
pkg_add_parser.add_argument(
"--version", "-v", default=None, help="Version of the package (optional)"
)
pkg_add_parser.add_argument(
"--force-download",
"-f",
action="store_true",
help="Force download even if package is in cache",
)
pkg_add_parser.add_argument(
"--refresh-registry",
"-r",
action="store_true",
help="Force refresh of registry data",
)
pkg_add_parser.add_argument(
"--auto-approve",
action="store_true",
help="Automatically approve changes installation of deps for automation scenario",
)
pkg_add_parser.add_argument(
"--host",
help="Comma-separated list of MCP host platforms to configure (e.g., claude-desktop,cursor)",
)
pkg_add_parser.add_argument(
"--dry-run", action="store_true", help="Preview changes without execution"
)
# Remove package command
pkg_remove_parser = pkg_subparsers.add_parser(
"remove", help="Remove a package from the current environment"
)
pkg_remove_parser.add_argument("package_name", help="Name of the package to remove")
pkg_remove_parser.add_argument(
"--env",
"-e",
default=None,
help="Environment name (default: current environment)",
)
pkg_remove_parser.add_argument(
"--dry-run", action="store_true", help="Preview changes without execution"
)
pkg_remove_parser.add_argument(
"--auto-approve", action="store_true", help="Skip confirmation prompt"
)
# List packages command
pkg_list_parser = pkg_subparsers.add_parser(
"list", help="List packages in an environment"
)
pkg_list_parser.add_argument(
"--env", "-e", help="Environment name (default: current environment)"
)
# Sync package MCP servers command
pkg_sync_parser = pkg_subparsers.add_parser(
"sync", help="Synchronize package MCP servers to host platforms"
)
pkg_sync_parser.add_argument(
"package_name", help="Name of the package whose MCP servers to sync"
)
pkg_sync_parser.add_argument(
"--host",
required=True,
help="Comma-separated list of host platforms to sync to (or 'all')",
)
pkg_sync_parser.add_argument(
"--env",
"-e",
default=None,
help="Environment name (default: current environment)",
)
pkg_sync_parser.add_argument(
"--dry-run", action="store_true", help="Preview changes without execution"
)
pkg_sync_parser.add_argument(
"--auto-approve", action="store_true", help="Skip confirmation prompts"
)
pkg_sync_parser.add_argument(
"--no-backup", action="store_true", help="Disable default backup behavior"
)
def _setup_mcp_commands(subparsers):
"""Set up 'hatch mcp' command parsers."""
mcp_subparsers = subparsers.add_parser(
"mcp", help="MCP host configuration commands"
).add_subparsers(dest="mcp_command", help="MCP command to execute")
# MCP discovery commands
mcp_discover_subparsers = mcp_subparsers.add_parser(
"discover", help="Discover MCP hosts and servers"
).add_subparsers(dest="discover_command", help="Discovery command to execute")
# Discover hosts command
mcp_discover_hosts_parser = mcp_discover_subparsers.add_parser(
"hosts", help="Discover available MCP host platforms"
)
mcp_discover_hosts_parser.add_argument(
"--json", action="store_true", help="Output in JSON format"
)
# Discover servers command
mcp_discover_servers_parser = mcp_discover_subparsers.add_parser(
"servers", help="Discover configured MCP servers"
)
mcp_discover_servers_parser.add_argument(
"--env",
"-e",
default=None,
help="Environment name (default: current environment)",
)
# MCP list commands
mcp_list_subparsers = mcp_subparsers.add_parser(
"list", help="List MCP hosts and servers"
).add_subparsers(dest="list_command", help="List command to execute")
# List hosts command - host-centric design per R10 §3.1
mcp_list_hosts_parser = mcp_list_subparsers.add_parser(
"hosts", help="List host/server pairs from host configuration files"
)
mcp_list_hosts_parser.add_argument(
"--server",
help="Filter by server name using regex pattern",
)
mcp_list_hosts_parser.add_argument(
"--json", action="store_true", help="Output in JSON format"
)
# List servers command - per R10 §3.2 (--pattern removed, use mcp list hosts --server instead)
mcp_list_servers_parser = mcp_list_subparsers.add_parser(
"servers", help="List server/host pairs from host configuration files"
)
mcp_list_servers_parser.add_argument(
"--host",
help="Filter by host name using regex pattern",
)
mcp_list_servers_parser.add_argument(
"--json", action="store_true", help="Output in JSON format"
)
# MCP show commands (detailed views) - per R11 specification
mcp_show_subparsers = mcp_subparsers.add_parser(
"show", help="Show detailed MCP host or server configuration"
).add_subparsers(dest="show_command", help="Show command to execute")
# Show hosts command - host-centric detailed view per R11 §2.1
mcp_show_hosts_parser = mcp_show_subparsers.add_parser(
"hosts", help="Show detailed host configurations"
)
mcp_show_hosts_parser.add_argument(
"--server",
help="Filter by server name using regex pattern",
)
mcp_show_hosts_parser.add_argument(
"--json", action="store_true", help="Output in JSON format"
)
# Show servers command - server-centric detailed view per R11 §2.2
mcp_show_servers_parser = mcp_show_subparsers.add_parser(
"servers", help="Show detailed server configurations across hosts"
)
mcp_show_servers_parser.add_argument(
"--host",
help="Filter by host name using regex pattern",
)
mcp_show_servers_parser.add_argument(
"--json", action="store_true", help="Output in JSON format"
)
# MCP backup commands
mcp_backup_subparsers = mcp_subparsers.add_parser(
"backup", help="Backup management commands"
).add_subparsers(dest="backup_command", help="Backup command to execute")
# Restore backup command
mcp_backup_restore_parser = mcp_backup_subparsers.add_parser(
"restore", help="Restore MCP host configuration from backup"
)
mcp_backup_restore_parser.add_argument(
"host", help="Host platform to restore (e.g., claude-desktop, cursor)"
)
mcp_backup_restore_parser.add_argument(
"--backup-file",
"-f",
default=None,
help="Specific backup file to restore (default: latest)",
)
mcp_backup_restore_parser.add_argument(
"--dry-run",
action="store_true",
help="Preview restore operation without execution",
)
mcp_backup_restore_parser.add_argument(
"--auto-approve", action="store_true", help="Skip confirmation prompts"
)
# List backups command
mcp_backup_list_parser = mcp_backup_subparsers.add_parser(
"list", help="List available backups for MCP host"
)
mcp_backup_list_parser.add_argument(
"host", help="Host platform to list backups for (e.g., claude-desktop, cursor)"
)
mcp_backup_list_parser.add_argument(
"--detailed", "-d", action="store_true", help="Show detailed backup information"
)
mcp_backup_list_parser.add_argument(
"--json", action="store_true", help="Output in JSON format"
)
# Clean backups command
mcp_backup_clean_parser = mcp_backup_subparsers.add_parser(
"clean", help="Clean old backups based on criteria"
)
mcp_backup_clean_parser.add_argument(
"host", help="Host platform to clean backups for (e.g., claude-desktop, cursor)"
)
mcp_backup_clean_parser.add_argument(
"--older-than-days", type=int, help="Remove backups older than specified days"
)
mcp_backup_clean_parser.add_argument(
"--keep-count",
type=int,
help="Keep only the specified number of newest backups",
)
mcp_backup_clean_parser.add_argument(
"--dry-run",
action="store_true",
help="Preview cleanup operation without execution",
)
mcp_backup_clean_parser.add_argument(
"--auto-approve", action="store_true", help="Skip confirmation prompts"
)
# MCP configure command
mcp_configure_parser = mcp_subparsers.add_parser(
"configure", help="Configure MCP server directly on host"
)
mcp_configure_parser.add_argument(
"server_name", help="Name for the MCP server [hosts: all]"
)
mcp_configure_parser.add_argument(
"--host",
required=True,
help="Host platform to configure (e.g., claude-desktop, cursor) [hosts: all]",
)
# Create mutually exclusive group for server type
server_type_group = mcp_configure_parser.add_mutually_exclusive_group()
server_type_group.add_argument(
"--command",
dest="server_command",
help="Command to execute the MCP server (for local servers) [hosts: all]",
)
server_type_group.add_argument(
"--url",
help="Server URL for remote MCP servers (SSE transport) [hosts: all except claude-desktop, claude-code]",
)
server_type_group.add_argument(
"--http-url", help="HTTP streaming endpoint URL [hosts: gemini]"
)
mcp_configure_parser.add_argument(
"--args",
nargs="*",
help="Arguments for the MCP server command (only with --command) [hosts: all]",
)
mcp_configure_parser.add_argument(
"--env-var",
action="append",
help="Environment variables (format: KEY=VALUE) [hosts: all]",
)
mcp_configure_parser.add_argument(
"--header",
action="append",
help="HTTP headers for remote servers (format: KEY=VALUE, only with --url) [hosts: all except claude-desktop, claude-code]",
)
# Host-specific arguments (Gemini)
mcp_configure_parser.add_argument(
"--timeout", type=int, help="Request timeout in milliseconds [hosts: gemini]"
)
mcp_configure_parser.add_argument(
"--trust",
action="store_true",
help="Bypass tool call confirmations [hosts: gemini]",
)
mcp_configure_parser.add_argument(
"--cwd", help="Working directory for stdio transport [hosts: gemini, codex]"
)
mcp_configure_parser.add_argument(
"--include-tools",
nargs="*",
help="Tool allowlist / enabled tools [hosts: gemini, codex]",
)
mcp_configure_parser.add_argument(
"--exclude-tools",
nargs="*",
help="Tool blocklist / disabled tools [hosts: gemini, codex]",
)
# Host-specific arguments (Cursor/VS Code/LM Studio)
mcp_configure_parser.add_argument(
"--env-file", help="Path to environment file [hosts: cursor, vscode, lmstudio]"
)
# Host-specific arguments (VS Code)
mcp_configure_parser.add_argument(
"--input",
action="append",
help="Input variable definitions in format: type,id,description[,password=true] [hosts: vscode]",
)
# Host-specific arguments (Kiro)
mcp_configure_parser.add_argument(
"--disabled",
action="store_true",
default=None,
help="Disable the MCP server [hosts: kiro]",
)
mcp_configure_parser.add_argument(
"--auto-approve-tools",
action="append",
help="Tool names to auto-approve without prompting [hosts: kiro]",
)
mcp_configure_parser.add_argument(
"--disable-tools", action="append", help="Tool names to disable [hosts: kiro]"
)
# Codex-specific arguments
mcp_configure_parser.add_argument(
"--env-vars",
action="append",
help="Environment variable names to whitelist/forward [hosts: codex]",
)
mcp_configure_parser.add_argument(
"--startup-timeout",
type=int,
help="Server startup timeout in seconds (default: 10) [hosts: codex]",
)
mcp_configure_parser.add_argument(
"--tool-timeout",
type=int,
help="Tool execution timeout in seconds (default: 60) [hosts: codex]",
)
mcp_configure_parser.add_argument(
"--enabled",
action="store_true",
default=None,
help="Enable the MCP server [hosts: codex]",
)
mcp_configure_parser.add_argument(
"--bearer-token-env-var",
type=str,
help="Name of environment variable containing bearer token for Authorization header [hosts: codex]",
)
mcp_configure_parser.add_argument(
"--env-header",
action="append",
help="HTTP header from environment variable in KEY=ENV_VAR_NAME format [hosts: codex]",
)
mcp_configure_parser.add_argument(
"--no-backup",
action="store_true",
help="Skip backup creation before configuration [hosts: all]",
)
mcp_configure_parser.add_argument(
"--dry-run",
action="store_true",
help="Preview configuration without execution [hosts: all]",
)
mcp_configure_parser.add_argument(
"--auto-approve",
action="store_true",
help="Skip confirmation prompts [hosts: all]",
)
# MCP remove commands
mcp_remove_subparsers = mcp_subparsers.add_parser(
"remove", help="Remove MCP servers or host configurations"
).add_subparsers(dest="remove_command", help="Remove command to execute")
# Remove server command
mcp_remove_server_parser = mcp_remove_subparsers.add_parser(
"server", help="Remove MCP server from hosts"
)
mcp_remove_server_parser.add_argument(
"server_name", help="Name of the MCP server to remove"
)
mcp_remove_server_parser.add_argument(
"--host", help="Target hosts (comma-separated or 'all')"
)
mcp_remove_server_parser.add_argument(
"--env", "-e", help="Environment name (for environment-based removal)"
)
mcp_remove_server_parser.add_argument(
"--no-backup", action="store_true", help="Skip backup creation before removal"
)
mcp_remove_server_parser.add_argument(
"--dry-run", action="store_true", help="Preview removal without execution"
)
mcp_remove_server_parser.add_argument(
"--auto-approve", action="store_true", help="Skip confirmation prompts"
)
# Remove host command
mcp_remove_host_parser = mcp_remove_subparsers.add_parser(
"host", help="Remove entire host configuration"
)
mcp_remove_host_parser.add_argument(
"host_name", help="Host platform to remove (e.g., claude-desktop, cursor)"
)
mcp_remove_host_parser.add_argument(
"--no-backup", action="store_true", help="Skip backup creation before removal"
)
mcp_remove_host_parser.add_argument(
"--dry-run", action="store_true", help="Preview removal without execution"
)
mcp_remove_host_parser.add_argument(
"--auto-approve", action="store_true", help="Skip confirmation prompts"
)
# MCP sync command
mcp_sync_parser = mcp_subparsers.add_parser(
"sync", help="Synchronize MCP configurations between environments and hosts"
)
# Source options (mutually exclusive)
sync_source_group = mcp_sync_parser.add_mutually_exclusive_group(required=True)
sync_source_group.add_argument("--from-env", help="Source environment name")
sync_source_group.add_argument("--from-host", help="Source host platform")
# Target options
mcp_sync_parser.add_argument(
"--to-host", required=True, help="Target hosts (comma-separated or 'all')"
)
# Filter options (mutually exclusive)
sync_filter_group = mcp_sync_parser.add_mutually_exclusive_group()
sync_filter_group.add_argument(
"--servers", help="Specific server names to sync (comma-separated)"
)
sync_filter_group.add_argument(
"--pattern", help="Regex pattern for server selection"
)
# Standard options
mcp_sync_parser.add_argument(
"--dry-run",
action="store_true",
help="Preview synchronization without execution",
)
mcp_sync_parser.add_argument(
"--auto-approve", action="store_true", help="Skip confirmation prompts"
)
mcp_sync_parser.add_argument(
"--no-backup",
action="store_true",
help="Skip backup creation before synchronization",
)
mcp_sync_parser.add_argument(
"--detailed",
nargs="?",
const="all",
default=None,
help="Show field-level details (optionally filter by consequence types: created,updated,synced,etc. or 'all')",
)
def _route_env_command(args):
"""Route environment commands to handlers."""
from hatch.cli.cli_env import (
handle_env_create,
handle_env_remove,
handle_env_list,
handle_env_list_hosts,
handle_env_list_servers,
handle_env_use,
handle_env_current,
handle_env_show,
handle_env_python_init,
handle_env_python_info,
handle_env_python_remove,
handle_env_python_shell,
handle_env_python_add_hatch_mcp,
)
if args.env_command == "create":
return handle_env_create(args)
elif args.env_command == "remove":
return handle_env_remove(args)
elif args.env_command == "list":
# Check for subcommand (hosts, servers) or default list behavior
list_command = getattr(args, "list_command", None)
if list_command == "hosts":
return handle_env_list_hosts(args)
elif list_command == "servers":
return handle_env_list_servers(args)
else:
# Default: list environments
return handle_env_list(args)
elif args.env_command == "use":
return handle_env_use(args)
elif args.env_command == "current":
return handle_env_current(args)
elif args.env_command == "show":
return handle_env_show(args)
elif args.env_command == "python":
if args.python_command == "init":
return handle_env_python_init(args)
elif args.python_command == "info":
return handle_env_python_info(args)
elif args.python_command == "remove":
return handle_env_python_remove(args)
elif args.python_command == "shell":
return handle_env_python_shell(args)
elif args.python_command == "add-hatch-mcp":
return handle_env_python_add_hatch_mcp(args)
else:
print("Unknown Python environment command")
return 1
else:
print("Unknown environment command")
return 1
def _route_package_command(args):
"""Route package commands to handlers."""
from hatch.cli.cli_package import (
handle_package_add,
handle_package_remove,
handle_package_list,
handle_package_sync,
)
if args.pkg_command == "add":
return handle_package_add(args)
elif args.pkg_command == "remove":
return handle_package_remove(args)
elif args.pkg_command == "list":
return handle_package_list(args)
elif args.pkg_command == "sync":
return handle_package_sync(args)
else:
print("Unknown package command")
return 1
def _route_mcp_command(args):
"""Route MCP commands to handlers."""
from hatch.cli.cli_mcp import (
handle_mcp_discover_hosts,
handle_mcp_discover_servers,
handle_mcp_list_hosts,
handle_mcp_list_servers,
handle_mcp_show_hosts,
handle_mcp_show_servers,
handle_mcp_backup_restore,
handle_mcp_backup_list,
handle_mcp_backup_clean,
handle_mcp_configure,
handle_mcp_remove_server,
handle_mcp_remove_host,
handle_mcp_sync,
)
if args.mcp_command == "discover":
if args.discover_command == "hosts":
return handle_mcp_discover_hosts(args)
elif args.discover_command == "servers":
return handle_mcp_discover_servers(args)
else:
print("Unknown discover command")
return 1
elif args.mcp_command == "list":
if args.list_command == "hosts":
return handle_mcp_list_hosts(args)
elif args.list_command == "servers":
return handle_mcp_list_servers(args)
else:
print("Unknown list command")
return 1
elif args.mcp_command == "show":
show_command = getattr(args, "show_command", None)
if show_command == "hosts":
return handle_mcp_show_hosts(args)
elif show_command == "servers":
return handle_mcp_show_servers(args)
else:
print(
"Unknown show command. Use 'hatch mcp show hosts' or 'hatch mcp show servers'"
)
return 1
elif args.mcp_command == "backup":
if args.backup_command == "restore":
return handle_mcp_backup_restore(args)
elif args.backup_command == "list":
return handle_mcp_backup_list(args)
elif args.backup_command == "clean":
return handle_mcp_backup_clean(args)
else:
print("Unknown backup command")
return 1
elif args.mcp_command == "configure":
return handle_mcp_configure(args)
elif args.mcp_command == "remove":
if args.remove_command == "server":
return handle_mcp_remove_server(args)
elif args.remove_command == "host":
return handle_mcp_remove_host(args)
else:
print("Unknown remove command")
return 1
elif args.mcp_command == "sync":
return handle_mcp_sync(args)
else:
print("Unknown MCP command")
return 1
def main() -> int:
"""Main entry point for Hatch CLI.
Parses command-line arguments and routes to appropriate handlers for:
- Package template creation
- Package validation
- Environment management
- Package management
- MCP host configuration
Returns:
int: Exit code (0 for success, 1 for errors)
"""
# Configure logging
logging.basicConfig(
level=logging.WARNING,
format="%(asctime)s - %(name)s - %(levelname)s - %(message)s",
)
# Create argument parser
parser = HatchArgumentParser(description="Hatch package manager CLI")
# Add version argument
parser.add_argument(
"--version", action="version", version=f"%(prog)s {get_hatch_version()}"
)
subparsers = parser.add_subparsers(dest="command", help="Command to execute")
# Set up command parsers
_setup_create_command(subparsers)
_setup_validate_command(subparsers)
_setup_env_commands(subparsers)
_setup_package_commands(subparsers)
_setup_mcp_commands(subparsers)
# General arguments for the environment manager
parser.add_argument(
"--envs-dir",
default=Path.home() / ".hatch" / "envs",
help="Directory to store environments",