在本快速入门中,你将使用 适用于 MCP 的 C# SDK 创建最小模型上下文协议(MCP)服务器,使用 GitHub Copilot 连接到它,并将其发布到 NuGet。 MCP 服务器是通过模型上下文协议(MCP)向客户端公开功能的服务。
注释
Microsoft.Extensions.AI.Templates 体验目前处于预览版状态。 该模板使用 ModelContextProtocol 库和 MCP 注册表 server.json 架构,它们均处于预览状态。
先决条件
- .NET 10.0 SDK (预览版 6 或更高版本)
- Visual Studio Code
- 适用于 Visual Studio Code 的 GitHub Copilot 扩展
- NuGet.org 帐户
创建项目
在终端窗口中,安装 MCP 服务器模板(版本 9.10.0-preview.3.25513.3 或更高版本):
dotnet new install Microsoft.Extensions.AI.Templates使用
dotnet new mcpserver以下命令创建新的 MCP 服务器应用:dotnet new mcpserver -n SampleMcpServer默认情况下,此命令将创建一个自包含的工具包,该包面向 .NET 支持的所有最常见平台。 若要查看更多选项,请使用
dotnet new mcpserver --help。导航到
SampleMcpServer目录:cd SampleMcpServer生成项目:
dotnet build更新
<PackageId>文件中的.csproj,以便在 NuGet.org 中实现唯一,例如<NuGet.org username>.SampleMcpServer。
在 Visual Studio Code 中配置 MCP 服务器
配置用于 Visual Studio Code 的 GitHub Copilot 以使用自定义 MCP 服务器:
如果尚未打开,请在 Visual Studio Code 中打开项目文件夹。
在项目的根目录中创建文件夹
.vscode。在
mcp.json文件夹中添加一个.vscode文件,内容如下:{ "servers": { "SampleMcpServer": { "type": "stdio", "command": "dotnet", "args": [ "run", "--project", "<relative-path-to-project-file>" ] } } }注释
VS Code 从工作区根目录执行 MCP 服务器。
<relative-path-to-project-file>占位符应指向您的 .NET 项目文件。 例如,此示例 McpServer 应用的值将是SampleMcpServer.csproj。保存文件。
测试 MCP 服务器
MCP 服务器模板包括一个名为 get_random_number 的工具,可用于测试以及作为开发的起点。
在 Visual Studio Code 中打开 GitHub Copilot 并切换到代理模式。
选择 “选择工具 ”图标,验证 SampleMcpServer 是否可用于列出的示例工具。
输入运行 get_random_number 工具的提示:
Give me a random number between 1 and 100.GitHub Copilot 请求获取权限以运行针对提示的 get_random_number 工具。 选择 “继续 ”或使用箭头选择更具体的行为:
- 当前会话 始终在当前 GitHub Copilot 代理模式会话中运行作。
- 当前工作区 始终运行当前 Visual Studio Code 工作区的命令。
- 始终允许 将此操作设置为始终在 GitHub Copilot 代理模式的任何会话或任何 Visual Studio Code 工作区中运行。
验证服务器是否使用随机数进行响应:
Your random number is 42.
添加输入和配置选项
在此示例中,将增强 MCP 服务器以使用环境变量中设置的配置值。 这可能是 MCP 服务器正常运行所需的配置,例如 API 密钥、要连接到的终结点或本地目录路径。
在
GetRandomNumber中的Tools/RandomNumberTools.cs方法后,添加另一个工具方法。 更新工具代码以使用环境变量。[McpServerTool] [Description("Describes random weather in the provided city.")] public string GetCityWeather( [Description("Name of the city to return weather for")] string city) { // Read the environment variable during tool execution. // Alternatively, this could be read during startup and passed via IOptions dependency injection var weather = Environment.GetEnvironmentVariable("WEATHER_CHOICES"); if (string.IsNullOrWhiteSpace(weather)) { weather = "balmy,rainy,stormy"; } var weatherChoices = weather.Split(","); var selectedWeatherIndex = Random.Shared.Next(0, weatherChoices.Length); return $"The weather in {city} is {weatherChoices[selectedWeatherIndex]}."; }将
.vscode/mcp.json更新为设置WEATHER_CHOICES环境变量以进行测试。{ "servers": { "SampleMcpServer": { "type": "stdio", "command": "dotnet", "args": [ "run", "--project", "<relative-path-to-project-file>" ], "env": { "WEATHER_CHOICES": "sunny,humid,freezing" } } } }在 VS Code 中使用 Copilot 尝试另一个提示,例如:
What is the weather in Redmond, Washington?VS Code 应返回随机天气说明。
更新
.mcp/server.json以声明您的环境变量输入。 文件server.json架构由 MCP 注册表项目 定义,NuGet.org 用于生成 VS Code MCP 配置。使用
environmentVariables属性声明应用程序的环境变量,这些变量将由客户端使用 MCP 服务器(例如 VS Code)来设置。使用该
packageArguments属性定义将传递给应用的 CLI 参数。 有关更多示例,请参阅 MCP 注册表项目。
{ "$schema": "https://static.modelcontextprotocol.io/schemas/2025-09-29/server.schema.json", "description": "<your description here>", "name": "io.github.<your GitHub username here>/<your repo name>", "version": "<your package version here>", "packages": [ { "registryType": "nuget", "registryBaseUrl": "https://api.nuget.org", "identifier": "<your package ID here>", "version": "<your package version here>", "transport": { "type": "stdio" }, "packageArguments": [], "environmentVariables": [ { "name": "WEATHER_CHOICES", "value": "{weather_choices}", "variables": { "weather_choices": { "description": "Comma separated list of weather descriptions to randomly select.", "isRequired": true, "isSecret": false } } } ] } ], "repository": { "url": "https://github.com/<your GitHub username here>/<your repo name>", "source": "github" } }NuGet.org 在
server.json中使用的唯一信息是第一个packages数组项,该项的registryType值与nuget匹配。 除了该属性之外的其他顶级属性packages当前未使用,并且适用于即将推出的中央 MCP 注册表。 可以保留占位符值,直到 MCP 注册表处于活动状态并准备好接受 MCP 服务器条目。
在继续之前,可以 再次测试 MCP 服务器 。
打包并发布到 NuGet
打包项目:
dotnet pack -c Release此命令基于列表中的
<RuntimeIdentifiers>列表生成一个工具包和多个特定于平台的SampleMcpServer.csproj包。将包发布到 NuGet:
dotnet nuget push bin/Release/*.nupkg --api-key <your-api-key> --source https://api.nuget.org/v3/index.json请务必发布所有
.nupkg文件,以确保每个受支持的平台都可以运行 MCP 服务器。如果要在发布到 NuGet.org 之前测试发布流程,可以在 NuGet Gallery 集成环境中注册帐户:https://int.nugettest.org命令会被修改为:
pushdotnet nuget push bin/Release/*.nupkg --api-key <your-api-key> --source https://apiint.nugettest.org/v3/index.json
有关详细信息,请参阅 发布包。
发现 NuGet.org 上的 MCP 服务器
在 NuGet.org 上搜索您的 MCP 服务器包(如果是发布到集成环境,则在 int.nugettest.org 上搜索),并从列表中选择它。
查看包详细信息,并从“MCP 服务器”选项卡中复制 JSON。
在
mcp.json文件夹中的.vscode文件中,添加复制的 JSON,如下所示:{ "inputs": [ { "type": "promptString", "id": "weather_choices", "description": "Comma separated list of weather descriptions to randomly select.", "password": false } ], "servers": { "Contoso.SampleMcpServer": { "type": "stdio", "command": "dnx", "args": ["Contoso.SampleMcpServer@0.0.1-beta", "--yes"], "env": { "WEATHER_CHOICES": "${input:weather_choices}" } } } }如果发布到 NuGet Gallery 集成环境中,则需要在
"--add-source", "https://apiint.nugettest.org/v3/index.json"数组末尾添加"args"。保存文件。
在 GitHub Copilot 中,选择 “选择工具 ”图标以验证 SampleMcpServer 是否可用于列出的工具。
输入运行新 get_city_weather 工具的提示:
What is the weather in Redmond?如果将输入添加到 MCP 服务器(例如
WEATHER_CHOICES),系统会提示您提供数值。验证服务器是否响应随机天气:
The weather in Redmond is balmy.
常见问题
找不到运行 SampleMcpServer 所需的命令“dnx”
如果 VS Code 在启动 MCP 服务器时显示此错误,则需要安装兼容版本的 .NET SDK。
该 dnx 命令作为 .NET SDK 的一部分提供,从版本 10 开始。
安装 .NET 10 SDK 以解决此问题。
GitHub Copilot 不使用你的工具(提供答案而不调用你的工具)
通常而言,会告知 GitHub Copilot 这样的 AI 代理,其某些工具由客户端应用程序(例如 VS Code)提供。 某些工具(如示例随机数工具)可能不会被 AI 代理利用,因为它内置了类似的功能。
如果您的工具未被使用,请检查以下步骤:
- 验证工具是否显示在 VS Code 已启用的工具列表中。 有关如何检查此问题 ,请参阅“测试 MCP 服务器 ”中的屏幕截图。
- 在提示中显式引用工具的名称。 在 VS Code 中,可以按名称引用工具。 例如,
Using #get_random_weather, what is the weather in Redmond?。 - 验证 MCP 服务器是否能够启动。 可以通过单击 VS Code 用户或工作区设置中 MCP 服务器配置上方可见的“开始”按钮进行检查。