Microsoft.Extensions.AI integration for the Model Context Protocol (MCP). Enables seamless use of MCP tools as AI functions in any IChatClient implementation.
$ dotnet add package McpDotNet.Extensions.AIMicrosoft.Extensions.AI integration for the Model Context Protocol (MCP). Enables seamless use of MCP tools as AI functions in any IChatClient implementation. Built on top of mcpdotnet.
The Model Context Protocol (MCP) is an open protocol that standardizes how applications provide context to Large Language Models (LLMs). It enables secure integration between LLMs and various data sources and tools.
For more information about MCP:
The goal of this library is to provide a simple and efficient way to integrate MCP capabilities with Microsoft.Extensions.AI.Abstractions. It is designed to be easy to use, wrapping boilerplate code and providing a clean API for AI developers to use leverage MCP capabilities without having to worry about the underlying protocol.
To use McpDotNet.Extensions.AI, first install it via NuGet:
dotnet add package McpDotNet.Extensions.AI
Create a configuration object for the MCP server you want to connect to:
var config = new McpServerConfig
{
Id = "everything",
Name = "Everything",
TransportType = TransportTypes.StdIo,
TransportOptions = new Dictionary<string, string>
{
["command"] = "npx",
["arguments"] = "-y @modelcontextprotocol/server-everything",
}
};
Then create an MCP session scope and use it with a chat client of your choice:
await using var sessionScope = await McpSessionScope.CreateAsync(config);
IChatClient openaiClient = new OpenAIClient("sk-your-key")
.AsChatClient("gpt-4o-mini");
IChatClient chatClient = new ChatClientBuilder(openaiClient)
.UseFunctionInvocation()
.Build();
// Create message list
IList<ChatMessage> messages =
[
// Add a system message
new(ChatRole.System, "You are a helpful assistant."),
new(ChatRole.User, "Placeholder for the actual user message.")
];
// Call the chat client
var response = await chatClient.GetResponseAsync(messages,
new() { Tools = sessionScope.Tools });
As you can see, all you really need to do is create one or more configuration objects matching the MCP servers you want to use, create an MCP session scope, and then the tools exposed by the server(s) will be available to you through the Tools property of the McpSessionScope object.
The McpSessionScope object implements IAsyncDisposable, and managing the connection lifecycle is as simple as using the await using pattern. Note that you have to keep the McpSessionScope object alive for as long as you want to use the tools it provides.
This project is licensed under the MIT License - see the LICENSE file for details.