MCP平台(.NET)
基于插件的MCP服务器平台。编写工具一次,部署到任何地方。
封装结构
| 包 | 角色 |
|---|---|
McpPlatform.Core | 仅限合同-- IMcpPlugin, PluginDescriptor, PluginLoadException |
McpPlatform.Hosting | 插件加载器, McpStdioHostBuilder,DI扩展 |
McpGenericServer | 参考stdio服务器——运行内置工具+动态加载插件 |
插件的工作原理
在启动时, McpGenericServer 扫描 plugins/ 目录。\ 每 .dll 其中包含一个类实现 IMcpPlugin 被加载到一个隔离的\ AssemblyLoadContext 及其 Register() 方法被调用。\ 该插件注册了自己的工具、HTTP客户端、配置绑定——任何它需要的东西。
McpGenericServer.exe
└── plugins/
├── MyCompany.Mcp.Crm.dll ← loaded automatically
├── MyCompany.Mcp.Crm.deps.json ← required for dependency resolution
└── MyCompany.Mcp.Hr.dll ← loaded automatically编写插件(在单独的仓库中)
1.安装NuGet
2.实现IMcpPlugin
using McpPlatform.Core.Plugins;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using ModelContextProtocol.Server;
public sealed class CrmPlugin : IMcpPlugin
{
public string Name => "Crm";
public string Version => "1.0.0";
public void Register(IServiceCollection services, IConfiguration configuration)
{
services.AddHttpClient(client =>
{
client.BaseAddress = new Uri(configuration["Crm:BaseUrl"]!);
});
services
.AddMcpServer()
.WithTools();
}
}3.正常编写工具
[McpServerToolType]
public sealed class CrmTools
{
private readonly ICrmClient _crm;
public CrmTools(ICrmClient crm) => _crm = crm;
[McpServerTool(Name = "crm_get_customer"), Description("Returns customer by ID.")]
public async Task GetCustomer(
[Description("Customer ID")] string id,
CancellationToken cancellationToken)
{
var customer = await _crm.GetCustomerAsync(id, cancellationToken);
return new CustomerResult(customer.Id, customer.Name);
}
public sealed record CustomerResult(string Id, string Name);
}4.部署
将插件的构建输出复制到 plugins/ 旁边 McpGenericServer.exe.\ 服务器在下次启动时发现并加载它——不需要重新编译。
配置
appsettings.json 或环境变量:
{
"Mcp": {
"PluginsDirectory": "plugins",
"FailFastOnPluginError": false
}
}连接客户端
看 docs/connecting-clients.md 有关以下内容的分步说明:
- VS代码/GitHub复制人聊天(
.vscode/mcp.json) - 克劳德桌面
- 来自的程序连接。NET、Python和TypeScript
- 环境变量和
appsettings.json配置 - 将插件放置在
plugins/目录
构建和测试
dotnet build McpGenericServer.sln
dotnet test McpGenericServer.sln