@@ -523,7 +523,7 @@ async def subscribe_events(self) -> AsyncGenerator[Dict[str, Any], None]:
523
523
finally :
524
524
self ._event_subscribers .remove (queue )
525
525
526
- async def _initialize_gateway (self , url : str , authentication : Optional [Dict [str , str ]] = {} ) -> Any :
526
+ async def _initialize_gateway (self , url : str , authentication : Optional [Dict [str , str ]] = None ) -> Any :
527
527
"""Initialize connection to a gateway and retrieve its capabilities.
528
528
529
529
Args:
@@ -537,8 +537,10 @@ async def _initialize_gateway(self, url: str, authentication: Optional[Dict[str,
537
537
GatewayConnectionError: If initialization fails.
538
538
"""
539
539
try :
540
+ if authentication is None :
541
+ authentication = {}
540
542
541
- async def connect_to_sse_server (server_url : str , authentication : Optional [Dict [str , str ]] = {} ):
543
+ async def connect_to_sse_server (server_url : str , authentication : Optional [Dict [str , str ]] = None ):
542
544
"""
543
545
Connect to an MCP server running with SSE transport
544
546
@@ -549,25 +551,22 @@ async def connect_to_sse_server(server_url: str, authentication: Optional[Dict[s
549
551
Returns:
550
552
list, list: List of capabilities and tools
551
553
"""
554
+ if authentication is None :
555
+ authentication = {}
552
556
# Store the context managers so they stay alive
553
557
decoded_auth = decode_auth (authentication )
554
- _streams_context = sse_client (url = server_url , headers = decoded_auth )
555
- streams = await _streams_context .__aenter__ () # line 551
556
558
557
- _session_context = ClientSession (* streams )
558
- session : ClientSession = await _session_context .__aenter__ () # line 554
559
-
560
- # Initialize
561
- response = await session .initialize ()
562
- capabilities = response .capabilities .model_dump (by_alias = True , exclude_none = True )
563
-
564
- response = await session .list_tools ()
565
- tools = response .tools
566
- tools = [tool .model_dump (by_alias = True , exclude_none = True ) for tool in tools ]
567
- tools = [ToolCreate .model_validate (tool ) for tool in tools ]
568
-
569
- await _session_context .__aexit__ (None , None , None )
570
- await _streams_context .__aexit__ (None , None , None ) # line 566
559
+ # Use async with for both sse_client and ClientSession
560
+ async with sse_client (url = server_url , headers = decoded_auth ) as streams :
561
+ async with ClientSession (* streams ) as session :
562
+ # Initialize the session
563
+ response = await session .initialize ()
564
+ capabilities = response .capabilities .model_dump (by_alias = True , exclude_none = True )
565
+
566
+ response = await session .list_tools ()
567
+ tools = response .tools
568
+ tools = [tool .model_dump (by_alias = True , exclude_none = True ) for tool in tools ]
569
+ tools = [ToolCreate .model_validate (tool ) for tool in tools ]
571
570
572
571
return capabilities , tools
573
572
0 commit comments