先决条件
在为 Copilot编程助理 设置 MCP 服务器之前,请阅读“模型上下文协议 (MCP) 和 GitHub Copilot 编码代理”,以确保你理解 MCP 服务器和 Copilot编程助理 的相关概念。
简介
作为存储库管理员,可以将 MCP 服务器配置为在存储库中使用。 使用 JSON 格式的配置完成此操作,该配置指定要使用的 MCP 服务器的详细信息。 直接在 GitHub.com 上的存储库的设置中输入 JSON 配置。
组织和企业管理员还可以使用 YAML 前页将 MCP 服务器配置为 自定义代理 的一部分。 有关详细信息,请参阅“自定义代理配置”。
警告
配置 MCP 服务器后,Copilot 将能够自主使用服务器提供的工具且在使用之前不请求批准。
注意
- Copilot编程助理 仅支持 MCP 服务器提供的工具。 它不支持资源或提示。
- Copilot编程助理 目前不支持通过 OAuth 进行身份验证和授权的远程 MCP 服务器。
将 MCP 配置添加到存储库
存储库管理员可以按照以下步骤配置 MCP 服务器:
-
在 GitHub 上,导航到存储库的主页面。
-
在仓库名称下,单击 “Settings”****。 如果看不到“设置”选项卡,请选择“”下拉菜单,然后单击“设置”。

