Performs basic arithmetic calculations (from the demo implementation)
ModelContextProtocol.NET provides a comprehensive C# SDK for implementing Model Context Protocol (MCP) servers. It offers a robust framework for building tool integrations that can be used with AI models supporting the MCP standard. The library is designed to be Native AOT compatible and includes features like Standard I/O communication, a tool integration framework, and error management capabilities. With ModelContextProtocol.NET, developers can easily create custom tools that extend AI model capabilities, implement handlers for specific functionalities, and integrate with .NET hosting environments. The project is actively maintained and includes both core server components and practical demos to help developers get started quickly.
To get started with ModelContextProtocol.NET, you'll need to install the required NuGet packages:
dotnet add package ModelContextProtocol.NET.Server --prerelease
dotnet add package ModelContextProtocol.NET.Server.Hosting --prerelease
There are two main approaches to implementing an MCP server with this library:
// Create server info
var serverInfo = new Implementation { Name = "Your Server Name", Version = "1.0.0" };
// Configure and build server
var builder = new McpServerBuilder(serverInfo).AddStdioTransport();
// Add logging (required when using stdio transport)
builder.Services.AddLogging(/* configure your logging provider */);
// Add tool handlers
builder.Tools.AddHandler<YourToolHandler>();
// Add function-based tools
builder.Tools.AddFunction(
name: "yourToolName",
description: "Description of what your tool does",
parameterTypeInfo: YourParameterTypeJsonContext.Default.YourParameterType,
handler: (YourParameterType parameters, CancellationToken ct) => {
// Your tool implementation
return Task.FromResult(new YourReturnType { /* results */ });
}
);
// Build and start the server
var server = builder.Build();
server.Start();
await Task.Delay(-1); // Wait indefinitely
var builder = Host.CreateApplicationBuilder();
builder.Services.AddMcpServer(
new Implementation { Name = "Your Server Name", Version = "1.0.0" },
mcp => {
mcp.AddStdioTransport();
// Add your tools
mcp.Tools.AddHandler<YourToolHandler>();
mcp.Tools.AddFunction(/* same as above */);
},
keepDefaultLogging: false // clear default console logging
);
// Build and run the host
var host = builder.Build();
await host.RunAsync();
Since the MCP server uses Standard I/O for communication with AI models, you can't use console-based logging. You'll need to configure a different logging provider:
// Using Serilog
builder.Services.AddLogging(logging => logging.AddSerilog(yourSerilogLogger));
// Using NLog
builder.Services.AddLogging(logging => logging.AddNLog());
Tools are the core functionality of an MCP server. There are two ways to implement them:
builder.Tools.AddFunction(
name: "calculate",
description: "Performs basic arithmetic calculations",
parameterTypeInfo: CalculatorJsonContext.Default.CalculationRequest,
handler: (CalculationRequest request, CancellationToken ct) => {
// Implement calculation logic
return Task.FromResult(new CalculationResponse { Result = /* calculated value */ });
}
);
// Define your handler class
public class CalculatorHandler : IToolHandler
{
private readonly ILogger<CalculatorHandler> _logger;
public CalculatorHandler(ILogger<CalculatorHandler> logger)
{
_logger = logger;
}
public string Name => "calculate";
public string Description => "Performs basic arithmetic calculations";
public async Task<object> HandleAsync(JsonElement parameters, CancellationToken cancellationToken)
{
// Parse parameters and implement calculation logic
// Return result
}
}
// Register your handler
builder.Tools.AddHandler<CalculatorHandler>();
For a complete working example, refer to the calculator demo included in the repository (src/ModelContextProtocol.NET.Demo.Calculator
). This demo covers:
The library is actively being developed with additional features coming soon:
Check the project's GitHub repository for the latest updates and features.