DEV Community

Mehran Davoudi
Mehran Davoudi

Posted on

18 1 1 3

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:

Image of Stellar post

πŸš€ Stellar Dev Diaries Series: Episode 1 is LIVE!

Ever wondered what it takes to build a web3 startup from scratch? In the Stellar Dev Diaries series, we follow the journey of a team of developers building on the Stellar Network as they go from hackathon win to getting funded and launching on mainnet.

Read more

Top comments (1)

Collapse
 
caseyspaulding profile image
Casey Spaulding β€’

Thank you

Image of Stellar post

Check out Episode 1: How a Hackathon Project Became a Web3 Startup πŸš€

Ever wondered what it takes to build a web3 startup from scratch? In the Stellar Dev Diaries series, we follow the journey of a team of developers building on the Stellar Network as they go from hackathon win to getting funded and launching on mainnet.

Read 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