wsf

wsf

Create an MCP Server with .NET Core

In this article, I will show you how to create a simple MCP Server and test it in Cursor.

image

This tutorial uses 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

2. Install Packages#

Install these two packages:

dotnet add package MCPSharp

3. Write Code#

Replace the contents of Program.cs with:

using MCPSharp;
using System.ComponentModel;

//https://github.com/afrise/MCPSharp

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

MCPServer.Register<TimeTool>();
await MCPServer.StartAsync("CalculatorServer", "1.0.0");

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

    /// <summary>
    /// Adds two numbers together
    /// </summary>
    /// <param name="a">The first number to add</param>
    /// <param name="b">The second number to add</param>
    /// <returns>The sum of the two numbers</returns>
    [McpTool]
    public static int Add(
        [McpParameter(true)] int a,
        [McpParameter(true)] int b)
    {
        return a + b;
    }
}

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 is listening to stdin.

5. Test in Cursor#

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

Click "Add new global MCP Server" or open .cursor/mcp.json and add your MCP Server information as follows:

{
  "mcpServers": {
    "timemcp": {
      "command": "cmd",
      "args": [
        "/c",
        "D:/CodeRepository/Demo/mcp-dotnet/Tutorial.McpTimeServer/bin/Debug/net9.0/Tutorial.McpTimeServer.exe"
      ]
    }
  }
}


Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.