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
  • 毫不费力地使用一个命令运行LLM后端,API,前端和服务。

  • 1Panel-dev
  • 🔥1Panel提供了直观的Web接口和MCP服务器,用于在Linux服务器上管理网站,文件,容器,数据库和LLMS。

  • WangRongsheng
  • 🧑‍🚀 llm 资料总结(数据处理、模型训练、模型部署、 o1 模型、mcp 、小语言模型、视觉语言模型)|摘要世界上最好的LLM资源。

  • rulego
  • ⛓️Rulego是一种轻巧,高性能,嵌入式,下一代组件编排规则引擎框架。

  • Byaidu
  • PDF科学纸翻译带有保留格式的pdf -基于ai完整保留排版的pdf文档全文双语翻译

  • sigoden
  • 使用普通的bash/javascript/python函数轻松创建LLM工具和代理。

  • RockChinQ
  • 😎简单易用、🧩丰富生态 -大模型原生即时通信机器人平台| 适配QQ / 微信(企业微信、个人微信) /飞书 /钉钉 / discord / telegram / slack等平台| 支持chatgpt,deepseek,dify,claude,基于LLM的即时消息机器人平台,支持Discord,Telegram,微信,Lark,Dingtalk,QQ,Slack

  • hkr04
  • 轻巧的C ++ MCP(模型上下文协议)SDK

  • dmayboroda
  • 带有可配置容器的本地对话抹布

  • modelscope
  • 开始以更轻松的方式开始构建具有LLM授权的多代理应用程序。

    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!