Skip to content

Commit 7468ea0

Browse files
Copilotpontemonti
andcommitted
Add support for custom MCP server URLs
Co-authored-by: pontemonti <7850950+pontemonti@users.noreply.github.com>
1 parent d3fbcec commit 7468ea0

6 files changed

Lines changed: 61 additions & 13 deletions

File tree

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

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -97,9 +97,8 @@ async def add_tool_servers_to_agent(
9797
# Add servers as MCPStreamableHTTPTool instances
9898
for config in server_configs:
9999
try:
100-
server_url = getattr(config, "server_url", None) or getattr(
101-
config, "mcp_server_unique_name", None
102-
)
100+
# Use custom URL if provided, otherwise use the unique name
101+
server_url = config.url if config.url else config.mcp_server_unique_name
103102
if not server_url:
104103
self._logger.warning(f"MCP server config missing server_url: {config}")
105104
continue
@@ -115,7 +114,7 @@ async def add_tool_servers_to_agent(
115114
self._orchestrator_name
116115
)
117116

118-
server_name = getattr(config, "mcp_server_name", "Unknown")
117+
server_name = config.mcp_server_name
119118

120119
# Create and configure MCPStreamableHTTPTool
121120
mcp_tools = MCPStreamableHTTPTool(
@@ -134,7 +133,7 @@ async def add_tool_servers_to_agent(
134133
self._logger.info(f"Added MCP plugin '{server_name}' to agent tools")
135134

136135
except Exception as tool_ex:
137-
server_name = getattr(config, "mcp_server_name", "Unknown")
136+
server_name = config.mcp_server_name if hasattr(config, "mcp_server_name") else "Unknown"
138137
self._logger.warning(
139138
f"Failed to create MCP plugin for {server_name}: {tool_ex}"
140139
)

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,8 +178,11 @@ async def _get_mcp_tool_definitions_and_resources(
178178
else server.mcp_server_name
179179
)
180180

181+
# Use custom URL if provided, otherwise use the unique name
182+
server_url = server.url if server.url else server.mcp_server_unique_name
183+
181184
# Create MCP tool using Azure Foundry SDK
182-
mcp_tool = McpTool(server_label=server_label, server_url=server.mcp_server_unique_name)
185+
mcp_tool = McpTool(server_label=server_label, server_url=server_url)
183186

184187
# Configure the tool
185188
mcp_tool.set_approval_mode("never")

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,9 +101,13 @@ async def add_tool_servers_to_agent(
101101
# Convert MCP server configs to MCPServerInfo objects
102102
mcp_servers_info = []
103103
for server_config in mcp_server_configs:
104+
# Use custom URL if provided, otherwise use the unique name
105+
server_url = (
106+
server_config.url if server_config.url else server_config.mcp_server_unique_name
107+
)
104108
server_info = MCPServerInfo(
105109
name=server_config.mcp_server_name,
106-
url=server_config.mcp_server_unique_name,
110+
url=server_url,
107111
)
108112
mcp_servers_info.append(server_info)
109113

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,9 +125,12 @@ async def add_tool_servers_to_agent(
125125
self._orchestrator_name
126126
)
127127

128+
# Use custom URL if provided, otherwise use the unique name
129+
server_url = server.url if server.url else server.mcp_server_unique_name
130+
128131
plugin = MCPStreamableHttpPlugin(
129132
name=server.mcp_server_name,
130-
url=server.mcp_server_unique_name,
133+
url=server_url,
131134
headers=headers,
132135
)
133136

libraries/microsoft-agents-a365-tooling/microsoft_agents_a365/tooling/models/mcp_server_config.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
"""
66

77
from dataclasses import dataclass
8+
from typing import Optional
89

910

1011
@dataclass
@@ -19,6 +20,10 @@ class MCPServerConfig:
1920
#: Gets or sets the unique name of the MCP server.
2021
mcp_server_unique_name: str
2122

23+
#: Gets or sets the custom URL for the MCP server. If provided, this URL will be used
24+
#: instead of constructing the URL from the base URL and unique name.
25+
url: Optional[str] = None
26+
2227
def __post_init__(self):
2328
"""Validate the configuration after initialization."""
2429
if not self.mcp_server_name:

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

Lines changed: 39 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -398,10 +398,18 @@ def _parse_manifest_server_config(
398398
if not self._validate_server_strings(name, server_name):
399399
return None
400400

401-
# Construct full URL using environment utilities
402-
full_url = build_mcp_server_url(server_name)
403-
404-
return MCPServerConfig(mcp_server_name=name, mcp_server_unique_name=full_url)
401+
# Check if a custom URL is provided
402+
custom_url = self._extract_server_url(server_element)
403+
404+
# If custom URL is provided, use it directly; otherwise construct from base URL
405+
if custom_url and custom_url.strip():
406+
return MCPServerConfig(
407+
mcp_server_name=name, mcp_server_unique_name=server_name, url=custom_url
408+
)
409+
else:
410+
# Construct full URL using environment utilities
411+
full_url = build_mcp_server_url(server_name)
412+
return MCPServerConfig(mcp_server_name=name, mcp_server_unique_name=full_url)
405413

406414
except Exception:
407415
return None
@@ -425,7 +433,16 @@ def _parse_gateway_server_config(
425433
if not self._validate_server_strings(name, endpoint):
426434
return None
427435

428-
return MCPServerConfig(mcp_server_name=name, mcp_server_unique_name=endpoint)
436+
# Check if a custom URL is provided by the gateway
437+
custom_url = self._extract_server_url(server_element)
438+
439+
# If custom URL is provided, use it; otherwise use the endpoint as-is
440+
if custom_url and custom_url.strip():
441+
return MCPServerConfig(
442+
mcp_server_name=name, mcp_server_unique_name=endpoint, url=custom_url
443+
)
444+
else:
445+
return MCPServerConfig(mcp_server_name=name, mcp_server_unique_name=endpoint)
429446

430447
except Exception:
431448
return None
@@ -480,6 +497,23 @@ def _extract_server_unique_name(self, server_element: Dict[str, Any]) -> Optiona
480497
return server_element["mcpServerUniqueName"]
481498
return None
482499

500+
def _extract_server_url(self, server_element: Dict[str, Any]) -> Optional[str]:
501+
"""
502+
Extracts custom server URL from configuration element.
503+
504+
Args:
505+
server_element: Configuration dictionary.
506+
507+
Returns:
508+
Server URL string or None.
509+
"""
510+
# Check for 'mcpServerUrl' (manifest) or 'url' (gateway)
511+
if "mcpServerUrl" in server_element and isinstance(server_element["mcpServerUrl"], str):
512+
return server_element["mcpServerUrl"]
513+
if "url" in server_element and isinstance(server_element["url"], str):
514+
return server_element["url"]
515+
return None
516+
483517
def _validate_server_strings(self, name: Optional[str], unique_name: Optional[str]) -> bool:
484518
"""
485519
Validates that server name and unique name are valid strings.

0 commit comments

Comments
 (0)