ARTICLE DETAIL

建站实战干货

来自一线的建站与推广经验沉淀,每一条都经过真实交付验证。

如何用 ASP.NET Core 项目模板创建 C MCP 服务器、本地测试并发布为 NuGet 包

2026/9/12 17:09:03 拓冰建站 浏览量
如何用 ASP.NET Core 项目模板创建 C MCP 服务器、本地测试并发布为 NuGet 包 如何用 ASP.NET Core 项目模板创建 C# MCP 服务器、本地测试并发布为 NuGet 包【免费下载链接】aspnetcoreASP.NET Core is a cross-platform .NET framework for building modern cloud-based web applications on Windows, Mac, or Linux.项目地址: https://gitcode.com/GitHub_Trending/as/aspnetcore如果你要用 C# 写一个 MCPModel Context Protocol服务器配置到 VS Code 或 Visual Studio 中使用再打包成 NuGet 包发布给他人安装不必从零搭建aspnetcore 仓库的模板区带有一个 MCP 服务器项目模板模板短名mcpserver模板名 “MCP Server App”它会生成带get_random_number示例工具的服务器骨架本地stdio变体还预置了完整的 NuGet 打包元数据。本文以localstdio模板为主线走通“创建 → 本地测试 → 打包发布”remoteHTTP模板作为本地测试的可选分支。适用前提模板Framework参数目前只有net11.0一个选项默认值开发环境的 .NET SDK 需要能面向该框架本地测试按模板生成的 README 的方式依赖 VS Code 或 Visual Studio 及其 Copilot Chat。用dotnet new mcpserver创建项目dotnet new mcpserver模板支持的参数短名来自模板的dotnetcli.host.json参数短名默认值作用--transport-tlocallocal生成控制台应用使用 stdio 传输remote生成 ASP.NET Core Web 应用使用 http 传输--self-contained—true以自包含方式发布不依赖目标机上的共享框架--aot—false启用 native AOT 发布--framework—net11.0目标框架当前唯一选项是net11.0--no-restore—false创建后跳过自动 restore即默认命令等价于--transport local且自包含开启。模板包由仓库中 Microsoft.McpServer.ProjectTemplates.csproj 构建包名为Microsoft.McpServer.ProjectTemplates.主.次版本号如果本地 SDK 中没有mcpserver模板需要先用dotnet new install安装对应的模板包。local 变体生成什么local 变体的 csproj 由 McpServer-Local-CSharp.csproj.in 生成生成后包含这些内容文件路径相对于生成的项目目录McpServer-CSharp.csproj包含PackAsTooltrue/PackAsTool、PackageTypeMcpServer/PackageType默认PackageIdSampleMcpServer/PackageId、PackageVersion0.1.0-beta/PackageVersion并把.mcp\server.json和README.md打进 NuGet 包引用Microsoft.Extensions.Hosting与ModelContextProtocol包。Program.cs通过AddMcpServer().WithStdioServerTransport().WithToolsRandomNumberTools()注册 stdio 传输与工具所有日志写入 stderr因为 stdout 专用于 MCP 协议消息。Tools/RandomNumberTools.cs带[McpServerTool]特性的示例工具GetRandomNumber(min 0, max 100)实现见 RandomNumberTools.cs。.mcp/server.jsonMCP 服务器声明文件含若干占位符发布前必须替换见“发布为 NuGet 包”一节。README.md本地开发与发布的指引内容即本文步骤的来源local/README.md。--self-contained true默认时项目会设置RuntimeIdentifierswin-x64;win-arm64;osx-arm64;linux-x64;linux-arm64;linux-musl-x64/RuntimeIdentifiers以及SelfContained、PublishSelfContained、PublishSingleFile--self-contained false时改为RollForwardMajor/RollForward的框架依赖模式目标机必须安装相应 .NET runtime否则服务器不会启动。本地测试 stdio 服务器不需要先打包直接让 IDE 从源码运行服务器。在 VS Code 或 Visual Studio 的 MCP 服务器配置中加入模板 README 给出的配置示例{ servers: { McpServer-CSharp: { type: stdio, command: dotnet, args: [ run, --project, PATH TO PROJECT DIRECTORY ] } } }其中PATH TO PROJECT DIRECTORY是模板 README 中的占位符替换为你生成的项目目录的实际路径。这样 IDE 会用dotnet run直接启动项目。验证方式模板 README 给出的测试方法配置完成后在 Copilot Chat 中请求随机数例如发送Give me 3 random numbers。成功时 Copilot 会调用McpServer-CSharpMCP 服务器上的get_random_number工具并显示结果。可选分支remoteHTTP变体的本地测试dotnet new mcpserver --transport remote生成的是 ASP.NET Core Web 应用引用ModelContextProtocol.AspNetCore包。其Program.cs使用AddMcpServer().WithHttpTransport(...)模板默认设置Stateless true适用于不需要 sampling、elicitation 等服务到客户端请求的服务器并调用app.MapMcp()。生成的 launchSettings.json 有两个启动配置httphttp://localhost:9996httpshttps://localhost:9995;http://localhost:9996运行项目后把 IDE 的 MCP 服务器配置为 http 类型remote/README.md 的配置示例{ servers: { McpServer-CSharp: { type: http, url: http://localhost:9996 } } }Visual Studio 下可改用https://localhost:9995。验证方式与 stdio 相同在 Copilot Chat 发送Give me 3 random numbers期望它调用get_random_number工具并显示结果。另一种验证方式是直接用项目自带的 McpServer-CSharp.http 文件向服务器发 JSON-RPC 请求。文件中的{{HostAddress}}由文件顶部的HostAddress变量解析Visual Studio 下是https://localhost:9995其他环境下是http://localhost:9996以非 Visual Studio 环境为例已解析为具体地址POST http://localhost:9996/ Accept: application/json, text/event-stream Content-Type: application/json MCP-Protocol-Version: 2025-11-25 { jsonrpc: 2.0, id: 1, method: tools/call, params: { name: get_random_number } }remote 变体的一个已知问题来自其 README在 VS Code 中连接https://localhost:9995会失败与自签名开发者证书有关即使证书已被系统信任改用http://localhost:9996可以连接成功。发布为 NuGet 包local 变体只有 local 变体的 csproj 带PackAsTool/PackageTypeMcpServer打包元数据以下发布路径适用 local 变体。模板 README 给出的发布前清单按上一节步骤在本地测试过 MCP 服务器。更新.csproj中的包元数据特别是PackageId默认是占位性质示例值SampleMcpServer。更新.mcp/server.json来声明你的 MCP 服务器。模板文件server.json中发布前需要替换的占位符nameio.github.your GitHub username here/your repo namepackages[0].identifieryour package ID here应与你设置的PackageId一致packages[0].environmentVariables与packageArguments声明服务器的输入模板 README 指向的 configuring inputs 指南为外部文档仓库内未附正文repository.urlhttps://github.com/your GitHub username here/your repo name执行打包dotnet pack -c Releasebin/Release目录会包含生成的.nupkg包文件确认该文件存在即打包完成。推送到 NuGet.orgyour-api-key替换为你自己的 NuGet.org API keydotnet nuget push bin/Release/*.nupkg --api-key your-api-key --source https://api.nuget.org/v3/index.json从 NuGet.org 配置已发布的服务器包发布后VS Code 和 Visual Studio 都使用dnx命令从 NuGet.org 下载并安装该 MCP 服务器包VS Code在工作区创建WORKSPACE DIRECTORY/.vscode/mcp.json替换为你的工作区目录Visual Studio在解决方案目录创建SOLUTION DIRECTORY\.mcp.json两个 IDE 共用的服务器定义模板 README 示例{ servers: { McpServer-CSharp: { type: stdio, command: dnx, args: [ your package ID here, --version, your package version here, --yes ] } } }其中your package ID here替换为项目中设置的PackageIdyour package version here替换为包版本。配置后服务器会由dnx自动下载对应版本的包并运行不再需要读者机器上有该项目源码。限制与已知问题自包含变体必须为每个目标平台单独构建默认覆盖 6 个 RIDwin-x64、win-arm64、osx-arm64、linux-x64、linux-arm64、linux-musl-x64需要更多平台时修改项目的RuntimeIdentifiers /列表。框架依赖变体要求目标机安装相应 runtimelocal 变体为 .NET runtimeremote 变体为 ASP.NET Core runtime模板配置了 roll-forward 到更高 major 版本没有任何可用 runtime 时服务器不会启动。remote 变体是 Web 应用用于以 ASP.NET Core Web 应用形式运行 MCP 服务器其 csproj 不含打包元数据不走上文的 NuGet 发布路径。VS Code 连接 https 开发者证书失败是 remote 变体记录的已知问题改用 http 端点9996 端口即可。主要资料来源local/README.md、remote/README.md、template.json 与模板内容文件路径均在src/ProjectTemplates/McpServer.ProjectTemplates/下。【免费下载链接】aspnetcoreASP.NET Core is a cross-platform .NET framework for building modern cloud-based web applications on Windows, Mac, or Linux.项目地址: https://gitcode.com/GitHub_Trending/as/aspnetcore创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考