Skip to content

Commit e727324

Browse files
refactor(cli): use ResultReporter in MCP remove handlers
1 parent 9d52d24 commit e727324

File tree

1 file changed

+63
-43
lines changed

1 file changed

+63
-43
lines changed

hatch/cli/cli_mcp.py

Lines changed: 63 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -898,7 +898,11 @@ def handle_mcp_remove(args: Namespace) -> int:
898898
Returns:
899899
int: EXIT_SUCCESS (0) on success, EXIT_ERROR (1) on failure
900900
"""
901-
from hatch.cli.cli_utils import request_confirmation
901+
from hatch.cli.cli_utils import (
902+
request_confirmation,
903+
ResultReporter,
904+
ConsequenceType,
905+
)
902906

903907
host = args.host
904908
server_name = args.server_name
@@ -916,17 +920,21 @@ def handle_mcp_remove(args: Namespace) -> int:
916920
)
917921
return EXIT_ERROR
918922

923+
# Create ResultReporter for unified output
924+
reporter = ResultReporter("hatch mcp remove", dry_run=dry_run)
925+
reporter.add(ConsequenceType.REMOVE, f"Server '{server_name}' from '{host}'")
926+
919927
if dry_run:
920-
print(
921-
f"[DRY RUN] Would remove MCP server '{server_name}' from host '{host}'"
922-
)
923-
print(f"[DRY RUN] Backup: {'Disabled' if no_backup else 'Enabled'}")
928+
reporter.report_result()
924929
return EXIT_SUCCESS
925930

931+
# Show prompt for confirmation
932+
prompt = reporter.report_prompt()
933+
if prompt:
934+
print(prompt)
935+
926936
# Confirm operation unless auto-approved
927-
if not request_confirmation(
928-
f"Remove MCP server '{server_name}' from host '{host}'?", auto_approve
929-
):
937+
if not request_confirmation("Proceed?", auto_approve):
930938
print("Operation cancelled.")
931939
return EXIT_SUCCESS
932940

@@ -937,11 +945,9 @@ def handle_mcp_remove(args: Namespace) -> int:
937945
)
938946

939947
if result.success:
940-
print(
941-
f"[SUCCESS] Successfully removed MCP server '{server_name}' from host '{host}'"
942-
)
948+
reporter.report_result()
943949
if result.backup_path:
944-
print(f" Backup created: {result.backup_path}")
950+
print(f" Backup: {result.backup_path}")
945951
return EXIT_SUCCESS
946952
else:
947953
print(
@@ -972,7 +978,12 @@ def handle_mcp_remove_server(args: Namespace) -> int:
972978
Returns:
973979
int: EXIT_SUCCESS (0) on success, EXIT_ERROR (1) on failure
974980
"""
975-
from hatch.cli.cli_utils import request_confirmation, parse_host_list
981+
from hatch.cli.cli_utils import (
982+
request_confirmation,
983+
parse_host_list,
984+
ResultReporter,
985+
ConsequenceType,
986+
)
976987

977988
env_manager = args.env_manager
978989
server_name = args.server_name
@@ -998,35 +1009,40 @@ def handle_mcp_remove_server(args: Namespace) -> int:
9981009
print("Error: No valid hosts specified")
9991010
return EXIT_ERROR
10001011

1012+
# Create ResultReporter for unified output
1013+
reporter = ResultReporter("hatch mcp remove-server", dry_run=dry_run)
1014+
for host in target_hosts:
1015+
reporter.add(ConsequenceType.REMOVE, f"Server '{server_name}' from '{host}'")
1016+
10011017
if dry_run:
1002-
print(
1003-
f"[DRY RUN] Would remove MCP server '{server_name}' from hosts: {', '.join(target_hosts)}"
1004-
)
1005-
print(f"[DRY RUN] Backup: {'Disabled' if no_backup else 'Enabled'}")
1018+
reporter.report_result()
10061019
return EXIT_SUCCESS
10071020

1021+
# Show prompt for confirmation
1022+
prompt = reporter.report_prompt()
1023+
if prompt:
1024+
print(prompt)
1025+
10081026
# Confirm operation unless auto-approved
1009-
hosts_str = ", ".join(target_hosts)
1010-
if not request_confirmation(
1011-
f"Remove MCP server '{server_name}' from hosts: {hosts_str}?", auto_approve
1012-
):
1027+
if not request_confirmation("Proceed?", auto_approve):
10131028
print("Operation cancelled.")
10141029
return EXIT_SUCCESS
10151030

