Skip to content

This is a simple example of how to create a Model Context Protocol (MCP) server in C#. The MCP server allows you to define tools that can be called from the command line or from other applications.

Notifications You must be signed in to change notification settings

garrardkitchen/mcp-example

Repository files navigation

This is my first effort at creating a Model Context Protocol (MCP) server in C#. I used the reference as a guide and have documented my steps here.

Model Context Protocol (MCP) Server in C#

This is a simple example of how to create a Model Context Protocol (MCP) server in C#. The MCP server allows you to define tools that can be called from the command line or from other applications.

Prerequisites

  • .NET 9.0 SDK or later
  • Visual Studio Code (recommended)

To start, I entered the following command to create a new console application:

dotnet new console -n Mcp.Echo  

I followed this by adding the following dependencies to my project:

dotnet add package ModelContextProtocol --prerelease
dotnet add package Microsoft.Extensions.Hosting

I pasted the following code into my Program.cs file, overwriting the existing code:

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

var builder = Host.CreateApplicationBuilder(args);
builder.Logging.AddConsole(consoleLogOptions =>
{
    // Configure all logs to go to stderr
    consoleLogOptions.LogToStandardErrorThreshold = LogLevel.Trace;
});

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

await builder.Build().RunAsync();

I had to add the follow using as it was missing:

using Microsoft.Extensions.Logging;

This is the warning I received:

Warning

ILoggingBuilder' does not contain a definition for 'AddConsole' and no accessible extension method 'AddConsole' accepting a first argument of type 'ILoggingBuilder' could be found (are you missing a using directive or an assembly reference?)

I then created a new file called EchoTool.cs and pasted in the following code:

using System.ComponentModel;
using ModelContextProtocol.Server;

[McpServerToolType]
public static class EchoTool
{
    [McpServerTool, Description("Echoes the message back to the client.")]
    public static string Echo(string message) => $"Hello from C#: {message}";

    [McpServerTool, Description("Echoes in reverse the message sent by the client.")]
    public static string ReverseEcho(string message) => new string(message.Reverse().ToArray());
}

I then added the file `.vscode/mcp.json' with this content:

{
    "inputs": [],
    "servers": {
        "MyFirstMCP": {
            "type": "stdio",
            "command": "dotnet",
            "args": [
                "run",
                "--project",
                "/Users/garrardkitchen/source/mcp/Mcp.Echo/Mcp.Echo.csproj"
            ]
        }
    }
}

You add the server config to the .vscode folder so that it is not shared with other projects.

I then built it to make sure everything was working:

dotnet build

I then either returned to the mcp.json file and clicked the Start button above MyFirstMCP to start the MCP Server. You can also do this in the Agent view:

The MCP server is now running.

Returning to the Agent view, I entered the following request:

Reacting to what the agent was telling me, I entered:

run #ReverseEcho Garrard

And that's it, my first MCP server in C#! Spectacular!

Yay me 🤘!

License

MIT License

Troubleshooting

  • If you see build errors, ensure you have the correct .NET SDK installed.
  • If the MCP server does not start, check the path in your mcp.json and ensure all dependencies are installed.
  • For logging issues, ensure you have the correct using Microsoft.Extensions.Logging; directive in your Program.cs.

References

About

This is a simple example of how to create a Model Context Protocol (MCP) server in C#. The MCP server allows you to define tools that can be called from the command line or from other applications.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages