WolfPack.FastMcpServer
1.0.1
dotnet add package WolfPack.FastMcpServer --version 1.0.1
NuGet\Install-Package WolfPack.FastMcpServer -Version 1.0.1
<PackageReference Include="WolfPack.FastMcpServer" Version="1.0.1" />
<PackageVersion Include="WolfPack.FastMcpServer" Version="1.0.1" />
<PackageReference Include="WolfPack.FastMcpServer" />
paket add WolfPack.FastMcpServer --version 1.0.1
#r "nuget: WolfPack.FastMcpServer, 1.0.1"
#:package WolfPack.FastMcpServer@1.0.1
#addin nuget:?package=WolfPack.FastMcpServer&version=1.0.1
#tool nuget:?package=WolfPack.FastMcpServer&version=1.0.1
WolfPack.FastMcpServer
一个 MCP (Model Context Protocol) 服务端快速开发库,支持依赖注入、自定义参数、多传输模式(Stdio、HTTP/SSE)和智能日志配置。
🚀 快速开始
using WolfPack.Mcp;
public class Program
{
public static async Task Main(string[] args)
{
await FastMcpServer.RunAsync(args, options =>
{
options.ServerName = "我的MCP服务器";
options.ConfigureMcp = mcpBuilder =>
{
mcpBuilder.WithTools<MyTools>();
};
});
}
}
启动服务器:
dotnet run # Stdio 模式(默认)
dotnet run -- --port 8080 # HTTP 模式
dotnet run -- --help # 显示所有选项
✨ 特性
- 🚀 一行启动: 用最少的代码创建 MCP 服务器
- 🔧 依赖注入: 完整的 .NET 依赖注入支持
- 🌐 多传输模式: Stdio、HTTP/SSE、双模式自动切换
- 📝 智能日志: 根据传输模式自动优化日志配置
- ⚙️ 自定义参数: 轻松添加命令行参数和配置
- 🛡️ 生产就绪: 内置错误处理、参数验证、帮助信息
📖 基本用法
1. 创建工具类
public class MyTools
{
[McpServerTool]
[Description("获取当前时间")]
public static string GetCurrentTime()
{
return DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
}
[McpServerTool]
[Description("两数相加")]
public static int Add(
[Description("第一个数字")] int a,
[Description("第二个数字")] int b)
{
return a + b;
}
}
2. 配置服务器
await FastMcpServer.RunAsync(args, options =>
{
options.ServerName = "我的计算器";
options.ServerVersion = "1.0.0";
// 注册工具
options.ConfigureMcp = mcpBuilder =>
{
mcpBuilder.WithTools<MyTools>();
};
// 配置依赖注入(可选)
options.ConfigureServices = services =>
{
services.AddSingleton<IMyService, MyService>();
};
});
⚙️ 命令行参数
参数 | 简写 | 描述 | 示例 |
---|---|---|---|
--port |
-p |
HTTP 服务端口 | --port 8080 |
--host |
--hostname |
HTTP 服务主机地址 | --host 0.0.0.0 |
--dual |
-d |
启用双传输模式 | --dual |
--stdio |
-s |
强制 Stdio 模式 | --stdio |
--help |
-h |
显示帮助信息 | --help |
启动示例:
dotnet run # Stdio 模式(默认)
dotnet run -- --port 8080 # HTTP 模式,本地访问
dotnet run -- --port 8080 --host 0.0.0.0 # HTTP 模式,允许外部访问
dotnet run -- --port 8080 --dual # 双模式,同时支持两种传输
🔧 高级功能
1. 依赖注入
await FastMcpServer.RunAsync(args, options =>
{
options.ConfigureServices = services =>
{
// 注册服务
services.AddSingleton<IWeatherService, WeatherService>();
services.AddHttpClient();
services.AddMemoryCache();
// 配置选项
services.Configure<ApiOptions>(config =>
{
config.ApiKey = Environment.GetEnvironmentVariable("API_KEY");
});
};
});
2. 自定义参数
await FastMcpServer.RunAsync(args, options =>
{
// 添加自定义参数
options
.AddStringArgument("--api-key", "-k", description: "API 密钥")
.AddIntArgument("--timeout", defaultValue: 30, description: "超时时间(秒)")
.AddBoolArgument("--enable-cache", description: "启用缓存")
.AddCustomHelp("环境变量: API_KEY - 设置 API 密钥", 10);
options.ConfigureServices = services =>
{
// 使用自定义参数
var apiKey = options.GetStringArgument("--api-key");
var timeout = options.GetIntArgument("--timeout") ?? 30;
var enableCache = options.GetBoolArgument("--enable-cache");
services.AddSingleton(new Config { ApiKey = apiKey });
};
});
3. 在工具中使用依赖注入
public class MyTools
{
[McpServerTool]
[Description("调用外部 API")]
public static async Task<string> CallApi(
[Description("API 端点")] string endpoint,
IApiService apiService, // 自动注入
ILogger<MyTools> logger) // 自动注入
{
logger.LogInformation("正在调用 API: {Endpoint}", endpoint);
return await apiService.CallAsync(endpoint);
}
}
📦 安装
安装 NuGet 包:
dotnet add package WolfPack.FastMcpServer
或通过包管理器:
Install-Package WolfPack.FastMcpServer
🌟 为什么选择 WolfPack.FastMcpServer?
传统方式 | WolfPack.FastMcpServer |
---|---|
❌ 需要 100+ 行样板代码 | ✅ 一行代码启动服务器 |
❌ 手动传输模式配置 | ✅ 自动检测和切换 |
❌ 复杂的日志配置 | ✅ 智能日志优化 |
❌ 手动参数解析 | ✅ 声明式参数配置 |
❌ 复杂的依赖注入设置 | ✅ 开箱即用的 DI 支持 |
立即开始使用 WolfPack.FastMcpServer,让 MCP 服务器开发变得简单高效!
📄 许可证
本项目采用 MIT 许可证 - 详情请参阅 LICENSE 文件。
🤝 贡献
欢迎贡献!请随时提交 Pull Request。
📞 支持
如果您有任何问题或疑问,请在 GitHub 上创建 issue。
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 is compatible. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
-
net8.0
- ModelContextProtocol (>= 0.3.0-preview.3)
- ModelContextProtocol.AspNetCore (>= 0.3.0-preview.3)
-
net9.0
- ModelContextProtocol (>= 0.3.0-preview.3)
- ModelContextProtocol.AspNetCore (>= 0.3.0-preview.3)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
v1.0.1:
- README.md 文档修正
v1.0.0:
- 初始版本发布
- 一行代码创建 MCP 服务器
- 完整的依赖注入支持
- 自定义命令行参数
- 多传输模式(Stdio、HTTP/SSE、双模式)
- 智能日志配置
- 生产就绪的错误处理