Add file-based MCP server sample for .NET 10 (#3) (#931)

This commit is contained in:
Peder Holdgaard Pedersen 2025-11-12 21:59:14 +01:00 • committed by GitHub
commit 54576033eb
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -0,0 +1,30 @@
#!/usr/bin/env -S dotnet run --
#:package Microsoft.Extensions.Hosting
#:project ../../src/ModelContextProtocol/ModelContextProtocol.csproj
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using ModelContextProtocol.Server;
using System.ComponentModel;
var builder = Host.CreateApplicationBuilder(args);
builder.Services.AddMcpServer()
.WithStdioServerTransport()
.WithTools<EchoTool>();
builder.Logging.AddConsole(options =>
{
options.LogToStandardErrorThreshold = LogLevel.Trace;
});
await builder.Build().RunAsync();
// File-scoped tool class
[McpServerToolType]
file class EchoTool
{
[McpServerTool(Name = "echo"), Description("Echoes the message back to the client.")]
public static string Echo([Description("The message to echo back.")] string message) => $"Echo: {message}";
}