Gemini模型协议服务器

一个提供访问Google Gemini API的模型上下文协议服务器,支持智能网页搜索、内容生成等功能,可在本地或远程运行。

  • by philschmid
  • Python
  • 本地部署
  • 54 星标
  • 更新于
在 GitHub 查看 philschmid/gemini-mcp-server

使用说明

[!注意]
MCP服务器当前在以下位置可用 https://gemini-mcp-server-231532712093.europe-west1.run.app/mcp/.它被部署到Google Cloud Run,可以使用AI Studio API密钥进行身份验证。看见 示例/test_remote.py 例如,如何将服务器与 google-genai 客户。

Gemini MCP服务器

一个模型上下文协议服务器,提供访问谷歌的Gemini API。此服务器使LLM能够执行智能网络搜索、生成内容和访问其他Gemini功能。它支持STDIO和流式http传输模式,可以在本地或远程运行。如果您使用STDIO模式,它将尝试使用 GEMINI_API_KEY 环境变量。如果您使用流式http模式,它将尝试使用Authorization标头中的Bearer令牌。

可用工具:

  • 网络搜索 -使用Gemini执行网络搜索,并返回带有引用的合成结果
    • query (string,必填):要执行的搜索查询
    • include_citations (布尔值,可选):是否在响应中包含引用。默认值为 False.
  • 使用gmini -将任务委托给指定的Gemini 2.5型号(Pro或Flash)。
    • prompt (string,必填):双子座的提示或任务。
    • model (字符串,可选):使用Gemini模型。默认值为 gemini-2.5-flash-preview-05-20.

安装

pip install git+https://github.com/philschmid/gemini-mcp-server.git

认证

  • STDIO模式:用途 GEMINI_API_KEY 环境变量
  • HTTP模式:需要Authorization标头中的Bearer令牌

运行服务器

STDIO模式(本地/直接集成)

GEMINI_API_KEY="your_gemini_api_key_here" gemini-mcp --transport stdio

HTTP模式(网络访问)

gemini-mcp --transport streamable-http

服务器将于启动 http://0.0.0.0:8000/mcp/

部署

您可以将Gemini MCP服务器作为远程MCP服务器部署到Google Cloud Run,使其易于供任何客户端使用。

要部署服务器,请从终端运行以下命令,替换 [PROJECT-ID] 和 [REGION] 使用您的Google Cloud项目ID和所需地区:

# Set your project ID and region
export PROJECT_ID=remote-mcp-test-462811
export REGION=europe-west1
export SERVICE_NAME=gemini-mcp-server

# Authenticate with Google Cloud
gcloud auth login
gcloud config set project $PROJECT_ID

# Enable required services
gcloud services enable run.googleapis.com artifactregistry.googleapis.com cloudbuild.googleapis.com

# Deploy the service
gcloud run deploy $SERVICE_NAME \
  --source . \
  --region $REGION \
  --port 8000 \
  --allow-unauthenticated

该命令将构建Docker镜像,将其推送到Google Artifact Registry,并将其部署到Cloud Run。部署完成后,您将获得服务的URL。我们将允许未经身份验证的访问该服务,这意味着任何拥有该URL的人都可以向服务器发送请求,服务器本身受到授权标头的保护。如果您想确保服务的安全,可以按照 Cloud Run文档.

清理

SERVICE_NAME=gemini-mcp-server
REGION=europe-west1
gcloud run services delete $SERVICE_NAME --region $REGION

使用示例

添加到您的 mcpServers 配置:

STDIO模式:

{
  "mcpServers": {
    "gemini-search": {
      "command": "gemini-mcp",
      "args": ["--transport", "stdio"],
      "env": {
        "GEMINI_API_KEY": "your_gemini_api_key_here"
      }
    }
  }
}

HTTP模式:

{
  "mcpServers": {
    "gemini-mcp": {
      "url": "https://remote-mcp-test.com/mcp/", // replace with your remote mcp server url
      "headers": { "Authorization": "Bearer YOUR_KEY" } // replace with your AI Studio API key
    }
  }
}

或查看示例 示例/test_remote.py 文件。

from mcp.client.streamable_http import streamablehttp_client

remote_url = "https://remote-mcp-test.com/mcp/" # replace with your remote mcp server url

async with streamablehttp_client(
    remote_url, headers={"Authorization": f"Bearer {api_key}"}
) as (read, write, _):

与MCP检查员一起

使用可流式传输的http启动服务器,并使用MCP检查器测试您的服务器。或者启动检查器并使用stdio运行服务器。

npx @modelcontextprotocol/inspector

Web搜索工具示例

随着 include_citations 着手 False:

{
  "text": "Recent advancements in AI include breakthrough developments in large language models, computer vision, and autonomous systems..."
}

随着 include_citations 着手 True:

{
  "text": "Recent advancements in AI include breakthrough developments in large language models, computer vision, and autonomous systems...",
  "web_search_queries": ["latest AI developments 2024", "AI breakthroughs"],
  "citations": [
    {
      "start_index": 24,
      "end_index": 56,
      "sources": [
        {
          "title": "Latest AI Developments 2024",
          "uri": "https://example.com/ai-news"
        }
        ...
      ],
      "text": "breakthrough developments in large language models"
    },
    ...
  ]
}

使用Gemini工具响应示例

{
  "text": "The capital of France is Paris."
}

测试

要运行测试,请从根目录运行以下命令:

注意:您需要设置 GEMINI_API_KEY 运行测试的环境变量。

pytest

许可证

该项目根据MIT许可证获得许可。