Microsoft is collaborating with Anthropic to create an official C# SDK for the Model Context Protocol (MCP). MCP has seen rapid adoption in the AI community, and this partnership aims to enhance the integration of AI models into C# applications.
The SDK is being developed as an open-source project in the modelcontextprotocol GitHub organization, making it easy for developers to find and collaborate on the project. The library is available as a NuGet package, ModelContextProtocol.
The starting point for this library was a project called mcpdotnet, initiated by Peder Holdgaard Pederson. We are grateful for the work done by Peder and other contributors to that repository, which created a solid foundation for the official MCP C# library.
What is MCP?
The Model Context Protocol (MCP) is an open protocol created by Anthropic to enable integration between LLM applications and external tools and data sources. It was originally released in November 2024 and recently was updated to add new streaming capabilities. The protocol is designed to be extensible and flexible, allowing developers to create custom tools and data sources that can be used with LLMs.
A number of Microsoft products have already added support for MCP, including Copilot Studio, VS Code’s new GitHub Copilot agent mode, and Semantic Kernel. And many Microsoft products are creating MCP servers to access their functionality. The GitHub MCP Server and Playwright MCP for browser automation are popular examples, with many more in the works.
Why C#?
C# is a popular programming language used by many developers, especially in the enterprise space. By creating an official C# SDK for MCP, Microsoft aims to make it easier for developers to integrate AI models into their C# applications and build MCP servers using C#. The C# SDK also leverages the significant performance improvements in modern .NET, providing excellent speed and efficiency for AI applications. With .NET’s optimized runtime and support for containerization, services perform well in local development scenarios. And many of Microsoft’s core products are written in C#, including Visual Studio, the majority of Azure services, services powering Microsoft Teams and XBOX, and many more. All these products can benefit from the Model Context Protocol, and the C# SDK provides the foundation for that.
A brief explanation of MCP
An AI application can use MCP to connect to external tools and data sources, which it can then offer to the LLM as part of its context. This allows the LLM to access and use external data and tools, enhancing its capabilities and enabling it to perform more complex tasks.
The AI application, called a Host in MCP, communicates to MCP servers through an MCP client. The MCP client understands the MCP protocol and can send requests to the MCP server, which then processes the request and returns a response. Here’s a diagram adapted from the MCP documentation that illustrates this:
There are a set of standard messages that the MCP client and server can exchange, including:
Message | Description |
---|---|
InitializeRequest | This request is sent from the client to the server when it first connects, asking it to begin initialization |
ListToolsRequest | Sent from the client to request a list of tools the server has |
CallToolRequest | Used by the client to invoke a tool provided by the server |
ListResourcesRequest | Sent from the client to request a list of resources the server has |
ReadResourceRequest | Sent from the client to the server, to read a specific resource URI |
ListPromptsRequest | Sent from the client to request a list of prompts and prompt templates the server has |
GetPromptRequest | Used by the client to get a prompt provided by the server |
PingRequest | A ping, issued by either the server or the client, to check that the other party is still alive |
CreateMessageRequest | A request from the server to sample an LLM via the client. The client has full discretion over which model to select. The client should also inform the user before beginning sampling, to allow them to inspect the request (human in the loop) and decide whether to approve it |
SetLevelRequest | A request from the client to the server, to enable or adjust logging |
As you can see, the MCP protocol is designed to be flexible and extensible, allowing developers to create custom tools and data sources that can be used with LLMs. And there are already many many MCP servers available, with more being created every day. The Readme in the modelcontextprotocol/servers repo contains lists of reference servers, third party servers, and community servers that collectively number in the hundreds.
A simple example – the Echo server
It is really easy to create a simple MCP server using the official C# SDK. Here we’ll use the example Echo server that is included in the SDK README. The Echo server simply echoes back any message it receives from the client, prefixed with “hello “.
Starting with a new dotnet console application, add the Microsoft.Extensions.Hosting
and prerelease ModelContextProtocol
NuGet packages:
dotnet add package Microsoft.Extensions.Hosting
dotnet add package ModelContextProtocol --prerelease
Then, replace the code in the Program.cs
file with the following code:
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}";
}
Note: the MCP C# SDK is in preview and APIs may change. We will continuously update this blog as the SDK evolves, but if you encounter any problems with the code above, try using the first code sample in the “Getting Started (Server)” section of the mcp-csharp README.
Now to see the server in action, you can use the MCP Inspector, a visual testing tool for MCP servers.
Since the MCP inspector is a node application, you don’t need to install it, you can just run it using npx
. The argument to the inspector is a command that will run the server. In this case, we want to run the server in the current directory, so we can use dotnet run
:
npx @modelcontextprotocol/inspector dotnet run
You may be prompted to install the inspector, and you can do that by pressing y
and hitting enter. The inspector will start and execute the command specified to start the server. You’ll see some output in the terminal indicating that the server is starting up, and then it will show a message indicating that the inspector UI is ready:
MCP Inspector is up and running at http://localhost:5173
Open a browser window and navigate to the URL shown in the terminal. You should see something like this:
Now click the “Connect” button to connect to the server. The inspector will connect to the server and offer to request a list of tools. If the server had any resources or prompts, it would also offer to request a list of resources or prompts.
Click the “List Tools” button to see the list of tools the server has. In this case only one tool is available, the Echo tool.
Now click on the “Echo” tool to see the details of the tool. You will see the tool description and entry boxes for any parameters the tool accepts — in this case, the Echo tool accepts a single parameter called “message”.
Enter a message in the “message” box and click the “Run Tool” button. In inspector will invoke the tool and display the result.
This is a very simple example to introduce the concepts at a basic level. For some more involved examples, see the samples directory in the SDK repo.
Conclusion
That’s a quick introduction to the Model Context Protocol and the new MCP C# SDK. The SDK is still in its early stages, but it is already a powerful tool for integrating AI models into C# applications. The SDK is open source and available on GitHub, and we welcome contributions from the community.
Try out one of the samples in the SDK repo or create your own MCP server using the SDK. If you encounter any problems, please open an issue on the GitHub repo.
We are excited to see what developers will create with the MCP C# SDK, and we look forward to seeing the MCP ecosystem grow and evolve.
What are the plans to support OAuth with OpenID Connect and https protocols?
We plan to support all the Authentication protocols described in the MCP spec. We don’t have a definite target date for this but it is very high on our priority list.
Superb !!
Now to use powershell to create and manage mcps