共用方式為


開始使用 Azure MCP 伺服器搭配 .NET

Azure MCP 伺服器會使用模型內容通訊協定 (MCP) 來標準化 AI 應用程式與外部工具和資料來源之間的整合,讓 AI 系統能夠執行內容感知 Azure 資源的作業。

在本文中,您將瞭解如何完成下列工作:

  • 安裝並驗證 Azure MCP 伺服器
  • 使用自訂 .NET 用戶端連線到 Azure MCP 伺服器
  • 執行提示以測試 Azure MCP 伺服器作業並管理 Azure 資源

先決條件

備註

您想要使用 Azure MCP 伺服器存取的 Azure 資源必須已存在於您的 Azure 訂用帳戶中。 此外,您的使用者帳戶必須具有為這些資源指派的必要 RBAC 角色和許可

登入本地開發

Azure MCP 伺服器會透過 Microsoft Entra ID 使用權杖型驗證來提供順暢的驗證體驗。 在內部,Azure MCP 伺服器會使用 DefaultAzureCredentialAzure 身分識別程式庫 來驗證使用者。

您必須使用 Azure 帳戶登入本機支援的工具 DefaultAzureCredential 之一,才能使用 Azure MCP 伺服器。 使用終端機視窗登入,例如 Visual Studio Code 終端機:

az login

成功登入上述其中一個工具之後,Azure MCP 伺服器可以自動探索您的認證,並使用它們來驗證和執行 Azure 服務上的作業。

備註

您也可以透過 Visual Studio 登入 Azure。 Azure MCP 伺服器只能執行登入使用者具有執行權限的作業。

建立 .NET 主機應用程式

完成下列步驟以建立 .NET 主控台應用程式。 應用程式會連線到 AI 模型,並作為連線到 Azure MCP 伺服器之 MCP 用戶端的主機。

建立專案

  1. 開啟終端機,前往您要建立專案的空白資料夾。

  2. 執行下列命令以建立新的 .NET 主控台應用程式:

    dotnet new console -n MCPHostApp
    
  3. 導覽至新建立的專案資料夾:

    cd MCPHostApp
    
  4. 在您選擇的編輯器中開啟專案資料夾,例如 Visual Studio Code:

    code .
    

新增相依性

  1. 在終端機中,執行下列命令來新增必要的 NuGet 套件:

    dotnet add package Azure.AI.OpenAI --prerelease
    dotnet add package Azure.Identity
    dotnet add package Microsoft.Extensions.AI --prerelease
    dotnet add package Microsoft.Extensions.AI.OpenAI --prerelease
    dotnet add package ModelContextProtocol --prerelease
    
  2. 檢查檔案,確認 MCPHostApp.csproj 已新增套件。

  3. 執行下列命令來建置專案,並確保一切設定正確:

    dotnet build
    

新增應用程式程式碼

以下列程式碼取代 Program.cs 的內容:

using Azure.AI.OpenAI;
using Azure.Identity;
using Microsoft.Extensions.AI;
using ModelContextProtocol.Client;

// Create an IChatClient
IChatClient client =
    new ChatClientBuilder(
        new AzureOpenAIClient(new Uri("<your-azure-openai-endpoint>"), 
        new DefaultAzureCredential())
        .GetChatClient("gpt-4o").AsIChatClient())
    .UseFunctionInvocation()
    .Build();

// Create the MCP client
var mcpClient = await McpClient.CreateAsync(
    new StdioClientTransport(new()
    {
        Command = "npx",
        Arguments = ["-y", "@azure/mcp@latest", "server", "start"],
        Name = "Azure MCP",
    }));

// Get all available tools from the MCP server
Console.WriteLine("Available tools:");
var tools = await mcpClient.ListToolsAsync();
foreach (var tool in tools)
{
    Console.WriteLine($"{tool}");
}
Console.WriteLine();

// Conversational loop that can utilize the tools
List<ChatMessage> messages = [];
while (true)
{
    Console.Write("Prompt: ");
    messages.Add(new(ChatRole.User, Console.ReadLine()));

    List<ChatResponseUpdate> updates = [];
    await foreach (var update in client
        .GetStreamingResponseAsync(messages, new() { Tools = [.. tools] }))
    {
        Console.Write(update);
        updates.Add(update);
    }
    Console.WriteLine();

    messages.AddMessages(updates);
}

上述程式碼會完成下列工作:

  • 使用程式庫初始IChatClientMicrosoft.Extensions.AI抽象概念。
  • 建立 MCP 用戶端,以使用標準 I/O 傳輸與 Azure MCP 伺服器互動。 提供的 npx 命令和對應的引數會下載並啟動 Azure MCP 伺服器。
  • 從 MCP 伺服器擷取並顯示可用工具的清單,這是標準 MCP 功能。
  • 實現一個對話循環,處理用戶提示並利用工具進行響應。

執行並測試應用程式

完成下列步驟來測試您的 .NET 主機應用程式:

  1. 在開啟專案根目錄的終端機視窗中,執行下列命令來啟動應用程式:

    dotnet run
    
  2. 應用程式執行之後,請輸入下列測試提示:

    List all of the resource groups in my subscription
    

    上一個提示的輸出應類似於以下文本:

    The following resource groups are available for your subscription:
    
    1. **DefaultResourceGroup-EUS** (Location: `eastus`)
    2. **rg-testing** (Location: `centralus`)
    3. **rg-azd** (Location: `eastus2`)
    4. **msdocs-sample** (Location: `southcentralus`)
    14. **ai-testing** (Location: `eastus2`)
    
    Let me know if you need further details or actions related to any of these resource groups!
    
  3. 使用其他相關提示探索和測試 Azure MCP 作業,例如:

    List all of the storage accounts in my subscription
    Get the available tables in my storage accounts
    

後續步驟