Our journey continues, as we keep finding new features to explore in the Microsoft.Extensions.AI
library. Today we have a look at the support for MCP.
This post is part of a blog series. Other posts so far:
- Part I – An introduction to Microsoft.Extensions.AI
- Part II – ASP.NET Core integration
- Part III –Tool calling
- Part IV – Telemetry integration
- Part V – Chat history
- Part VI – Structured output
- Part VII - MCP integration (this post)
What is MCP?
I know, I know, you should have been hiding under a rock if you have never heard about MCP before. But just in case; MCP (Model Context Protocol) is an open protocol developed by Anthropic that provides a standardized way to connect AI models to different data sources and tools.
This allows us to use tool calling without having to build our own plugins (as I demonstrated in Part III of this blog series).
Using MCP with Microsoft.Extensions.AI
The first thing you need is an MCP server. Today there are typically 2 ways that an MCP server can exchange information:
- Standard Input Output (STDIO)
- Streamable HTTP
Remark: The Streamable HTTP option replaces the HTTP+SSE transport from protocol version 2024-11-05. You can optionally still use Server-Sent Events (SSE) to stream multiple server messages.
I will be using Streamable HTTP in this example with the Playwright MCP server.
- Start the server by executing the following command:
npx @playwright/mcp@latest --port 8931
- Now open your project and add the following NuGet package:
dotnet add ModelContextProtocol
- Next step is to instantiate an MCP client and get the tools provided by the MCP server:
- As these tools are instances of
McpClientTool
, which inherits fromAIFunction
, we can pass them directly in as ‘plugins’ to ourChatOptions
:
That’s all that we need to do!