ModelContextProtocol 0.1.0-preview.1.25171.12

Prefix Reserved
This is a prerelease version of ModelContextProtocol.
There is a newer prerelease version of this package available.
See the version list below for details.
The owner has unlisted this package. This could mean that the package is deprecated, has security vulnerabilities or shouldn't be used anymore.
dotnet add package ModelContextProtocol --version 0.1.0-preview.1.25171.12
                    
NuGet\Install-Package ModelContextProtocol -Version 0.1.0-preview.1.25171.12
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="ModelContextProtocol" Version="0.1.0-preview.1.25171.12" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="ModelContextProtocol" Version="0.1.0-preview.1.25171.12" />
                    
Directory.Packages.props
<PackageReference Include="ModelContextProtocol" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add ModelContextProtocol --version 0.1.0-preview.1.25171.12
                    
#r "nuget: ModelContextProtocol, 0.1.0-preview.1.25171.12"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package ModelContextProtocol@0.1.0-preview.1.25171.12
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=ModelContextProtocol&version=0.1.0-preview.1.25171.12&prerelease
                    
Install as a Cake Addin
#tool nuget:?package=ModelContextProtocol&version=0.1.0-preview.1.25171.12&prerelease
                    
Install as a Cake Tool

MCP C# SDK

The official C# SDK for the Model Context Protocol, enabling .NET applications to connect to and interact with MCP clients and servers.

This is a preview release. 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:

Getting Started (Client)

Then create a client and start using tools, or other capabilities, from the servers you configure:

McpClientOptions options = new()
{
    ClientInfo = new() { Name = "TestClient", Version = "1.0.0" }
};

McpServerConfig config = new()
{
    Id = "everything",
    Name = "Everything",
    TransportType = TransportTypes.StdIo,
    TransportOptions = new()
    {
        ["command"] = "npx",
        ["arguments"] = "-y @modelcontextprotocol/server-everything",
    }
};

var client = await McpClientFactory.CreateAsync(config, options);

// Print the list of tools available from the server.
await foreach (var tool in 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() { ["message"] = "Hello MCP!" },
    CancellationToken.None);

// echo always returns one and only one text content object
Console.WriteLine(result.Content.First(c => c.Type == "text").Text);

Note that you should pass CancellationToken objects suitable for your use case, to enable proper error handling, timeouts, etc. This example also does not paginate the tools list, which may be necessary for large tool sets. See the IntegrationTests project for an example of pagination, as well as examples of how to handle Prompts and Resources.

It is also highly recommended that you pass a proper LoggerFactory instance to the factory constructor, to enable logging of MCP client operations.

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.

Remember you can connect to any MCP server, not just ones created using ModelContextProtocol. The protocol is designed to be server-agnostic, so you can use this library to connect to any compliant server.

Tools can be exposed easily as AIFunction instances so that they are immediately usable with IChatClients.

// Get available functions.
IList<AIFunction> tools = await client.GetAIFunctionsAsync();

// Call the chat client using the tools.
IChatClient chatClient = ...;
var response = await chatClient.GetResponseAsync(
    "your prompt here",
    new() 
    {
        Tools = [.. tools],
    });

Getting Started (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 McpToolType attribute, and registers all methods with the McpTool attribute as tools.)

using ModelContextProtocol;
using ModelContextProtocol.Server;
using Microsoft.Extensions.Hosting;
using System.ComponentModel;

var builder = Host.CreateEmptyApplicationBuilder(settings: null);
builder.Services
    .AddMcpServer()
    .WithStdioServerTransport()
    .WithTools();
await builder.Build().RunAsync();

[McpToolType]
public static class EchoTool
{
    [McpTool, Description("Echoes the message back to the client.")]
    public static string Echo(string message) => $"hello {message}";
}

More control is also available, with fine-grained control over configuring the server and how it should handle client requests. For example:

using ModelContextProtocol.Protocol.Transport;
using ModelContextProtocol.Protocol.Types;
using ModelContextProtocol.Server;
using Microsoft.Extensions.Logging.Abstractions;

McpServerOptions options = new()
{
    ServerInfo = new() { Name = "MyServer", Version = "1.0.0" },
    Capabilities = new() 
    {
        Tools = new()
        {
            ListToolsHandler = async (request, cancellationToken) =>
            {
                return new ListToolsResult()
                {
                    Tools =
                    [
                        new Tool()
                        {
                            Name = "echo",
                            Description = "Echoes the input back to the client.",
                            InputSchema = new JsonSchema()
                            {
                                Type = "object",
                                Properties = new Dictionary<string, JsonSchemaProperty>()
                                {
                                    ["message"] = new JsonSchemaProperty() { Type = "string", Description = "The input to echo back." }
                                }
                            },
                        }
                    ]
                };
            },

            CallToolHandler = async (request, cancellationToken) =>
            {
                if (request.Params?.Name == "echo")
                {
                    if (request.Params.Arguments?.TryGetValue("message", out var message) is not true)
                    {
                        throw new McpServerException("Missing required argument 'message'");
                    }

                    return new CallToolResponse()
                    {
                        Content = [new Content() { Text = $"Echo: {message}", Type = "text" }]
                    };
                }

                throw new McpServerException($"Unknown tool: '{request.Params?.Name}'");
            },
        }
    },
};

await using IMcpServer server = McpServerFactory.Create(new StdioServerTransport("MyServer"), options);

await server.StartAsync();

// Run until process is stopped by the client (parent process)
await Task.Delay(Timeout.Infinite);

License

This project is licensed under the MIT License.

