The Model Context Protocol (MCP) is an open standard that defines how applications share context with large language models (LLMs). MCP provides a standardized way to connect AI models to different data sources and tools, enabling them to work together more effectively.
You can use MCP to extend the capabilities of Copilot 编码智能体 by connecting it to other tools and services.
该代理可使用本地 MCP 服务器提供的工具。 例如,Playwright MCP 服务器提供了一些工具,用于在执行请求的任务时与网页进行交互,并获取额外的上下文信息。
有关 MCP 的详细信息,请参阅官方 MCP 文档。 有关一些当前可用的 MCP 服务器的信息,请参阅 MCP 服务器存储库。
注意
- Copilot 编码智能体 仅支持 MCP 服务器提供的工具。 它不支持资源或提示。
- Copilot 编码智能体 目前仅支持本地 MCP 服务器。 若要了解有关传输类型的详细信息,请参阅官方 MCP 文档。
通过使用 MCP 服务器保持安全性
配置 MCP 服务器后,Copilot 将能够自主使用服务器提供的工具且在使用之前不请求批准。
建议将服务器权限限制为仅可使用只读工具。 可通过使用 tools
配置选项,仅向 Copilot 公开已知且安全的工具。
关于在存储库中设置 MCP 服务器
作为存储库管理员,可以将 MCP 服务器配置为在存储库中使用。 这通过 JSON 格式的配置完成,该配置指定要使用的 MCP 服务器的详细信息。 直接在 GitHub.com 上的存储库的设置中输入 JSON 配置。
将 MCP 服务器配置为在存储库中使用后,配置中指定的工具将可供 Copilot 编码智能体 用于每个分配的任务。
创建 JSON MCP 配置
使用专用 JSON 格式配置 MCP 服务器。 JSON 必须包含一个 mcpServers
对象,其中键是 MCP 服务器的名称(例��� playwright
),值是具有该 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 ],
...
},
...
}
}
配置对象可包含以下键:
command
(string
):启动 MCP 服务器时要运行的命令。args
(string[]
):要传递给command
的参数。tools
(string[]
):要启用的 MCP 服务器中的工具。 有可能在服务器的文档或代码中找到工具列表。 建议将特定工具列入允许列表,但也可以通过在*
数组中包含所有工具来启用这些工具。type
(string
):可选字段。 Copilot 编码智能体 仅接受"local"
。env
(object
):要传递给服务器的环境变量。 此对象须将应向 MCP 服务器公开的环境变量的名称映射到以下任一项:- 已配置的 GitHub Actions 机密的名称,以
COPILOT_MCP_
开头。 - 字符串值。
- 已配置的 GitHub Actions 机密的名称,以
示例配置
示例:Playwright
Playwright MCP 服务器提供了使 Copilot 能够浏览 Internet 的工具。
{ "mcpServers": { "playwright": { "command": "docker", "args": ["run", "-i", "--rm", "--init", "mcp/playwright"], "tools": ["*"] } } }
{
"mcpServers": {
"playwright": {
"command": "docker",
"args": ["run", "-i", "--rm", "--init", "mcp/playwright"],
"tools": ["*"]
}
}
}
示例:Sentry
Sentry MCP 服务器为 Copilot 提供经身份验证的访问权限,从而能够访问 Sentry 中记录的异常。
// If you copy and paste this example, you will need to remove the comments prefixed with `//`, which are not valid JSON. { "mcpServers": { "sentry": { "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 GitHub Actions secret with a name starting with // `COPILOT_MCP_` "SENTRY_AUTH_TOKEN": "COPILOT_MCP_SENTRY_AUTH_TOKEN" } } } }
// If you copy and paste this example, you will need to remove the comments prefixed with `//`, which are not valid JSON.
{
"mcpServers": {
"sentry": {
"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 GitHub Actions secret with a name starting with
// `COPILOT_MCP_`
"SENTRY_AUTH_TOKEN": "COPILOT_MCP_SENTRY_AUTH_TOKEN"
}
}
}
}
示例:Notion
Notion MCP 服务器向 Copilot 经身份验证的访问权限,从而能够访问来自 Notion 的笔记和其他内容。
// If you copy and paste this example, you will need to remove the comments prefixed with `//`, which are not valid JSON. { "mcpServers": { "notionApi": { "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": {
"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
Azure MCP 服务器在 Copilot 与重要的 Azure 服务(例如 Azure Cosmos DB 和 Azure 存储平台)之间建立无缝连接。
若要将 Azure MCP 与 Copilot 编码智能体 配合使用,需要更新存储库的 copilot-setup-steps.yml
文件,使其包含 Azure 登录工作流步骤。
-
在 Microsoft Entra 应用程序中配置 OIDC,以便信任 GitHub。 请参阅将 Azure 登录操作与 OpenID Connect 结合使用。
-
如果还没有作工作流文件,在存储库中添加
.github/workflows/copilot-setup-steps.yml
操作工作流文件。 -
将 Azure 登录步骤添加到
copilot-setup-steps
工作流作业。YAML 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@a1b2c3d4e5f6a7b8c9d0e1f2a3b4c5d6e7f8a9b0 with: client-id: $ tenant-id: $ subscription-id: $
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@a1b2c3d4e5f6a7b8c9d0e1f2a3b4c5d6e7f8a9b0 with: client-id: $ tenant-id: $ subscription-id: $
此配置可确保在
azure/login
Copilot 编码智能体 运行时会执行该操作。 -
在存储库的 Copilot 环境中,为
AZURE_CLIENT_ID
、AZURE_TENANT_ID
和AZURE_SUBSCRIPTION_ID
添加机密。 -
通过将
azure
对象添加到 MCP 配置中来配置 Azure MCP 服务器。JSON { "mcpServers": { "Azure MCP Server": { "command": "npx", "args": [ "-y", "@azure/mcp@latest", "server", "start" ] } } }
{ "mcpServers": { "Azure MCP Server": { "command": "npx", "args": [ "-y", "@azure/mcp@latest", "server", "start" ] } } }
从 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 文档。
将配置添加到存储库
存储库管理员可以按照以下步骤配置 MCP 服务器:
-
在 GitHub 上,导航到存储库的主页面。
-
在存储库名称下,单击 “设置”。 如果看不到“设置”选项卡,请选择“”下拉菜单,然后单击“设置”。
-
在边栏的“Code & automation”部分,单击 Copilot,然后单击 Copilot 代理********。
-
**** 在“MCP configuration”部分中添加配置。
-
单击“ 保存”。
系统将验证配置以确保其语法正确。
-
如果 MCP 服务器需要密钥或机密,请将机密添加到 Copilot 环境。 仅当机密名称带有前缀
COPILOT_MCP_
才可用于 MCP 配置。 请参阅为 Copilot 编码智能体 设置 Copilot 环境。
为 Copilot 编码智能体 设置 Copilot 环境
某些 MCP 服务器需要密钥或机密。 若要在 Copilot 编码智能体 中利用这些服务器,可以在 Copilot 的环境中添加机密。 这可确保正确识别机密并将其传递给已配置的适用的 MCP 服务器。
要为存储库配置 Copilot 环境,需要具有存储库管理员身份。
-
在 GitHub 上,导航到存储库的主页面。
-
在存储库名称下,单击 “设置”。 如果看不到“设置”选项卡,请选择“”下拉菜单,然后单击“设置”。
-
在左侧边栏中,单击“环境”。
-
单击“新建环境”。
-
**** 调用新环境
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
操作工作流文件来安装它们。 有关详细信息,请参阅“Customizing the development environment for Copilot coding agent”。
自定义内置 GitHub MCP 服务器
GitHub MCP 服务器默认处于启用状态,为 Copilot 提供访问 GitHub 数据(如问题和拉取请求)的权限。
MCP 服务器默认通过特定作用范围的令牌连接到 GitHub,该令牌对当前存储库仅具有只读访问权限。
如果要允许 Copilot 访问当前存储库以外的数据,可以向其提供具有更广泛访问权限的 personal access token。
-
创建具有适当权限的 personal access token。 建议使用 fine-grained personal access token,可在其中将令牌访问权限限为特定存储库的只读权限。 有关 personal access tokens 的详细信息,请参阅 管理个人访问令牌。
-
在 GitHub 上,导航到存储库的主页面。
-
在存储库名称下,单击 “设置”。 如果看不到“设置”选项卡,请选择“”下拉菜单,然后单击“设置”。
-
在边栏的“Code & automation”部分,单击 Copilot,然后单击 Copilot 代理********。
-
**** 在“MCP configuration”部分中添加配置。
-
单击“ 保存”。
-
在左侧边栏中,单击“环境”。
-
单击
copilot
环境。 -
**** 在“Environment secrets”下,单击“Add environment secret”。
-
**** 调用机密
COPILOT_MCP_GITHUB_PERSONAL_ACCESS_TOKEN
,在“Value”字段中输入 personal access token,然后单击“Add secret”。
最佳做法
-
启用第三方 MCP 服务器可能会影响代理的性能和输出的质量。 详尽审查第三方 MCP 服务器,确保其满足组织的要求。
-
默认情况下,Copilot 编码智能体 无权编写 MCP 服务器工具。 但是,某些 MCP 服务器确实包含此类工具。 请务必查看要使用的 MCP 服务器中可用的工具。 仅使用必要的工具更新 MCP 配置中的
tools
字段。 -
在保存配置之前,请仔细查看配置的 MCP 服务器,以确保配置了正确的服务器。