MCP ModelContextProtocol netcore库

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}";
}

作者:spike

分类: Net

创作时间:2025-04-19

更新时间:2025-04-22

联系方式放在中括号之中例如[[email protected]],回复评论在开头加上标号例如:#1