Cover image
Try Now
2025-04-14

3 years

Works with Finder

0

Github Watches

0

Github Forks

0

Github Stars

MCP Server build with dotnet

Description

This is a simple server build with dotnet. It is a simple server that can be used to test the MCP protocol. It is not a full implementation of the MCP protocol, but it can be used to test the protocol and see how it works.

Follow reference modelcontextprotocol/csharp-sdk: The official C# SDK for Model Context Protocol servers and clients. Maintained in collaboration with Microsoft.

Steps to build this repo

1. Init

Create a new directory and init a new dotnet project

md mcp-dotnet
cd mcp-dotnet
dotnet new webapi
dotnet new .gitignore

git init
git add .
git commit -m "init"

2. MCP packages added

Add the MCP packages

dotnet add package ModelContextProtocol --prerelease
dotnet add package Microsoft.Extensions.Hosting
git add .
git commit -m "MCP packages added"

3. Add Docker support

Create Dockerfile

# Use the official .NET SDK image as a build stage
FROM mcr.microsoft.com/dotnet/sdk:9.0 AS build

# Set the working directory in the container
WORKDIR /app

# Copy the project file and restore dependencies
COPY client-repo.csproj ./
RUN dotnet restore

# Copy the rest of the application code
COPY . ./

# Build the application
RUN dotnet publish -c Release -o /out

# Use the official .NET runtime image for the runtime stage
FROM mcr.microsoft.com/dotnet/aspnet:9.0

# Set the working directory in the container
WORKDIR /app

# Copy the published output from the build stage
COPY --from=build /out ./

# Expose the port the application runs on
EXPOSE 80

# Set the entry point for the container
ENTRYPOINT ["dotnet", "client-repo.dll"]

4. Update Program.cs

Copy the web sample.

using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
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();

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

5. get_customer_information

YOu can change the functionality of the server to be able to get information for a given github user.

using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
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();

[McpServerToolType]
public static class EchoTool
{
    [McpServerTool, Description("Echoes the message back to the client.")]
    public static string Echo(string message) => $"This text is echoed from MCP -  {message}";

    [McpServerTool, Description("Gets the information of a given user")]
    public static async Task<string> get_user_information(string handle)
    {
        using var httpClient = new HttpClient();
        httpClient.DefaultRequestHeaders.UserAgent.TryParseAdd("request");

        var response = await httpClient.GetAsync($"https://api.github.com/users/{handle}");
        if (response.IsSuccessStatusCode)
        {
            var content = await response.Content.ReadAsStringAsync();
            return content;
        }
        else
        {
            return $"Error: Unable to fetch user information. Status Code: {response.StatusCode}";
        }
    }
}

Refresh the docker image with the new code.

docker build -t mcp-dotnet .

Installation building locally

Follow these steps to build locally and run the server using Docker.

  1. Clone the Repository:

    git clone https://github.com/octodemo/mcp-dotnet
    cd mcp-dotnet
    
  2. Build container:

    docker build -t mcp-dotnet .
    
  3. Configure MCP Client

To use this MCP server in your own MCP client, you can reference it as a tool in your MCP configuration. Below is an example of how to configure the MCP server in your client:

{
  "Servers": {
    "mcp-dotnet": {
      "command": "docker",
      "args": [ "run", "-i", "--rm", "mcp-dotnet" ]
    }
  }
}

Installation using ghcr.io image

Configure MCP Client to use the image from ghcr.io

{
  "Servers": {
    "mcp-dotnet": {
      "command": "docker",
      "args": [ "run", "-i", "--rm", "ghcr.io/octodemo/mcp-dotnet" ]
    }
  }
}

push image to ghcr.io

Skip any step if you have already done it.

# login to gh with package write scope
gh auth refresh --scopes write:packages

# set credentials for docker for ghcr.io
gh auth token | docker login ghcr.io -u <MY_HANDLE> --password-stdin

# tag image mcp-dotnet
docker tag mcp-dotnet ghcr.io/octodemo/mcp-dotnet:latest

# push the image to ghcr.io
docker push ghcr.io/octodemo/mcp-dotnet

相关推荐

  • av
  • Ejecute sin esfuerzo LLM Backends, API, frontends y servicios con un solo comando.

  • 1Panel-dev
  • 🔥 1Panel proporciona una interfaz web intuitiva y un servidor MCP para administrar sitios web, archivos, contenedores, bases de datos y LLM en un servidor de Linux.

  • WangRongsheng
  • 🧑‍🚀 全世界最好的 llM 资料总结(数据处理、模型训练、模型部署、 O1 模型、 MCP 、小语言模型、视觉语言模型) | Resumen de los mejores recursos del mundo.

  • Byaidu
  • Traducción de papel científico en PDF con formatos preservados - 基于 Ai 完整保留排版的 PDF 文档全文双语翻译 , 支持 支持 支持 支持 支持 支持 支持 支持 支持 支持 支持 支持 等服务 等服务 等服务 提供 提供 提供 提供 提供 提供 提供 提供 提供 提供 提供 提供 cli/mcp/docker/zotero

  • rulego
  • ⛓️Rulego es un marco de motor de regla de orquestación de componentes de alta generación de alto rendimiento, de alto rendimiento y de alto rendimiento para GO.

  • sigoden
  • Cree fácilmente herramientas y agentes de LLM utilizando funciones Plain Bash/JavaScript/Python.

  • hkr04
  • SDK liviano C ++ MCP (Protocolo de contexto del modelo)

  • RockChinQ
  • 😎简单易用、🧩丰富生态 - 大模型原生即时通信机器人平台 | 适配 Qq / 微信(企业微信、个人微信) / 飞书 / 钉钉 / Discord / Telegram / Slack 等平台 | 支持 Chatgpt 、 Deepseek 、 DiFy 、 Claude 、 Gemini 、 Xai 、 PPIO 、 Ollama 、 LM Studio 、阿里云百炼、火山方舟、 Siliconflow 、 Qwen 、 Moonshot 、 Chatglm 、 SillyTraven 、 MCP 等 LLM 的机器人 / Agente | Plataforma de bots de mensajería instantánea basada en LLM, admite Discord, Telegram, WeChat, Lark, Dingtalk, QQ, Slack

  • dmayboroda
  • Trapo conversacional local con contenedores configurables

  • paulwing
  • Un repositorio de pruebas creado con el servicio MCP

    Reviews

    5 (2)
    Avatar
    user_OxqfFVGf
    2025-04-24

    As a dedicated user of mcp-dotnet, I find it incredibly efficient and versatile for my .NET projects. Created by rulasg, this tool streamlines my development process, boosting productivity. Highly recommend it for any developer looking to improve their workflow.

    Avatar
    user_DnUmaYc9
    2025-04-24

    As a devoted user of mcp-dotnet by rulasg, I can confidently say that this product truly enhances development efficiency. It's intuitive and integrates seamlessly with various frameworks. The detailed documentation and community support make it a go-to for both beginners and advanced developers. Highly recommend it for anyone in the .NET ecosystem!