共用方式為


使用 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 聊天視窗,並確定您已登入。 然後點擊 工具 底部工具欄中的圖標以調出 工具 菜單。 您應該會在可用伺服器清單中看到名為「nuget」的MCP伺服器。

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

開始使用 VS Code

若要在 VS Code 中設定 NuGet MCP 伺服器,請按一下下方適當的按鈕,即可進行設定。

在 VS Code 中安裝 在 VS Code Insiders 中安裝

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

若要確認 MCP 伺服器是否正常運作,請開啟 GitHub Copilot 聊天視窗,並確定您已登入。 然後點擊 工具 底部工具欄中的圖標以調出 工具 菜單。 您應該會在可用伺服器清單中看到名為「nuget」的MCP伺服器。

開始使用 GitHub Copilot Agent

您也可以將 MCP 伺服器設定為與 GitHub Copilot 搭配使用,做為存放庫中的編碼代理程式。 請確定您已將存放庫設定為使用 GitHub Copilot 編碼代理程式

瀏覽至您的存放庫,然後按一下 設定 索引標籤。展開 Copilot 區段,然後按一下 編碼代理程式。

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 Preview 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 聊天視窗中輸入下列提示:

修正我的套件弱點

MCP 伺服器將分析您專案的依賴項,並建議對具有已知漏洞的套件進行更新。

更新所有套件

NuGet MCP 伺服器也可以將您的套件更新為最新的相容版本。 若要使用 MCP 伺服器更新所有套件,請在 GitHub Copilot 聊天視窗中輸入下列提示:

將我所有的套件更新到最新的相容版本

MCP 伺服器將分析您專案的目標框架,並建議更新與您的專案相容的最新版本套件。

將套件更新至特定版本

NuGet MCP 伺服器可以將特定套件更新為您指定的版本。 若要這樣做,請在 GitHub Copilot Chat 視窗中輸入下列提示:

將套件 [PackageName] 更新為版本 [VersionNumber]

Support

如果您遇到 NuGet MCP 伺服器的問題或有任何其他意見反應,請在 NuGet GitHub 存放庫上開啟問題。 請在問題範本中提供所需的資訊,以便我們更好地理解和解決您的問題或建議。