ModelContextProtocol-NET
by: salty-flower
ModelContextProtocol.NET
📌Overview
Purpose: To provide a C# SDK implementation of the Model Context Protocol (MCP) that facilitates communication between tools and applications.
Overview: ModelContextProtocol.NET is an SDK designed to create and manage servers compliant with the Model Context Protocol. It supports standard input/output communication and allows for easy tool integration through a structured architecture.
Key Features:
-
Standard I/O Communication: Facilitates seamless data exchange through standard input and output, ensuring compatibility across platforms.
-
Tool Integration Framework: Allows developers to implement and integrate custom tools within the MCP framework, enhancing functionality and usability.
-
Native AOT Compatible: Supports Native Ahead-Of-Time (AOT) compilation, enabling better performance and smaller binaries for applications.
-
Calculator Demo Implementation: Provides a fully functional calculator demo that showcases practical implementation of features, including logging, request handling, and error management.
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
Refer to src/ModelContextProtocol.NET.Demo.Calculator
for a fully functional calculator demo covering:
- 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 cannot be sent to the console. Configure a logging provider that writes to other destinations.
If no logging provider is configured, a null logger will be used, resulting in no logs.
The generic host builder adds console-based logger by default (ILoggerFactory
and ILogger<T>
). AddMcpServer
removes them by default for the same reason.
To keep console logging, 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.
You can either implement a handler class to benefit from dependency injection or supply a function directly.
More documentation and implementation guides coming soon.
Technical Details
Architecture
The project consists of several components:
- Core Library: Fundamental protocol implementation
- Server Components: Communication and request processing
- Server Hosting: Integration of MCP server with .NET generic host
- Demo: Working examples including a calculator application
Development Status
The project is actively developed. Core features like stdio communication and tool integration are complete and usable. Advanced features are under development.
Contributing
Contributions are welcome! Submit pull requests or create issues for bugs and feature requests.
License
Apache 2.0