McpToolRegistrationService Class

Provides MCP tool registration services for Azure Foundry agents.

This service handles registration and management of MCP (Model Context Protocol) tool servers with Azure Foundry agents using the Azure AI SDK. It provides seamless integration between MCP servers and Azure Foundry's agent framework.

Features:

  • Automatic MCP server discovery and configuration
  • Azure identity integration with DefaultAzureCredential
  • Tool definitions and resources management
  • Support for both development (ToolingManifest.json) and production (gateway API) scenarios
  • Comprehensive error handling and logging

Constructor

McpToolRegistrationService()

Parameters

Name Description
logger
Default value: None
credential
Default value: None

Examples


>>> service = McpToolRegistrationService()
>>> service.add_tool_servers_to_agent(project_client, agent_id, token)

Methods

__init__

Initialize the MCP Tool Registration Service for Azure Foundry.

__new__
add_tool_servers_to_agent

Adds MCP tool servers to an Azure Foundry agent.

send_chat_history

Retrieve and send chat history from Azure AI Foundry to the MCP platform.

This method retrieves messages from the Azure AI Foundry Agents API using the provided client and thread ID, converts them to ChatHistoryMessage format, and sends them to the MCP platform.

send_chat_history_messages

Send Azure AI Foundry chat history messages to the MCP platform.

This method accepts a sequence of Azure AI Foundry ThreadMessage objects, converts them to ChatHistoryMessage format, and sends them to the MCP platform for real-time threat protection.

__init__

Initialize the MCP Tool Registration Service for Azure Foundry.

__init__(logger: Logger | None = None, credential: DefaultAzureCredential | None = None)

Parameters

Name Description
logger

Logger instance for logging operations.

Default value: None
credential
<xref:azure.identity._credentials.default.DefaultAzureCredential> | None

Azure credential for authentication. If None, DefaultAzureCredential will be used.

Default value: None

__new__

__new__(**kwargs)

add_tool_servers_to_agent

Adds MCP tool servers to an Azure Foundry agent.

async add_tool_servers_to_agent(project_client: AIProjectClient, auth: Authorization, auth_handler_name: str, context: TurnContext, auth_token: str | None = None) -> None

Parameters

Name Description
project_client
Required
<xref:azure.ai.projects._patch.AIProjectClient>

The Azure Foundry AIProjectClient instance.

auth
Required

Authorization handler for token exchange.

auth_handler_name
Required
str

Name of the authorization handler.

context
Required

Turn context for the current operation.

auth_token
str | None

Authentication token to access the MCP servers.

Default value: None

Returns

Type Description

Exceptions

Type Description

If project_client is None or required parameters are invalid.

If there's an error during MCP tool registration.

send_chat_history

Retrieve and send chat history from Azure AI Foundry to the MCP platform.

This method retrieves messages from the Azure AI Foundry Agents API using the provided client and thread ID, converts them to ChatHistoryMessage format, and sends them to the MCP platform.

async send_chat_history(agents_client: AgentsClient, thread_id: str, turn_context: TurnContext, tool_options: ToolOptions | None = None) -> OperationResult

Parameters

Name Description
agents_client
Required
<xref:azure.ai.agents._patch.AgentsClient>

The Azure AI Foundry AgentsClient instance.

thread_id
Required
str

The thread ID containing the messages to send.

turn_context
Required

TurnContext from the Agents SDK containing conversation info.

tool_options

Optional configuration for the request.

Default value: None

Returns

Type Description

OperationResult indicating success or failure.

Exceptions

Type Description

If agents_client, thread_id, or turn_context is None/empty.

Examples


>>> from azure.ai.agents import AgentsClient
>>> from azure.identity import DefaultAzureCredential
>>>
>>> client = AgentsClient(endpoint, credential=DefaultAzureCredential())
>>> service = McpToolRegistrationService()
>>> result = await service.send_chat_history(
...     client, thread_id, turn_context
... )

send_chat_history_messages

Send Azure AI Foundry chat history messages to the MCP platform.

This method accepts a sequence of Azure AI Foundry ThreadMessage objects, converts them to ChatHistoryMessage format, and sends them to the MCP platform for real-time threat protection.

async send_chat_history_messages(turn_context: TurnContext, messages: Sequence[ThreadMessage], tool_options: ToolOptions | None = None) -> OperationResult

Parameters

Name Description
turn_context
Required

TurnContext from the Agents SDK containing conversation info.

messages
Required
Sequence[<xref:azure.ai.agents.models._patch.ThreadMessage>]

Sequence of Azure AI Foundry ThreadMessage objects to send.

tool_options

Optional configuration for the request.

Default value: None

Returns

Type Description

OperationResult indicating success or failure.

Exceptions

Type Description

If turn_context or messages is None.

Examples


>>> service = McpToolRegistrationService()
>>> messages = await agents_client.messages.list(thread_id=thread_id)
>>> result = await service.send_chat_history_messages(
...     turn_context, list(messages)
... )
>>> if result.succeeded:
...     print("Chat history sent successfully")