NET8创建MCP-Stdio服务
1.安装依赖
dotnet add package ModelContextProtocol --prerelease
dotnet add package Microsoft.Extensions.Hosting
2.配置启动项
var builder = Host.CreateEmptyApplicationBuilder(settings: null);
builder.Services.AddMcpServer()
.WithStdioServerTransport()
.WithToolsFromAssembly();
var app = builder.Build();
await app.RunAsync();
3.工具类函数
[McpServerToolType]
public static class CalculateTool
{
///<summary>
// 计算加法
///</summary>
[McpServerTool, Description("计算俩个数的和")]
public static async Task<string> Add(
[Description("第一个数字")] int number1,
[Description("第二个数字")] int number2)
{
// 计算加法
int result = number1 + number2;
return $"计算结果: {result}";
}
}
4.客户端调用配置
{
"mcpServers": {
"tool": {
"command": "dotnet",
"args": [
"程序所在目录/Mcp_Stdio.dll"
],
"env": {
}
}
}
}