本文說明有效使用 Azure MCP Server 的基本概念,包括如何操作多個 Azure 服務、處理錯誤、優化效能,以及在實際情境中套用常見模式。
多服務工作流程
Azure MCP 伺服器能在單一對話或工作流程中協調多個 Azure 服務間的操作。 此功能讓您能在 Azure Storage、Azure Cosmos DB 和 Azure Key Vault 等服務間串接操作,而無需切換上下文。
多服務操作的伺服器模式
Azure MCP 伺服器支援不同模式,影響工具的曝光方式及與多個服務的互動方式:
命名空間模式(預設)
命名空間模式按 Azure 服務群組工具,每個服務命名空間只暴露一個工具。 此模式為多服務工作流程提供平衡的方法。
{
"mcpServers": {
"Azure MCP Server": {
"command": "npx",
"args": ["-y", "@azure/mcp@latest", "server", "start"]
}
}
}
使用命名空間模式,你可以提出以下請求:
- 「列出我的儲存帳戶,然後給我看第一個帳戶裡的容器。」
- 「從我的密鑰庫取得秘密,並用它們連接我的宇宙資料庫」
整合模式(建議給 AI 代理人)
合併模式將相關作業整合成群組,進而形成專為 AI 智能代理優化的精選工具。 此模式在功能性與可用性之間取得最佳平衡。
{
"mcpServers": {
"Azure MCP Server": {
"command": "npx",
"args": ["-y", "@azure/mcp@latest", "server", "start", "--mode", "consolidated"]
}
}
}
整合工具以使用者意圖命名,例如 get_azure_databases_details,使其在自然語言互動中更直覺。
欲了解完整的伺服器模式及其配置清單,請參閱 故障排除指南。
全模式
此 all 模式會分別顯示 200+ 個獨立工具。 此模式適合需要細緻控制的情況,但某些客戶端應用程式可能會超出工具的限制。
{
"mcpServers": {
"Azure MCP Server": {
"command": "npx",
"args": ["-y", "@azure/mcp@latest", "server", "start", "--mode", "all"]
}
}
}
跨服務串接操作
你可以在一次對話中串連多個 Azure 服務的操作。 例如,您可以:
- 查詢 Azure Key Vault 以獲取資料庫憑證
- 利用這些憑證連接 Azure Cosmos 資料庫
- 來自 Cosmos DB 的查詢資料
- 將結果儲存在 Azure Storage
跨服務串連操作的範例提示包括:
- 「從密鑰保管庫 'my-vault' 取得資料庫連接字串,連接資料庫,並列出集合」
- 「列出我訂閱中的所有儲存帳號,然後對每個帳號展示容器」
- 建立一個新的儲存容器,並將我金鑰庫中的機密上傳到 Blob 儲存體中。
依服務篩選工具
你可以使用 --namespace 選項設定 Azure MCP 伺服器,僅公開特定服務。 此方法適用於精細化工作流程或多個 MCP 伺服器實例時。
{
"mcpServers": {
"Azure Storage": {
"command": "npx",
"args": ["-y", "@azure/mcp@latest", "server", "start", "--namespace", "storage"]
},
"Azure KeyVault": {
"command": "npx",
"args": ["-y", "@azure/mcp@latest", "server", "start", "--namespace", "keyvault"]
}
}
}
此配置會為儲存與金鑰庫建立獨立的 MCP 伺服器實例,讓您依據領域或專案需求組織工具。
錯誤處理與重試邏輯
Azure MCP 伺服器處理與 Azure 服務互動時可能發生的各種錯誤情境。 了解這些模式有助於你排除問題並建立韌性的工作流程。
驗證錯誤
在使用 Azure 資源時,認證是常見的錯誤來源。
401 未授權錯誤
401錯誤表示存取權杖無效或遺失。 當下列情況時,可能會發生此錯誤:
- 資源上的本地授權(存取金鑰)是被停用的
- 認證令牌已過期
- 目前沒有有效的憑證可供參考
解決方法:
- 確認您是否使用
az login或您偏好的認證方式來認證到 Azure。 - 確認資源是否允許你使用的認證方式
- 對於已停用存取金鑰的資源,請確保您擁有適當的角色式存取控制(RBAC)權限。
403 個禁止錯誤
403 錯誤表示已認證的使用者沒有足夠的權限存取所請求的資源。
常見原因:
- 資源群組或訂閱層級缺少 RBAC 權限
- 錯誤的訂閱或租戶情境
- 當多個帳號同時登入時,使用一個未預期的帳號
解決方法:
- 確認 RBAC 權限是否分配到正確的範圍
- 在提示中明確指定訂閱和租戶:「列出我在訂閱
subscription-name中,位於租戶tenant-name的所有儲存帳號」 - 將環境變數
AZURE_MCP_ONLY_USE_BROKER_CREDENTIAL設為true以提示選擇帳號
網路與防火牆限制
企業環境通常有網路控制,可能會影響 Azure MCP 伺服器的連線。
認證所需的端點:
login.microsoftonline.com:443login.windows.net:443management.azure.com:443graph.microsoft.com:443
資源專屬端點取決於你使用的 Azure 服務,例如:
- Azure 儲存:
*.blob.core.windows.net:443 - Azure Key Vault:
*.vault.azure.net:443 - Azure Cosmos DB:
*.documents.azure.com:443
如果你是用企業代理伺服器,請用環境變數設定代理設定:
export HTTP_PROXY=http://proxy.company.com:8080
export HTTPS_PROXY=http://proxy.company.com:8080
export NO_PROXY=localhost,127.0.0.1
暫時性失敗
Azure MCP 伺服器依賴 Azure SDK,這些 SDK 內建針對暫時性失敗的重試邏輯。 這些 SDK 會自動處理:
- 網路連線問題
- 服務限速與速率限制
- 暫時停駛
重試策略採用指數退避,以避免在高負載或事故情境下對服務造成過度負擔。
錯誤訊息與診斷
當錯誤發生時,Azure MCP 伺服器會提供詳細的錯誤訊息,幫助你理解並解決問題。 如需完整的故障排除指引,請參閱故障排除 指南。
最佳化提示
優化您的 Azure MCP 伺服器設定能提升效能、降低令牌使用量,並提升使用者體驗。
工具數量管理
有些 MCP 用戶端對可處理的工具數量有限制。 例如,Visual Studio Code Copilot 每個請求限制使用128個工具。
使用合併模式
整合模式提供完整功能,同時保持在客戶工具限制內:
{
"mcpServers": {
"Azure MCP": {
"command": "npx",
"args": ["-y", "@azure/mcp@latest", "server", "start", "--mode", "consolidated"]
}
}
}
此配置展現了精選工具,將相關作業歸類,優化功能性與 AI 代理效能。
使用選擇性工具載入
只載入你特定工作流程所需的工具:
{
"mcpServers": {
"Azure Essentials": {
"command": "npx",
"args": [
"-y",
"@azure/mcp@latest",
"server",
"start",
"--tool",
"azmcp_subscription_list",
"--tool",
"azmcp_group_list",
"--tool",
"azmcp_storage_account_get"
]
}
}
}
使用自訂聊天模式
Visual Studio Code 支援 自訂聊天模式 ,讓你能為不同情境設定不同的工具集。 這種方法讓你能根據當前任務在不同工具配置間切換,同時保持在客戶端限制內。
令牌管理
有效的令牌管理能改善回應時間並降低語言模型的成本。
使用明確的提示
具體提示可減少工具呼叫次數及所需上下文:
效果較差:
- 「告訴我關於我的Azure資源」
更有效:
- 列出訂閱 'my-subscription' 中資源群組 'my-rg' 的儲存帳號
- 「給我看儲存帳號『mystorageaccount』的連線字串。」
適當地規劃作業範圍
若可能,應將操作範圍涵蓋特定資源或資源群組,而非查詢整個訂閱:
效率較低:
- 「列出我訂閱中的所有資源,並篩選儲存帳號」
更有效率:
- 「將儲存帳號列入資源群組 'production-rg'」」
批次操作
Azure MCP 伺服器支援特定情境下的批次操作。 使用多種資源時:
順序操作:
List storage accounts, then for each one show the containers
適當時可進行平行運算:
Get details for storage accounts 'account1', 'account2', and 'account3'
Azure MCP 伺服器及底層 Azure SDK 會在適當時自動處理批次處理,優化網路呼叫並降低延遲。
快取與重用
Azure MCP 伺服器在會話中維持連線狀態。 為了優化效能:
- 保持 MCP 伺服器實例持續運作,而非頻繁啟動和停止
- 在後續查詢中重複使用訂閱與資源群組資訊
- 請在您的工作流程中快取資源詳細資訊,以便多次引用時使用。
常見使用案例
Azure MCP 伺服器支援多種實際情境。 本節提供常見模式與工作流程的範例。
部署新服務
在部署新的 Azure 服務時,您可以使用 Azure MCP 伺服器來自動化基礎架構的設定與設定:
建立資源小組與資源:
Create a resource group called 'webapp-prod' in East US, then create a storage account called 'webappdata' in that resource group配置資源:
Get the storage account connection string and store it as a secret in key vault 'webapp-kv' with name 'StorageConnectionString'驗證部署:
List all resources in resource group 'webapp-prod' and verify that the storage account and key vault exist
線上網站問題除錯
Azure MCP 伺服器能協助你診斷並排除生產環境問題:
檢查資源健康狀況:
Check the health status of App Service 'my-webapp' and list any recent service health incidents查詢日誌:
Query Application Insights for exceptions in the last hour from application 'my-webapp'檢查組態:
Get the application settings for App Service 'my-webapp' and check if the database connection string is configured correctly檢查相依性:
List the databases in SQL server 'my-sql-server' and verify connectivity to database 'my-database'
管理祕密與設定
一個常見的模式是跨環境管理秘密與設定:
審計祕訣:
List all secrets in key vault 'production-kv' and show me which ones are expiring in the next 30 days輪換憑證:
Generate a new connection string for storage account 'mystore', update the secret 'StorageConnection' in key vault 'production-kv', and restart App Service 'my-webapp'同步設定:
Get all key-value pairs from App Configuration 'my-appconfig' with label 'production' and compare them to the staging environment
跨服務的資料操作
Azure MCP 伺服器擅長協調多個服務間的資料作業:
提取、轉換、載入(ETL):
Get data from Cosmos DB container 'raw-data', transform it using the provided logic, and upload the results to blob container 'processed-data' in storage account 'analytics'備份與檔案:
Export all documents from Cosmos DB container 'transactions' and create a snapshot in blob storage with timestamp跨服務查詢:
Query Azure Data Explorer for events where error count is greater than 100, then lookup the associated user data from Cosmos DB
基礎設施稽核與合規
使用 Azure MCP 伺服器來稽核基礎設施並驗證合規性:
安全稽核:
List all storage accounts in my subscription and check which ones don't have firewall rules configured成本分析:
List all SQL databases in my subscription, show their pricing tiers, and identify candidates for downgrading資源標註:
List all resources in subscription 'production-sub' without an 'Environment' tag and show their resource groups
開發與測試工作流程
開發者可使用 Azure MCP 伺服器來簡化開發工作流程:
環境設置:
Create a development environment with a storage account, key vault, and App Service in resource group 'dev-environment'測試資料管理:
Copy data from production storage container 'data' to development storage container 'test-data', anonymizing sensitive fields組態管理:
Compare App Service settings between 'staging-webapp' and 'production-webapp' and highlight differences