DEV Community

Mehran Davoudi
Mehran Davoudi

Posted on

4 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

Deploy with ease. Manage efficiently. Scale faster.

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 (0)

AWS Security LIVE!

Join us for AWS Security LIVE!

Discover the future of cloud security. Tune in live for trends, tips, and solutions from AWS and AWS Partners.

Learn More

πŸ‘‹ Kindness is contagious

Explore a trove of insights in this engaging article, celebrated within our welcoming DEV Community. Developers from every background are invited to join and enhance our shared wisdom.

A genuine "thank you" can truly uplift someone’s day. Feel free to express your gratitude in the comments below!

On DEV, our collective exchange of knowledge lightens the road ahead and strengthens our community bonds. Found something valuable here? A small thank you to the author can make a big difference.

Okay