⚠️ Important: This solution has been updated to use the stdio transport as recommended by MCP Specification 2025-06-18. The original SSE transport has been deprecated.
This .NET solution demonstrates how to build an MCP server using the current stdio transport. The stdio transport is simpler, more secure, and provides better performance than the deprecated SSE approach.
- .NET 9.0 SDK or later
- Basic understanding of .NET dependency injection
dotnet restoredotnet buildThe stdio server runs differently than the old HTTP-based server. Instead of starting a web server, it communicates through stdin/stdout:
dotnet runImportant: The server will appear to hang - this is normal! It's waiting for JSON-RPC messages from stdin.
npx @modelcontextprotocol/inspector dotnet runThis will:
- Launch your server as a subprocess
- Open a web interface for testing
- Allow you to test all server tools interactively
You can also test by launching the Inspector directly:
npx @modelcontextprotocol/inspector dotnet run --project .The server provides these tools:
- AddNumbers(a, b): Add two numbers together
- MultiplyNumbers(a, b): Multiply two numbers together
- GetGreeting(name): Generate a personalized greeting
- GetServerInfo(): Get information about the server
To use this server with Claude Desktop, add this configuration to your claude_desktop_config.json:
{
"mcpServers": {
"example-stdio-server": {
"command": "dotnet",
"args": ["run", "--project", "path/to/server.csproj"]
}
}
}dotnet/
├── Program.cs # Main server setup and configuration
├── Tools.cs # Tool implementations
├── server.csproj # Project file with dependencies
├── server.sln # Solution file
├── Properties/ # Project properties
└── README.md # This file
stdio transport (Current):
- ✅ Simpler setup - no web server needed
- ✅ Better security - no HTTP endpoints
- ✅ Uses
Host.CreateApplicationBuilder()instead ofWebApplication.CreateBuilder() - ✅
WithStdioTransport()instead ofWithHttpTransport() - ✅ Console application instead of web application
- ✅ Better performance
HTTP/SSE transport (Deprecated):
- ❌ Required ASP.NET Core web server
- ❌ Needed
app.MapMcp()routing setup - ❌ More complex configuration and dependencies
- ❌ Additional security considerations
- ❌ Now deprecated in MCP 2025-06-18
- Dependency Injection: Full DI support for services and logging
- Structured Logging: Proper logging to stderr using
ILogger<T> - Tool Attributes: Clean tool definition using
[McpServerTool]attributes - Async Support: All tools support async operations
- Error Handling: Graceful error handling and logging
- Use
ILoggerfor logging (never write to stdout directly) - Build with
dotnet buildbefore testing - Test with the Inspector for visual debugging
- All logging goes to stderr automatically
- The server handles graceful shutdown signals
This solution follows the current MCP specification and demonstrates best practices for stdio transport implementation using .NET.