
MCP-Dotnet
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.
-
Clone the Repository:
git clone https://github.com/octodemo/mcp-dotnet cd mcp-dotnet
-
Build container:
docker build -t mcp-dotnet .
-
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
相关推荐
😎简单易用、🧩丰富生态 -大模型原生即时通信机器人平台| 适配QQ / 微信(企业微信、个人微信) /飞书 /钉钉 / discord / telegram / slack等平台| 支持chatgpt,deepseek,dify,claude,基于LLM的即时消息机器人平台,支持Discord,Telegram,微信,Lark,Dingtalk,QQ,Slack
Reviews

user_OxqfFVGf
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.

user_DnUmaYc9
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!