注意
The GitHub MCP server is currently in 公共预览版 and subject to change.
关于 GitHub MCP 服务器
GitHub MCP 服务器是由 GitHub 提供并维护的模型上下文协议 (MCP) 服务器。 MCP 使你可以将 AI 功能集成到其他工具和服务中,通过提供基于上下文的 AI 辅助,提升你的开发体验。
有关 MCP 的详细信息,请参阅官方 MCP 文档。
你可以通过 Visual Studio Code 或其他支持远程 MCP 的编辑器远程访问 GitHub MCP 服务器;也可以在任何与 MCP 兼容的编辑器中本地运行该服务器,灵活选择托管解决方案的便捷性或自托管设置的可自定义性。
如果你希望使用远程 GitHub MCP 服务器,只需几个步骤即可完成,无需任何本地设置。 对于希望快速利用 GitHub AI 功能,又不想承担本地服务器运维负担的用户来说,这种方式非常实用。
在本地运行 GitHub MCP 服务器则需要更多设置,但可让你对 AI 交互实现更高程度的自定义和控制。
你可以使用 GitHub MCP 服务器来实现以下目的:
- 自动化并简化与代码相关的任务。
- 连接第三方工具(如 Cursor、Windsurf 或未来的集成),以利用 GitHub 的上下文和 AI 功能。
- 实现无需本地设置、可从任何设备访问的基于云的工作流。
可用性
目前,Visual Studio Code、JetBrains IDE、Xcode 等客户端广泛支持本地 MCP 服务器。
而对远程 MCP 服务器的支持也在不断扩大,现已包括 Visual Studio Code(支持 OAuth 或 PAT)、Visual Studio(仅支持 PAT)、Windsurf(仅支持 PAT)和 Cursor(仅支持 PAT)等编辑器。
如需了解你偏好的编辑器是否支持远程 MCP 服务器,请查阅该编辑器的相关文档。
先决条件
- GitHub 帐户。
- Visual Studio Code 或其他与 MCP 兼容的编辑器。
Setting up the GitHub MCP server in Visual Studio Code
The instructions below guide you through setting up the GitHub MCP server in Visual Studio Code. Other MCP-compatible editors may have similar steps, but the exact process may vary.
You can choose to set up the GitHub MCP server either remotely or locally, depending on your needs and preferences. You can also configure your GitHub MCP server for either:
- 特定仓库。 这使你能够将 MCP 服务器共享给任何在 Visual Studio Code 中打开该项目的人。 为此,请在仓库的根目录中,创建
.vscode/mcp.json
文件。 - Visual Studio Code 的个人实例。 你将是唯一有权访问已配置的 MCP 服务器的人员。 为此,请将配置添加到 Visual Studio Code 中的
settings.json
文件。 以这种方式配置的 MCP 服务器将在所有工作区中可用。
The remote GitHub MCP server uses one-click OAuth authentication by default, but you can also manually configure it to use a personal access token (PAT) for authentication. If you use OAuth, the MCP server will have the same access as your personal account. If you use a PAT, the MCP server will have access to the scopes granted by the PAT.
注意
If you are an Enterprise Managed User with PAT restrictions, you won't be able to use PAT authentication. If you have OAuth access policy restrictions, you will need the OAuth apps for each client to be enabled (except Visual Studio Code and Visual Studio).
- Remote MCP server configuration with OAuth
- Remote MCP server configuration with PAT
- Local MCP server setup
Remote MCP server configuration with OAuth
注意
The remote GitHub MCP server is not available to GitHub Enterprise Server users. If you are using GitHub Enterprise Server, you can install the GitHub MCP server locally. See Local MCP server setup.
You do not need to create a PAT or install any additional software to use the remote GitHub MCP server with OAuth. You can set it up directly in Visual Studio Code. You can also install individual toolsets, either in read-only mode or with full read/write access, allowing you to tailor the server's capabilities to your specific needs. For more information, see Tool configuration.
-
In Visual Studio Code, open the command palette by pressing Ctrl+Shift+P (Windows/Linux) / Command+Shift+P (Mac).
-
Type:
mcp: add server
and then press Enter. -
From the list, select HTTP (HTTP or Server-Sent Events).
-
In the Server URL field, enter
https://api.githubcopilot.com/mcp/
, and press Enter. -
Under Enter Server ID, press Enter to use the default server ID, or enter a custom server ID.
-
Under Choose where to save the configuration, select where you want to save the MCP server configuration.
- Visual Studio Code will open and add the configuration to the selected file in the editor, or create a new file if it doesn't exist.
-
In the Visual Studio Code popup, to authorize the MCP server with OAuth, click Allow and select your personal account from the list.
Remote MCP server configuration with PAT
To configure the remote GitHub MCP server with a PAT, ensure you have created a PAT with the necessary scopes for the access you want to grant to the MCP server. For more information, see 管理个人访问令牌.
You will need to manually configure the MCP server in Visual Studio Code to use the PAT for authorization.
-
In Visual Studio Code, open the command palette by pressing Ctrl+Shift+P (Windows/Linux) / Command+Shift+P (Mac).
-
Type:
mcp: add server
and then press Enter. -
From the list, select HTTP (HTTP or Server-Sent Events).
-
In the Server URL field, enter
https://api.githubcopilot.com/mcp/
, and press Enter. -
Under Enter Server ID, press Enter to use the default server ID, or enter a custom server ID.
-
Under Choose where to save the configuration, select where you want to save the MCP server configuration.
- Visual Studio Code will open and add the configuration to the selected file in the editor, or create a new file if it doesn't exist.
-
In the Visual Studio Code popup, to decline OAuth authorization, click Cancel.
-
You will need to manually edit the configuration file to use a PAT. In the configuration file, at the end of the
url
line, add the following:JSON , "headers": { "Authorization": "Bearer ${input:github_token}" } }, }, "inputs": [ { "id": "github_token", "type": "promptString", "description": "GitHub Personal Access Token", "password": true } ] }
, "headers": { "Authorization": "Bearer ${input:github_token}" } }, }, "inputs": [ { "id": "github_token", "type": "promptString", "description": "GitHub Personal Access Token", "password": true } ] }
-
A "Restart" button will appear in the file. Click "Restart" to restart the MCP server with the new configuration.
-
In the command palette, you will see a prompt to enter your GitHub token. Enter the PAT you created earlier, and press Enter.
- The MCP server will now be configured to use the PAT for authorization.
Local MCP server setup
注意
If you are a GitHub Enterprise Server user, and your enterprise has PAT restrictions enabled, you can only use API endpoints for scopes that are allowed by your enterprise's PAT policy. If all API endpoints are restricted, you will not be able to use the MCP server. If you are unsure about your enterprise's PAT policy, contact your enterprise administrator for more information.
Using the GitHub MCP server locally requires you to have Docker installed and running on your machine. Additionally, you can only authenticate with a PAT, as OAuth is not supported for local MCP servers.
-
Ensure you have Docker installed and running on your machine. See Docker installation instructions.
-
Create a PAT with (at least) the
read:packages
andrepo
scopes. For more information, see 管理个人访问令牌. -
Decide whether you want to configure the MCP server for a specific repository or for your personal instance of Visual Studio Code.
- If you are using a specific repository, open the
.vscode/mcp.json
file in Visual Studio Code, and add the following configuration:
JSON { "inputs": [ { "type": "promptString", "id": "github_token", "description": "GitHub Personal Access Token", "password": true } ], "servers": { "github": { "command": "docker", "args": [ "run", "-i", "--rm", "-e", "GITHUB_PERSONAL_ACCESS_TOKEN", "ghcr.io/github/github-mcp-server" ], "env": { "GITHUB_PERSONAL_ACCESS_TOKEN": "${input:github_token}" } } } }
{ "inputs": [ { "type": "promptString", "id": "github_token", "description": "GitHub Personal Access Token", "password": true } ], "servers": { "github": { "command": "docker", "args": [ "run", "-i", "--rm", "-e", "GITHUB_PERSONAL_ACCESS_TOKEN", "ghcr.io/github/github-mcp-server" ], "env": { "GITHUB_PERSONAL_ACCESS_TOKEN": "${input:github_token}" } } } }
- If you are using your personal instance of Visual Studio Code, open your
settings.json
file in Visual Studio Code:- Press Ctrl+Shift+P (Windows/Linux) or Cmd+Shift+P (macOS).
- Type Preferences: Open Settings (JSON) and select it.
- Add the following configuration:
JSON { "mcp": { "inputs": [ { "type": "promptString", "id": "github_token", "description": "GitHub Personal Access Token", "password": true } ], "servers": { "github": { "command": "docker", "args": [ "run", "-i", "--rm", "-e", "GITHUB_PERSONAL_ACCESS_TOKEN", "ghcr.io/github/github-mcp-server" ], "env": { "GITHUB_PERSONAL_ACCESS_TOKEN": "${input:github_token}" } } } } }
{ "mcp": { "inputs": [ { "type": "promptString", "id": "github_token", "description": "GitHub Personal Access Token", "password": true } ], "servers": { "github": { "command": "docker", "args": [ "run", "-i", "--rm", "-e", "GITHUB_PERSONAL_ACCESS_TOKEN", "ghcr.io/github/github-mcp-server" ], "env": { "GITHUB_PERSONAL_ACCESS_TOKEN": "${input:github_token}" } } } } }
- If you are using a specific repository, open the
-
Save the file.
-
In the command palette, you will see a prompt to enter your GitHub token. Enter the PAT you created earlier, and press Enter.
- The MCP server will now be configured to run locally with the PAT for authorization.
Tool configuration
The GitHub MCP server supports installing individual toolsets, either in read-only mode or with full read/write access, allowing you to tailor the server's capabilities to your specific needs. For one-click installation options of each toolset, see the GitHub MCP server repository.
Using the GitHub MCP server in Visual Studio Code
The GitHub MCP server enables you to perform a wide range of actions on GitHub, via Copilot Chat in Visual Studio Code.
- 单击 Visual Studio Code 标题栏中的 图标,打开 Copilot Chat。
- 在 Copilot Chat 框中,从弹出菜单选择“Agent”****。
- To see the available actions, in the Copilot Chat box, click the Select tools icon.
- In the Tools dropdown, under MCP Server:GitHub, you will see a list of available actions.
- In the Copilot Chat box, type a command or question related to the action you want to perform, and press Enter.
- For example, you can ask the GitHub MCP server to create a new issue, list pull requests, or retrieve repository information.
- The GitHub MCP server will process your request and provide a response in the chat interface.
- In the Copilot Chat box, you may be asked to give additional permissions or provide more information to complete the action.
- Follow the prompts to complete the action.
故障排除
如果你在使用 GitHub MCP 服务器时遇到问题,可以尝试以下一些常见的故障排除步骤:
授权问题
如果你在授权 MCP 服务器时遇到问题,请确保:
- 你已在所选的 IDE 中登录 GitHub。
如果你使用 personal access token (PAT) 进行身份验证,请确保:
- 你的 GitHub PAT 是有效的,并且具备你希望执行的操作所需的权限范围。
- 当在 Visual Studio Code 中提示输入 PAT 时,你输入了正确的 PAT。
Copilot 代理模式问题
如果你在使用 Copilot Chat 代理模式时遇到问题,请确保:
- 你已在 Copilot Chat 框中选择了正确的代理。
- 你已在 Visual Studio Code 中正确配置了 MCP 服务器。
- 你拥有执行目标操作所需的权限。
一般提示
如果你遇到其他与 GitHub MCP 服务器相关的问题,可以尝试以下通用的故障排除建议:
- 检查 MCP 服务器的输出日志,查看是否有任何错误信息。
- 如果你是在本地运行 MCP 服务器,请确保你的本地环境已正确设置为支持运行 Docker 容器。
- 尝试重启 MCP 服务器或 IDE。