No description available
A C# SDK implementation of the Model Context Protocol (MCP).
See src/ModelContextProtocol.NET.Demo.Calculator for a fully functional calculator demo that covers:
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
// 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
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();
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())
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.
The project is structured into multiple components:
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.
Contributions are welcome! Please feel free to submit pull requests or create issues for bugs and feature requests.
Apache 2.0
No configuration available
Related projects feature coming soon
Will recommend related projects based on sub-categories