McpHelper.Core 1.0.9

dotnet add package McpHelper.Core --version 1.0.9
                    
NuGet\Install-Package McpHelper.Core -Version 1.0.9
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="McpHelper.Core" Version="1.0.9" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="McpHelper.Core" Version="1.0.9" />
                    
Directory.Packages.props
<PackageReference Include="McpHelper.Core" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add McpHelper.Core --version 1.0.9
                    
#r "nuget: McpHelper.Core, 1.0.9"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package McpHelper.Core@1.0.9
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=McpHelper.Core&version=1.0.9
                    
Install as a Cake Addin
#tool nuget:?package=McpHelper.Core&version=1.0.9
                    
Install as a Cake Tool

McpHelper.Core

MCP Helper 核心库 - ASP.NET Core 中间件,用于公开程序信息查询接口,支持 MCP Server 和多种认证方式。

功能特性

  • MCP Server - 支持 Streamable HTTP 和传统 SSE 两种传输模式
  • MCP Client - 连接远程后端 MCP 服务并代理调用
  • 程序信息查询 - 暴露版本、运行时、系统、内存等信息的 REST API
  • 健康检查 - 支持依赖服务的健康状态检查
  • 多种认证 - API Key、JWT 认证,可扩展数据库认证

安装

dotnet add package McpHelper.Core

依赖

包名 版本 说明
ModelContextProtocol.AspNetCore 1.2.0 MCP 协议实现

目标框架

  • .NET 10.0

快速开始

1. 基础使用

using McpHelper.Extensions;

var builder = WebApplication.CreateBuilder(args);

// 注册中间件
builder.Services.AddMcpHelper();

var app = builder.Build();

// 启用中间件
app.MapMcpHelper();

app.Run();

2. 完整配置

appsettings.json:

{
  "AppMiddleware": {
    "Authentication": {
      "Type": "ApiKey",
      "ApiKey": {
        "Keys": ["your-api-key"],
        "HeaderName": "X-Api-Key"
      }
    },
    "ProgramInfo": {
      "Enabled": true,
      "EndpointPrefix": "/api/program-info",
      "ProductName": "MyService",
      "ProductVersion": "1.0.0"
    },
    "Mcp": {
      "Enabled": true,
      "ServerName": "MyService",
      "ServerVersion": "1.0.0",
      "SseEndpoint": "/api/mcp",
      "EnableLegacySse": false,
      "Stateless": true
    }
  }
}

Program.cs:

using McpHelper.Extensions;

var builder = WebApplication.CreateBuilder(args);

// 一行代码注册所有中间件
builder.Services.AddAppMiddleware(builder.Configuration);

var app = builder.Build();

// 一行代码启用所有中间件
app.UseAppMiddleware();

app.Run();

配置选项

ProgramInfoOptions

属性 类型 默认值 说明
Enabled bool true 是否启用程序信息端点
EndpointPrefix string "/api/program-info" 端点前缀
ProductName string Assembly Name 产品名称
ProductVersion string Assembly Version 产品版本

MopOptions (MCP 配置)

属性 类型 默认值 说明
Enabled bool true 是否启用 MCP Server
ServerName string "McpHelper" 服务器名称
ServerVersion string "1.0.0" 服务器版本
SseEndpoint string "/api/mcp" MCP 端点
EnableLegacySse bool false 是否启用传统 SSE 模式
Stateless bool true 是否无状态模式
TransportType MopTransportType Sse 传输类型(Sse/Stdio)

AuthenticationOptions

属性 类型 说明
Type AuthenticationType 认证类型(ApiKey/Jwt/Database/None)
ApiKey ApiKeyOptions API Key 认证配置
Jwt JwtOptions JWT 认证配置

API 端点

端点 方法 说明
/api/program-info GET 完整程序信息
/api/program-info/version GET 版本信息
/api/program-info/runtime GET 运行时信息
/api/program-info/system GET 系统信息
/api/program-info/memory GET 内存信息
/api/program-info/health GET 健康状态

MCP 传输模式

Streamable HTTP(推荐)

