|
| 1 | +"""Continue MCP client implementation.""" |
| 2 | + |
| 3 | +from pathlib import Path |
| 4 | + |
| 5 | +import yaml |
| 6 | + |
| 7 | +from .base import print_squared_frame |
| 8 | +from .base_client import MCPClient |
| 9 | + |
| 10 | + |
| 11 | +class ContinueMCPClient(MCPClient): |
| 12 | + """MCP client for Continue.dev.""" |
| 13 | + |
| 14 | + def __init__(self): |
| 15 | + super().__init__("continue") |
| 16 | + |
| 17 | + def _get_config_paths(self, scope: str): |
| 18 | + home = Path.home() |
| 19 | + if scope == "user": |
| 20 | + return [home / ".continue" / "config.yaml"] |
| 21 | + elif scope == "project": |
| 22 | + return [Path.cwd() / ".continue" / "config.yaml"] |
| 23 | + else: # all |
| 24 | + return [ |
| 25 | + home / ".continue" / "config.yaml", |
| 26 | + Path.cwd() / ".continue" / "config.yaml", |
| 27 | + ] |
| 28 | + |
| 29 | + def _get_config_locations(self, tool_name: str): |
| 30 | + # Used by list_servers() |
| 31 | + return self._get_config_paths("all") |
| 32 | + |
| 33 | + def _convert_server_config_to_client_format(self, server_config) -> dict: |
| 34 | + # Continue uses the standard MCP "mcpServers" format (stdio/http) |
| 35 | + return super()._convert_server_config_to_client_format(server_config) |
| 36 | + |
| 37 | + def _add_server_config_to_file( |
| 38 | + self, config_path, server_name: str, client_config: dict |
| 39 | + ) -> bool: |
| 40 | + config_path = Path(config_path) |
| 41 | + try: |
| 42 | + config = {} |
| 43 | + if config_path.exists(): |
| 44 | + with open(config_path, "r", encoding="utf-8") as f: |
| 45 | + loaded = yaml.safe_load(f) or {} |
| 46 | + if isinstance(loaded, dict): |
| 47 | + config = loaded |
| 48 | + |
| 49 | + if "mcpServers" not in config or not isinstance( |
| 50 | + config.get("mcpServers"), dict |
| 51 | + ): |
| 52 | + config["mcpServers"] = {} |
| 53 | + |
| 54 | + config["mcpServers"][server_name] = client_config |
| 55 | + |
| 56 | + config_path.parent.mkdir(parents=True, exist_ok=True) |
| 57 | + with open(config_path, "w", encoding="utf-8") as f: |
| 58 | + yaml.safe_dump(config, f, sort_keys=False) |
| 59 | + return True |
| 60 | + except Exception as e: |
| 61 | + print(f"Error adding server to Continue config {config_path}: {e}") |
| 62 | + return False |
| 63 | + |
| 64 | + def add_server(self, server_name: str, scope: str = "user") -> bool: |
| 65 | + server_config = self.get_server_config_from_registry(server_name) |
| 66 | + if not server_config: |
| 67 | + print_squared_frame( |
| 68 | + f"{self.tool_name.upper()} - {server_name.upper()}", |
| 69 | + f"Error: Server '{server_name}' not found in registry", |
| 70 | + ) |
| 71 | + return False |
| 72 | + |
| 73 | + client_config = self._convert_server_config_to_client_format(server_config) |
| 74 | + config_paths = self._get_config_paths(scope) |
| 75 | + for config_path in config_paths: |
| 76 | + if self._add_server_config_to_file(config_path, server_name, client_config): |
| 77 | + level = ( |
| 78 | + "user-level" if config_path == config_paths[0] else "project-level" |
| 79 | + ) |
| 80 | + print( |
| 81 | + f"✓ Successfully added {server_name} to {level} Continue configuration" |
| 82 | + ) |
| 83 | + return True |
| 84 | + |
| 85 | + print_squared_frame( |
| 86 | + f"{self.tool_name.upper()} - {server_name.upper()}", |
| 87 | + "Error: Failed to add server to any config file", |
| 88 | + ) |
| 89 | + return False |
| 90 | + |
| 91 | + def remove_server(self, server_name: str, scope: str = "user") -> bool: |
| 92 | + config_paths = self._get_config_paths(scope) |
| 93 | + removed_any = False |
| 94 | + |
| 95 | + for config_path in config_paths: |
| 96 | + config_path = Path(config_path) |
| 97 | + if not config_path.exists(): |
| 98 | + continue |
| 99 | + |
| 100 | + try: |
| 101 | + with open(config_path, "r", encoding="utf-8") as f: |
| 102 | + config = yaml.safe_load(f) or {} |
| 103 | + if not isinstance(config, dict): |
| 104 | + continue |
| 105 | + |
| 106 | + if "mcpServers" in config and isinstance(config["mcpServers"], dict): |
| 107 | + if server_name in config["mcpServers"]: |
| 108 | + del config["mcpServers"][server_name] |
| 109 | + with open(config_path, "w", encoding="utf-8") as f: |
| 110 | + yaml.safe_dump(config, f, sort_keys=False) |
| 111 | + removed_any = True |
| 112 | + break |
| 113 | + except Exception: |
| 114 | + continue |
| 115 | + |
| 116 | + if removed_any: |
| 117 | + print(f" Removed {server_name} from {self.tool_name} configuration") |
| 118 | + else: |
| 119 | + print(f" {server_name} not found in {self.tool_name} configuration") |
| 120 | + return removed_any |
| 121 | + |
| 122 | + def list_servers(self, scope: str = "all") -> bool: |
| 123 | + tool_configs = self.get_tool_config(self.tool_name) |
| 124 | + if not tool_configs: |
| 125 | + print(f"No MCP server configurations found for {self.tool_name}") |
| 126 | + return False |
| 127 | + |
| 128 | + config_locations = self._get_config_locations(self.tool_name) |
| 129 | + user_servers = {} |
| 130 | + project_servers = {} |
| 131 | + |
| 132 | + home = Path.home() |
| 133 | + cwd = Path.cwd() |
| 134 | + |
| 135 | + for config_path in config_locations: |
| 136 | + config_path = Path(config_path) |
| 137 | + if not config_path.exists(): |
| 138 | + continue |
| 139 | + try: |
| 140 | + with open(config_path, "r", encoding="utf-8") as f: |
| 141 | + config = yaml.safe_load(f) or {} |
| 142 | + if not isinstance(config, dict): |
| 143 | + continue |
| 144 | + servers = config.get("mcpServers") |
| 145 | + if not isinstance(servers, dict): |
| 146 | + continue |
| 147 | + |
| 148 | + if config_path == home / ".continue" / "config.yaml": |
| 149 | + user_servers.update(servers) |
| 150 | + elif config_path == cwd / ".continue" / "config.yaml": |
| 151 | + project_servers.update(servers) |
| 152 | + except Exception: |
| 153 | + continue |
| 154 | + |
| 155 | + content_lines = [] |
| 156 | + show_user = scope in ["all", "user"] |
| 157 | + show_project = scope in ["all", "project"] |
| 158 | + |
| 159 | + if show_user and user_servers: |
| 160 | + content_lines.append("User-level servers:") |
| 161 | + content_lines.extend( |
| 162 | + [f" {name}: {cfg}" for name, cfg in user_servers.items()] |
| 163 | + ) |
| 164 | + if show_project and project_servers: |
| 165 | + content_lines.append("") |
| 166 | + |
| 167 | + if show_project and project_servers: |
| 168 | + content_lines.append("Project-level servers:") |
| 169 | + content_lines.extend( |
| 170 | + [f" {name}: {cfg}" for name, cfg in project_servers.items()] |
| 171 | + ) |
| 172 | + |
| 173 | + if content_lines: |
| 174 | + print_squared_frame( |
| 175 | + f"{self.tool_name.upper()} MCP SERVERS", "\n".join(content_lines) |
| 176 | + ) |
| 177 | + else: |
| 178 | + print_squared_frame( |
| 179 | + f"{self.tool_name.upper()} MCP SERVERS", "No MCP servers configured" |
| 180 | + ) |
| 181 | + return True |
0 commit comments