|
| 1 | +""" |
| 2 | +Edit command for MCP (formerly config) |
| 3 | +""" |
| 4 | + |
| 5 | +import os |
| 6 | +import json |
| 7 | +import click |
| 8 | +import subprocess |
| 9 | +from rich.console import Console |
| 10 | +from rich.panel import Panel |
| 11 | +from rich.prompt import Confirm |
| 12 | + |
| 13 | +from mcp.clients.claude_desktop import ClaudeDesktopManager |
| 14 | +from mcp.clients.windsurf import WindsurfManager |
| 15 | +from mcp.utils.config import ConfigManager |
| 16 | + |
| 17 | +console = Console() |
| 18 | +config_manager = ConfigManager() |
| 19 | +claude_manager = ClaudeDesktopManager() |
| 20 | +windsurf_manager = WindsurfManager() |
| 21 | + |
| 22 | +@click.command() |
| 23 | +@click.option("--edit", is_flag=True, help="Open the active client's config in default editor") |
| 24 | +@click.option("--create", is_flag=True, help="Create a basic config file if it doesn't exist") |
| 25 | +def edit(edit, create): |
| 26 | + """View or edit the active MCP client's configuration file. |
| 27 | + |
| 28 | + The edit command operates on the currently active MCP client (set via 'mcp client'). |
| 29 | + By default, this is Claude Desktop, but can be changed to other supported clients. |
| 30 | + |
| 31 | + Examples: |
| 32 | + mcp edit # Show current client's config file location and content |
| 33 | + mcp edit --edit # Open the config file in your default editor |
| 34 | + mcp edit --create # Create a basic config file if it doesn't exist |
| 35 | + """ |
| 36 | + # Get the active client and its corresponding manager |
| 37 | + active_client = config_manager.get_active_client() |
| 38 | + |
| 39 | + # Select appropriate client manager based on active client |
| 40 | + if active_client == "claude-desktop": |
| 41 | + client_manager = claude_manager |
| 42 | + client_name = "Claude Desktop" |
| 43 | + install_url = "https://claude.ai/download" |
| 44 | + is_installed_method = client_manager.is_claude_desktop_installed |
| 45 | + elif active_client == "windsurf": |
| 46 | + client_manager = windsurf_manager |
| 47 | + client_name = "Windsurf" |
| 48 | + install_url = "https://codeium.com/windsurf/download" |
| 49 | + is_installed_method = client_manager.is_windsurf_installed |
| 50 | + else: |
| 51 | + console.print(f"[bold red]Error:[/] Unsupported active client: {active_client}") |
| 52 | + console.print("Please switch to a supported client using 'mcp client <client-name>'") |
| 53 | + return |
| 54 | + |
| 55 | + # Get the client config file path |
| 56 | + config_path = client_manager.config_path |
| 57 | + |
| 58 | + # Check if the client is installed |
| 59 | + if not is_installed_method(): |
| 60 | + console.print(f"[bold red]Error:[/] {client_name} installation not detected.") |
| 61 | + console.print(f"Please download and install {client_name} from {install_url}") |
| 62 | + return |
| 63 | + |
| 64 | + # Check if config file exists |
| 65 | + config_exists = os.path.exists(config_path) |
| 66 | + |
| 67 | + # Display the config file information |
| 68 | + console.print(f"[bold]{client_name} config file:[/] {config_path}") |
| 69 | + console.print(f"[bold]Status:[/] {'[green]Exists[/]' if config_exists else '[yellow]Does not exist[/]'}\n") |
| 70 | + |
| 71 | + # Create config file if requested and it doesn't exist |
| 72 | + if not config_exists and create: |
| 73 | + console.print(f"[bold yellow]Creating new {client_name} config file...[/]") |
| 74 | + |
| 75 | + # Create a basic config template |
| 76 | + basic_config = { |
| 77 | + "mcpServers": { |
| 78 | + "filesystem": { |
| 79 | + "command": "npx", |
| 80 | + "args": [ |
| 81 | + "-y", |
| 82 | + "@modelcontextprotocol/server-filesystem", |
| 83 | + os.path.expanduser("~/Desktop"), |
| 84 | + os.path.expanduser("~/Downloads") |
| 85 | + ] |
| 86 | + } |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + # Create the directory if it doesn't exist |
| 91 | + os.makedirs(os.path.dirname(config_path), exist_ok=True) |
| 92 | + |
| 93 | + # Write the template to file |
| 94 | + try: |
| 95 | + with open(config_path, 'w') as f: |
| 96 | + json.dump(basic_config, f, indent=2) |
| 97 | + console.print("[green]Successfully created config file![/]\n") |
| 98 | + config_exists = True |
| 99 | + except Exception as e: |
| 100 | + console.print(f"[bold red]Error creating config file:[/] {str(e)}") |
| 101 | + return |
| 102 | + |
| 103 | + # Show the current configuration if it exists |
| 104 | + if config_exists: |
| 105 | + try: |
| 106 | + with open(config_path, 'r') as f: |
| 107 | + config_content = f.read() |
| 108 | + |
| 109 | + # Display the content |
| 110 | + console.print("[bold]Current configuration:[/]") |
| 111 | + panel = Panel(config_content, title=f"{client_name} Config", expand=False) |
| 112 | + console.print(panel) |
| 113 | + |
| 114 | + # Count the configured servers |
| 115 | + try: |
| 116 | + config_json = json.loads(config_content) |
| 117 | + server_count = len(config_json.get("mcpServers", {})) |
| 118 | + console.print(f"[bold]Configured servers:[/] {server_count}") |
| 119 | + |
| 120 | + # Display detailed information for each server |
| 121 | + if server_count > 0: |
| 122 | + console.print("\n[bold]MCP Server Details:[/]") |
| 123 | + for server_name, server_config in config_json.get("mcpServers", {}).items(): |
| 124 | + console.print(f"\n[bold cyan]{server_name}[/]") |
| 125 | + console.print(f" Command: [green]{server_config.get('command', 'N/A')}[/]") |
| 126 | + |
| 127 | + # Display arguments |
| 128 | + args = server_config.get('args', []) |
| 129 | + if args: |
| 130 | + console.print(" Arguments:") |
| 131 | + for i, arg in enumerate(args): |
| 132 | + console.print(f" {i}: [yellow]{arg}[/]") |
| 133 | + |
| 134 | + # Display environment variables |
| 135 | + env_vars = server_config.get('env', {}) |
| 136 | + if env_vars: |
| 137 | + console.print(" Environment Variables:") |
| 138 | + for key, value in env_vars.items(): |
| 139 | + console.print(f" [bold blue]{key}[/] = [green]\"{value}\"[/]") |
| 140 | + else: |
| 141 | + console.print(" Environment Variables: [italic]None[/]") |
| 142 | + |
| 143 | + # Add a separator line |
| 144 | + console.print(" " + "-" * 50) |
| 145 | + |
| 146 | + except json.JSONDecodeError: |
| 147 | + console.print("[yellow]Warning: Config file contains invalid JSON[/]") |
| 148 | + |
| 149 | + except Exception as e: |
| 150 | + console.print(f"[bold red]Error reading config file:[/] {str(e)}") |
| 151 | + |
| 152 | + # Prompt to edit if file exists, or if we need to create it |
| 153 | + should_edit = edit |
| 154 | + if not should_edit and config_exists: |
| 155 | + should_edit = Confirm.ask("Would you like to open this file in your default editor?") |
| 156 | + elif not config_exists and not create: |
| 157 | + should_create = Confirm.ask("Config file doesn't exist. Would you like to create it?") |
| 158 | + if should_create: |
| 159 | + # Create a basic config template |
| 160 | + basic_config = { |
| 161 | + "mcpServers": {} |
| 162 | + } |
| 163 | + |
| 164 | + # Create the directory if it doesn't exist |
| 165 | + os.makedirs(os.path.dirname(config_path), exist_ok=True) |
| 166 | + |
| 167 | + # Write the template to file |
| 168 | + try: |
| 169 | + with open(config_path, 'w') as f: |
| 170 | + json.dump(basic_config, f, indent=2) |
| 171 | + console.print("[green]Successfully created config file![/]") |
| 172 | + should_edit = Confirm.ask("Would you like to open it in your default editor?") |
| 173 | + config_exists = True |
| 174 | + except Exception as e: |
| 175 | + console.print(f"[bold red]Error creating config file:[/] {str(e)}") |
| 176 | + return |
| 177 | + |
| 178 | + # Open in default editor if requested |
| 179 | + if should_edit and config_exists: |
| 180 | + try: |
| 181 | + console.print("[bold green]Opening config file in your default editor...[/]") |
| 182 | + |
| 183 | + # Use appropriate command based on platform |
| 184 | + if os.name == 'nt': # Windows |
| 185 | + os.startfile(config_path) |
| 186 | + elif os.name == 'posix': # macOS and Linux |
| 187 | + subprocess.run(['open', config_path] if os.uname().sysname == 'Darwin' else ['xdg-open', config_path]) |
| 188 | + |
| 189 | + console.print(f"[italic]After editing, {client_name} must be restarted for changes to take effect.[/]") |
| 190 | + except Exception as e: |
| 191 | + console.print(f"[bold red]Error opening editor:[/] {str(e)}") |
| 192 | + console.print(f"You can manually edit the file at: {config_path}") |
0 commit comments