可以通过将其连接到远程 模型上下文协议(MCP) 服务器上托管的工具(自带 MCP 服务器终结点)来扩展 Azure AI Foundry 代理的功能。
如何使用模型上下文协议工具
本部分介绍如何将 Azure Foundry (Azure AI)与托管模型上下文协议 (MCP) 服务器集成配合使用来创建 AI 代理。 该代理可以利用由 Azure Foundry 服务管理和执行的 MCP 工具,从而实现对外部资源的安全和受控访问。
主要功能
- 托管 MCP 服务器:MCP 服务器由 Azure AI Foundry 托管和管理,无需管理服务器基础结构
- 永久性代理:代理是创建和存储服务器端的,允许有状态会话
- 工具审批工作流:MCP 工具调用的可配置审批机制
工作原理
1. 环境设置
此示例需要两个环境变量:
-
AZURE_FOUNDRY_PROJECT_ENDPOINT:Azure AI Foundry 项目终结点 URL -
AZURE_FOUNDRY_PROJECT_MODEL_ID:模型部署名称(默认为“gpt-4.1-mini”)
var endpoint = Environment.GetEnvironmentVariable("AZURE_FOUNDRY_PROJECT_ENDPOINT")
?? throw new InvalidOperationException("AZURE_FOUNDRY_PROJECT_ENDPOINT is not set.");
var model = Environment.GetEnvironmentVariable("AZURE_FOUNDRY_PROJECT_MODEL_ID") ?? "gpt-4.1-mini";
2. 代理配置
代理配置了特定的说明和元数据:
const string AgentName = "MicrosoftLearnAgent";
const string AgentInstructions = "You answer questions by searching the Microsoft Learn content only.";
这会创建一个专用于使用 Microsoft Learn 文档回答问题的代理。
3. MCP 工具定义
此示例创建指向托管 MCP 服务器的 MCP 工具定义:
var mcpTool = new MCPToolDefinition(
serverLabel: "microsoft_learn",
serverUrl: "https://free.blessedness.top/api/mcp");
mcpTool.AllowedTools.Add("microsoft_docs_search");
关键组件:
- serverLabel:MCP 服务器实例的唯一标识符
- serverUrl:托管 MCP 服务器的 URL
- AllowedTools:指定代理可以使用的 MCP 服务器中的哪些工具
4. 持久代理创建
代理是使用 Azure AI Foundry Persistent Agents SDK 在服务器端创建的:
var persistentAgentsClient = new PersistentAgentsClient(endpoint, new AzureCliCredential());
var agentMetadata = await persistentAgentsClient.Administration.CreateAgentAsync(
model: model,
name: AgentName,
instructions: AgentInstructions,
tools: [mcpTool]);
这会创建一个持久性代理,该代理:
- 驻留在 Azure AI Foundry 服务上
- 有权访问指定的 MCP 工具
- 可以跨多个交互维护聊天状态
5. 代理检索和执行
创建的代理作为 AIAgent 实例进行检索:
AIAgent agent = await persistentAgentsClient.GetAIAgentAsync(agentMetadata.Value.Id);
6. 工具资源配置
此示例使用审批设置配置工具资源:
var runOptions = new ChatClientAgentRunOptions()
{
ChatOptions = new()
{
RawRepresentationFactory = (_) => new ThreadAndRunOptions()
{
ToolResources = new MCPToolResource(serverLabel: "microsoft_learn")
{
RequireApproval = new MCPApproval("never"),
}.ToToolResources()
}
}
};
密钥配置:
- MCPToolResource:将 MCP 服务器实例链接到代理执行
-
RequireApproval:控制何时需要用户批准工具调用
-
"never":工具在未经批准的情况下自动执行 -
"always":所有工具调用都需要用户批准 - 还可以配置自定义审批规则
-
7. 代理执行
使用配置 MCP 工具调用代理并执行问题:
AgentThread thread = agent.GetNewThread();
var response = await agent.RunAsync(
"Please summarize the Azure AI Agent documentation related to MCP Tool calling?",
thread,
runOptions);
Console.WriteLine(response);
8. 清理
此示例演示了适当的资源清理:
await persistentAgentsClient.Administration.DeleteAgentAsync(agent.Id);
Python Azure AI Foundry MCP 集成
Azure AI Foundry 通过 Python 代理框架与模型上下文协议 (MCP) 服务器无缝集成。 该服务管理 MCP 服务器托管和执行,消除基础结构管理,同时提供对外部工具的安全受控访问。
环境设置
通过环境变量配置 Azure AI Foundry 项目凭据:
import os
from azure.identity.aio import AzureCliCredential
from agent_framework.azure import AzureAIAgentClient
# Required environment variables
os.environ["AZURE_AI_PROJECT_ENDPOINT"] = "https://<your-project>.services.ai.azure.com/api/projects/<project-id>"
os.environ["AZURE_AI_MODEL_DEPLOYMENT_NAME"] = "gpt-4o-mini" # Optional, defaults to this
基本 MCP 集成
使用托管 MCP 工具创建 Azure AI Foundry 代理:
import asyncio
from agent_framework import HostedMCPTool
from agent_framework.azure import AzureAIAgentClient
from azure.identity.aio import AzureCliCredential
async def basic_foundry_mcp_example():
"""Basic example of Azure AI Foundry agent with hosted MCP tools."""
async with (
AzureCliCredential() as credential,
AzureAIAgentClient(async_credential=credential) as chat_client,
):
# Enable Azure AI observability (optional but recommended)
await chat_client.setup_azure_ai_observability()
# Create agent with hosted MCP tool
agent = chat_client.create_agent(
name="MicrosoftLearnAgent",
instructions="You answer questions by searching Microsoft Learn content only.",
tools=HostedMCPTool(
name="Microsoft Learn MCP",
url="https://free.blessedness.top/api/mcp",
),
)
# Simple query without approval workflow
result = await agent.run(
"Please summarize the Azure AI Agent documentation related to MCP tool calling?"
)
print(result)
if __name__ == "__main__":
asyncio.run(basic_foundry_mcp_example())
多工具 MCP 配置
将多个托管 MCP 工具与单个代理配合使用:
async def multi_tool_mcp_example():
"""Example using multiple hosted MCP tools."""
async with (
AzureCliCredential() as credential,
AzureAIAgentClient(async_credential=credential) as chat_client,
):
await chat_client.setup_azure_ai_observability()
# Create agent with multiple MCP tools
agent = chat_client.create_agent(
name="MultiToolAgent",
instructions="You can search documentation and access GitHub repositories.",
tools=[
HostedMCPTool(
name="Microsoft Learn MCP",
url="https://free.blessedness.top/api/mcp",
approval_mode="never_require", # Auto-approve documentation searches
),
HostedMCPTool(
name="GitHub MCP",
url="https://api.github.com/mcp",
approval_mode="always_require", # Require approval for GitHub operations
headers={"Authorization": "Bearer github-token"},
),
],
)
result = await agent.run(
"Find Azure documentation and also check the latest commits in microsoft/semantic-kernel"
)
print(result)
if __name__ == "__main__":
asyncio.run(multi_tool_mcp_example())
Python 代理框架提供与 Azure AI Foundry 托管的 MCP 功能的无缝集成,支持对外部工具的安全且可缩放的访问,同时保持生产应用程序所需的灵活性和控制。