|
47 | 47 | from hatch.cli.cli_mcp import ( |
48 | 48 | handle_mcp_discover_hosts as _handle_mcp_discover_hosts, |
49 | 49 | handle_mcp_discover_servers as _handle_mcp_discover_servers, |
| 50 | + handle_mcp_list_hosts as _handle_mcp_list_hosts, |
| 51 | + handle_mcp_list_servers as _handle_mcp_list_servers, |
50 | 52 | ) |
51 | 53 |
|
52 | 54 |
|
@@ -80,167 +82,27 @@ def handle_mcp_list_hosts( |
80 | 82 | env_name: Optional[str] = None, |
81 | 83 | detailed: bool = False, |
82 | 84 | ): |
83 | | - """Handle 'hatch mcp list hosts' command - shows configured hosts in environment.""" |
84 | | - try: |
85 | | - from collections import defaultdict |
86 | | - |
87 | | - # Resolve environment name |
88 | | - target_env = env_name or env_manager.get_current_environment() |
89 | | - |
90 | | - # Validate environment exists |
91 | | - if not env_manager.environment_exists(target_env): |
92 | | - available_envs = env_manager.list_environments() |
93 | | - print(f"Error: Environment '{target_env}' does not exist.") |
94 | | - if available_envs: |
95 | | - print(f"Available environments: {', '.join(available_envs)}") |
96 | | - return 1 |
97 | | - |
98 | | - # Collect hosts from configured_hosts across all packages in environment |
99 | | - hosts = defaultdict(int) |
100 | | - host_details = defaultdict(list) |
101 | | - |
102 | | - try: |
103 | | - env_data = env_manager.get_environment_data(target_env) |
104 | | - packages = env_data.get("packages", []) |
105 | | - |
106 | | - for package in packages: |
107 | | - package_name = package.get("name", "unknown") |
108 | | - configured_hosts = package.get("configured_hosts", {}) |
109 | | - |
110 | | - for host_name, host_config in configured_hosts.items(): |
111 | | - hosts[host_name] += 1 |
112 | | - if detailed: |
113 | | - config_path = host_config.get("config_path", "N/A") |
114 | | - configured_at = host_config.get("configured_at", "N/A") |
115 | | - host_details[host_name].append( |
116 | | - { |
117 | | - "package": package_name, |
118 | | - "config_path": config_path, |
119 | | - "configured_at": configured_at, |
120 | | - } |
121 | | - ) |
122 | | - |
123 | | - except Exception as e: |
124 | | - print(f"Error reading environment data: {e}") |
125 | | - return 1 |
126 | | - |
127 | | - # Display results |
128 | | - if not hosts: |
129 | | - print(f"No configured hosts for environment '{target_env}'") |
130 | | - return 0 |
131 | | - |
132 | | - print(f"Configured hosts for environment '{target_env}':") |
133 | | - |
134 | | - for host_name, package_count in sorted(hosts.items()): |
135 | | - if detailed: |
136 | | - print(f"\n{host_name} ({package_count} packages):") |
137 | | - for detail in host_details[host_name]: |
138 | | - print(f" - Package: {detail['package']}") |
139 | | - print(f" Config path: {detail['config_path']}") |
140 | | - print(f" Configured at: {detail['configured_at']}") |
141 | | - else: |
142 | | - print(f" - {host_name} ({package_count} packages)") |
143 | | - |
144 | | - return 0 |
145 | | - except Exception as e: |
146 | | - print(f"Error listing hosts: {e}") |
147 | | - return 1 |
| 85 | + """Handle 'hatch mcp list hosts' command - shows configured hosts in environment. |
| 86 | + |
| 87 | + Delegates to hatch.cli.cli_mcp.handle_mcp_list_hosts. |
| 88 | + This wrapper maintains backward compatibility during refactoring. |
| 89 | + """ |
| 90 | + from argparse import Namespace |
| 91 | + args = Namespace(env_manager=env_manager, env=env_name, detailed=detailed) |
| 92 | + return _handle_mcp_list_hosts(args) |
148 | 93 |
|
149 | 94 |
|
150 | 95 | def handle_mcp_list_servers( |
151 | 96 | env_manager: HatchEnvironmentManager, env_name: Optional[str] = None |
152 | 97 | ): |
153 | | - """Handle 'hatch mcp list servers' command.""" |
154 | | - try: |
155 | | - env_name = env_name or env_manager.get_current_environment() |
156 | | - |
157 | | - if not env_manager.environment_exists(env_name): |
158 | | - print(f"Error: Environment '{env_name}' does not exist") |
159 | | - return 1 |
160 | | - |
161 | | - packages = env_manager.list_packages(env_name) |
162 | | - mcp_packages = [] |
163 | | - |
164 | | - for package in packages: |
165 | | - # Check if package has host configuration tracking (indicating MCP server) |
166 | | - configured_hosts = package.get("configured_hosts", {}) |
167 | | - if configured_hosts: |
168 | | - # Use the tracked server configuration from any host |
169 | | - first_host = next(iter(configured_hosts.values())) |
170 | | - server_config_data = first_host.get("server_config", {}) |
171 | | - |
172 | | - # Create a simple server config object |
173 | | - class SimpleServerConfig: |
174 | | - def __init__(self, data): |
175 | | - self.name = data.get("name", package["name"]) |
176 | | - self.command = data.get("command", "unknown") |
177 | | - self.args = data.get("args", []) |
178 | | - |
179 | | - server_config = SimpleServerConfig(server_config_data) |
180 | | - mcp_packages.append( |
181 | | - {"package": package, "server_config": server_config} |
182 | | - ) |
183 | | - else: |
184 | | - # Try the original method as fallback |
185 | | - try: |
186 | | - server_config = get_package_mcp_server_config( |
187 | | - env_manager, env_name, package["name"] |
188 | | - ) |
189 | | - mcp_packages.append( |
190 | | - {"package": package, "server_config": server_config} |
191 | | - ) |
192 | | - except: |
193 | | - # Package doesn't have MCP server or method failed |
194 | | - continue |
195 | | - |
196 | | - if not mcp_packages: |
197 | | - print(f"No MCP servers configured in environment '{env_name}'") |
198 | | - return 0 |
199 | | - |
200 | | - print(f"MCP servers in environment '{env_name}':") |
201 | | - print(f"{'Server Name':<20} {'Package':<20} {'Version':<10} {'Command'}") |
202 | | - print("-" * 80) |
203 | | - |
204 | | - for item in mcp_packages: |
205 | | - package = item["package"] |
206 | | - server_config = item["server_config"] |
207 | | - |
208 | | - server_name = server_config.name |
209 | | - package_name = package["name"] |
210 | | - version = package.get("version", "unknown") |
211 | | - command = f"{server_config.command} {' '.join(server_config.args)}" |
212 | | - |
213 | | - print(f"{server_name:<20} {package_name:<20} {version:<10} {command}") |
214 | | - |
215 | | - # Display host configuration tracking information |
216 | | - configured_hosts = package.get("configured_hosts", {}) |
217 | | - if configured_hosts: |
218 | | - print(f"{'':>20} Configured on hosts:") |
219 | | - for hostname, host_config in configured_hosts.items(): |
220 | | - config_path = host_config.get("config_path", "unknown") |
221 | | - last_synced = host_config.get("last_synced", "unknown") |
222 | | - # Format the timestamp for better readability |
223 | | - if last_synced != "unknown": |
224 | | - try: |
225 | | - from datetime import datetime |
226 | | - |
227 | | - dt = datetime.fromisoformat( |
228 | | - last_synced.replace("Z", "+00:00") |
229 | | - ) |
230 | | - last_synced = dt.strftime("%Y-%m-%d %H:%M:%S") |
231 | | - except: |
232 | | - pass # Keep original format if parsing fails |
233 | | - print( |
234 | | - f"{'':>22} - {hostname}: {config_path} (synced: {last_synced})" |
235 | | - ) |
236 | | - else: |
237 | | - print(f"{'':>20} No host configurations tracked") |
238 | | - print() # Add blank line between servers |
239 | | - |
240 | | - return 0 |
241 | | - except Exception as e: |
242 | | - print(f"Error listing servers: {e}") |
243 | | - return 1 |
| 98 | + """Handle 'hatch mcp list servers' command. |
| 99 | + |
| 100 | + Delegates to hatch.cli.cli_mcp.handle_mcp_list_servers. |
| 101 | + This wrapper maintains backward compatibility during refactoring. |
| 102 | + """ |
| 103 | + from argparse import Namespace |
| 104 | + args = Namespace(env_manager=env_manager, env=env_name) |
| 105 | + return _handle_mcp_list_servers(args) |
244 | 106 |
|
245 | 107 |
|
246 | 108 | def handle_mcp_backup_restore( |
|
0 commit comments