Skip to content

Commit 61a4b66

Browse files
committed
Create client_manager
1 parent 2ff47a4 commit 61a4b66

File tree

3 files changed

+130
-62
lines changed

3 files changed

+130
-62
lines changed

src/mcpm/commands/add.py

Lines changed: 17 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -12,53 +12,36 @@
1212
from rich.prompt import Confirm
1313

1414
from mcpm.utils.repository import RepositoryManager
15-
from mcpm.clients.windsurf import WindsurfManager
16-
from mcpm.clients.claude_desktop import ClaudeDesktopManager
17-
from mcpm.clients.cursor import CursorManager
1815
from mcpm.utils.server_config import ServerConfig
19-
from mcpm.utils.config import ConfigManager
16+
from mcpm.utils.client_manager import get_active_client, get_active_client_manager, get_active_client_info
2017

2118
console = Console()
2219
repo_manager = RepositoryManager()
23-
config_manager = ConfigManager()
24-
25-
# Map of client names to their manager classes
26-
CLIENT_MANAGERS = {
27-
"windsurf": WindsurfManager,
28-
"claude-desktop": ClaudeDesktopManager,
29-
"cursor": CursorManager
30-
}
3120

3221
@click.command()
3322
@click.argument("server_name")
34-
@click.option("--client", "-c", help="Client to add the server to (windsurf, claude-desktop, cursor)")
3523
@click.option("--force", is_flag=True, help="Force reinstall if server is already installed")
36-
def add(server_name, client=None, force=False):
24+
def add(server_name, force=False):
3725
"""Add an MCP server to a client configuration.
3826
3927
Examples:
4028
mcpm add time
41-
mcpm add github --client windsurf
4229
mcpm add everything --force
4330
"""
44-
# If no client is specified, use the active client
31+
# Get the active client info
32+
client = get_active_client()
4533
if not client:
46-
client = config_manager.get_active_client()
47-
if not client:
48-
console.print("[bold red]Error:[/] No active client found.")
49-
console.print("Please specify a client with --client option or set an active client with 'mcpm client set <client>'.")
50-
return
51-
console.print(f"[yellow]Using active client: {client}[/]")
34+
console.print("[bold red]Error:[/] No active client found.")
35+
console.print("Please set an active client with 'mcpm client set <client>'.")
36+
return
37+
console.print(f"[yellow]Using active client: {client}[/]")
5238

53-
# Verify client is valid
54-
if client not in CLIENT_MANAGERS:
39+
# Get client manager
40+
client_manager = get_active_client_manager()
41+
if client_manager is None:
5542
console.print(f"[bold red]Error:[/] Unsupported client '{client}'.")
56-
console.print(f"Supported clients: {', '.join(CLIENT_MANAGERS.keys())}")
5743
return
5844

59-
# Initialize client manager
60-
client_manager = CLIENT_MANAGERS[client]()
61-
6245
# Check if server already exists in client config
6346
existing_server = client_manager.get_server(server_name)
6447
if existing_server and not force:
@@ -87,8 +70,11 @@ def add(server_name, client=None, force=False):
8770
author_url = author_info.get("url", "")
8871
console.print(f"[dim]Author: {author_name} {author_url}[/]")
8972

73+
# Get client display name from the utility
74+
_, client_display_name, _ = get_active_client_info()
75+
9076
# Confirm addition
91-
if not force and not Confirm.ask(f"Add this server to {client}?"):
77+
if not force and not Confirm.ask(f"Add this server to {client_display_name}?"):
9278
console.print("[yellow]Operation cancelled.[/]")
9379
return
9480

@@ -343,9 +329,8 @@ def add(server_name, client=None, force=False):
343329
success = client_manager.add_server(server_config)
344330

345331
if success:
346-
# Update the central tracking of enabled servers for this client
347-
config_manager.enable_server_for_client(server_name, client)
348-
console.print(f"[bold green]Successfully added {display_name} v{version} to {client}![/]")
332+
# Server has been successfully added to the client configuration
333+
console.print(f"[bold green]Successfully added {display_name} v{version} to {client_display_name}![/]")
349334

350335
# Display usage examples if available
351336
examples = server_metadata.get("examples", [])

src/mcpm/commands/edit.py

Lines changed: 7 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,9 @@
1010
from rich.panel import Panel
1111
from rich.prompt import Confirm
1212

13-
from mcpm.clients.claude_desktop import ClaudeDesktopManager
14-
from mcpm.clients.windsurf import WindsurfManager
15-
from mcpm.clients.cursor import CursorManager
16-
from mcpm.utils.config import ConfigManager
13+
from mcpm.utils.client_manager import get_active_client_info
1714

1815
console = Console()
19-
config_manager = ConfigManager()
20-
claude_manager = ClaudeDesktopManager()
21-
windsurf_manager = WindsurfManager()
22-
cursor_manager = CursorManager()
2316

