Bitzsoft.Integrations.McpServer 1.0.0-alpha.7

This is a prerelease version of Bitzsoft.Integrations.McpServer.
dotnet add package Bitzsoft.Integrations.McpServer --version 1.0.0-alpha.7
                    
NuGet\Install-Package Bitzsoft.Integrations.McpServer -Version 1.0.0-alpha.7
                    
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="Bitzsoft.Integrations.McpServer" Version="1.0.0-alpha.7" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Bitzsoft.Integrations.McpServer" Version="1.0.0-alpha.7" />
                    
Directory.Packages.props
<PackageReference Include="Bitzsoft.Integrations.McpServer" />
                    
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 Bitzsoft.Integrations.McpServer --version 1.0.0-alpha.7
                    
#r "nuget: Bitzsoft.Integrations.McpServer, 1.0.0-alpha.7"
                    
#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 Bitzsoft.Integrations.McpServer@1.0.0-alpha.7
                    
#: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=Bitzsoft.Integrations.McpServer&version=1.0.0-alpha.7&prerelease
                    
Install as a Cake Addin
#tool nuget:?package=Bitzsoft.Integrations.McpServer&version=1.0.0-alpha.7&prerelease
                    
Install as a Cake Tool

Bitzsoft.Integrations.McpServer

MCP (Model Context Protocol) 服务端集成,快速构建符合 MCP 标准的工具服务,支持 HTTP 传输和标准 I/O 传输。

功能特性

  • 基于 ModelContextProtocol 官方库构建,完全兼容 MCP 协议规范
  • 同时支持 HTTP 传输和 Stdio 传输两种模式
  • 使用 [McpServerToolType][McpServerTool] 特性声明式定义工具
  • 内置实用工具(日期时间、UUID 生成、文本统计等),可按需扩展
  • SSE 客户端配置支持,可连接远程 MCP 服务端
  • ASP.NET Core 集成,一行代码完成服务注册

安装

dotnet add package Bitzsoft.Integrations.McpServer

或直接在项目文件中添加:

<PackageReference Include="Bitzsoft.Integrations.McpServer" Version="*" />

配置

appsettings.json 中添加以下配置节:

{
  "MCPOptions": {
    "Enabled": true,
    "ServerName": "BitzOrcas.MCP",
    "ServerVersion": "1.0.0"
  },
  "MCPSseClientOptions": {
    "Name": "MyMcpClient",
    "Url": "https://mcp-server.example.com/sse",
    "UseStreamableHttp": true,
    "ConnectionTimeout": "00:00:30",
    "AdditionalHeaders": {
      "Authorization": "Bearer your-token"
    }
  }
}

MCPOptions

字段 类型 默认值 说明
Enabled bool false 是否启用 MCP 服务
ServerName string "BitzOrcas.MCP" 服务器名称
ServerVersion string "1.0.0" 服务器版本号

MCPSseClientOptions

字段 类型 默认值 说明
Name string "" 客户端名称
Url string "" SSE 端点 URL
UseStreamableHttp bool false 是否使用流式 HTTP
AdditionalHeaders Dictionary<string, string>? null 附加请求头
ConnectionTimeout TimeSpan 30s 连接超时时间

注册服务

在 ASP.NET Core 应用的 Program.cs 中注册 MCP 服务:

using Bitzsoft.Integrations.McpServer.Models;

var builder = WebApplication.CreateBuilder(args);

// 方式一:使用默认配置
builder.Services.AddMCPServer();

// 方式二:代码内配置
builder.Services.AddMCPServer(options =>
{
    options.Enabled = true;
    options.ServerName = "MyApp.MCP";
    options.ServerVersion = "2.0.0";
});

// 方式三:从 appsettings.json 绑定
builder.Services.AddMCPServer(
    options => builder.Configuration.GetSection("MCPOptions").Bind(options));

var app = builder.Build();

// 映射 MCP 端点(HTTP 传输模式需要)
app.MapMcp();

app.Run();

使用示例

定义自定义工具

创建一个静态类,使用 [McpServerToolType] 标记,并在每个工具方法上添加 [McpServerTool] 特性:

using System.ComponentModel;
using ModelContextProtocol.Server;

namespace MyCompany.Integrations.McpTools;

/// <summary>
/// 法律文档查询工具集
/// </summary>
[McpServerToolType]
public static class LegalDocumentTools
{
    /// <summary>
    /// 根据案件编号查询案件摘要
    /// </summary>
    [McpServerTool, Description("Retrieve a case summary by case number.")]
    public static string GetCaseSummary(string caseNumber)
    {
        // 实际实现应注入业务服务或数据库查询
        return $"案件 {caseNumber} 的摘要信息:合同纠纷,原告张某诉被告李某...";
    }

    /// <summary>
    /// 搜索法律条文
    /// </summary>
    [McpServerTool, Description("Search legal articles by keyword.")]
    public static string SearchLegalArticles(string keyword, int limit = 5)
    {
        return $"找到 {limit} 条与「{keyword}」相关的法律条文:\n"
             + "1. 《民法典》第一百四十三条...\n"
             + "2. 《劳动合同法》第十条...";
    }
}

工具类所在的程序集会在调用 AddMCPServer() 时通过 WithToolsFromAssembly() 自动扫描并注册。如果自定义工具位于独立程序集中,需确保在注册前已加载该程序集。

在 Controller 中调用工具(集成测试场景)

using Bitzsoft.Integrations.McpServer.Tools;
using Microsoft.AspNetCore.Mvc;

[ApiController]
[Route("api/[controller]")]
public class McpTestController : ControllerBase
{
    /// <summary>
    /// 测试 MCP 工具是否正常注册
    /// </summary>
    [HttpGet("uuid")]
    public IActionResult TestUuid()
    {
        // 直接调用本地工具方法验证逻辑
        var result = UtilityTools.GenerateUuid();
        return Ok(new { result });
    }
}

内置实用工具

本包内置了以下实用工具(定义在 UtilityTools 类中),可直接使用或作为参考:

工具 方法签名 说明
GetCurrentDateTime GetCurrentDateTime() 获取当前日期和时间(yyyy-MM-dd HH:mm:ss)
GetCurrentDate GetCurrentDate() 获取当前日期(yyyy-MM-dd)
GetDaysBetween GetDaysBetween(string startDate, string endDate) 计算两个日期之间的天数差
GenerateUuid GenerateUuid() 生成一个新的 UUID(GUID)
CountText CountText(string text) 统计文本的字符数和词数

相关包

Product 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 was computed.  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 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 (1)

Showing the top 1 NuGet packages that depend on Bitzsoft.Integrations.McpServer:

Package Downloads
Bitzsoft.Integrations.All

Bitzsoft 第三方集成聚合包 — 包含全部 Integration 模块

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.0.0-alpha.7 56 6/16/2026
1.0.0-alpha.6 61 6/16/2026
1.0.0-alpha.5 59 6/14/2026
1.0.0-alpha.3 55 6/7/2026
1.0.0-alpha.2 58 5/29/2026
1.0.0-alpha.1 54 5/28/2026