Skip to content

Commit b9996c6

Browse files
authored
feat: csharp mcp (#21)
* feat: csharp mcp * chore: entrypoint.sh * chore: remove unused file * fix: basic image * fix: Dockerfile bug * fix: dockerfile bug * fix: bug * fix: bug * chore: update README.md
1 parent d75da06 commit b9996c6

File tree

10 files changed

+232
-0
lines changed

10 files changed

+232
-0
lines changed

Service/mcp/csharp-1-0/Dockerfile

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
FROM ghcr.io/labring-actions/devbox/debian-ssh-12.6:547a61
2+
3+
USER root
4+
RUN cd /home/devbox/project && \
5+
rm -rf ./*
6+
7+
COPY /Service/mcp/csharp-1-0/project /home/devbox/project
8+
9+
RUN chown -R devbox:devbox /home/devbox/project && \
10+
chmod -R u+rw /home/devbox/project && \
11+
chmod -R +x /home/devbox/project/entrypoint.sh
12+
13+
# Install dotnet
14+
RUN apt update && \
15+
apt install -y wget && \
16+
wget https://packages.microsoft.com/config/debian/12/packages-microsoft-prod.deb -O packages-microsoft-prod.deb && \
17+
sudo dpkg -i packages-microsoft-prod.deb && \
18+
rm packages-microsoft-prod.deb && \
19+
sudo apt-get update && \
20+
sudo apt-get install -y dotnet-sdk-9.0 && \
21+
chown -R devbox:devbox /home/devbox/project && \
22+
chmod -R u+rw /home/devbox/project && \
23+
chmod -R +x /home/devbox/project/entrypoint.sh && \
24+
rm -rf /var/lib/apt/lists/*
25+
26+
27+
RUN mkdir -p /home/devbox/.devbox
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# Build results
2+
[Dd]ebug/
3+
[Dd]ebugPublic/
4+
[Rr]elease/
5+
[Rr]eleases/
6+
x64/
7+
x86/
8+
[Ww][Ii][Nn]32/
9+
[Aa][Rr][Mm]/
10+
[Aa][Rr][Mm]64/
11+
bld/
12+
[Bb]in/
13+
[Oo]bj/
14+
[Ll]og/
15+
[Ll]ogs/
16+
17+
# Visual Studio files
18+
.vs/
19+
*.user
20+
*.userosscache
21+
*.sln.docstates
22+
*.userprefs
23+
24+
# Visual Studio Code
25+
.vscode/
26+
*.code-workspace
27+
28+
# User-specific files
29+
*.rsuser
30+
*.suo
31+
*.user
32+
*.userosscache
33+
*.sln.docstates
34+
35+
# Environment files
36+
appsettings.Development.json
37+
38+
# Temporary files
39+
*.tmp
40+
*.temp
41+
*.swp
42+
*~
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<Project Sdk="Microsoft.NET.Sdk.Web">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net9.0</TargetFramework>
5+
<Nullable>enable</Nullable>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
</PropertyGroup>
8+
9+
<ItemGroup>
10+
<PackageReference Include="ModelContextProtocol" Version="0.1.0-preview.4" />
11+
<PackageReference Include="ModelContextProtocol.AspNetCore" Version="0.1.0-preview.4" />
12+
</ItemGroup>
13+
14+
</Project>
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
using ModelContextProtocol.AspNetCore;
2+
3+
var builder = WebApplication.CreateBuilder(args);
4+
builder.Services.AddMcpServer().WithToolsFromAssembly();
5+
6+
builder.Services.AddCors(options =>
7+
{
8+
options.AddDefaultPolicy(policy =>
9+
{
10+
policy.WithOrigins()
11+
.AllowAnyHeader()
12+
.AllowAnyMethod();
13+
});
14+
});
15+
16+
var app = builder.Build();
17+
18+
app.UseCors();
19+
app.MapMcp();
20+
21+
app.Run();
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
"$schema": "https://json.schemastore.org/launchsettings.json",
3+
"profiles": {
4+
"http": {
5+
"commandName": "Project",
6+
"dotnetRunMessages": true,
7+
"launchBrowser": true,
8+
"applicationUrl": "http://localhost:3001",
9+
"environmentVariables": {
10+
"ASPNETCORE_ENVIRONMENT": "Development"
11+
}
12+
},
13+
"https": {
14+
"commandName": "Project",
15+
"dotnetRunMessages": true,
16+
"launchBrowser": true,
17+
"applicationUrl": "https://localhost:7133;http://localhost:3001",
18+
"environmentVariables": {
19+
"ASPNETCORE_ENVIRONMENT": "Development"
20+
}
21+
}
22+
}
23+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# C# MCP Server Example
2+
3+
This is a C# and ASP.NET Core-based MCP (Model Context Protocol) server example project that demonstrates basic server functionality implementation.
4+
5+
## Project Description
6+
7+
This project creates a lightweight MCP server using the ASP.NET Core framework. The server runs on port 3001 by default and provides example tools such as Echo and LLM sampling. The project supports both development and production environment modes with graceful shutdown capabilities.
8+
9+
## Environment
10+
11+
This project runs on a Debian 12 system with .NET 9.0 SDK. The development environment is pre-configured in the Devbox environment, so you don't need to worry about installing .NET SDK or system dependencies. The development environment includes all necessary tools for building and running C# applications. If you need to make adjustments to match your specific requirements, you can modify the configuration files accordingly.
12+
13+
## Project Execution
14+
15+
**Development mode:** For normal development environment, simply enter Devbox and run `bash entrypoint.sh` in the terminal. This will compile your C# code with debugging flags and run the executable.
16+
17+
**Production mode:** After release, the project will be automatically packaged into a Docker image and deployed according to the `entrypoint.sh` script (run `bash entrypoint.sh production`). This will build an optimized executable binary and run it.
18+
19+
DevBox: Code. Build. Deploy. We've Got the Rest.
20+
With DevBox, you can focus entirely on writing great code while we handle the infrastructure, scaling, and deployment. Seamless development from start to production.
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using ModelContextProtocol.Server;
2+
using System.ComponentModel;
3+
4+
namespace TestServerWithHosting.Tools;
5+
6+
[McpServerToolType]
7+
public static class EchoTool
8+
{
9+
[McpServerTool, Description("Echoes the input back to the client.")]
10+
public static string Echo(string message)
11+
{
12+
return "hello " + message;
13+
}
14+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using Microsoft.Extensions.AI;
2+
using ModelContextProtocol.Server;
3+
using System.ComponentModel;
4+
5+
namespace TestServerWithHosting.Tools;
6+
7+
/// <summary>
8+
/// This tool uses dependency injection and async method
9+
/// </summary>
10+
[McpServerToolType]
11+
public static class SampleLlmTool
12+
{
13+
[McpServerTool(Name = "sampleLLM"), Description("Samples from an LLM using MCP's sampling feature")]
14+
public static async Task<string> SampleLLM(
15+
IMcpServer thisServer,
16+
[Description("The prompt to send to the LLM")] string prompt,
17+
[Description("Maximum number of tokens to generate")] int maxTokens,
18+
CancellationToken cancellationToken)
19+
{
20+
ChatMessage[] messages =
21+
[
22+
new(ChatRole.System, "You are a helpful test server."),
23+
new(ChatRole.User, prompt),
24+
];
25+
26+
ChatOptions options = new()
27+
{
28+
MaxOutputTokens = maxTokens,
29+
Temperature = 0.7f,
30+
};
31+
32+
var samplingResponse = await thisServer.AsSamplingChatClient().GetResponseAsync(messages, options, cancellationToken);
33+
34+
return $"LLM sampling result: {samplingResponse}";
35+
}
36+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"Logging": {
3+
"LogLevel": {
4+
"Default": "Information",
5+
"Microsoft.AspNetCore": "Warning"
6+
}
7+
},
8+
"AllowedHosts": "*"
9+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#!/bin/bash
2+
3+
app_env=${1:-development}
4+
5+
# Development environment commands
6+
dev_commands() {
7+
echo "Running MCP Csharp development environment..."
8+
dotnet run
9+
}
10+
11+
# Production environment commands
12+
prod_commands() {
13+
echo "Running MCP Csharp production build..."
14+
dotnet publish
15+
echo "Starting MCP Csharp production preview server..."
16+
dotnet run -- --host 0.0.0.0 --port 3001
17+
}
18+
19+
# Check environment variables to determine the running environment
20+
if [ "$app_env" = "production" ] || [ "$app_env" = "prod" ] ; then
21+
echo "Production environment detected"
22+
prod_commands
23+
else
24+
echo "Development environment detected"
25+
dev_commands
26+
fi

0 commit comments

Comments
 (0)