Skip to content

Commit 558d320

Browse files
feat: support alias for profile
1 parent 4dcb638 commit 558d320

File tree

4 files changed

+28
-20
lines changed

4 files changed

+28
-20
lines changed

src/mcpm/clients/base.py

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import os
99
import platform
1010
import re
11-
from typing import Any, Dict, List, Optional, Union
11+
from typing import Any, Dict, List, Optional, Union, override
1212

1313
from pydantic import TypeAdapter
1414
from ruamel.yaml import YAML
@@ -136,13 +136,14 @@ def is_client_installed(self) -> bool:
136136
pass
137137

138138
@abc.abstractmethod
139-
def activate_profile(self, profile_name: str, router_config: Dict[str, Any]) -> bool:
139+
def activate_profile(self, profile_name: str, router_config: Dict[str, Any], alias_name: str | None = None) -> bool:
140140
"""
141141
Activate a profile in the client config
142142
143143
Args:
144144
profile_name: Name of the profile
145145
router_config: Router configuration
146+
alias_name: Alias name for the router in client config
146147
147148
Returns:
148149
bool: Success or failure
@@ -406,11 +407,13 @@ def is_client_installed(self) -> bool:
406407
# Can be overridden by subclasses
407408
return os.path.isdir(os.path.dirname(self.config_path))
408409

409-
def activate_profile(self, profile_name: str, router_config: Dict[str, Any]) -> bool:
410+
def activate_profile(self, profile_name: str, router_config: Dict[str, Any], alias_name: str | None = None) -> bool:
410411
"""Activate a profile in the client config
411412
412413
Args:
413414
profile_name: Name of the profile
415+
router_config: Router configuration
416+
alias_name: Alias name for the router in client config
414417
415418
Returns:
416419
bool: Success or failure
@@ -419,11 +422,11 @@ def activate_profile(self, profile_name: str, router_config: Dict[str, Any]) ->
419422
port = router_config["port"]
420423
default_base_url = f"http://{host}:{port}/sse"
421424

422-
server_config = self._format_router_server(profile_name, default_base_url)
425+
server_config = self._format_router_server(profile_name, default_base_url, alias_name)
423426
return self.add_server(server_config)
424427

425-
def _format_router_server(self, profile_name, base_url) -> ServerConfig:
426-
return format_server_url(self.client_key, profile_name, base_url)
428+
def _format_router_server(self, profile_name, base_url, alias_name: str | None = None) -> ServerConfig:
429+
return format_server_url(self.client_key, profile_name, base_url, alias_name)
427430

428431
def deactivate_profile(self) -> bool:
429432
"""Deactivate a profile in the client config
@@ -673,7 +676,7 @@ def is_client_installed(self) -> bool:
673676
# Check if the config directory exists
674677
return os.path.isdir(os.path.dirname(self.config_path))
675678

676-
def activate_profile(self, profile_name: str, router_config: Dict[str, Any]) -> bool:
679+
def activate_profile(self, profile_name: str, router_config: Dict[str, Any], alias_name: str | None = None) -> bool:
677680
"""Activate a profile in the client config
678681
679682
Args:
@@ -686,12 +689,13 @@ def activate_profile(self, profile_name: str, router_config: Dict[str, Any]) ->
686689
port = router_config["port"]
687690
default_base_url = f"http://{host}:{port}/sse"
688691

689-
server_config = self._format_router_server(profile_name, default_base_url)
692+
server_config = self._format_router_server(profile_name, default_base_url, alias_name)
690693
return self.add_server(server_config)
691694

692-
def _format_router_server(self, profile_name, base_url) -> ServerConfig:
693-
return format_server_url(self.client_key, profile_name, base_url)
695+
def _format_router_server(self, profile_name, base_url, server_name: str | None = None) -> ServerConfig:
696+
return format_server_url(self.client_key, profile_name, base_url, server_name)
694697

698+
@override
695699
def deactivate_profile(self) -> bool:
696700
"""Deactivate a profile in the client config
697701

src/mcpm/commands/target_operations/add.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -523,11 +523,11 @@ def _replace_argument_variables(value: str, prev_value: str, variables: dict) ->
523523

524524

525525
def add_profile_to_client(profile_name: str, client: str, alias: str | None = None, force: bool = False):
526-
if not force and not Confirm.ask(f"Add this profile to {client}{' as ' + alias if alias else ''}?"):
526+
if not force and not Confirm.ask(f"Add this profile {profile_name} to {client}{' as ' + alias if alias else ''}?"):
527527
console.print("[yellow]Operation cancelled.[/]")
528528
return
529529

530-
success = client_add_profile(profile_name, client)
530+
success = client_add_profile(profile_name, client, alias)
531531
if success:
532532
console.print(f"[bold green]Successfully added profile {profile_name} to {client}![/]")
533533
else:

src/mcpm/commands/target_operations/common.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ def profile_get_server(profile: str, server: str) -> ServerConfig | None:
9494
return profile_manager.get_profile_server(profile, server)
9595

9696

97-
def client_add_profile(profile_name: str, client: str) -> bool:
97+
def client_add_profile(profile_name: str, client: str, alias_name: str | None = None) -> bool:
9898
client_manager = ClientRegistry.get_client_manager(client)
9999
if not client_manager:
100100
console.print(f"[bold red]Error:[/] Client '{client}' not found.")
@@ -104,5 +104,5 @@ def client_add_profile(profile_name: str, client: str) -> bool:
104104
console.print("[bold red]Error:[/] Router config not found.")
105105
return False
106106

107-
success = client_manager.activate_profile(profile_name, router_config)
107+
success = client_manager.activate_profile(profile_name, router_config, alias_name)
108108
return success

src/mcpm/utils/router_server.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,29 @@
22
from mcpm.core.schema import ServerConfig, SSEServerConfig, STDIOServerConfig
33

44

5-
def format_server_url(client: str, profile: str, router_url: str) -> ServerConfig:
5+
def format_server_url(client: str, profile: str, router_url: str, server_name: str | None = None) -> ServerConfig:
66
return SSEServerConfig(
7-
name=ROUTER_SERVER_NAME,
7+
name=server_name if server_name else ROUTER_SERVER_NAME,
88
url=f"{router_url}?/client={client}&profile={profile}",
99
)
1010

1111

12-
def format_server_url_with_proxy_param(client: str, profile: str, router_url: str) -> ServerConfig:
12+
def format_server_url_with_proxy_param(
13+
client: str, profile: str, router_url: str, server_name: str | None = None
14+
) -> ServerConfig:
1315
result = STDIOServerConfig(
14-
name=ROUTER_SERVER_NAME,
16+
name=server_name if server_name else ROUTER_SERVER_NAME,
1517
command="uvx",
1618
args=["mcp-proxy", f"{router_url}?/client={client}&profile={profile}"],
1719
)
1820
return result
1921

2022

21-
def format_server_url_with_proxy_headers(client: str, profile: str, router_url: str) -> ServerConfig:
23+
def format_server_url_with_proxy_headers(
24+
client: str, profile: str, router_url: str, server_name: str | None = None
25+
) -> ServerConfig:
2226
result = STDIOServerConfig(
23-
name=ROUTER_SERVER_NAME,
27+
name=server_name if server_name else ROUTER_SERVER_NAME,
2428
command="uvx",
2529
args=["mcp-proxy", router_url, "--headers", "profile", profile],
2630
)

0 commit comments

Comments
 (0)