こんにちは、MLBお兄さんこと松村です。
ニューヨーク・ヤンキースはトレードデッドラインで他球団からリリーバーとユーティリティプレイヤーを補強し、勝負の後半戦に臨んでいます。ですが最近は負けが混んでいるので、もう一踏ん張りしてほしいところです。
社内で利用するアプリケーションで Microsoft Learn の MCP サーバーを活用するため、 Azure Functions を MCP クライアントとして実装する方法を整理しました。
Model Context Protocol とは
Model Context Protocol (MCP) は、AI アプリケーションとデータソースやツールを標準化された方法で接続するためのプロトコルです。
Claude Desktop や Visual Studio Code をクライアントとして、MCP サーバーに接続して利用することができます。
そのなかでも Microsoft Learn MCP サーバーは、Microsoft Learn(公式ドキュメント)に対して検索を実行できる MCP サーバーとして提供されています。
私は VS Code の GitHub Copilot Chat を使って Microsoft Learn のドキュメントを検索するためによく利用しています。
Azure Functions での MCP クライアント実装
今回は C# で Azure Functions v4 を使用して、Microsoft Learn MCP サーバーに接続する MCP クライアントを実装しました。
実装したコードは私のリポジトリで公開していますので、参考にしてみてください。
Azure Functions は .NET 9 Isolated で検証しています。
C# で MCP サーバーを呼び出すには、こちらの MCP SDK の NuGet パッケージを利用します。
リポジトリにはサンプルコードも含まれているため、それを真似しながら実装することができます。
MCP サーバーの接続設定
MCP クライアントから MCP サーバーに接続する方法は2つあります。
1. stdio:標準入力と標準出力を使用する方法
2. HTTP with Server-Sent Events (SSE):HTTP 接続を使用する方法
Microsoft Learn MCP サーバーはリモートサーバーが公開されているため、HTTP with SSE の接続方式を使用します。
// 接続設定 SseClientTransport clientTransport = new( new SseClientTransportOptions { Name = "Microsoft Learn MCP Server", Endpoint = new Uri("https://learn.microsoft.com/api/mcp"), TransportMode = ModelContextProtocol.Client.HttpTransportMode.StreamableHttp }); // クライアントの生成 IMcpClient client = await ModelContextProtocol.Client.McpClientFactory.CreateAsync(clientTransport);
ツールの選択
MCP サーバーにある機能を「ツール」といいます。
アプリケーションで使用するツールを選択するには、ツールリストを取得して目的のツールを特定します。
ドキュメントを検索するツールは microsoft_docs_search という名前で提供されています。
IList<McpClientTool> tools = await client.ListToolsAsync(); McpClientTool tool = tools.FirstOrDefault(t => t.Name == "microsoft_docs_search"); if (tool is null) { return new BadRequestObjectResult("No tools found in MCP server."); }
ツールの実行
特定したツールを実行します。
ツールによってはパラメーターが必要な場合があるため、 Dictionary<string, object?> 型で指定します。
Microsoft Learn MCP サーバーでは query というパラメーターに、検索したいキーワードや文章を指定します。
CallToolResult result = await client.CallToolAsync( tool.Name, new Dictionary<string, object?> { ["query"] = "Please tell me about Azure Functions." });
結果の取得
CallToolResult メソッドの戻り値の Content プロパティに、ツールの実行結果が含まれています。
テキストコンテンツは TextContentBlock 型にキャストしたうえで、内容を参照できます。
Microsoft Learn MCP サーバーは JSON 形式で結果を返します。そのため、専用のクラスを定義してデシリアライズします。
[ { "title": "What is Azure Functions?", "content": "# What is Azure Functions?\nAzure Functions is a serverless solution that allows you to build robust apps while using less code, and with less infrastructure and lower costs. Instead of worrying about deploying and maintaining servers, you can use the cloud infrastructure to provide all the up-to-date resources needed to keep your applications running.\nYou focus on the code that matters most to you, in the most productive language for you, and Azure Functions handles the rest. For a list of supported languages, see [Supported languages in Azure Functions](https://learn.microsoft.com/en-us/azure/azure-functions/supported-languages).\n## Scenarios\nFunctions provides a comprehensive set of event-driven [triggers and bindings](https://learn.microsoft.com/en-us/azure/azure-functions/functions-triggers-bindings) that connect your functions to other services without having to write extra code.\nThe following list includes common integrated scenarios that use Functions.\n| If you want to... | then...| \n| --- | --- |\n| [Process file uploads](https://learn.microsoft.com/en-us/azure/azure-functions/functions-scenarios#process-file-uploads) | Run code when a file is uploaded or changed in blob storage. |\n| [Process data in real time](https://learn.microsoft.com/en-us/azure/azure-functions/functions-scenarios#real-time-stream-and-event-processing) | Capture and transform data from event and IoT source streams on the way to storage. |\n| [Run AI inference](https://learn.microsoft.com/en-us/azure/azure-functions/functions-scenarios#machine-learning-and-ai) | Pull text from a queue and present it to various AI services for analysis and classification. |\n| [Run scheduled task](https://learn.microsoft.com/en-us/azure/azure-functions/functions-scenarios#run-scheduled-tasks) | Execute data clean-up code on predefined timed intervals. |\n| [Build a scalable web API](https://learn.microsoft.com/en-us/azure/azure-functions/functions-scenarios#build-a-scalable-web-api) | Implement a set of REST endpoints for your web applications using HTTP triggers. |\n| [Build a serverless workflow](https://learn.microsoft.com/en-us/azure/azure-functions/functions-scenarios#build-a-serverless-workflow) | Create an event-driven workflow from a series of functions using Durable Functions. |\n| [Respond to database changes](https://learn.microsoft.com/en-us/azure/azure-functions/functions-scenarios#respond-to-database-changes) | Run custom logic when a document is created or updated in a database. |\n| [Create reliable message systems](https://learn.microsoft.com/en-us/azure/azure-functions/functions-scenarios#create-reliable-message-systems) | Process message queues using Azure Queue Storage, Service Bus, or Event Hubs. |\n\nThese scenarios allow you to build event-driven systems using modern architectural patterns. For more information, see [Azure Functions scenarios](https://learn.microsoft.com/en-us/azure/azure-functions/functions-scenarios).", "contentUrl": "https://learn.microsoft.com/en-us/azure/azure-functions/functions-overview" }, // 中略 ]
public class MSLearnContent { [JsonPropertyName("title")] public string? Title { get; set; } [JsonPropertyName("content")] public string? Content { get; set; } [JsonPropertyName("contentUrl")] public string? ContentUrl { get; set; } }
注意点
記事執筆時点では、Microsoft Learn MCP サーバーの microsoft_docs_search ツールには question というパラメーターに関する説明が含まれていますが、現在は非推奨となっています。
詳しくはこちらの Issue を参照してください。
まとめ
Microsoft Learn MCP サーバーは VS Code などの開発環境でも大いに活用できますが、アプリケーションに組み込むことで、社内ツールとしても利用できるメリットが大きいです。
ぜひ皆さんも MCP クライアントを作ってみましょう。
サービス一覧 www.alterbooth.com cloudpointer.tech www.alterbooth.com


