|
| 1 | +import click |
| 2 | +from rich.console import Console |
| 3 | +from rich.markup import escape |
| 4 | +from rich.table import Table |
| 5 | + |
| 6 | +from mcpm.clients.client_registry import ClientRegistry |
| 7 | +from mcpm.profile.profile_manager import ProfileManager |
| 8 | + |
| 9 | +profile_manager = ProfileManager() |
| 10 | +console = Console() |
| 11 | + |
| 12 | + |
| 13 | +@click.group() |
| 14 | +def profile(): |
| 15 | + """Manage MCPM profiles.""" |
| 16 | + pass |
| 17 | + |
| 18 | + |
| 19 | +@click.command() |
| 20 | +@click.option("--verbose", "-v", is_flag=True, help="Show detailed server information") |
| 21 | +def list(verbose=False): |
| 22 | + """List all MCPM profiles.""" |
| 23 | + profiles = profile_manager.list_profiles() |
| 24 | + if not profiles: |
| 25 | + console.print("\n[yellow]No profiles found.[/]\n") |
| 26 | + return |
| 27 | + console.print(f"\n[green]Found {len(profiles)} profile(s)[/]\n") |
| 28 | + table = Table(show_header=True, header_style="bold") |
| 29 | + table.add_column("Name", style="cyan") |
| 30 | + table.add_column("Servers", overflow="fold") |
| 31 | + if verbose: |
| 32 | + table.add_column("Server Details", overflow="fold") |
| 33 | + for profile_name, configs in profiles.items(): |
| 34 | + server_names = [config.name for config in configs] |
| 35 | + row = [profile_name, ", ".join(server_names)] |
| 36 | + if verbose: |
| 37 | + details = [] |
| 38 | + for config in configs: |
| 39 | + details.append(f"{config.name}: {config.command} {' '.join(config.args)}") |
| 40 | + row.append("\n".join(details)) |
| 41 | + table.add_row(*row) |
| 42 | + console.print(table) |
| 43 | + |
| 44 | + |
| 45 | +@click.command() |
| 46 | +@click.argument("profile") |
| 47 | +@click.option("--force", is_flag=True, help="Force add even if profile already exists") |
| 48 | +def add(profile, force=False): |
| 49 | + """Add a new MCPM profile.""" |
| 50 | + if profile_manager.get_profile(profile) is not None and not force: |
| 51 | + console.print(f"[bold red]Error:[/] Profile '{profile}' already exists.") |
| 52 | + console.print("Use '--force' to overwrite the existing profile.") |
| 53 | + return |
| 54 | + |
| 55 | + profile_manager.new_profile(profile) |
| 56 | + |
| 57 | + console.print(f"\n[green]Profile '{profile}' added successfully.[/]\n") |
| 58 | + console.print(f"You can now add servers to this profile with 'mcpm add --profile {profile} <server_name>'\n") |
| 59 | + console.print( |
| 60 | + f"Or apply existing config to this profile with 'mcpm profile apply {profile} --server <server_name>'\n" |
| 61 | + ) |
| 62 | + |
| 63 | + |
| 64 | +@click.command() |
| 65 | +@click.argument("profile") |
| 66 | +@click.option("--server", "-s", required=True, help="Server to apply config to") |
| 67 | +def apply(profile, server): |
| 68 | + """Apply an existing MCPM config to a profile.""" |
| 69 | + client_manager = ClientRegistry.get_active_client_manager() |
| 70 | + client = ClientRegistry.get_active_client() |
| 71 | + client_info = ClientRegistry.get_client_info(client) |
| 72 | + client_name = client_info.get("name", client) |
| 73 | + |
| 74 | + # Check if client is supported |
| 75 | + if client_manager is None: |
| 76 | + console.print("[bold red]Error:[/] Unsupported active client") |
| 77 | + console.print("Please switch to a supported client using 'mcpm client <client-name>'") |
| 78 | + return |
| 79 | + |
| 80 | + # Check if the server exists in the active client |
| 81 | + server_info = client_manager.get_server(server) |
| 82 | + if server_info is None: |
| 83 | + console.print(f"[bold red]Error:[/] Server '{server}' not found in {client_name}.") |
| 84 | + return |
| 85 | + |
| 86 | + # Get profile |
| 87 | + profile_info = profile_manager.get_profile(profile) |
| 88 | + if profile_info is None: |
| 89 | + console.print(f"[bold red]Error:[/] Profile '{profile}' not found.") |
| 90 | + return |
| 91 | + |
| 92 | + # Save profile |
| 93 | + profile_manager.set_profile(profile, server_info) |
| 94 | + console.print(f"\n[green]Server '{server}' applied to profile '{profile}' successfully.[/]\n") |
| 95 | + |
| 96 | + |
| 97 | +@click.command() |
| 98 | +@click.argument("profile_name") |
| 99 | +def delete(profile_name): |
| 100 | + """Delete an MCPM profile.""" |
| 101 | + if not profile_manager.delete_profile(profile_name): |
| 102 | + console.print(f"[bold red]Error:[/] Profile '{profile_name}' not found.") |
| 103 | + return |
| 104 | + console.print(f"\n[green]Profile '{profile_name}' deleted successfully.[/]\n") |
| 105 | + |
| 106 | + |
| 107 | +@click.command() |
| 108 | +@click.argument("profile_name") |
| 109 | +def rename(profile_name): |
| 110 | + """Rename an MCPM profile.""" |
| 111 | + new_profile_name = click.prompt("Enter new profile name", type=str) |
| 112 | + if profile_manager.get_profile(new_profile_name) is not None: |
| 113 | + console.print(f"[bold red]Error:[/] Profile '{new_profile_name}' already exists.") |
| 114 | + return |
| 115 | + if not profile_manager.rename_profile(profile_name, new_profile_name): |
| 116 | + console.print(f"[bold red]Error:[/] Profile '{profile_name}' not found.") |
| 117 | + return |
| 118 | + console.print(f"\n[green]Profile '{profile_name}' renamed to '{new_profile_name}' successfully.[/]\n") |
| 119 | + |
| 120 | + |
| 121 | +@click.command() |
| 122 | +@click.argument("profile") |
| 123 | +@click.option("--server", "-s", required=True, help="Server to remove from profile") |
| 124 | +def remove_server(server, profile): |
| 125 | + """Remove a server from an MCPM profile.""" |
| 126 | + if not profile_manager.remove_server(server, profile): |
| 127 | + console.print(f"[bold red]Error:[/] Server '{server}' not found in profile '{profile}'.") |
| 128 | + return |
| 129 | + console.print(f"\n[green]Server '{server}' removed from profile '{profile}' successfully.[/]\n") |
| 130 | + |
| 131 | + |
| 132 | +@click.command() |
| 133 | +@click.argument("profile") |
| 134 | +def show(profile): |
| 135 | + """Show the servers in an MCPM profile.""" |
| 136 | + profile_info = profile_manager.get_profile(profile) |
| 137 | + if profile_info is None: |
| 138 | + console.print(f"[bold red]Error:[/] Profile '{profile}' not found.") |
| 139 | + return |
| 140 | + console.print(f"\n[green]Profile '{profile}' contains the following servers:[/]\n") |
| 141 | + for server in profile_info: |
| 142 | + console.print(f"[bold cyan]{server.name}[/]") |
| 143 | + command = server.command |
| 144 | + console.print(f" Command: [green]{command}[/]") |
| 145 | + |
| 146 | + # Display arguments |
| 147 | + args = server.args |
| 148 | + if args: |
| 149 | + console.print(" Arguments:") |
| 150 | + for i, arg in enumerate(args): |
| 151 | + console.print(f" {i}: [yellow]{escape(arg)}[/]") |
| 152 | + |
| 153 | + # Display environment variables |
| 154 | + env_vars = server.env |
| 155 | + if env_vars: |
| 156 | + console.print(" Environment Variables:") |
| 157 | + for key, value in env_vars.items(): |
| 158 | + console.print(f' [bold blue]{key}[/] = [green]"{value}"[/]') |
| 159 | + else: |
| 160 | + console.print(" Environment Variables: [italic]None[/]") |
| 161 | + |
| 162 | + # Add a separator line between servers |
| 163 | + console.print(" " + "-" * 50) |
| 164 | + console.print("\n") |
| 165 | + |
| 166 | + |
| 167 | +# Register all commands with the profile group |
| 168 | +profile.add_command(list) |
| 169 | +profile.add_command(add) |
| 170 | +profile.add_command(show) |
| 171 | +profile.add_command(apply) |
| 172 | +profile.add_command(delete) |
| 173 | +profile.add_command(rename) |
| 174 | +profile.add_command(remove_server, name="rm-server") |
0 commit comments