ModelContextProtocol.NET.Server
0.3.2-alpha
dotnet add package ModelContextProtocol.NET.Server --version 0.3.2-alpha
NuGet\Install-Package ModelContextProtocol.NET.Server -Version 0.3.2-alpha
<PackageReference Include="ModelContextProtocol.NET.Server" Version="0.3.2-alpha" />
paket add ModelContextProtocol.NET.Server --version 0.3.2-alpha
#r "nuget: ModelContextProtocol.NET.Server, 0.3.2-alpha"
// Install ModelContextProtocol.NET.Server as a Cake Addin #addin nuget:?package=ModelContextProtocol.NET.Server&version=0.3.2-alpha&prerelease // Install ModelContextProtocol.NET.Server as a Cake Tool #tool nuget:?package=ModelContextProtocol.NET.Server&version=0.3.2-alpha&prerelease
ModelContextProtocol.NET
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(<see below>);
builder.Tools.AddHandler<YourToolHandler>();
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<T>
.
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
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net9.0 is compatible. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. |
-
net9.0
- FluentValidation (>= 11.11.0)
- Microsoft.Extensions.DependencyInjection (>= 9.0.1)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 9.0.1)
- Microsoft.Extensions.Logging (>= 9.0.1)
- Microsoft.Extensions.Logging.Abstractions (>= 9.0.1)
- ModelContextProtocol.NET.Core (>= 0.3.2-alpha)
NuGet packages (1)
Showing the top 1 NuGet packages that depend on ModelContextProtocol.NET.Server:
Package | Downloads |
---|---|
ModelContextProtocol.NET.Server.Hosting
Hosting extensions for the Model Context Protocol (MCP) Server |
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last updated |
---|---|---|
0.3.2-alpha | 44 | 1/25/2025 |
0.3.1-alpha | 38 | 1/18/2025 |
0.1.0-alpha | 54 | 12/18/2024 |