1414)
1515from microsoft_agents_a365 .tooling .utils .constants import Constants
1616
17+ from microsoft_agents_a365 .tooling .utils .utility import (
18+ get_mcp_platform_authentication_scope ,
19+ get_use_environment_id ,
20+ )
21+
1722
1823class McpToolRegistrationService :
1924 """
@@ -43,9 +48,9 @@ async def add_tool_servers_to_agent(
4348 initial_tools : List [Any ],
4449 agentic_app_id : str ,
4550 environment_id : str ,
46- auth : Optional [Authorization ] = None ,
51+ auth : Authorization ,
52+ turn_context : TurnContext ,
4753 auth_token : Optional [str ] = None ,
48- turn_context : Optional [TurnContext ] = None ,
4954 ) -> Optional [ChatAgent ]:
5055 """
5156 Add MCP tool servers to a chat agent (mirrors .NET implementation).
@@ -56,14 +61,20 @@ async def add_tool_servers_to_agent(
5661 initial_tools: List of initial tools to add to the agent
5762 agentic_app_id: Agentic app identifier for the agent
5863 environment_id: Environment identifier for MCP server discovery
59- auth: Optional authorization context
64+ auth: Authorization context for token exchange
65+ turn_context: Turn context for the operation
6066 auth_token: Optional bearer token for authentication
61- turn_context: Optional turn context for the operation
6267
6368 Returns:
6469 ChatAgent instance with MCP tools registered, or None if creation failed
6570 """
6671 try :
72+ # Exchange token if not provided
73+ if not auth_token :
74+ scopes = get_mcp_platform_authentication_scope ()
75+ authToken = await auth .exchange_token (turn_context , scopes , "AGENTIC" )
76+ auth_token = authToken .token
77+
6778 self ._logger .info (
6879 f"Listing MCP tool servers for agent { agentic_app_id } in environment { environment_id } "
6980 )
@@ -96,7 +107,7 @@ async def add_tool_servers_to_agent(
96107 headers [Constants .Headers .AUTHORIZATION ] = (
97108 f"{ Constants .Headers .BEARER_PREFIX } { auth_token } "
98109 )
99- if environment_id :
110+ if get_use_environment_id () and environment_id :
100111 headers [Constants .Headers .ENVIRONMENT_ID ] = environment_id
101112
102113 server_name = getattr (config , "mcp_server_name" , "Unknown" )
0 commit comments