MCP是一个典型的C/S架构模式,即客户端 和 服务端,它们之间采用一种标准的消息格式(JSON-RPC)进行通信.
The MCP C# SDK has great samples of creating MCP server, MCP client
MCP Server in C# .NET
Table of Contents
To get started MCP SSE Server:
Create a ASP.NET Core Web API project.
Dotnet add package "ModelContextProtocol.AspNetCore.
Update your Program.cs to set up the MCP SSE server.
Configure MCP SSE server to use the exit Tools.
Simple MCP SSE Server Server in C#
Step 1: Create a ASP.NET Core Web API project
创建一个.NET 8.0 ASP.NET WebAPI应用.基于ASP.NET Core web API的MCP SSE Server
Step 2: Add Required Packages MCP SDK
use the modelcontextprotocol / csharp-sdk library.This library provides a simple way to create an MCP server and now supports SSE.
dotnet add package "ModelContextProtocol.AspNetCore" Version="0.2.0-preview.1"
安装MCP SDK
<ItemGroup>
<PackageReference Include="ModelContextProtocol.AspNetCore" Version="0.2.0-preview.1" />
<PackageReference Include="Microsoft.Extensions.Hosting" Version="9.0.5" />
</ItemGroup>
https://github.com/iaspnetcore/MCPServer/commit/fd50c9b5fd9c8d9e46b6dde9e4f99f7bac57d24f
The official C# SDK for Model Context Protocol servers and clients
ModelContextProtocol sdk
https://github.com/modelcontextprotocol/csharp-sdk
Step 3: Define Tools
创建一个Tools目录,然后添加一个TimeTool.cs
src/McpServerConsole.Server/Tools/TimeTool.cs
using ModelContextProtocol.Server;
using System.ComponentModel;
namespace MCPSSEServer.Server.Tools
{
/// <summary>
/// 静态类型 不能用作 参数 WithTools<TimeTool>();
/// </summary>
[McpServerToolType]
public class TimeTool
{
[McpServerTool, Description("Get the current time for a city")]
public static string GetCurrentTime(string city) =>
$"It is {DateTime.Now.Hour}:{DateTime.Now.Minute} in {city}.";
}
}
https://github.com/iaspnetcore/MCPServer/commit/1f94a3f1c78c2aac376cff24d9317df696806eb0
step 4. Update Program.cs - Update your Program.cs to set up the MCP SSE server
Update your Program.cs to set up the MCP SSE server
服务端配置(Program.cs)
using ModelContextProtocol.AspNetCore;
using System.ComponentModel;
using MCPSSEServer.Server.Tools;
namespace MCPSSEServer.Server
{
public class Program
{
public static void Main(string[] args)
{
Console.WriteLine("Starting MCPSSEServer Server...");
var builder = WebApplication.CreateBuilder(args);
builder.Services
// Add MCP Server to IoC
.AddMcpServer()
//SSE
.WithHttpTransport()
// Register MCP Tool
.WithTools<TimeTool>();
// Add services to the container.
builder.Services.AddControllers();
var app = builder.Build();
// Map Mcp endpoints
app.MapMcp();
// Configure the HTTP request pipeline.
app.UseAuthorization();
app.MapControllers();
app.Run();
}
}
}
WithToolsFromAssembly扩展方法,会自动扫描程序集中添加了McpServerTool标签的类进行注册
https://github.com/iaspnetcore/MCPServer/commit/33e996ee7c6707871ba7441d3f78cabbe2d117b4
2.MCP includes two standard transport(Built-in Transport Types) implementations
Standard Input/Output (stdio)标准IO传输方式-> .WithStdioServerTransport
Server-Sent Events (SSE) SSE传输方式 ->
Note:
The SSE Transport has been deprecated as of MCP specification version 2025-03-26. Please use the HTTP Stream Transport instead, which implements the new Streamable HTTP transport specification.
step 5. run your MCP server
cd F:\developer_mcp\src\MCPSSEServer.Server
dotnet run
Starting MCPSSEServer Server...
info: Microsoft.Hosting.Lifetime[14]
Now listening on: http://localhost:5159
info: Microsoft.Hosting.Lifetime[0]
Application started. Press Ctrl+C to shut down.
info: Microsoft.Hosting.Lifetime[0]
Hosting environment: Development
info: Microsoft.Hosting.Lifetime[0]
Content root path: F:\developer_mcp\src\MCPSSEServer.Server
Step 6.Test the server
test the server using any web browser.
http://localhost:5159/sse
output
event: endpoint
data: /message?sessionId=VoXW78yYf0lPCcaYcgUibQ
To use the MCP server, we can use Claude, Visual Studio Code, or any other client supporting MCP with SSE.
Building MCP Client in C#-McpServerConsole.Client
use any MCP client to send requests to the MCPSSEServer server.
Step 1: Create a Console Application named with MCPSSEServer.Client
MCPSSEServer: create MCPSSEServer.Client Console APP
Step 2: Add Required Packages
dotnet add package ModelContextProtocol" Version="0.2.0-preview.1
step 3. Update your Program.cs to connect the MCP SSE server
step 4. List avalible tools
F:\developer_mcp
https://github.com/iaspnetcore/MCPServer/tree/master/src/MCPSSEServer.Server
blog:https://www.iaspnetcore.com/blogpost-683471ea5fb02c067703a799-mcp-server-in-c-nettransporttypesse
https://modelcontextprotocol.io/docs/concepts/transports
1.https://www.cnblogs.com/edisonchou/p/-/introduction-to-mcp-csharp-sdk
2.https://anuraj.dev/blog/building-a-server-sent-events-sse-mcp-server-with-aspnetcore/
3.https://laurentkempe.com/2025/04/05/sse-powered-mcp-server-with-csharp-and-dotnet-in-157mb-executable/
https://github.com/laurentkempe/aiPlayground/blob/main/MCPSSEServer/Program.cs
4.
https://github.com/modelcontextprotocol/csharp-sdk/tree/main/samples/AspNetCoreSseServer