Skip to content

Commit f66a5e9

Browse files
authored
Merge pull request #7 from IBM/fix-connection-close
Use async context manager for initializing gateway
2 parents 04b0c82 + ddfa4d7 commit f66a5e9

File tree

1 file changed

+17
-18
lines changed

1 file changed

+17
-18
lines changed

mcpgateway/services/gateway_service.py

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -523,7 +523,7 @@ async def subscribe_events(self) -> AsyncGenerator[Dict[str, Any], None]:
523523
finally:
524524
self._event_subscribers.remove(queue)
525525

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:
527527
"""Initialize connection to a gateway and retrieve its capabilities.
528528
529529
Args:
@@ -537,8 +537,10 @@ async def _initialize_gateway(self, url: str, authentication: Optional[Dict[str,
537537
GatewayConnectionError: If initialization fails.
538538
"""
539539
try:
540+
if authentication is None:
541+
authentication = {}
540542

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):
542544
"""
543545
Connect to an MCP server running with SSE transport
544546
@@ -549,25 +551,22 @@ async def connect_to_sse_server(server_url: str, authentication: Optional[Dict[s
549551
Returns:
550552
list, list: List of capabilities and tools
551553
"""
554+
if authentication is None:
555+
authentication = {}
552556
# Store the context managers so they stay alive
553557
decoded_auth = decode_auth(authentication)
554-
_streams_context = sse_client(url=server_url, headers=decoded_auth)
555-
streams = await _streams_context.__aenter__() # line 551
556558

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]
571570

572571
return capabilities, tools
573572

0 commit comments

Comments
 (0)