10161031
# Perform removal on each host
10171032
mcp_manager = MCPHostConfigurationManager()
10181033
success_count = 0
10191034
total_count = len(target_hosts)
1035+
1036+
# Create result reporter for actual results
1037+
result_reporter = ResultReporter("hatch mcp remove-server", dry_run=False)
10201038

10211039
for host in target_hosts:
10221040
result = mcp_manager.remove_server(
10231041
server_name=server_name, hostname=host, no_backup=no_backup
10241042
)
10251043

10261044
if result.success:
1027-
print(f"[SUCCESS] Successfully removed '{server_name}' from '{host}'")
1028-
if result.backup_path:
1029-
print(f" Backup created: {result.backup_path}")
1045+
result_reporter.add(ConsequenceType.REMOVE, f"'{server_name}' from '{host}'")
10301046
success_count += 1
10311047

10321048
# Update environment tracking for current environment only
@@ -1036,18 +1052,15 @@ def handle_mcp_remove_server(args: Namespace) -> int:
10361052
current_env, server_name, host
10371053
)
10381054
else:
1039-
print(
1040-
f"[ERROR] Failed to remove '{server_name}' from '{host}': {result.error_message}"
1041-
)
1055+
result_reporter.add(ConsequenceType.SKIP, f"'{server_name}' from '{host}': {result.error_message}")
10421056

10431057
# Summary
10441058
if success_count == total_count:
1045-
print(f"[SUCCESS] Removed '{server_name}' from all {total_count} hosts")
1059+
result_reporter.report_result()
10461060
return EXIT_SUCCESS
10471061
elif success_count > 0:
1048-
print(
1049-
f"[PARTIAL SUCCESS] Removed '{server_name}' from {success_count}/{total_count} hosts"
1050-
)
1062+
print(f"[WARNING] Partial success: {success_count}/{total_count} hosts")
1063+
result_reporter.report_result()
10511064
return EXIT_ERROR
10521065
else:
10531066
print(f"[ERROR] Failed to remove '{server_name}' from any hosts")
@@ -1074,7 +1087,11 @@ def handle_mcp_remove_host(args: Namespace) -> int:
10741087
Returns:
10751088
int: EXIT_SUCCESS (0) on success, EXIT_ERROR (1) on failure
10761089
"""
1077-
from hatch.cli.cli_utils import request_confirmation
1090+
from hatch.cli.cli_utils import (
1091+
request_confirmation,
1092+
ResultReporter,
1093+
ConsequenceType,
1094+
)
10781095

10791096
env_manager = args.env_manager
10801097
host_name = args.host_name
@@ -1092,16 +1109,21 @@ def handle_mcp_remove_host(args: Namespace) -> int:
10921109
)
10931110
return EXIT_ERROR
10941111

1112+
# Create ResultReporter for unified output
1113+
reporter = ResultReporter("hatch mcp remove-host", dry_run=dry_run)
1114+
reporter.add(ConsequenceType.REMOVE, f"All servers from host '{host_name}'")
1115+
10951116
if dry_run:
1096-
print(f"[DRY RUN] Would remove entire host configuration for '{host_name}'")
1097-
print(f"[DRY RUN] Backup: {'Disabled' if no_backup else 'Enabled'}")
1117+
reporter.report_result()
10981118
return EXIT_SUCCESS
10991119

1120+
# Show prompt for confirmation
1121+
prompt = reporter.report_prompt()
1122+
if prompt:
1123+
print(prompt)
1124+
11001125
# Confirm operation unless auto-approved
1101-
if not request_confirmation(
1102-
f"Remove entire host configuration for '{host_name}'? This will remove ALL MCP servers from this host.",
1103-
auto_approve,
1104-
):
1126+
if not request_confirmation("Proceed?", auto_approve):
11051127
print("Operation cancelled.")
11061128
return EXIT_SUCCESS
11071129

@@ -1112,16 +1134,14 @@ def handle_mcp_remove_host(args: Namespace) -> int:
11121134
)
11131135

11141136
if result.success:
1115-
print(
1116-
f"[SUCCESS] Successfully removed host configuration for '{host_name}'"
1117-
)
1137+
reporter.report_result()
11181138
if result.backup_path:
1119-
print(f" Backup created: {result.backup_path}")
1139+
print(f" Backup: {result.backup_path}")
11201140

11211141
# Update environment tracking across all environments
11221142
updates_count = env_manager.clear_host_from_all_packages_all_envs(host_name)
11231143
if updates_count > 0:
1124-
print(f"Updated {updates_count} package entries across environments")
1144+
print(f" Updated {updates_count} package entries across environments")
11251145

11261146
return EXIT_SUCCESS
11271147
else:

0 commit comments

Comments
 (0)