Server-Sent Events (SSE) provide an excellent transport mechanism for Model Context Protocol (MCP) servers, enabling real-time, unidirectional communication from server to client. In this comprehensive guide, we'll explore how to build a robust MCP server using SSE transport with .NET, covering everything from basic SSE implementation to advanced MCP server patterns.
Prerequisites
- Basic understanding of .NET and C# programming
- Familiarity with HTTP protocols and web APIs
- Knowledge of asynchronous programming concepts
- Understanding of MCP fundamentals
What You'll Learn
- What is SSE Transport
- Building basic SSE application
- Building an MCP Server with SSE Transport
- Claude configuration to work with SSE MCP
Understanding Server-Sent Events (SSE)
Server-Sent Events (SSE) is a web standard that allows a server to push data to a client over a single HTTP connection. Unlike WebSockets, SSE provides unidirectional communication from server to client, making it perfect for scenarios where you need to stream data or send real-time updates.
Key Benefits of SSE for MCP
- ✓Simplicity: Built on standard HTTP, no special protocols needed
- ✓Automatic Reconnection: Browsers automatically reconnect on connection loss
- ✓Event-driven: Perfect for MCP's message-based architecture
- ✓Firewall Friendly: Works through most corporate firewalls
- ✓Low Overhead: Minimal protocol overhead compared to WebSockets
SSE Message Format
data: {"type": "response", "id": "123", "result": {"success": true}}
data: {"type": "notification", "method": "progress", "params": {"percent": 50}}
data: {"type": "error", "id": "124", "error": {"code": -1, "message": "Invalid request"}}
Implementing Basic SSE server in .NET and Html
Let's start by implementing a basic SSE endpoint in .NET. This foundation will serve as the transport layer for our MCP server implementation.
Creating new project
dotnet new webapi
cd <project>
Creating Program.cs file
We will also add CORS to be able easily test it locally. For production, please make sure is it disabled.
using System.Text;
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddCors(options =>
{
options.AddPolicy("AllowAll", policy =>
{
policy.AllowAnyOrigin()
.AllowAnyMethod()
.AllowAnyHeader();
});
});
var app = builder.Build();
app.UseCors("AllowAll");
app.MapGet("/sse", async (HttpContext context) =>
{
context.Response.Headers.Add("Content-Type", "text/event-stream");
context.Response.Headers.Add("Cache-Control", "no-cache");
context.Response.Headers.Add("Connection", "keep-alive");
for (int i = 0; i < 10; i++)
{
var data = $"data: {DateTime.Now:O}\n\n";
var bytes = Encoding.UTF8.GetBytes(data);
await context.Response.Body.WriteAsync(bytes);
await context.Response.Body.FlushAsync();
await Task.Delay(1000); // 1 second
}
});
app.Run();
Creating index.html page
<!DOCTYPE html>
<html>
<body>
<h2>SSE from .NET</h2>
<pre id="output"></pre>
<script>
const output = document.getElementById("output");
const evtSource = new EventSource("http://localhost:5203/sse");
evtSource.onmessage = function(event) {
output.textContent += event.data + "\n";
};
</script>
</body>
</html>
Now build and run the SSE server, copy and paste the server url to the web page, and open the page. If everything is setup correctly the messages will start appear on the web page with 1 second delay.
Creating Program.cs for MCP SSE server
Now let's implement a MCP server which will use SSE Transport.
Create new project
dotnet new webapi
cd <project>
dotnet package add ModelContextProtocol.AspNetCore --prerelease
Create the server in Program.cs
using SSE;
var builder = WebApplication.CreateBuilder(args);
builder.Services
.AddMcpServer()
.WithHttpTransport()
.WithTools<Tools>();
builder.Services.AddHttpClient();
var app = builder.Build();
app.MapMcp();
app.Run();
Implement Tools.cs
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using ModelContextProtocol.Server;
namespace SSE;
[McpServerToolType]
public sealed class Tools
{
[McpServerTool, Description("Echo the input")]
public async Task<string> Echo([Description("The unput from the user"), Required] string input)
{
return await Task.FromResult(input);
}
}
Claude Integration and Testing
To test our SSE MCP server, let's update the 'claude_desktop_config.json' file. Claude does not have native SSE support yet, so we need to use mcp-remote tool.
Configuration
{
"mcpServers": {
"echo": {
"command": "npx",
"args": [
"mcp-remote",
"http://localhost:5071/sse"
]
}
}
}
Conclusion
Building an MCP server with SSE transport provides a robust, scalable solution for real-time AI tool integration. The combination of SSE's simplicity and MCP's standardized protocol creates a powerful foundation for building AI-powered applications that can stream responses and handle complex tool interactions.
Key benefits of this approach include automatic reconnection handling, firewall-friendly communication, and the ability to stream large responses without blocking the client. The .NET implementation provides excellent performance and scalability for production deployments.
Next Steps
- Implement custom tools specific to your use case
- Add state management
- Add authentication and authorization
- Set up monitoring and logging
- Deploy to your preferred cloud platform
- Integrate with other AI systems
Related Articles
Learn how to build a Model Context Protocol (MCP) server using .NET and C#. Step-by-step tutorial with code examples and best practices.
Learn how to containerize and deploy MCP servers using Docker with practical examples, Claude configuration, and production best practices.
Learn about Model Context Protocol (MCP) servers, their architecture, and how to build your own MCP server to extend AI capabilities.
Discover how implementing Model Context Protocol (MCP) can transform your business operations and drive sustainable growth.