Skip to content

Commit 3cf72c8

Browse files
committed
Update to optionally use environment id
1 parent 294d69a commit 3cf72c8

File tree

5 files changed

+42
-10
lines changed

5 files changed

+42
-10
lines changed

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
from azure.ai.agents.models import McpTool, ToolResources
1919
from microsoft_agents.hosting.core import Authorization, TurnContext
2020

21-
from ...common.utils.utility import get_ppapi_token_scope
21+
from ...common.utils.utility import get_ppapi_token_scope, get_use_environment_id
2222

2323
# Local imports
2424
from microsoft_kairo.tooling.common.services.mcp_tool_server_configuration_service import (
@@ -194,7 +194,8 @@ async def _get_mcp_tool_definitions_and_resources(
194194
mcp_tool.update_headers(Constants.Headers.AUTHORIZATION, header_value)
195195

196196
# Set environment ID header
197-
mcp_tool.update_headers(Constants.Headers.ENVIRONMENT_ID, environment_id)
197+
if get_use_environment_id() and environment_id:
198+
mcp_tool.update_headers(Constants.Headers.ENVIRONMENT_ID, environment_id)
198199

199200
# Add to collections
200201
tool_definitions.extend(mcp_tool.definitions)

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,10 @@
1313
McpToolServerConfigurationService,
1414
)
1515

16-
from microsoft_agents_a365.tooling.utils.utility import get_ppapi_token_scope
16+
from microsoft_agents_a365.tooling.utils.utility import (
17+
get_ppapi_token_scope,
18+
get_use_environment_id,
19+
)
1720

1821

1922
# TODO: This is not needed. Remove this.
@@ -114,7 +117,7 @@ async def add_tool_servers_to_agent(
114117
headers = si.headers or {}
115118
if auth_token:
116119
headers["Authorization"] = f"Bearer {auth_token}"
117-
if environment_id:
120+
if get_use_environment_id() and environment_id:
118121
headers["x-ms-environment-id"] = environment_id
119122

120123
# Create MCPServerStreamableHttpParams with proper configuration

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
)
2525
from ...common.models import MCPServerConfig
2626
from ...common.utils.constants import Constants
27-
from ...common.utils.utility import get_tools_mode, get_ppapi_token_scope
27+
from ...common.utils.utility import get_tools_mode, get_ppapi_token_scope, get_use_environment_id
2828

2929

3030
from semantic_kernel.connectors.mcp import MCPStreamableHttpPlugin
@@ -135,16 +135,20 @@ async def add_tool_servers_to_agent(
135135

136136
if tools_mode == "MockMCPServer":
137137
# Mock server does not require bearer auth, but still forward environment id if available.
138-
if environment_id:
138+
if get_use_environment_id() and environment_id:
139139
headers[Constants.Headers.ENVIRONMENT_ID] = environment_id
140140

141141
if mock_auth_header := os.getenv("MOCK_MCP_AUTHORIZATION"):
142142
headers[Constants.Headers.AUTHORIZATION] = mock_auth_header
143-
else:
143+
elif get_use_environment_id():
144144
headers = {
145145
Constants.Headers.AUTHORIZATION: f"{Constants.Headers.BEARER_PREFIX} {auth_token}",
146146
Constants.Headers.ENVIRONMENT_ID: environment_id,
147147
}
148+
else:
149+
headers = {
150+
Constants.Headers.AUTHORIZATION: f"{Constants.Headers.BEARER_PREFIX} {auth_token}",
151+
}
148152

149153
plugin = MCPStreamableHttpPlugin(
150154
name=server.mcp_server_name,

libraries/microsoft-agents-a365-tooling/microsoft_agents_a365/tooling/services/mcp_tool_server_configuration_service.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,11 @@
3030
# Local imports
3131
from ..models import MCPServerConfig
3232
from ..utils import Constants
33-
from ..utils.utility import get_tooling_gateway_for_digital_worker, build_mcp_server_url
33+
from ..utils.utility import (
34+
get_tooling_gateway_for_digital_worker,
35+
build_mcp_server_url,
36+
get_use_environment_id,
37+
)
3438

3539

3640
# ==============================================================================
@@ -346,9 +350,13 @@ def _prepare_gateway_headers(self, auth_token: str, environment_id: str) -> Dict
346350
Returns:
347351
Dictionary of HTTP headers.
348352
"""
353+
if get_use_environment_id():
354+
return {
355+
"Authorization": f"{Constants.Headers.BEARER_PREFIX} {auth_token}",
356+
Constants.Headers.ENVIRONMENT_ID: environment_id,
357+
}
349358
return {
350359
"Authorization": f"{Constants.Headers.BEARER_PREFIX} {auth_token}",
351-
Constants.Headers.ENVIRONMENT_ID: environment_id,
352360
}
353361

354362
async def _parse_gateway_response(

libraries/microsoft-agents-a365-tooling/microsoft_agents_a365/tooling/utils/utility.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,9 @@ def get_mcp_base_url() -> str:
5050
if tools_mode == ToolsMode.MOCK_MCP_SERVER:
5151
return os.getenv("MOCK_MCP_SERVER_URL", "http://localhost:5309/mcp-mock/agents/servers")
5252

53+
if not get_use_environment_id():
54+
return f"{_get_mcp_platform_base_url()}/agents/servers"
55+
5356
return f"{_get_mcp_platform_base_url()}/mcp/environments"
5457

5558

@@ -67,7 +70,9 @@ def build_mcp_server_url(environment_id: str, server_name: str) -> str:
6770
base_url = get_mcp_base_url()
6871
environment = _get_current_environment().lower()
6972

70-
if environment == "development" and base_url.endswith("servers"):
73+
if not get_use_environment_id() or (
74+
environment == "development" and base_url.endswith("servers")
75+
):
7176
return f"{base_url}/{server_name}"
7277
else:
7378
return f"{base_url}/{environment_id}/servers/{server_name}"
@@ -96,6 +101,17 @@ def _get_mcp_platform_base_url() -> str:
96101
return MCP_PLATFORM_PROD_BASE_URL
97102

98103

104+
def get_use_environment_id() -> bool:
105+
"""
106+
Determines whether to use environment ID in MCP server URL construction.
107+
108+
Returns:
109+
bool: True if environment ID should be used, False otherwise.
110+
"""
111+
use_environment = os.getenv("USE_ENVIRONMENT_ID", "true").lower()
112+
return use_environment == "true"
113+
114+
99115
def get_tools_mode() -> ToolsMode:
100116
"""
101117
Gets the tools mode for the application.

0 commit comments

Comments
 (0)