DEV Community

Mehran Davoudi
Mehran Davoudi

Posted on

14 1 1 2

Create an MCP Server with .NET and C#

In this post, I'll show you how to create a simple MCP Server and test it in Cursor.

For this tutorial, I'm using the official csharp-sdk, which is still in its early stages:

1. Create a simple project

First, create an empty console project:

dotnet new console -n Tutorial.McpTimeServer
Enter fullscreen mode Exit fullscreen mode

2. Install packages

Install these 2 packages:

dotnet add package Microsoft.Extensions.Hosting
dotnet add package ModelContextProtocol --prerelease
Enter fullscreen mode Exit fullscreen mode

3. Write the code

Replace the Program.cs content with this:

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

Console.WriteLine("Hello MCP World!");

var builder = Host.CreateEmptyApplicationBuilder(settings: null);

builder.Services
       .AddMcpServer()
       .WithStdioServerTransport()
       .WithTools();

var host = builder.Build();
await host.RunAsync();


[McpToolType]
public static class TimeTool
{
    [McpTool, Description("Gets the current time.")]
    public static string GetCurrentTime() => DateTimeOffset.Now.ToString();
}
Enter fullscreen mode Exit fullscreen mode

4. Run the project

If you run the project using dotnet run, you will see Hello MCP World! and the program will remain open as it's listening to stdin.

5. Test it on Cursor

Now it's time to configure it on Cursor. Go to File -> Preferences -> Cursor Settings.

Cursor Settings

Click on "Add new global MCP Server" or open .cursor/mcp.json and add your MCP Server information like this:

{
  "mcpServers": {
    "timemcp": {
      "command": "cmd",
      "args": [
        "/c",
        "C:/Users/Mehran/source/repos/Tutorial.McpTimeServer/bin/Debug/net9.0/Tutorial.McpTimeServer.exe"
      ]
    }
  }
}
Enter fullscreen mode Exit fullscreen mode

Now your MCP Servers page should look like this:

MCP Servers

5. Test your MCP Server

Now ask about the time, and it will use your tool.
Run Tool

And if you click on "Run tool," it displays the time when the MCP tool we created was called.

MCP Result

6. Find the code

You can find the complete code on my GitHub:

Heroku

Amplify your impact where it matters most — building exceptional apps.

Leave the infrastructure headaches to us, while you focus on pushing boundaries, realizing your vision, and making a lasting impression on your users.

Get Started

Top comments (1)

Collapse
 
caseyspaulding profile image
Casey Spaulding

Thank you

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

👋 Kindness is contagious

Engage with a wealth of insights in this thoughtful article, valued within the supportive DEV Community. Coders of every background are welcome to join in and add to our collective wisdom.

A sincere "thank you" often brightens someone’s day. Share your gratitude in the comments below!

On DEV, the act of sharing knowledge eases our journey and fortifies our community ties. Found value in this? A quick thank you to the author can make a significant impact.

Okay