使用 NuGet 模型上下文协议 (MCP) 服务器

要求

若要运行 MCP 服务器,必须安装 .NET 10 SDK 或更高版本 。 此版本的 .NET 添加了一个命令, dnx该命令用于从 nuget.org 下载、安装和运行 MCP 服务器。

若要验证 .NET 版本,请在终端中运行以下命令:

dotnet --info

Visual Studio 2026 入门

在 Visual Studio 2026 中,NuGet MCP 服务器是内置的,但必须启用一次才能使用其功能。

若要启用 NuGet MCP 服务器,请执行以下步骤:

  1. 打开 Visual Studio 2026。
  2. 打开 GitHub Copilot 聊天窗口,确保已登录。
  3. 单击底部工具栏中的工具图标以显示“工具”菜单。
  4. 找到名为“nuget”的 MCP 服务器,并选中该框以启用它。

Visual Studio 2026 中的 GitHub Copilot 聊天工具窗口

Visual Studio 2022 入门

注释

必须具有 Visual Studio 2022 17.14 或更高版本才能配置 MCP 服务器。

在 Visual Studio 2022 中,必须将 NuGet MCP 服务器手动添加到配置。 Visual Studio 2022 使用多个配置文件来定义 MCP 服务器。 有关要配置的文件的详细信息,请参阅 “使用 MCP 服务器 ”。

确认正确的配置文件后,请将下面的 JSON 代码片段添加到 mcp.json 中。

{
  "servers": {
    "nuget": {
      "type": "stdio",
      "command": "dnx",
      "args": [ "NuGet.Mcp.Server", "--source", "https://api.nuget.org/v3/index.json", "--yes" ]
    }
  }
}

这将配置 Visual Studio 2022,以便从 nuget.org 使用最新版本的 NuGet MCP 服务器。若要验证 MCP 服务器是否正常工作,请打开 GitHub Copilot Chat 窗口并确保已登录。 然后单击底部工具栏中的“工具”图标以显示“工具”菜单。 应在可用服务器列表中看到名为“nuget”的 MCP 服务器。

Visual Studio 2022 中的 GitHub Copilot 聊天工具窗口

VS Code 入门

若要在 VS Code 中配置 NuGet MCP 服务器,请单击下面的相应按钮,并设置该服务器。

在 VS Code 中安装 在 VS Code 预览体验成员中安装

VS Code 中的 GitHub Copilot 聊天工具窗口

若要验证 MCP 服务器是否正常工作,请打开 GitHub Copilot Chat 窗口并确保已登录。 然后单击底部工具栏中的“工具”图标以显示“工具”菜单。 应在可用服务器列表中看到名为“nuget”的 MCP 服务器。

GitHub Copilot Agent 入门

还可以将 MCP 服务器配置为在存储库中将 GitHub Copilot 用作编码代理。 确保已将存储库配置为使用 GitHub Copilot 编码代理

浏览到你的存储库,然后单击“设置”选项卡。展开 Copilot 部分,然后单击“Coding Agents”。

GitHub Copilot 编码代理设置

向下滚动到 模型上下文协议(MCP) 部分,并将以下 JSON 代码片段添加到 mcpServers 配置中:

{ 
  "mcpServers": {
    "NuGet": {
      "type": "local",
      "command": "dnx",
      "args": ["NuGet.Mcp.Server", "--yes"],
      "tools": ["*"],
      "env": {}
    }
  } 
}

这将提供所有 NuGet 的 MCP 服务器工具。 如果需要特定工具,可以在参数数组中 "tools" 列出它们。

最后,单击“ 保存 MCP 配置 ”按钮保存更改。

配置 NuGet MCP 后,还需要创建 GitHub Actions 工作流来安装 .NET 10 预览版 6 或更高版本,以便 dnx 命令可用于运行 MCP 服务器。 为此,可以在存储库中创建以下工作流文件

.github/workflows/copilot-setup-steps.yml

此工作流文件的内容应如下所示:

name: "Copilot Setup Steps"

# Automatically run the setup steps when they are changed to allow for easy validation, and
# allow manual testing through the repository's "Actions" tab
on:
  workflow_dispatch:
  push:
    paths:
      - .github/workflows/copilot-setup-steps.yml
  pull_request:
    paths:
      - .github/workflows/copilot-setup-steps.yml

jobs:
  # The job MUST be called `copilot-setup-steps` or it will not be picked up by Copilot.
  copilot-setup-steps:
    runs-on: ubuntu-latest

    # Set the permissions to the lowest permissions possible needed for your steps.
    # Copilot will be given its own token for its operations.
    permissions:
      # If you want to clone the repository as part of your setup steps, for example to install dependencies, you'll need the `contents: read` permission. If you don't clone the repository in your setup steps, Copilot will do this for you automatically after the steps complete.
      contents: read

    # You can define any steps you want, and they will run before the agent starts.
    # If you do not check out your code, Copilot will do this for you.
    steps:
      - name: Install .NET 10.x
        uses: actions/setup-dotnet@v5
        with:
          dotnet-version: |
            10.x
          dotnet-quality: preview

      - name: dotnet --info
        run: dotnet --info

这将确保当 GitHub Copilot 作为您存储库中的编码代理运行时,dnx 命令可用于启动 NuGet MCP 服务器。

修复包漏洞

NuGet MCP 服务器可以帮助你识别和修复项目中的包漏洞。 若要使用 MCP 服务器修复漏洞,请在 GitHub Copilot Chat 窗口中输入以下提示:

修复我的包漏洞

MCP 服务器将分析项目的依赖项,并建议更新具有已知漏洞的包。

更新所有包

NuGet MCP 服务器还可以将包更新到最新的兼容版本。 若要使用 MCP 服务器更新所有包,请在 GitHub Copilot Chat 窗口中输入以下提示:

将所有包更新到最新的兼容版本

MCP 服务器将分析项目的目标框架,并建议更新与项目兼容的最新版本的包。

将包更新到特定版本

NuGet MCP 服务器可将特定包更新为指定的版本。 为此,请在 GitHub Copilot 聊天窗口中输入以下提示:

将包 [PackageName] 更新为版本 [VersionNumber]

Support

如果遇到 NuGet MCP 服务器问题或有任何其他反馈,请在 NuGet GitHub 存储库中提出问题。 请在问题模板中提供请求的信息,以便我们可以更好地了解并解决你的问题或建议。