Back to MCP Catalog

ModelContextProtocol.NET MCP Server

Developer ToolsC#
A C# SDK implementation of the Model Context Protocol
Available Tools

calculate

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.

Installation

To get started with ModelContextProtocol.NET, you'll need to install the required NuGet packages:

  1. Install the server package:
dotnet add package ModelContextProtocol.NET.Server --prerelease
  1. For hosting integration, add the server hosting package:
dotnet add package ModelContextProtocol.NET.Server.Hosting --prerelease

Basic Usage

There are two main approaches to implementing an MCP server with this library:

Without Hosting

// 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

With Hosting

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();

Logging Configuration

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());

Implementing Tools

Tools are the core functionality of an MCP server. There are two ways to implement them:

  1. Function-based tools: Quick and simple for straightforward tools
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 */ });
    }
);
  1. Handler-based tools: Better for complex tools that need dependency injection
// 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>();

Example Implementation

For a complete working example, refer to the calculator demo included in the repository (src/ModelContextProtocol.NET.Demo.Calculator). This demo covers:

  • Proper logging setup
  • Tool handler implementation
  • Request/response handling
  • Error management

Advanced Features

The library is actively being developed with additional features coming soon:

  • WebSocket support for alternative communication channels
  • Resource management for handling files and other resources
  • Prompt system for more sophisticated interactions

Check the project's GitHub repository for the latest updates and features.

Related MCPs

Apple Shortcuts
Developer ToolsJavaScript

Control Apple Shortcuts automations from AI assistants

Clojars Dependency Lookup
Developer ToolsJavaScript

Fetch dependency information from Clojars, the Clojure community's artifact repository

Simple Timeserver
Developer ToolsPython

Provides Claude with current time and timezone information

About Model Context Protocol

Model Context Protocol (MCP) allows AI models to access external tools and services, extending their capabilities beyond their training data.

Generate Cursor Documentation

Save time on coding by generating custom documentation and prompts for Cursor IDE.