本项目是一个基于 Model Context Protocol (MCP) 的实践项目,即 Azure 故障排除器 MCP 服务器。它能让智能代理(如 Claude 或 Semantic Kernel)逐步诊断和修复 Azure 资源。简单来说,它能使您的 AI 应用与 Azure 的 Guided Troubleshooter 对话,实现常见故障排除工作流的自动化。
此项目实现了一个 MCP 服务器,将 Azure 的自帮助 API 暴露为工具集。一旦连接到支持 MCP 的主机(如 Claude Desktop 或 SK 应用),您可以让 AI 助手通过调用这些工具来解决问题。
要使用此项目,您需要以下内容:
在 packages.config 中添加以下 NuGet 包:
<package id="ModelContextProtocol" version="1.0.0" />
<package id="Microsoft.Extensions.Hosting" version="6.0.0" />
<package id="Azure.ResourceManager.SelfHelp" version="1.0.0" />
<package id="Azure.Identity" version="1.4.0" />
在终端中运行以下命令以安装这些包:
dotnet add package ModelContextProtocol --version 1.0.0
dotnet add package Microsoft.Extensions.Hosting --version 6.0.0
dotnet add package Azure.ResourceManager.SelfHelp --version 1.0.0
dotnet add package Azure.Identity --version 1.4.0
在 Program.cs 文件中,配置服务宿主:
using Microsoft.Extensions.Hosting;
using ModelContextProtocol;
public class Program
{
public static void Main(string[] args)
{
CreateHostBuilder(args).Build().Run();
}
private static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHost(webBuilder =>
webBuilder.UseStartup())
.UseMcpServer(new McpServerOptions
{
Input = System.Console.OpenStandardInput(),
Output = System.Console.OpenStandardOutput()
});
}
在 AzureTroubleshooterTools.cs 文件中,定义故障排除工具:
using Microsoft.Azure.ResourceManager.SelfHelp;
using ModelContextProtocol;
public static class AzureTroubleshooterTools
{
public static async Task<string> CreateTroubleshootSession()
{
// 初始化 Azure 客户端
var armClient = new ArmClient(new DefaultAzureCredential());
var troubleshooterResource = await armClient.GetSelfHelpAsync();
return $"故障排除会话已创建。可用工具:{string.Join(", ", troubleshooterResource.Tools)}";
}
public static async Task<string> GetTroubleshootStep(string sessionId)
{
// 获取故障排除步骤
var armClient = new ArmClient(new DefaultAzureCredential());
var troubleshooterResource = await armClient.GetSelfHelpAsync();
return $"步骤 ID:{sessionId},请按照以下提示操作:{troubleshooterResource.Tools.FirstOrDefault(t => t.Id == sessionId)?.Instructions}";
}
public static async Task<string> ResetTroubleshootSession()
{
var armClient = new ArmClient(new DefaultAzureCredential());
await armClient.ResetSelfHelpAsync();
return "故障排除会话已重置。请重新开始。";
}
}
在 appsettings.json 中,配置 MCP 主机以使用您的工具:
{
"Mcp": {
"Hosts": [
{
"Name": "AzureTroubleshooter",
"Type": "Process",
"Command": "dotnet",
"Arguments": ["run"],
"WorkingDirectory": "YourProjectPath"
}
]
}
}
在终端中运行以下命令以启动服务器:
dotnet run
现在,您可以使用 MCP 主机(如 Claude)调用这些工具来逐步解决问题。例如:
{
"tool": "AzureTroubleshooter.CreateTroubleshootSession"
}
{
"tool": "AzureTroubleshooter.GetTroubleshootStep",
"sessionId": "123"
}
{
"tool": "AzureTroubleshooter.ResetTroubleshootSession"
}
通过构建一个基于 MCP 的 Azure 自帮助服务器,您可以让 AI 应用以更智能、更高效的方式处理云资源的问题。这种方法不仅可以提高故障排除的效率,还可以扩展到其他 Microsoft API。
💡 使用建议
- 确保您的 Azure 账户有足够的权限来执行故障排除操作。
- 在生产环境中,建议使用适当的错误处理和日志记录机制。
- 可以根据需要扩展工具的功能,例如添加更多故障排除步骤或集成第三方服务。
希望这些步骤能帮助您成功构建一个基于 MCP 的 Azure 故障排除服务器!如果有任何问题,请随时提问。