2424from mcpm .profile .profile_config import ProfileConfigManager
2525from mcpm .schemas .server_config import ServerConfig
2626from mcpm .utils .config import (
27- DEFAULT_HOST ,
28- DEFAULT_PORT ,
29- DEFAULT_SHARE_ADDRESS ,
3027 PROMPT_SPLITOR ,
3128 RESOURCE_SPLITOR ,
3229 RESOURCE_TEMPLATE_SPLITOR ,
3532)
3633
3734from .client_connection import ServerConnection
35+ from .router_config import RouterConfig
3836from .transport import RouterSseTransport
3937from .watcher import ConfigWatcher
4038
@@ -49,15 +47,16 @@ class MCPRouter:
4947 Example:
5048 ```python
5149 # Initialize with a custom API key
52- router = MCPRouter(api_key="your-api-key")
50+ router = MCPRouter(router_config=RouterConfig( api_key="your-api-key") )
5351
5452 # Initialize with custom router configuration
55- router_config = {
56- "host": "localhost",
57- "port": 8080,
58- "share_address": "custom.share.address:8080"
59- }
60- router = MCPRouter(api_key="your-api-key", router_config=router_config)
53+ router_config = RouterConfig(
54+ host="localhost",
55+ port=8080,
56+ share_address="custom.share.address:8080",
57+ api_key="your-api-key"
58+ )
59+ router = MCPRouter(router_config=router_config)
6160
6261 # Create a global config from the router's configuration
6362 router.create_global_config()
@@ -68,18 +67,13 @@ def __init__(
6867 self ,
6968 reload_server : bool = False ,
7069 profile_path : str | None = None ,
71- strict : bool = False ,
72- api_key : str | None = None ,
73- router_config : dict | None = None ,
70+ router_config : RouterConfig | None = None ,
7471 ) -> None :
7572 """
7673 Initialize the router.
7774
7875 :param reload_server: Whether to reload the server when the config changes
7976 :param profile_path: Path to the profile file
80- :param strict: Whether to use strict mode for duplicated tool name.
81- If True, raise error when duplicated tool name is found else auto resolve by adding server name prefix
82- :param api_key: Optional API key to use for authentication.
8377 :param router_config: Optional router configuration to use instead of the global config
8478 """
8579 self .server_sessions : t .Dict [str , ServerConnection ] = {}
@@ -94,27 +88,27 @@ def __init__(
9488 self .watcher : Optional [ConfigWatcher ] = None
9589 if reload_server :
9690 self .watcher = ConfigWatcher (self .profile_manager .profile_path )
97- self .strict : bool = strict
98- self .api_key = api_key
99- self .router_config = router_config
91+ self .router_config = router_config if router_config is not None else RouterConfig ()
10092
10193 def create_global_config (self ) -> None :
10294 """
10395 Create a global configuration from the router's configuration.
10496 This is useful if you want to initialize the router with a config
10597 but also want that config to be available globally.
10698 """
107- if self .api_key is not None :
108- config_manager = ConfigManager ()
109- # Save the API key to the global config
110- config_manager .save_share_config (api_key = self .api_key )
111-
112- # If router_config is provided, save it to the global config
113- if self .router_config is not None :
114- host = self .router_config .get ("host" , DEFAULT_HOST )
115- port = self .router_config .get ("port" , DEFAULT_PORT )
116- share_address = self .router_config .get ("share_address" , DEFAULT_SHARE_ADDRESS )
117- config_manager .save_router_config (host , port , share_address )
99+ # Skip if router_config is None or there's no explicit api_key set
100+ if self .router_config is None or self .router_config .api_key is None :
101+ return
102+
103+ config_manager = ConfigManager ()
104+
105+ # Save the API key to the global config
106+ config_manager .save_share_config (api_key = self .router_config .api_key )
107+
108+ # Save router configuration to the global config
109+ config_manager .save_router_config (
110+ self .router_config .host , self .router_config .port , self .router_config .share_address
111+ )
118112
119113 def get_unique_servers (self ) -> list [ServerConfig ]:
120114 profiles = self .profile_manager .list_profiles ()
@@ -191,7 +185,7 @@ async def add_server(self, server_id: str, server_config: ServerConfig) -> None:
191185 # To make sure tool name is unique across all servers
192186 tool_name = tool .name
193187 if tool_name in self .capabilities_to_server_id ["tools" ]:
194- if self .strict :
188+ if self .router_config . strict :
195189 raise ValueError (
196190 f"Tool { tool_name } already exists. Please use unique tool names across all servers."
197191 )
@@ -210,7 +204,7 @@ async def add_server(self, server_id: str, server_config: ServerConfig) -> None:
210204 # To make sure prompt name is unique across all servers
211205 prompt_name = prompt .name
212206 if prompt_name in self .capabilities_to_server_id ["prompts" ]:
213- if self .strict :
207+ if self .router_config . strict :
214208 raise ValueError (
215209 f"Prompt { prompt_name } already exists. Please use unique prompt names across all servers."
216210 )
@@ -229,7 +223,7 @@ async def add_server(self, server_id: str, server_config: ServerConfig) -> None:
229223 # To make sure resource URI is unique across all servers
230224 resource_uri = resource .uri
231225 if str (resource_uri ) in self .capabilities_to_server_id ["resources" ]:
232- if self .strict :
226+ if self .router_config . strict :
233227 raise ValueError (
234228 f"Resource { resource_uri } already exists. Please use unique resource URIs across all servers."
235229 )
@@ -256,7 +250,7 @@ async def add_server(self, server_id: str, server_config: ServerConfig) -> None:
256250 # To make sure resource template URI is unique across all servers
257251 resource_template_uri_template = resource_template .uriTemplate
258252 if resource_template_uri_template in self .capabilities_to_server_id ["resource_templates" ]:
259- if self .strict :
253+ if self .router_config . strict :
260254 raise ValueError (
261255 f"Resource template { resource_template_uri_template } already exists. Please use unique resource template URIs across all servers."
262256 )
@@ -564,7 +558,8 @@ async def get_sse_server_app(
564558 await self .initialize_router ()
565559
566560 # Pass the API key to the RouterSseTransport
567- sse = RouterSseTransport ("/messages/" , api_key = self .api_key )
561+ api_key = None if self .router_config is None else self .router_config .api_key
562+ sse = RouterSseTransport ("/messages/" , api_key = api_key )
568563
569564 async def handle_sse (request : Request ) -> None :
570565 async with sse .connect_sse (
0 commit comments