Product 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 was computed.  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 was computed.  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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (80)

Showing the top 5 NuGet packages that depend on ModelContextProtocol:

Package Downloads
ModelContextProtocol.AspNetCore

ASP.NET Core extensions for the C# Model Context Protocol (MCP) SDK.

BotSharp.Core

Open source LLM application framework to build scalable, flexible and robust AI system.

Senparc.Weixin.AspNet

微信 SDK - Senparc.Weixin.AspNet 模块 Senparc.Weixin SDK 开源项目: https://github.com/JeffreySu/WeiXinMPSDK

Microsoft.Debugging.Platform.DbgX

Wraps DbgEng in a managed Task Asynchronous Pattern (e.g. async/await) so that consumers of this library do not need to be aware of the oddities of the dbgeng threading model. It creates a child process to host the engine out of process (using EngHost.exe) to allow native 32/64 bit debugging of binaries.

PayTech.RestHost

Easy Rest Api Development Library

GitHub repositories (37)

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
JeffreySu/WeiXinMPSDK
微信全平台 .NET SDK, Senparc.Weixin for C#,支持 .NET Framework 及 .NET Core、.NET 10.0。已支持微信公众号、小程序、小游戏、微信支付、企业微信/企业号、开放平台、JSSDK、微信周边等全平台。 WeChat SDK for C#.
dotnet/aspire
Aspire is the tool for code-first, extensible, observable dev and deploy.
DearVa/Everywhere
A context-aware AI assistant for your desktop. Ready to respond intelligently, seamlessly integrating multiple LLMs and MCP tools.
dotnet/extensions
This repository contains a suite of libraries that provide facilities commonly needed when creating production-ready applications.
SciSharp/BotSharp
AI Multi-Agent Framework in .NET
microsoft/mcp
Catalog of official Microsoft MCP (Model Context Protocol) server implementations for AI-powered data access and tool integration
microsoft/Generative-AI-for-beginners-dotnet
Five lessons, learn how to really apply AI to your .NET Applications
IoTSharp/IoTSharp
IoTSharp is an open-source IoT platform for data collection, processing, visualization, and device management.
awaescher/OllamaSharp
The easiest way to use Ollama in .NET
Azure/azure-mcp
The Azure MCP Server, bringing the power of Azure to your agents.
iioter/iotgateway
An industrial IoTGateway with B/S architecture that enables bidirectional communication between industrial devices (southbound connections) and IoT platforms (northbound connections). It supports numerous industrial protocols, and can connect to various IoT cloud platforms.
tryAGI/LangChain
C# implementation of LangChain. We try to be as close to the original as possible in terms of abstractions, but are open to new entities.
getcellm/cellm
Use LLMs in Excel formulas
mixcore/mix.core
🚀 A future-proof enterprise web CMS supporting both headless and decoupled approaches. Build any type of app with customizable APIs on ASP.NET Core/.NET Core. Completely open-source and designed for flexibility.
IvanMurzak/Unity-MCP
AI-powered bridge connecting LLMs and advanced AI agents to the Unity Editor via the Model Context Protocol (MCP). Chat with AI to generate code, debug errors, and automate game development tasks directly within your project.
CommunityToolkit/Aspire
A community project with additional components and extensions for Aspire
lofcz/LLMTornado
The .NET library to build AI agents with 25+ built-in connectors.
CervantesSec/cervantes
Cervantes is an open-source, collaborative platform designed specifically for pentesters and red teams. It serves as a comprehensive management tool, streamlining the organization of projects, clients, vulnerabilities, and reports in a single, centralized location.
junkai-li/NetCoreKevin
基于NET搭建-AI智能体-现代化Saas企业级前后端分离架构-开启智能应用的无限可能:前端Vue3、IDS4单点登录、多缓存、自动任务、分布式、一库多租户、日志、授权和鉴权、CAP集成事件、SignalR、领域事件、ESL、MCP协议服务、IOC模块化注入、Cors、Quartz自动任务、多短信集成、AI、AgentFramework智能体、AISemanticKernel集成、RAG检索增强、OCR验证码识别、API多版本兼容、单元集成测试、RabbitMQ
Version Downloads Last Updated
0.5.0-preview.1 12,710 12/5/2025
0.4.1-preview.1 21,740 11/25/2025
0.4.0-preview.3 365,191 10/20/2025
0.4.0-preview.2 126,467 10/8/2025
0.4.0-preview.1 94,050 9/25/2025
0.3.0-preview.4 465,374 8/20/2025
0.3.0-preview.3 304,336 7/16/2025
0.3.0-preview.2 125,585 7/3/2025
0.3.0-preview.1 145,217 6/20/2025
0.2.0-preview.3 150,622 6/3/2025
0.2.0-preview.2 63,132 5/29/2025
0.2.0-preview.1 87,967 5/16/2025
0.1.0-preview.14 15,336 5/15/2025
0.1.0-preview.13 23,831 5/10/2025
0.1.0-preview.12 16,810 5/5/2025
0.1.0-preview.11 67,643 4/24/2025
0.1.0-preview.10 41,264 4/19/2025
0.1.0-preview.9 23,711 4/15/2025
0.1.0-preview.8 14,638 4/11/2025
0.1.0-preview.7 7,415 4/9/2025
0.1.0-preview.6 10,895 4/4/2025
0.1.0-preview.5 2,268 4/3/2025
0.1.0-preview.4 10,900 3/31/2025
0.1.0-preview.3 330 3/31/2025
0.1.0-preview.2 6,976 3/27/2025