{
  "Mcp": {
    "EnableLegacySse": false,
    "Stateless": true
  }
}

端点: POST /api/mcp

传统 SSE

{
  "Mcp": {
    "EnableLegacySse": true,
    "Stateless": false
  }
}

端点: GET /api/mcp/sse + POST /api/mcp/message

Stdio 模式

适用于控制台应用,通过标准输入输出通信。

var mcpOptions = new ProgramInfoOptions
{
    Mcp = new MopOptions
    {
        Enabled = true,
        TransportType = MopTransportType.Stdio,
        ServerName = "ConsoleApp"
    }
};

await mcpOptions.RunMcpServerAsync();

认证配置

API Key 认证

{
  "Authentication": {
    "Type": "ApiKey",
    "ApiKey": {
      "Keys": ["key1", "key2"],
      "HeaderName": "X-Api-Key"
    }
  }
}

JWT 认证

{
  "Authentication": {
    "Type": "Jwt",
    "Jwt": {
      "UseHostAuthentication": true,
      "RequiredRoles": ["admin"]
    }
  }
}

自定义 MCP 工具

定义工具类

using System.ComponentModel;
using ModelContextProtocol.Server;
using System.Text.Json;

[McpServerToolType]
public class CustomTools
{
    private readonly IMyService _service;

    public CustomTools(IMyService service)
    {
        _service = service;
    }

    [McpServerTool]
    [Description("计算两个数的和。参数 a:第一个数,参数 b:第二个数。")]
    public Task<string> Add(double a, double b)
    {
        return Task.FromResult(JsonSerializer.Serialize(new { a, b, result = a + b }));
    }
}

注册工具

// 注册服务
builder.Services.AddSingleton<IMyService, MyService>();

// 注册中间件
builder.Services.AddAppMiddleware(builder.Configuration);

// 注册自定义工具(推荐)
builder.Services.AddMcpLocalTool<CustomTools>();

// 或批量注册
builder.Services.AddMcpLocalTools(registry =>
{
    registry.RegisterLocalTool<CustomTools>();
    registry.RegisterLocalTool<AdminTools>();
});

后端服务代理

配置后端

{
  "Mcp": {
    "Backends": [
      {
        "Name": "RemoteService",
        "Url": "http://192.168.1.100:5000",
        "ConnectionMode": "Mcp",
        "TransportMode": "Auto",
        "McpEndpoint": "/api/mcp",
        "ApiKey": "your-key"
      }
    ]
  }
}

后端工具

工具名 说明
list_backends 列出所有后端服务
get_backend_info 获取指定后端信息
get_backend_health 获取指定后端健康状态

扩展方法

AddMcpHelper

注册核心服务:

builder.Services.AddMcpHelper();

AddAppMiddleware

注册所有中间件(推荐):

builder.Services.AddAppMiddleware(builder.Configuration);

AddMcpLocalTool

注册单个工具类:

builder.Services.AddMcpLocalTool<CustomTools>();

AddMcpLocalTools

批量注册工具类:

builder.Services.AddMcpLocalTools(registry =>
{
    registry.RegisterLocalTool<CustomTools>();
    registry.RegisterLocalTool<AdminTools>();
});

相关项目

  • McpHelper.SQLite - SQLite 数据库认证扩展
  • McpHelper.PostgreSQL - PostgreSQL 数据库认证扩展
  • McpHelper.Linker - CLI 工具,动态加载 MCP 工具

许可证

MIT License

Product Compatible and additional computed target framework versions.
.NET net10.0 is compatible.  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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (2)

Showing the top 2 NuGet packages that depend on McpHelper.Core:

Package Downloads
McpHelper.PostgreSQL

McpHelper PostgreSQL 数据库认证扩展

McpHelper.SQLite

McpHelper SQLite 数据库认证扩展

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.0.9 262 4/19/2026
1.0.8 122 4/5/2026
1.0.7 118 4/4/2026
1.0.6 115 4/3/2026
1.0.5 111 4/3/2026
1.0.4 111 4/3/2026
1.0.3 114 4/3/2026
1.0.1 114 4/3/2026
1.0.0 115 4/3/2026