教程
¥Tutorials
本页包含与 Gemini CLI 交互的教程。
¥This page contains tutorials for interacting with Gemini CLI.
设置模型上下文协议 (MCP) 服务器
¥Setting up a Model Context Protocol (MCP) server
在使用第三方 MCP 服务器之前,请确保您信任其来源并了解其提供的工具。使用第三方服务器的风险由您自行承担。
¥[!CAUTION] Before using a third-party MCP server, ensure you trust its source and understand the tools it provides. Your use of third-party servers is at your own risk.
本教程演示如何使用GitHub MCP 服务器作为示例。GitHub MCP 服务器提供了与 GitHub 存储库交互的工具,例如创建问题和评论拉取请求。
¥This tutorial demonstrates how to set up a MCP server, using the GitHub MCP server as an example. The GitHub MCP server provides tools for interacting with GitHub repositories, such as creating issues and commenting on pull requests.
先决条件
¥Prerequisites
开始之前,请确保已安装并配置以下内容:
¥Before you begin, ensure you have the following installed and configured:
Docker:安装并运行Docker。
¥Docker: Install and run Docker.
GitHub 个人访问令牌 (PAT):创建新的经典的或者细粒度PAT 具有必要的范围。
¥GitHub Personal Access Token (PAT): Create a new classic or fine-grained PAT with the necessary scopes.
指导
¥Guide
配置 MCP 服务器settings.json
¥Configure the MCP server in settings.json
在项目的根目录中,创建或打开.gemini/settings.json文件. 在文件中,添加mcpServers配置块,提供如何启动 GitHub MCP 服务器的说明。
¥In your project's root directory, create or open the .gemini/settings.json file. Within the file, add the mcpServers configuration block, which provides instructions for how to launch the GitHub MCP server.
{
"mcpServers": {
"github": {
"command": "docker",
"args": [
"run",
"-i",
"--rm",
"-e",
"GITHUB_PERSONAL_ACCESS_TOKEN",
"ghcr.io/github/github-mcp-server"
],
"env": {
"GITHUB_PERSONAL_ACCESS_TOKEN": "${GITHUB_PERSONAL_ACCESS_TOKEN}"
}
}
}
}
设置你的 GitHub 令牌
¥Set your GitHub token
使用可访问个人和私有存储库的广泛范围的个人访问令牌可能会导致私有存储库中的信息泄露到公共存储库中。我们建议使用细粒度的访问令牌,该令牌不会同时共享对公共和私有存储库的访问权限。
¥[!CAUTION] Using a broadly scoped personal access token that has access to personal and private repositories can lead to information from the private repository being leaked into the public repository. We recommend using a fine-grained access token that doesn't share access to both public and private repositories.
使用环境变量来存储您的 GitHub PAT:
¥Use an environment variable to store your GitHub PAT:
GITHUB_PERSONAL_ACCESS_TOKEN="pat_YourActualGitHubTokenHere"
Gemini CLI 在以下位置使用此值mcpServers您在settings.json文件。
¥Gemini CLI uses this value in the mcpServers configuration that you defined in the settings.json file.
启动 Gemini CLI 并验证连接
¥Launch Gemini CLI and verify the connection
当你启动 Gemini CLI 时,它会自动读取你的配置并在后台启动 GitHub MCP 服务器。然后,你可以使用自然语言提示来要求 Gemini CLI 执行 GitHub 操作。例如:
¥When you launch Gemini CLI, it automatically reads your configuration and launches the GitHub MCP server in the background. You can then use natural language prompts to ask Gemini CLI to perform GitHub actions. For example:
"get all open issues assigned to me in the 'foo/bar' repo and prioritize them"