Skip to main content

Command Palette

Search for a command to run...

Step-by-Step Guide to Building a MCP Server using C# for Claude for Desktop

Updated
•3 min read

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

  1. Create a new C# console application

  2. 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:

  1. We start by configuring a host application with the Host.CreateApplicationBuilder.

  2. We set up console logging to help with debugging.

  3. We register the MCP server with .AddMcpServer() and configure it to use standard I/O with .WithStdioServerTransport().

  4. We register our tools with .WithToolsFromAssembly().

  5. We create a simple tool class marked with [McpServerToolType] attribute.

  6. We define a method Hello marked 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

  1. Build and run your C# project to check for any errors.

  2. Launch Claude for Desktop.

  3. You should see your tool listed under "Available MCP Tools".

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

  1. Once you approve, Claude can use your tool:

How It Works

When Claude calls your MCP server:

  1. The request goes through the standard I/O transport.

  2. The SDK routes the request to your Hello method.

  3. Your method executes and returns the greeting and current time.

  4. 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!

316 views

More from this blog

Codingfreaks

40 posts

Helping others to learn