using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using ModelContextProtocol.Server;
using System.ComponentModel;
var builder = Host.CreateApplicationBuilder(args);
builder.Logging.AddConsole(consoleLogOptions =>
{
// 配置所有日志输出到stderr
consoleLogOptions.LogToStandardErrorThreshold = LogLevel.Trace;
});
builder.Services
.AddMcpServer()
.WithStdioServerTransport()
.WithToolsFromAssembly();
await builder.Build().RunAsync();
[McpServerToolType]
public static class EchoTool
{
[McpServerTool, Description("将消息回显给客户端。")]
public static string Echo(string message) => $"hello {message}";
}
C# MCP服务器 日志配置 依赖注入 服务端工具
这段代码展示了如何使用C#配置一个基于McpServer的应用程序,包含日志设置和服务初始化,并实现了一个简单的消息回显功能。