Skip to content

Commit b120e99

Browse files
committed
code review
1 parent 1e554cb commit b120e99

File tree

1 file changed

+19
-2
lines changed
  • libraries/microsoft-agents-a365-tooling-extensions-agentframework/microsoft_agents_a365/tooling/extensions/agentframework/services

1 file changed

+19
-2
lines changed

libraries/microsoft-agents-a365-tooling-extensions-agentframework/microsoft_agents_a365/tooling/extensions/agentframework/services/mcp_tool_registration_service.py

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@
2525
)
2626

2727

28+
# Default timeout for MCP server HTTP requests (in seconds)
29+
MCP_HTTP_CLIENT_TIMEOUT_SECONDS = 60.0
30+
31+
2832
class McpToolRegistrationService:
2933
"""
3034
Provides MCP tool registration services for Agent Framework agents.
@@ -47,6 +51,7 @@ def __init__(self, logger: Optional[logging.Logger] = None):
4751
logger=self._logger
4852
)
4953
self._connected_servers = []
54+
self._http_clients: List[httpx.AsyncClient] = []
5055

5156
async def add_tool_servers_to_agent(
5257
self,
@@ -116,7 +121,10 @@ async def add_tool_servers_to_agent(
116121
)
117122

118123
# Create httpx client with auth headers configured
119-
http_client = httpx.AsyncClient(headers=headers, timeout=60.0)
124+
http_client = httpx.AsyncClient(
125+
headers=headers, timeout=MCP_HTTP_CLIENT_TIMEOUT_SECONDS
126+
)
127+
self._http_clients.append(http_client)
120128

121129
# Create and configure MCPStreamableHTTPTool with http_client
122130
mcp_tools = MCPStreamableHTTPTool(
@@ -337,12 +345,21 @@ async def send_chat_history_from_store(
337345
async def cleanup(self):
338346
"""Clean up any resources used by the service."""
339347
try:
348+
# Close MCP server connections
340349
for plugin in self._connected_servers:
341350
try:
342351
if hasattr(plugin, "close"):
343352
await plugin.close()
344353
except Exception as cleanup_ex:
345-
self._logger.debug(f"Error during cleanup: {cleanup_ex}")
354+
self._logger.debug(f"Error during plugin cleanup: {cleanup_ex}")
346355
self._connected_servers.clear()
356+
357+
# Close httpx clients to prevent connection/file descriptor leaks
358+
for http_client in self._http_clients:
359+
try:
360+
await http_client.aclose()
361+
except Exception as client_ex:
362+
self._logger.debug(f"Error closing http client: {client_ex}")
363+
self._http_clients.clear()
347364
except Exception as ex:
348365
self._logger.debug(f"Error during service cleanup: {ex}")

0 commit comments

Comments
 (0)