Step-by-Step Guide to Building a MCP Server using C# for Claude for Desktop
The Model Context Protocol (MCP) now has an official C# SDK available as a NuGet preview package. This is a game-changer for .NET applications looking to standardize interactions with Large Language Models.
What is MCP?
Claude can use tools provided by specialized servers using the Model Context Protocol (MCP). This protocol allows Claude to interact with external applications and services, expanding its capabilities beyond conversation.
Prerequisites
Before we dive in, make sure you have:
VS Code or Visual Studio 2019 (or higher)
.NET 8 (or higher)
Claude for Desktop installed
Setting Up Your Project
Create a new C# console application
Add the required NuGet packages:
dotnet add package ModelContextProtocol --prerelease
dotnet add package Microsoft.Extensions.Hosting
Building Your First MCP Server with CSharp
Let's create a simple "SayHello" tool that returns a greeting with the current time. Here's the complete code:
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using ModelContextProtocol.Server;
using System.ComponentModel;
namespace HelloMCPServer
{
internal class Program
{
static async Task Main(string[] args)
{
var builder = Host.CreateApplicationBuilder(args);
builder.Logging.AddConsole(consoleLogOptions =>
{ consoleLogOptions.LogToStandardErrorThreshold = LogLevel.Trace;});
builder.Services
.AddMcpServer()
.WithStdioServerTransport()
.WithToolsFromAssembly();
await builder.Build().RunAsync();
}
}
[McpServerToolType]
public static class SayHelloTool
{
[McpServerTool(Name = "SayHello"), Description("Says Hello to caller name and current time")]
public static string Hello(string data) => $"hello {data}, current time {System.DateTime.Now.ToString()}";
}
}
Let's understand the key components:
We start by configuring a host application with the
Host.CreateApplicationBuilder.We set up console logging to help with debugging.
We register the MCP server with
.AddMcpServer()and configure it to use standard I/O with.WithStdioServerTransport().We register our tools with
.WithToolsFromAssembly().We create a simple tool class marked with
[McpServerToolType]attribute.We define a method
Hellomarked with[McpServerTool]attribute that takes a string parameter and returns a greeting with timestamp.
Configuring Claude for Desktop
To let Claude know about your local MCP server, you need to create a configuration file. Create a claude_desktop_config.json file with the following content:
{
"mcpServers": {
"myLocalMachine": {
"command": "dotnet",
"args": [
"run",
"--project",
"D:\\Users\\mural\\source\\repos\\HelloMCPServer\\HelloMCPServer",
"--no-build"
]
}
}
}
Make sure to update the path to match your project's location.
Testing Your MCP Server
Build and run your C# project to check for any errors.
Launch Claude for Desktop.
You should see your tool listed under "Available MCP Tools".

When you try to use the tool, Claude will ask for permission:

- Once you approve, Claude can use your tool:

How It Works
When Claude calls your MCP server:
The request goes through the standard I/O transport.
The SDK routes the request to your
Hellomethod.Your method executes and returns the greeting and current time.
Claude receives the response and can display it to the user.

Next Steps
This simple example just scratches the surface of what's possible with MCP. Here are some ideas for extending your MCP server:
Create tools that access databases or APIs
Implement tools that interact with your file system
Build tools that integrate with your organization's internal services
Conclusion
Building an MCP server in C# opens up exciting possibilities for extending Claude's capabilities. With the official SDK now available as a NuGet package, .NET developers can easily create custom tools that Claude can use to provide more powerful and context-aware assistance.
The combination of Claude's intelligence with custom tools allows for building specialized AI assistants that can interact with your existing systems and services in a standardized way.
Have you built any interesting MCP tools for Claude? Share your experiences in the comments!