ModelContextProtocol 0.8.0-preview.1
Prefix ReservedSee the version list below for details.
dotnet add package ModelContextProtocol --version 0.8.0-preview.1
NuGet\Install-Package ModelContextProtocol -Version 0.8.0-preview.1
<PackageReference Include="ModelContextProtocol" Version="0.8.0-preview.1" />
<PackageVersion Include="ModelContextProtocol" Version="0.8.0-preview.1" />
<PackageReference Include="ModelContextProtocol" />
paket add ModelContextProtocol --version 0.8.0-preview.1
#r "nuget: ModelContextProtocol, 0.8.0-preview.1"
#:package ModelContextProtocol@0.8.0-preview.1
#addin nuget:?package=ModelContextProtocol&version=0.8.0-preview.1&prerelease
#tool nuget:?package=ModelContextProtocol&version=0.8.0-preview.1&prerelease
MCP C# SDK
The official C# SDK for the Model Context Protocol, enabling .NET applications, services, and libraries to implement and interact with MCP clients and servers. Please visit our API documentation for more details on available functionality.
Packages
This SDK consists of three main packages:
ModelContextProtocol
- The main package with hosting and dependency injection extensions. This is the right fit for most projects that don't need HTTP server capabilities. This README serves as documentation for this package.
ModelContextProtocol.AspNetCore
- The library for HTTP-based MCP servers. Documentation
ModelContextProtocol.Core
- For people who only need to use the client or low-level server APIs and want the minimum number of dependencies. Documentation
This project is in preview; breaking changes can be introduced without prior notice.
About MCP
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:
Installation
To get started, install the package from NuGet
dotnet add package ModelContextProtocol --prerelease
Getting Started (Client)
To get started writing a client, the McpClient.CreateAsync method is used to instantiate and connect an McpClient
to a server. Once you have an McpClient, you can interact with it, such as to enumerate all available tools and invoke tools.
using ModelContextProtocol.Client;
using ModelContextProtocol.Protocol;
var clientTransport = new StdioClientTransport(new StdioClientTransportOptions
{
Name = "Everything",
Command = "npx",
Arguments = ["-y", "@modelcontextprotocol/server-everything"],
});
var client = await McpClient.CreateAsync(clientTransport);
// Print the list of tools available from the server.
foreach (var tool in await client.ListToolsAsync())
{
Console.WriteLine($"{tool.Name} ({tool.Description})");
}
// Execute a tool (this would normally be driven by LLM tool invocations).
var result = await client.CallToolAsync(
"echo",
new Dictionary<string, object?>() { ["message"] = "Hello MCP!" },
cancellationToken:CancellationToken.None);
// echo always returns one and only one text content object
Console.WriteLine(result.Content.OfType<TextContentBlock>().First().Text);
You can find samples demonstrating how to use ModelContextProtocol with an LLM SDK in the samples directory, and also refer to the tests project for more examples. Additional examples and documentation will be added as in the near future.
Clients can connect to any MCP server, not just ones created using this library. The protocol is designed to be server-agnostic, so you can use this library to connect to any compliant server.
Tools can be easily exposed for immediate use by IChatClients, because McpClientTool inherits from AIFunction.
// Get available functions.
IList<McpClientTool> tools = await client.ListToolsAsync();
// Call the chat client using the tools.
IChatClient chatClient = ...;
var response = await chatClient.GetResponseAsync(
"your prompt here",
new() { Tools = [.. tools] },
Getting Started (Server)
You can use the MCP Server project template to quickly get started with creating your own MCP server.
Here is an example of how to create an MCP server and register all tools from the current application.
It includes a simple echo tool as an example (this is included in the same file here for easy of copy and paste, but it needn't be in the same file...
the employed overload of WithTools examines the current assembly for classes with the McpServerToolType attribute, and registers all methods with the
McpServerTool attribute as tools.)
dotnet add package ModelContextProtocol --prerelease
dotnet add package Microsoft.Extensions.Hosting
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using ModelContextProtocol.Server;
using System.ComponentModel;
var builder = Host.CreateApplicationBuilder(args);
builder.Logging.AddConsole(consoleLogOptions =>
{
// Configure all logs to go to stderr
consoleLogOptions.LogToStandardErrorThreshold = LogLevel.Trace;
});
builder.Services
.AddMcpServer()
.WithStdioServerTransport()
.WithToolsFromAssembly();
await builder.Build().RunAsync();
[McpServerToolType]
public static class EchoTool
{
[McpServerTool, Description("Echoes the message back to the client.")]
public static string Echo(string message) => $"hello {message}";
}
Tools can have the McpServer representing the server injected via a parameter to the method, and can use that for interaction with
the connected client. Similarly, arguments may be injected via dependency injection. For example, this tool will use the supplied
McpServer to make sampling requests back to the client in order to summarize content it downloads from the specified url via
an HttpClient injected via dependency injection.
[McpServerTool(Name = "SummarizeContentFromUrl"), Description("Summarizes content downloaded from a specific URI")]
public static async Task<string> SummarizeDownloadedContent(
McpServer thisServer,
HttpClient httpClient,
[Description("The url from which to download the content to summarize")] string url,
CancellationToken cancellationToken)
{
string content = await httpClient.GetStringAsync(url);
ChatMessage[] messages =
[
new(ChatRole.User, "Briefly summarize the following downloaded content:"),
new(ChatRole.User, content),
];
ChatOptions options = new()
{
MaxOutputTokens = 256,
Temperature = 0.3f,
};
return $"Summary: {await thisServer.AsSamplingChatClient().GetResponseAsync(messages, options, cancellationToken)}";
}
Prompts can be exposed in a similar manner, using [McpServerPrompt], e.g.
[McpServerPromptType]
public static class MyPrompts
{
[McpServerPrompt, Description("Creates a prompt to summarize the provided message.")]
public static ChatMessage Summarize([Description("The content to summarize")] string content) =>
new(ChatRole.User, $"Please summarize this content into a single sentence: {content}");
}
More control is also available, with fine-grained control over configuring the server and how it should handle client requests. For example:
using ModelContextProtocol;
using ModelContextProtocol.Protocol;
using ModelContextProtocol.Server;
using System.Text.Json;
McpServerOptions options = new()
{
ServerInfo = new Implementation { Name = "MyServer", Version = "1.0.0" },
Handlers = new McpServerHandlers()
{
ListToolsHandler = (request, cancellationToken) =>
ValueTask.FromResult(new ListToolsResult
{
Tools =
[
new Tool
{
Name = "echo",
Description = "Echoes the input back to the client.",
InputSchema = JsonSerializer.Deserialize<JsonElement>("""
{
"type": "object",
"properties": {
"message": {
"type": "string",
"description": "The input to echo back"
}
},
"required": ["message"]
}
"""),
}
]
}),
CallToolHandler = (request, cancellationToken) =>
{
if (request.Params?.Name == "echo")
{
if (request.Params.Arguments?.TryGetValue("message", out var message) is not true)
{
throw new McpProtocolException("Missing required argument 'message'", McpErrorCode.InvalidParams);
}
return ValueTask.FromResult(new CallToolResult
{
Content = [new TextContentBlock { Text = $"Echo: {message}", Type = "text" }]
});
}
throw new McpProtocolException($"Unknown tool: '{request.Params?.Name}'", McpErrorCode.InvalidRequest);
}
}
};
await using McpServer server = McpServer.Create(new StdioServerTransport("MyServer"), options);
await server.RunAsync();
Descriptions can be added to tools, prompts, and resources in a variety of ways, including via the [Description] attribute from System.ComponentModel.
This attribute may be placed on a method to provide for the tool, prompt, or resource, or on individual parameters to describe each's purpose.
XML comments may also be used; if an [McpServerTool], [McpServerPrompt], or [McpServerResource]-attributed method is marked as partial,
XML comments placed on the method will be used automatically to generate [Description] attributes for the method and its parameters.
Acknowledgements
The starting point for this library was a project called mcpdotnet, initiated by Peder Holdgaard Pedersen. We are grateful for the work done by Peder and other contributors to that repository, which created a solid foundation for this library.
License
This project is licensed under the Apache License 2.0.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 is compatible. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 is compatible. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
| .NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
| .NET Standard | netstandard2.0 is compatible. netstandard2.1 was computed. |
| .NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
| MonoAndroid | monoandroid was computed. |
| MonoMac | monomac was computed. |
| MonoTouch | monotouch was computed. |
| Tizen | tizen40 was computed. tizen60 was computed. |
| Xamarin.iOS | xamarinios was computed. |
| Xamarin.Mac | xamarinmac was computed. |
| Xamarin.TVOS | xamarintvos was computed. |
| Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.0
- Microsoft.Extensions.Caching.Abstractions (>= 10.0.2)
- Microsoft.Extensions.Hosting.Abstractions (>= 10.0.2)
- ModelContextProtocol.Core (>= 0.8.0-preview.1)
-
net10.0
- Microsoft.Extensions.Caching.Abstractions (>= 10.0.2)
- Microsoft.Extensions.Hosting.Abstractions (>= 10.0.2)
- ModelContextProtocol.Core (>= 0.8.0-preview.1)
-
net8.0
- Microsoft.Extensions.Caching.Abstractions (>= 10.0.2)
- Microsoft.Extensions.Hosting.Abstractions (>= 10.0.2)
- ModelContextProtocol.Core (>= 0.8.0-preview.1)
-
net9.0
- Microsoft.Extensions.Caching.Abstractions (>= 10.0.2)
- Microsoft.Extensions.Hosting.Abstractions (>= 10.0.2)
- ModelContextProtocol.Core (>= 0.8.0-preview.1)
NuGet packages (232)
Showing the top 5 NuGet packages that depend on ModelContextProtocol:
| Package | Downloads |
|---|---|
|
Aspire.Hosting
Core abstractions for the Aspire application model. |
|
|
Aspire.Hosting.AppHost
Core library and MSBuild logic for Aspire AppHost projects. |
|
|
Aspire.Hosting.Azure
Azure resource types for Aspire. |
|
|
ModelContextProtocol.AspNetCore
ASP.NET Core extensions for the C# Model Context Protocol (MCP) SDK. |
|
|
Aspire.Hosting.Testing
Testing support for the Aspire application model. |
GitHub repositories (72)
Showing the top 20 popular GitHub repositories that depend on ModelContextProtocol:
| Repository | Stars |
|---|---|
|
microsoft/semantic-kernel
Integrate cutting-edge LLM technology quickly and easily into your apps
|
|
|
abpframework/abp
Open-source web application framework for ASP.NET Core! Offers an opinionated architecture to build enterprise software solutions with best practices on top of the .NET. Provides the fundamental infrastructure, cross-cutting-concern implementations, startup templates, application modules, UI themes, tooling and documentation.
|
|
|
unoplatform/uno
Open-source platform for building cross-platform native Mobile, Web, Desktop and Embedded apps quickly. Create rich, C#/XAML, single-codebase apps from any IDE. Hot Reload included! 90m+ NuGet Downloads!!
|
|
|
JeffreySu/WeiXinMPSDK
微信全平台 .NET SDK, Senparc.Weixin for C#,支持 .NET Framework 及 .NET Core、.NET 10.0。已支持微信公众号、小程序、小游戏、微信支付、企业微信/企业号、开放平台、JSSDK、微信周边等全平台。 WeChat SDK for C#.
|
|
|
ant-design-blazor/ant-design-blazor
🌈A rich set of enterprise-class UI components based on Ant Design and Blazor.
|
|
|
Azure/azure-sdk-for-net
This repository is for active development of the Azure SDK for .NET. For consumers of the SDK we recommend visiting our public developer docs at https://learn.microsoft.com/dotnet/azure/ or our versioned developer docs at https://azure.github.io/azure-sdk-for-net.
|
|
|
microsoft/aspire
Aspire is the tool for code-first, extensible, observable dev and deploy.
|
|
|
DearVa/Everywhere
Context-aware AI assistant for your desktop. Ready to respond intelligently, seamlessly integrating multiple LLMs and MCP tools.
|
|
|
Megabit/Blazorise
Blazorise is a component library built on top of Blazor with support for CSS frameworks like Bootstrap, Tailwind, Bulma, AntDesign, and Material.
|
|
|
dotnet/extensions
This repository contains a suite of libraries that provide facilities commonly needed when creating production-ready applications.
|
|
|
microsoft/mcp
Catalog of official Microsoft MCP (Model Context Protocol) server implementations for AI-powered data access and tool integration
|
|
|
SciSharp/BotSharp
AI Multi-Agent Framework in .NET
|
|
|
microsoft/Generative-AI-for-beginners-dotnet
Five lessons, learn how to really apply AI to your .NET Applications
|
|
|
OPCFoundation/UA-.NETStandard
OPC Unified Architecture .NET Standard
|
|
|
MCCTeam/Minecraft-Console-Client
Lightweight console for Minecraft chat and automated scripts. Currently supports up to Minecraft 26.1
|
|
|
KirillOsenkov/MSBuildStructuredLog
A logger for MSBuild that records a structured representation of executed targets, tasks, property and item values.
|
|
|
DuendeSoftware/products
The most flexible and standards-compliant OpenID Connect and OAuth 2.x framework for ASP.NET Core
|
|
|
Azure/data-api-builder
Data API builder provides modern REST, GraphQL endpoints and MCP tools to your Azure Databases and on-prem stores.
|
|
|
bitfoundation/bitplatform
Build all of your apps using what you already know and love ❤️
|
|
|
IoTSharp/IoTSharp
IoTSharp is an open-source IoT platform for data collection, processing, visualization, and device management.
|
| Version | Downloads | Last Updated |
|---|---|---|
| 1.2.0 | 524,714 | 3/27/2026 |
| 1.1.0 | 520,548 | 3/6/2026 |
| 1.0.0 | 1,512,674 | 2/25/2026 |
| 1.0.0-rc.1 | 10,764 | 2/24/2026 |
| 0.9.0-preview.2 | 25,147 | 2/21/2026 |
| 0.9.0-preview.1 | 16,982 | 2/20/2026 |
| 0.8.0-preview.1 | 422,579 | 2/5/2026 |
| 0.7.0-preview.1 | 219,651 | 1/28/2026 |
| 0.6.0-preview.1 | 210,376 | 1/14/2026 |
| 0.5.0-preview.1 | 356,322 | 12/5/2025 |
| 0.4.1-preview.1 | 130,246 | 11/25/2025 |
| 0.4.0-preview.3 | 1,188,763 | 10/20/2025 |
| 0.4.0-preview.2 | 264,972 | 10/8/2025 |
| 0.4.0-preview.1 | 192,551 | 9/25/2025 |
| 0.3.0-preview.4 | 773,183 | 8/20/2025 |
| 0.3.0-preview.3 | 557,404 | 7/16/2025 |
| 0.3.0-preview.2 | 177,300 | 7/3/2025 |
| 0.3.0-preview.1 | 190,796 | 6/20/2025 |
| 0.2.0-preview.3 | 209,527 | 6/3/2025 |