|
| 1 | +import shlex |
| 2 | + |
| 3 | +import click |
| 4 | +from rich.console import Console |
| 5 | +from rich.prompt import Confirm, Prompt |
| 6 | + |
| 7 | +from mcpm.commands.server_operations.common import client_add_server, determine_scope, profile_add_server |
| 8 | +from mcpm.schemas.server_config import SSEServerConfig, STDIOServerConfig |
| 9 | +from mcpm.utils.display import print_server_config |
| 10 | +from mcpm.utils.scope import ScopeType |
| 11 | + |
| 12 | +console = Console() |
| 13 | + |
| 14 | + |
| 15 | +@click.group() |
| 16 | +@click.help_option("-h", "--help") |
| 17 | +def import_server(): |
| 18 | + """Add server definitions manually.""" |
| 19 | + pass |
| 20 | + |
| 21 | + |
| 22 | +@import_server.command() |
| 23 | +@click.argument("server_name", required=True) |
| 24 | +@click.option("--command", "-c", help="Executable command", required=True) |
| 25 | +@click.option("--args", "-a", multiple=True, help="Arguments for the command (can be used multiple times)") |
| 26 | +@click.option("--env", "-e", multiple=True, help="Environment variables, format: ENV=val (can be used multiple times)") |
| 27 | +@click.option("--target", "-t", help="Target client or profile") |
| 28 | +@click.option("--force", is_flag=True, help="Force reinstall if server is already installed") |
| 29 | +@click.help_option("-h", "--help") |
| 30 | +def stdio(server_name, command, args, env, target, force): |
| 31 | + """Add a server by specifying command, args, and env variables. |
| 32 | + Examples: |
| 33 | +
|
| 34 | + \b |
| 35 | + mcpm import stdio <server_name> --command <command> --args <arg1> --args <arg2> --env <var1>=<value1> --env <var2>=<value2> |
| 36 | + """ |
| 37 | + scope_type, scope = determine_scope(target) |
| 38 | + if not scope: |
| 39 | + return |
| 40 | + |
| 41 | + # Extract env variables |
| 42 | + env_vars = {} |
| 43 | + for item in env: |
| 44 | + if "=" in item: |
| 45 | + key, value = item.split("=", 1) |
| 46 | + env_vars[key] = value |
| 47 | + else: |
| 48 | + console.print(f"[yellow]Ignoring invalid env: {item}[/]") |
| 49 | + |
| 50 | + try: |
| 51 | + # support spaces and quotes in args |
| 52 | + parsed_args = shlex.split(" ".join(args)) if args else [] |
| 53 | + server_config = STDIOServerConfig( |
| 54 | + name=server_name, |
| 55 | + command=command, |
| 56 | + args=parsed_args, |
| 57 | + env=env_vars, |
| 58 | + ) |
| 59 | + print_server_config(server_config) |
| 60 | + except ValueError as e: |
| 61 | + console.print(f"[bold red]Error:[/] {e}") |
| 62 | + return |
| 63 | + |
| 64 | + if not Confirm.ask(f"Add this server to {scope_type} {scope}?"): |
| 65 | + return |
| 66 | + console.print(f"[green]Importing server to {scope_type} {scope}[/]") |
| 67 | + |
| 68 | + if scope_type == ScopeType.CLIENT: |
| 69 | + success = client_add_server(scope, server_config, force) |
| 70 | + else: |
| 71 | + success = profile_add_server(scope, server_config, force) |
| 72 | + |
| 73 | + if success: |
| 74 | + console.print(f"[bold green]Stdio server '{server_name}' added successfully to {scope_type} {scope}.") |
| 75 | + else: |
| 76 | + console.print(f"[bold red]Failed to add stdio server '{server_name}' to {scope_type} {scope}.") |
| 77 | + |
| 78 | + |
| 79 | +@import_server.command() |
| 80 | +@click.argument("server_name", required=True) |
| 81 | +@click.option("--url", "-u", required=True, help="Server URL") |
| 82 | +@click.option("--header", "-H", multiple=True, help="HTTP headers, format: KEY=val (can be used multiple times)") |
| 83 | +@click.option("--target", "-t", help="Target to import server to") |
| 84 | +@click.option("--force", is_flag=True, help="Force reinstall if server is already installed") |
| 85 | +@click.help_option("-h", "--help") |
| 86 | +def sse(server_name, url, header, target, force): |
| 87 | + """Add a server by specifying a URL and headers. |
| 88 | + Examples: |
| 89 | +
|
| 90 | + \b |
| 91 | + mcpm import sse <server_name> --url <url> --header <key1>=<value1> --header <key2>=<value2> |
| 92 | + """ |
| 93 | + scope_type, scope = determine_scope(target) |
| 94 | + if not scope: |
| 95 | + return |
| 96 | + |
| 97 | + headers = {} |
| 98 | + for item in header: |
| 99 | + if "=" in item: |
| 100 | + key, value = item.split("=", 1) |
| 101 | + headers[key] = value |
| 102 | + else: |
| 103 | + console.print(f"[yellow]Ignoring invalid header: {item}[/]") |
| 104 | + |
| 105 | + try: |
| 106 | + server_config = SSEServerConfig( |
| 107 | + name=server_name, |
| 108 | + url=url, |
| 109 | + headers=headers, |
| 110 | + ) |
| 111 | + print_server_config(server_config) |
| 112 | + except ValueError as e: |
| 113 | + console.print(f"[bold red]Error:[/] {e}") |
| 114 | + return |
| 115 | + |
| 116 | + if not Confirm.ask(f"Add this server to {scope_type} {scope}?"): |
| 117 | + return |
| 118 | + console.print(f"[green]Importing server to {scope_type} {scope}[/]") |
| 119 | + |
| 120 | + if scope_type == ScopeType.CLIENT: |
| 121 | + success = client_add_server(scope, server_config, force) |
| 122 | + else: |
| 123 | + success = profile_add_server(scope, server_config, force) |
| 124 | + |
| 125 | + if success: |
| 126 | + console.print(f"[bold green]SSE server '{server_name}' added successfully to {scope_type} {scope}.") |
| 127 | + else: |
| 128 | + console.print(f"[bold red]Failed to add SSE server '{server_name}' to {scope_type} {scope}.") |
| 129 | + |
| 130 | + |
| 131 | +@import_server.command() |
| 132 | +@click.option("--target", "-t", help="Target to import server to") |
| 133 | +@click.help_option("-h", "--help") |
| 134 | +def interact(target: str | None = None): |
| 135 | + """Add a server by manually configuring it interactively.""" |
| 136 | + scope_type, scope = determine_scope(target) |
| 137 | + if not scope: |
| 138 | + return |
| 139 | + |
| 140 | + server_name = Prompt.ask("Enter server name") |
| 141 | + if not server_name: |
| 142 | + console.print("[red]Server name cannot be empty.[/]") |
| 143 | + return |
| 144 | + |
| 145 | + config_type = Prompt.ask("Select server type", choices=["stdio", "sse"], default="stdio") |
| 146 | + |
| 147 | + if config_type == "stdio": |
| 148 | + command = Prompt.ask("Enter command (executable)") |
| 149 | + args = Prompt.ask("Enter arguments (space-separated, optional)", default="") |
| 150 | + env_input = Prompt.ask("Enter env variables (format: KEY=VAL, comma-separated, optional)", default="") |
| 151 | + env = {} |
| 152 | + if env_input.strip(): |
| 153 | + for pair in env_input.split(","): |
| 154 | + if "=" in pair: |
| 155 | + k, v = pair.split("=", 1) |
| 156 | + env[k.strip()] = v.strip() |
| 157 | + try: |
| 158 | + # support spaces and quotes in args |
| 159 | + parsed_args = shlex.split(args) if args.strip() else [] |
| 160 | + server_config = STDIOServerConfig( |
| 161 | + name=server_name, |
| 162 | + command=command, |
| 163 | + args=parsed_args, |
| 164 | + env=env, |
| 165 | + ) |
| 166 | + except ValueError as e: |
| 167 | + console.print(f"[bold red]Error:[/] {e}") |
| 168 | + return |
| 169 | + elif config_type == "sse": |
| 170 | + url = Prompt.ask("Enter SSE server URL") |
| 171 | + headers_input = Prompt.ask("Enter HTTP headers (format: KEY=VAL, comma-separated, optional)", default="") |
| 172 | + headers = {} |
| 173 | + if headers_input.strip(): |
| 174 | + for pair in headers_input.split(","): |
| 175 | + if "=" in pair: |
| 176 | + k, v = pair.split("=", 1) |
| 177 | + headers[k.strip()] = v.strip() |
| 178 | + try: |
| 179 | + server_config = SSEServerConfig( |
| 180 | + name=server_name, |
| 181 | + url=url, |
| 182 | + headers=headers, |
| 183 | + ) |
| 184 | + except ValueError as e: |
| 185 | + console.print(f"[bold red]Error:[/] {e}") |
| 186 | + return |
| 187 | + else: |
| 188 | + console.print(f"[red]Unknown server type: {config_type}[/]") |
| 189 | + return |
| 190 | + |
| 191 | + print_server_config(server_config) |
| 192 | + if not Confirm.ask(f"Add this server to {scope_type} {scope}?"): |
| 193 | + return |
| 194 | + console.print(f"[green]Importing server to {scope_type} {scope}[/]") |
| 195 | + |
| 196 | + if scope_type == ScopeType.CLIENT: |
| 197 | + success = client_add_server(scope, server_config, False) |
| 198 | + else: |
| 199 | + success = profile_add_server(scope, server_config, False) |
| 200 | + |
| 201 | + if success: |
| 202 | + console.print( |
| 203 | + f"[bold green]{config_type.upper()} server '{server_name}' added successfully to {scope_type} {scope}." |
| 204 | + ) |
| 205 | + else: |
| 206 | + console.print(f"[bold red]Failed to add {config_type.upper()} server '{server_name}' to {scope_type} {scope}.") |
0 commit comments