-
在边栏的“代码与自动化”部分中,依次单击 Copilot 和 编程代理。
-
请在“MCP configuration”部分添加您的配置。
本文中的以下各节解释如何编写需要在此处输入的 JSON 配置。
-
单击“ 保存”。
系统将验证配置以确保其语法正确。
-
如果 MCP 服务器需要变量、密钥或机密,请在 Copilot 环境中添加变量或机密。 只有带有前缀名称
COPILOT_MCP_的变量和机密才可用于 MCP 配置。 请参阅为 Copilot编程助理 设置 Copilot 环境。
编写 MCP 服务器的 JSON 配置
使用专用 JSON 格式配置 MCP 服务器。 JSON 必须包含一个 mcpServers 对象,其中键是 MCP 服务器的名称(例如 sentry),值是具有该 MCP 服务器的配置的对象。
{
"mcpServers": {
"MCP SERVER 1": {
"command": "VALUE",
"args": [ VALUES ],
...
},
"MCP SERVER 2": {
"command": "VALUE",
"args": [ VALUES ],
...
},
...
}
}
{
"mcpServers": {
"MCP SERVER 1": {
"command": "VALUE",
"args": [ VALUES ],
...
},
"MCP SERVER 2": {
"command": "VALUE",
"args": [ VALUES ],
...
},
...
}
}
配置对象可包含以下键:
本地和远程 MCP 服务器的必需密钥****
-
`tools` (`string[]`):要启用的 MCP 服务器中的工具。 有可能在服务器的文档或代码中找到工具列表。 我们强烈建议你将特定的只读工具列入允许列表,因为代理将能够自主使用这些工具,且不会事先征求你的批准。 你还可通过在数组中包含 `*` 来启用所有工具。 -
`type` (`string`):Copilot编程助理 接受 `"local"`、`"stdio"`、`"http"` 或 `"sse"`。
本地 MCP 特定密钥****
*
command (string):必需。 启动 MCP 服务器时要运行的命令。
*
args (string[]):必需。 要传递给 command 的参数。
*
env (object):可选。 需传递至服务器的环境变量。 此对象应将应公开给 MCP 服务器的环境变量的名称映射到以下其中一项:
- 对 Copilot 环境中的机密或变量的替换引用,例如
$COPILOT_MCP_API_KEY或${COPILOT_MCP_API_KEY}。 引用的名称必须以 . 开头COPILOT_MCP_。 - 一个文本字符串值。
远程 MCP 特定密钥****
*
url (string):必需。 MCP 服务器的 URL。
*
headers (object):可选。 要附加到对服务器的请求的标头。 此对象应将标头键的名称映射到以下项之一:
- 对 Copilot 环境中的机密或变量的替换引用,例如
$COPILOT_MCP_API_KEY或${COPILOT_MCP_API_KEY}。 引用的名称必须以 . 开头COPILOT_MCP_。 - 一个文本字符串值。
除 tools & type 之外的所有 string 和 string[] 字段都支持使用你在 Copilot 环境中配置的变量或机密进行替换。
变量替换
引用你在 Copilot 环境中配置的环境变量时,受支持的语法模式如下:
| Syntax | 示例 |
|---|---|
$VAR | $COPILOT_MCP_API_KEY |
${VAR} | ${COPILOT_MCP_API_KEY} |
${VAR:-default} | ${COPILOT_MCP_API_KEY:-fallback_value} |
示例配置
以下示例显示了不同提供程序的 MCP 服务器配置。
-
[Sentry](#example-sentry) -
[Notion](#example-notion) -
[Azure](#example-azure) -
[Cloudflare](#example-cloudflare) -
[Azure DevOps](#example-azure-devops) -
[Atlassian](#example-atlassian)
示例:Sentry
[Sentry MCP 服务器](https://github.com/getsentry/sentry-mcp) 为 Copilot 提供认证访问,从而能够访问 [Sentry](https://sentry.io) 中记录的异常。
// If you copy and paste this example, you will need to remove the comments prefixed with `//`, which are not valid JSON.
{
"mcpServers": {
"sentry": {
"type": "local",
"command": "npx",
// We can use the $SENTRY_HOST environment variable which is passed to
// the server because of the `env` value below.
"args": ["@sentry/mcp-server@latest", "--host=$SENTRY_HOST"],
"tools": ["get_issue_details", "get_issue_summary"],
"env": {
// We can specify an environment variable value as a string...
"SENTRY_HOST": "https://contoso.sentry.io",
// or refer to a variable or secret in your Copilot environment
// with a name starting with `COPILOT_MCP_`
"SENTRY_ACCESS_TOKEN": "$COPILOT_MCP_SENTRY_ACCESS_TOKEN"
}
}
}
}
// If you copy and paste this example, you will need to remove the comments prefixed with `//`, which are not valid JSON.
{
"mcpServers": {
"sentry": {
"type": "local",
"command": "npx",
// We can use the $SENTRY_HOST environment variable which is passed to
// the server because of the `env` value below.
"args": ["@sentry/mcp-server@latest", "--host=$SENTRY_HOST"],
"tools": ["get_issue_details", "get_issue_summary"],
"env": {
// We can specify an environment variable value as a string...
"SENTRY_HOST": "https://contoso.sentry.io",
// or refer to a variable or secret in your Copilot environment
// with a name starting with `COPILOT_MCP_`
"SENTRY_ACCESS_TOKEN": "$COPILOT_MCP_SENTRY_ACCESS_TOKEN"
}
}
}
}
示例:Notion
[Notion MCP 服务器](https://github.com/makenotion/notion-mcp-server)为Copilot提供身份验证后对来自[Notion](https://notion.so)的笔记和其他内容的访问权限。
// If you copy and paste this example, you will need to remove the comments prefixed with `//`, which are not valid JSON.
{
"mcpServers": {
"notionApi": {
"type": "local",
"command": "docker",
"args": [
"run",
"--rm",
"-i",
"-e",
// We can use the $NOTION_API_KEY environment variable which is passed to
// the server because of the `env` value below.
"OPENAPI_MCP_HEADERS={\"Authorization\": \"Bearer $NOTION_API_KEY\", \"Notion-Version\": \"2022-06-28\"}",
"mcp/notion"
],
"env": {
// The value of the `COPILOT_MCP_NOTION_API_KEY` secret will be passed to the
// server command as an environment variable called `NOTION_API_KEY`
"NOTION_API_KEY": "$COPILOT_MCP_NOTION_API_KEY"
},
"tools": ["*"]
}
}
}
// If you copy and paste this example, you will need to remove the comments prefixed with `//`, which are not valid JSON.
{
"mcpServers": {
"notionApi": {
"type": "local",
"command": "docker",
"args": [
"run",
"--rm",
"-i",
"-e",
// We can use the $NOTION_API_KEY environment variable which is passed to
// the server because of the `env` value below.
"OPENAPI_MCP_HEADERS={\"Authorization\": \"Bearer $NOTION_API_KEY\", \"Notion-Version\": \"2022-06-28\"}",
"mcp/notion"
],
"env": {
// The value of the `COPILOT_MCP_NOTION_API_KEY` secret will be passed to the
// server command as an environment variable called `NOTION_API_KEY`
"NOTION_API_KEY": "$COPILOT_MCP_NOTION_API_KEY"
},
"tools": ["*"]
}
}
}
示例:Azure
Microsoft MCP 存储库包含 Azure MCP 服务器,该服务器允许 Copilot 在进行代码更改时了解订阅中的 Azure 特定文件和 Azure 资源。
若要使用 copilot-setup-steps.yml 文件自动配置存储库以使用Azure进行身份验证,以及用于身份验证的机密,请在本地克隆存储库,然后在存储库根目录中运行 Azure Developer CLI的 azd coding-agent config 命令。
运行命令并合并创建的拉取请求后,即可将 MCP 配置添加到存储库。
{
"mcpServers": {
"Azure": {
"type": "local",
"command": "npx",
"args": [
"-y",
"@azure/mcp@latest",
"server",
"start"
],
"tools": ["*"]
}
}
}
{
"mcpServers": {
"Azure": {
"type": "local",
"command": "npx",
"args": [
"-y",
"@azure/mcp@latest",
"server",
"start"
],
"tools": ["*"]
}
}
}
示例:Cloudflare
[Cloudflare MCP 服务器](https://github.com/cloudflare/mcp-server-cloudflare)在 Cloudflare 服务(包括处理文档和数据分析)之间创建连接。
{
"mcpServers": {
"cloudflare": {
"type": "sse",
"url": "https://docs.mcp.cloudflare.com/sse",
"tools": ["*"]
}
}
}
{
"mcpServers": {
"cloudflare": {
"type": "sse",
"url": "https://docs.mcp.cloudflare.com/sse",
"tools": ["*"]
}
}
}
示例:Azure DevOps
[Azure DevOps MCP 服务器](https://github.com/microsoft/azure-devops-mcp) 可在 Copilot 与 Azure DevOps 服务(包括工作项、管道及文档)之间创建无缝连接。
若要将 Azure DevOps MCP 服务器与 Copilot编程助理配合使用,必须更新存储库的 copilot-setup-steps.yml 文件,使其包含 Azure 登录工作流步骤。
-
在 Microsoft Entra 应用程序中配置 OIDC,以便信任 GitHub。 请参阅 使用 OpenID Connect 的 Azure 登录操作。
-
配置应用标识对 Azure DevOps 组织和项目的访问权限。 请参阅添加组织用户和管理访问权限。
-
如果你还没有工作流文件,请在你的存储库中添加
.github/workflows/copilot-setup-steps.ymlActions 工作流文件。 -
将Azure登录步骤添加到
copilot-setup-steps工作流作业。YAML # 此工作流使用未经 GitHub 认证的操作。 # 它们由第三方提供,并受 # 单独的服务条款、隐私政策和支持 # 文档。 on: workflow_dispatch: permissions: id-token: write contents: read jobs: copilot-setup-steps: runs-on: ubuntu-latest permissions: id-token: write contents: read environment: copilot steps: - name: Azure login uses: azure/login@a457da9ea143d694b1b9c7c869ebb04ebe844ef5 with: client-id: ${{ secrets.AZURE_CLIENT_ID }} tenant-id: ${{ secrets.AZURE_TENANT_ID }} allow-no-subscriptions: true# 此工作流使用未经 GitHub 认证的操作。 # 它们由第三方提供,并受 # 单独的服务条款、隐私政策和支持 # 文档。 on: workflow_dispatch: permissions: id-token: write contents: read jobs: copilot-setup-steps: runs-on: ubuntu-latest permissions: id-token: write contents: read environment: copilot steps: - name: Azure login uses: azure/login@a457da9ea143d694b1b9c7c869ebb04ebe844ef5 with: client-id: ${{ secrets.AZURE_CLIENT_ID }} tenant-id: ${{ secrets.AZURE_TENANT_ID }} allow-no-subscriptions: true此配置可确保在 Copilot编程助理 运行时执行
azure/login操作。 -
在存储库的 Copilot 环境中,为
AZURE_CLIENT_ID和AZURE_TENANT_ID添加机密。 -
通过在 MCP 配置中添加
ado对象并定义要让 Copilot编程助理使用的工具,配置 Azure DevOps MCP 服务器。
{
"mcpServers": {
"ado": {
"type": "local",
"command": "npx",
"args": ["-y", "@azure-devops/mcp", "<your-azure-devops-organization>", "-a", "azcli"],
"tools": ["wit_get_work_item", "wit_get_work_items_batch_by_ids", ...]
}
}
}
{
"mcpServers": {
"ado": {
"type": "local",
"command": "npx",
"args": ["-y", "@azure-devops/mcp", "<your-azure-devops-organization>", "-a", "azcli"],
"tools": ["wit_get_work_item", "wit_get_work_items_batch_by_ids", ...]
}
}
}
示例:Atlassian
Atlassian MCP 服务器授予 Copilot 在通过身份验证后访问 Atlassian 应用(Jira、Compass 和 Confluence)的权限。
有关使用 API 密钥向 Atlassian MCP 服务器进行身份验证的详细信息,请参阅 Atlassian 文档中 的 API 令牌配置身份验证 。
// If you copy and paste this example, you will need to remove the comments prefixed with `//`, which are not valid JSON.
{
"mcpServers": {
"atlassian-rovo-mcp": {
"command": "npx",
"type": "local",
"tools": ["*"],
"args": [
"mcp-remote@latest",
"https://mcp.atlassian.com/v1/mcp",
// We can use the $ATLASSIAN_API_KEY environment variable which is passed
// to the server because of the `env` value below.
"--header",
"Authorization: Basic $ATLASSIAN_API_KEY"
],
"env": {
// The value of the `COPILOT_MCP_ATLASSIAN_API_KEY` secret will be passed
// to the server command as an environment variable
// called `ATLASSIAN_API_KEY`.
"ATLASSIAN_API_KEY": "$COPILOT_MCP_ATLASSIAN_API_KEY"
}
}
}
}
// If you copy and paste this example, you will need to remove the comments prefixed with `//`, which are not valid JSON.
{
"mcpServers": {
"atlassian-rovo-mcp": {
"command": "npx",
"type": "local",
"tools": ["*"],
"args": [
"mcp-remote@latest",
"https://mcp.atlassian.com/v1/mcp",
// We can use the $ATLASSIAN_API_KEY environment variable which is passed
// to the server because of the `env` value below.
"--header",
"Authorization: Basic $ATLASSIAN_API_KEY"
],
"env": {
// The value of the `COPILOT_MCP_ATLASSIAN_API_KEY` secret will be passed
// to the server command as an environment variable
// called `ATLASSIAN_API_KEY`.
"ATLASSIAN_API_KEY": "$COPILOT_MCP_ATLASSIAN_API_KEY"
}
}
}
}
从 Visual Studio Code
重用 MCP 配置
如果已在 VS Code 中配置 MCP 服务器,可以对 Copilot编程助理 利用相似配置。
根据 VS Code 的配置方式,有可能可在存储库的 .vscode/mcp.json 文件或计算机的专用 settings.json 文件中找到 MCP 设置。
若要调整 Copilot编程助理 的配置,需要:
- 为每个 MCP 服务器添加一个
tools键,指定哪些工具可供 Copilot 使用。 - 如果配置了
inputs,直接切换为使用env。 - 如果配置了
envFile,直接切换为使用env。 - 将
args配置中对inputs的任何引用更新为引用env中的环境变量。
有关 VS Code 中 MCP 的详细信息,请参阅 VS Code 文档。
为 Copilot编程助理 设置 Copilot 环境
某些 MCP 服务器需要密钥或机密。 若要在 Copilot编程助理 中利用这些服务器,可以在 Copilot 的环境中添加机密。 这可确保正确识别机密并将其传递给已配置的适用的 MCP 服务器。
要为存储库配置 Copilot 环境,需要具有存储库管理员身份。
-
在 GitHub 上,导航到存储库的主页面。
-
在仓库名称下,单击 “Settings”****。 如果看不到“设置”选项卡,请选择“”下拉菜单,然后单击“设置”。

-
在左侧边栏中,单击“环境”。
-
单击“新建环境”。
-
`copilot`调用新环境 ****,然后单击“Configure environment”。 -
** **在“Environment secrets”下,单击“Add environment secret”。 -
`COPILOT_MCP_`将机密命名为以 **** 开头的名称,添加机密值,然后单击“Add secret”。
验证 MCP 配置
设置 MCP 配置后,应对其进行测试以确保设置正确。
- 在存储库中创建问题,然后将其分配给 Copilot。
- 等待几秒钟,Copilot 会对议题做出 👀 反应。
- 再等待几秒钟,Copilot 会创建拉取请求,该请求会显示在议题的时间线中。
- 单击时间线中创建的拉取请求,等待“Copilot started work”时间线事件出现。
-
** **单击“View session”,打开 Copilot编程助理 日志。 - 单击日志查看器右上角的省略号按钮 (...),然后单击边栏中的 Copilot。
- 单击Start MCP Servers步骤以展开日志。
- 如果 MCP 服务器成功启动,日志底部会列出其工具。
如果 MCP 服务器需要任何 GitHub Actions 运行程序上未默认安装的依赖项,如 uv 和 pipx,或需要专门设置步骤的依赖项,则可能需要创建 copilot-setup-steps.yml 操作工作流文件来安装它们。 有关详细信息,请参阅“自定义 GitHub Copilot 编码代理的开发环境”。
自定义内置 GitHub MCP 服务器
GitHub MCP 服务器默认启用并通过特定作用范围的令牌连接到 GitHub,该令牌对当前仓库仅具有只读访问权限。
如果要允许 Copilot 访问当前存储库以外的数据,可以向其提供具有更广泛访问权限的 personal access token。
-
创建具有适当权限的 personal access token。 建议使用 fine-grained personal access token,可以在其中将访问令牌的权限限为特定存储库的只读权限。 有关 personal access tokens 的详细信息,请参阅 管理个人访问令牌。
-
在 GitHub 上,导航到存储库的主页面。
-
在仓库名称下,单击 “Settings”****。 如果看不到“设置”选项卡,请选择“”下拉菜单,然后单击“设置”。

-
在边栏的“代码与自动化”部分中,依次单击 Copilot 和 编程代理。
-
请在“MCP configuration”部分添加您的配置。 例如,可以添加以下内容:
JavaScript // If you copy and paste this example, you will need to remove the comments prefixed with `//`, which are not valid JSON. { "mcpServers": { "github-mcp-server": { "type": "http", // Remove "/readonly" to enable wider access to all tools. // Then, use the "X-MCP-Toolsets" header to specify which toolsets you'd like to include. // Use the "tools" field to select individual tools from the toolsets. "url": "https://api.githubcopilot.com/mcp/readonly", "tools": ["*"], "headers": { "X-MCP-Toolsets": "repos,issues,users,pull_requests,code_security,secret_protection,actions,web_search" } } } }// If you copy and paste this example, you will need to remove the comments prefixed with `//`, which are not valid JSON. { "mcpServers": { "github-mcp-server": { "type": "http", // Remove "/readonly" to enable wider access to all tools. // Then, use the "X-MCP-Toolsets" header to specify which toolsets you'd like to include. // Use the "tools" field to select individual tools from the toolsets. "url": "https://api.githubcopilot.com/mcp/readonly", "tools": ["*"], "headers": { "X-MCP-Toolsets": "repos,issues,users,pull_requests,code_security,secret_protection,actions,web_search" } } } }有关工具集的详细信息,请参阅 GitHub 远程 MCP 服务器文档中的 自述文件 。
-
单击“ 保存”。
-
在左侧边栏中,单击“环境”。
-
单击
copilot环境。 -
** **在“Environment secrets”下,单击“Add environment secret”。 -
调用机密
COPILOT_MCP_GITHUB_PERSONAL_ACCESS_TOKEN,在“Value”字段中输入您的personal access token,然后点击添加机密。
有关如何在其他环境中配置 GitHub MCP 服务器的信息,请参阅“使用 GitHub MCP 服务器”。
后续步骤
-
[AUTOTITLE](/copilot/how-tos/copilot-cli/customize-copilot/add-mcp-servers) -
[AUTOTITLE](/copilot/how-tos/use-copilot-agents/coding-agent/create-custom-agents) -
[AUTOTITLE](/copilot/customizing-copilot/customizing-the-development-environment-for-copilot-coding-agent) -
[AUTOTITLE](/copilot/customizing-copilot/extending-copilot-chat-with-mcp)