2417
@click.command()
2518
@click.option("--edit", is_flag=True, help="Open the active client's config in default editor")
@@ -35,37 +28,21 @@ def edit(edit, create):
3528
mcpm edit --edit # Open the config file in your default editor
3629
mcpm edit --create # Create a basic config file if it doesn't exist
3730
"""
38-
# Get the active client and its corresponding manager
39-
active_client = config_manager.get_active_client()
31+
# Get the active client manager and related information
32+
client_manager, client_name, install_url = get_active_client_info()
4033

41-
# Select appropriate client manager based on active client
42-
if active_client == "claude-desktop":
43-
client_manager = claude_manager
44-
client_name = "Claude Desktop"
45-
install_url = "https://claude.ai/download"
46-
is_installed_method = client_manager.is_claude_desktop_installed
47-
elif active_client == "windsurf":
48-
client_manager = windsurf_manager
49-
client_name = "Windsurf"
50-
install_url = "https://codeium.com/windsurf/download"
51-
is_installed_method = client_manager.is_windsurf_installed
52-
elif active_client == "cursor":
53-
client_manager = cursor_manager
54-
client_name = "Cursor"
55-
install_url = "https://cursor.sh/download"
56-
is_installed_method = client_manager.is_cursor_installed
57-
else:
58-
console.print(f"[bold red]Error:[/] Unsupported active client: {active_client}")
34+
# Check if client is supported
35+
if client_manager is None:
36+
console.print("[bold red]Error:[/] Unsupported active client")
5937
console.print("Please switch to a supported client using 'mcpm client <client-name>'")
6038
return
6139

6240
# Get the client config file path
6341
config_path = client_manager.config_path
6442

6543
# Check if the client is installed
66-
if not is_installed_method():
44+
if not client_manager.is_client_installed():
6745
console.print(f"[bold red]Error:[/] {client_name} installation not detected.")
68-
console.print(f"Please download and install {client_name} from {install_url}")
6946
return
7047

7148
# Check if config file exists

src/mcpm/utils/client_manager.py

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
"""
2+
Client manager utilities for MCPM
3+
"""
4+
5+
from typing import Optional, Tuple
6+
7+
from mcpm.clients.claude_desktop import ClaudeDesktopManager
8+
from mcpm.clients.windsurf import WindsurfManager
9+
from mcpm.clients.cursor import CursorManager
10+
from mcpm.clients.base import BaseClientManager
11+
from mcpm.utils.config import ConfigManager
12+
13+
# Create client manager instances
14+
claude_manager = ClaudeDesktopManager()
15+
windsurf_manager = WindsurfManager()
16+
cursor_manager = CursorManager()
17+
18+
# Create config manager instance
19+
config_manager = ConfigManager()
20+
21+
# Client manager mapping
22+
CLIENT_MANAGERS = {
23+
"claude-desktop": claude_manager,
24+
"windsurf": windsurf_manager,
25+
"cursor": cursor_manager
26+
}
27+
28+
# Client display names
29+
CLIENT_DISPLAY_NAMES = {
30+
"claude-desktop": "Claude Desktop",
31+
"windsurf": "Windsurf",
32+
"cursor": "Cursor"
33+
}
34+
35+
# Client download URLs
36+
CLIENT_URLS = {
37+
"claude-desktop": "https://claude.ai/download",
38+
"windsurf": "https://codeium.com/windsurf/download",
39+
"cursor": "https://cursor.sh/download"
40+
}
41+
42+
def get_client_manager(client_name: str) -> Optional[BaseClientManager]:
43+
"""
44+
Get the client manager for a given client name
45+
46+
Args:
47+
client_name: Name of the client
48+
49+
Returns:
50+
BaseClientManager: Client manager instance or None if not found
51+
"""
52+
return CLIENT_MANAGERS.get(client_name)
53+
54+
def get_client_info(client_name: str) -> Tuple[Optional[BaseClientManager], str, str]:
55+
"""
56+
Get client manager and related information
57+
58+
Args:
59+
client_name: Name of the client
60+
61+
Returns:
62+
Tuple containing:
63+
- BaseClientManager: Client manager instance or None if not found
64+
- str: Display name of the client
65+
- str: Download URL for the client
66+
"""
67+
client_manager = get_client_manager(client_name)
68+
display_name = CLIENT_DISPLAY_NAMES.get(client_name, client_name)
69+
download_url = CLIENT_URLS.get(client_name, "")
70+
71+
return client_manager, display_name, download_url
72+
73+
74+
def get_active_client() -> str:
75+
"""
76+
Get the active client name from the config manager
77+
78+
Returns:
79+
str: Name of the active client
80+
"""
81+
return config_manager.get_active_client()
82+
83+
84+
def get_active_client_manager() -> Optional[BaseClientManager]:
85+
"""
86+
Get the client manager for the active client
87+
88+
Returns:
89+
BaseClientManager: Client manager instance for the active client, or None if not found
90+
"""
91+
active_client = get_active_client()
92+
return get_client_manager(active_client)
93+
94+
95+
def get_active_client_info() -> Tuple[Optional[BaseClientManager], str, str]:
96+
"""
97+
Get the client manager and related information for the active client
98+
99+
Returns:
100+
Tuple containing:
101+
- BaseClientManager: Client manager instance or None if not found
102+
- str: Display name of the client
103+
- str: Download URL for the client
104+
"""
105+
active_client = get_active_client()
106+
return get_client_info(active_client)

0 commit comments

Comments
 (0)