Skip to content

Commit 0082a80

Browse files
fixed logger in open ai
1 parent 19917d7 commit 0082a80

File tree

1 file changed

+24
-4
lines changed

1 file changed

+24
-4
lines changed

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

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
1+
# Copyright (c) Microsoft Corporation.
2+
# Licensed under the MIT License.
3+
14
from typing import Dict, Optional
25
from dataclasses import dataclass
6+
import logging
37

48
from agents import Agent
59

@@ -32,8 +36,15 @@ class MCPServerInfo:
3236
class McpToolRegistrationService:
3337
"""Service for managing MCP tools and servers for an agent"""
3438

35-
def __init__(self):
36-
self.config_service = McpToolServerConfigurationService()
39+
def __init__(self, logger: Optional[logging.Logger] = None):
40+
"""
41+
Initialize the MCP Tool Registration Service for OpenAI.
42+
43+
Args:
44+
logger: Logger instance for logging operations.
45+
"""
46+
self._logger = logger or logging.getLogger(self.__class__.__name__)
47+
self.config_service = McpToolServerConfigurationService(logger=self._logger)
3748

3849
async def add_tool_servers_to_agent(
3950
self,
@@ -69,10 +80,13 @@ async def add_tool_servers_to_agent(
6980
# Get MCP server configurations from the configuration service
7081
# mcp_server_configs = []
7182
# TODO: radevika: Update once the common project is merged.
83+
self._logger.info(f"Listing MCP tool servers for agent {agent_user_id} in environment {environment_id}")
7284
mcp_server_configs = await self.config_service.list_tool_servers(
7385
agent_user_id=agent_user_id, environment_id=environment_id, auth_token=auth_token
7486
)
7587

88+
self._logger.info(f"Loaded {len(mcp_server_configs)} MCP server configurations")
89+
7690
# Convert MCP server configs to MCPServerInfo objects
7791
mcp_servers_info = []
7892
for server_config in mcp_server_configs:
@@ -132,16 +146,18 @@ async def add_tool_servers_to_agent(
132146
connected_servers.append(mcp_server)
133147

134148
existing_server_urls.append(si.url)
149+
self._logger.info(f"Successfully connected to MCP server '{si.name}' at {si.url}")
135150

136151
except Exception as e:
137152
# Log the error but continue with other servers
138-
print(f"Failed to connect to MCP server {si.name} at {si.url}: {e}")
153+
self._logger.warning(f"Failed to connect to MCP server {si.name} at {si.url}: {e}")
139154
continue
140155

141156
# If we have new servers, we need to recreate the agent
142157
# The OpenAI Agents SDK requires MCP servers to be set during agent creation
143158
if new_mcp_servers:
144159
try:
160+
self._logger.info(f"Recreating agent with {len(new_mcp_servers)} new MCP servers")
145161
all_mcp_servers = existing_mcp_servers + new_mcp_servers
146162

147163
# Recreate the agent with all MCP servers
@@ -168,14 +184,17 @@ async def add_tool_servers_to_agent(
168184
self._connected_servers = []
169185
self._connected_servers.extend(connected_servers)
170186

187+
self._logger.info(f"Agent recreated successfully with {len(all_mcp_servers)} total MCP servers")
171188
# Return the new agent (caller needs to replace the old one)
172189
return new_agent
173190

174191
except Exception as e:
175192
# Clean up connected servers if agent creation fails
193+
self._logger.error(f"Failed to recreate agent with new MCP servers: {e}")
176194
await self._cleanup_servers(connected_servers)
177195
raise e
178196

197+
self._logger.info("No new MCP servers to add to agent")
179198
return agent
180199

181200
async def _cleanup_servers(self, servers):
@@ -184,8 +203,9 @@ async def _cleanup_servers(self, servers):
184203
try:
185204
if hasattr(server, "cleanup"):
186205
await server.cleanup()
187-
except Exception:
206+
except Exception as e:
188207
# Log cleanup errors but don't raise them
208+
self._logger.debug(f"Error during server cleanup: {e}")
189209
pass
190210

191211
async def cleanup_all_servers(self):

0 commit comments

Comments
 (0)