C

ModelContextProtocol.NET

...
Created 12/18/2024bysalty-flower

Language:

C#

Stars:

36

Forks:

4

ModelContextProtocol.NET

NuGet Version

A C# SDK implementation of the Model Context Protocol (MCP).

Features

Ready to Use

  • ✅ Standard I/O Communication
  • ✅ Tool Integration Framework
  • ✅ Native AOT Compatible
  • ✅ Calculator Demo Implementation

Under Development

  • 🚧 WebSocket Support
  • 🚧 Resource Management
  • 🚧 Prompt System

Demo

See src/ModelContextProtocol.NET.Demo.Calculator for a fully functional calculator demo that covers:

  • Logging setup
  • Tool handler implementation
  • Request/response handling
  • Error management

Getting Started

Install the server package:

dotnet add package ModelContextProtocol.NET.Server --prerelease

For hosting integration, add the server hosting package:

dotnet add package ModelContextProtocol.NET.Server.Hosting --prerelease

A. Without Hosting

// Create server info
var serverInfo = new Implementation { Name = "Calculator Demo Server", Version = "1.0.0" };

// Configure and build server
var builder = new McpServerBuilder(serverInfo).AddStdioTransport();
builder.Services.AddLogging();
builder.Tools.AddHandler();
builder.Tools.AddFunction(
    name: "YourToolName",
    description: "YourToolDescription",
    parameterTypeInfo: YourParameterTypeJsonContext.Default.YourParameterType,
    handler: (YourParameterType parameters, CancellationToken ct) => {
        // Your tool implementation
    }
);
// ...

var server = builder.Build();
server.Start();
await Task.Delay(-1); // Wait indefinitely

B. With Hosting

var builder = Host.CreateApplicationBuilder();
builder.Services.AddMcpServer(serverInfo, mcp => {
    mcp.AddStdioTransport();
    // same as without hosting

            
        
            
                }, keepDefaultLogging: false); // clear default console logging
// ...

var host = builder.Build();
await host.RunAsync();

Logging Configuration

McpServerBuilder uses Microsoft.Extensions.Logging.ILogger as the logging interface. When stdio transport is used, logs can't be sent to the console. You need to configure a logging provider that writes to other logging destination.

If no logging provider is configured, a null logger will be used, resulting in no logs being written.

The generic host builder will add console-based logger by default, namely ILoggerFactory and ILogger. AddMcpServer will remove them by default for the same reason as above. If you still want to keep them, set keepDefaultLogging to true in AddMcpServer.

// Using Serilog
.ConfigureLogging(logging => logging.AddSerilog(yourSerilogLogger))

// Using NLog
.ConfigureLogging(logging => logging.AddNLog())

Implementing Tools

Tools are implemented as handlers. For NativeAOT compatibility, a JsonTypeInfo is required for the parameter type. Then you can either implement a handler class to enjoy dependency injection etc., or supply a function directly.

More documentation and implementation guide coming soon.

Technical Details

Architecture

The project is structured into multiple components:

  • Core Library: Contains the fundamental protocol implementation
  • Server Components: Handles communication and request processing
  • Server Hosting: Integrates MCP server to .NET generic host
  • Demo: Provides working examples
    • a calculator application

Development Status

This project is under active development. While core features like stdio communication and tool integration are complete and usable, some advanced features are still being implemented.

Contributing

Contributions are welcome! Please feel free to submit pull requests or create issues for bugs and feature requests.

License

Apache 2.0

Last updated: 2/28/2025

Publisher info

salty-flower's avatar

salty-flower

Singapore
7
followers
10
following
11
repos

More MCP servers built with C#

MCPSqlServer

SQL Server MCP Server for Windsurf IDE - A standalone MCP server providing SQL Server integration capabilities

By ian-cowley2
MCPSharp

MCPSharp is a .NET library that helps you build Model Context Protocol (MCP) servers and clients - the standardized API protocol used by AI assistants and models.

By afrise78