Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
from azure.ai.agents.models import McpTool, ToolResources
from microsoft_agents.hosting.core import Authorization, TurnContext

from ...common.utils.utility import get_ppapi_token_scope
from ...common.utils.utility import get_ppapi_token_scope, get_use_environment_id

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

# Set environment ID header
mcp_tool.update_headers(Constants.Headers.ENVIRONMENT_ID, environment_id)
if get_use_environment_id() and environment_id:
mcp_tool.update_headers(Constants.Headers.ENVIRONMENT_ID, environment_id)

# Add to collections
tool_definitions.extend(mcp_tool.definitions)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@
McpToolServerConfigurationService,
)

from microsoft_agents_a365.tooling.utils.utility import get_ppapi_token_scope
from microsoft_agents_a365.tooling.utils.utility import (
get_ppapi_token_scope,
get_use_environment_id,
)


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

# Create MCPServerStreamableHttpParams with proper configuration
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
)
from ...common.models import MCPServerConfig
from ...common.utils.constants import Constants
from ...common.utils.utility import get_tools_mode, get_ppapi_token_scope
from ...common.utils.utility import get_tools_mode, get_ppapi_token_scope, get_use_environment_id


from semantic_kernel.connectors.mcp import MCPStreamableHttpPlugin
Expand Down Expand Up @@ -135,16 +135,20 @@ async def add_tool_servers_to_agent(

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

if mock_auth_header := os.getenv("MOCK_MCP_AUTHORIZATION"):
headers[Constants.Headers.AUTHORIZATION] = mock_auth_header
else:
elif get_use_environment_id():
headers = {
Constants.Headers.AUTHORIZATION: f"{Constants.Headers.BEARER_PREFIX} {auth_token}",
Constants.Headers.ENVIRONMENT_ID: environment_id,
}
else:
headers = {
Constants.Headers.AUTHORIZATION: f"{Constants.Headers.BEARER_PREFIX} {auth_token}",
}

plugin = MCPStreamableHttpPlugin(
name=server.mcp_server_name,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,11 @@
# Local imports
from ..models import MCPServerConfig
from ..utils import Constants
from ..utils.utility import get_tooling_gateway_for_digital_worker, build_mcp_server_url
from ..utils.utility import (
get_tooling_gateway_for_digital_worker,
build_mcp_server_url,
get_use_environment_id,
)


# ==============================================================================
Expand Down Expand Up @@ -346,9 +350,13 @@ def _prepare_gateway_headers(self, auth_token: str, environment_id: str) -> Dict
Returns:
Dictionary of HTTP headers.
"""
if get_use_environment_id():
return {
"Authorization": f"{Constants.Headers.BEARER_PREFIX} {auth_token}",
Constants.Headers.ENVIRONMENT_ID: environment_id,
}
return {
"Authorization": f"{Constants.Headers.BEARER_PREFIX} {auth_token}",
Constants.Headers.ENVIRONMENT_ID: environment_id,
}

async def _parse_gateway_response(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ def get_mcp_base_url() -> str:
if tools_mode == ToolsMode.MOCK_MCP_SERVER:
return os.getenv("MOCK_MCP_SERVER_URL", "http://localhost:5309/mcp-mock/agents/servers")

if not get_use_environment_id():
return f"{_get_mcp_platform_base_url()}/agents/servers"

return f"{_get_mcp_platform_base_url()}/mcp/environments"


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

if environment == "development" and base_url.endswith("servers"):
if not get_use_environment_id() or (
environment == "development" and base_url.endswith("servers")
):
return f"{base_url}/{server_name}"
else:
return f"{base_url}/{environment_id}/servers/{server_name}"
Expand Down Expand Up @@ -96,6 +101,17 @@ def _get_mcp_platform_base_url() -> str:
return MCP_PLATFORM_PROD_BASE_URL


def get_use_environment_id() -> bool:
"""
Determines whether to use environment ID in MCP server URL construction.

Returns:
bool: True if environment ID should be used, False otherwise.
"""
use_environment = os.getenv("USE_ENVIRONMENT_ID", "true").lower()
return use_environment == "true"


def get_tools_mode() -> ToolsMode:
"""
Gets the tools mode for the application.
Expand Down
Loading