Skip to main content
Glama
amitsh06

MCP Gemini Server

by amitsh06

MCP Gemini 服务器

模型上下文协议 (MCP) 的服务器实现,使 Claude 等 AI 助手能够与 Google 的 Gemini API 进行交互。

项目概述

该项目实现了一个遵循模型上下文协议 (MCP) 的服务器,允许 AI 助手与 Google 的 Gemini 模型进行通信。借助此 MCP 服务器,AI 助手可以通过 Gemini API 请求文本生成、文本分析以及维护聊天对话。

Related MCP server: Gemini MCP Server

特征

  • 客户端-服务器通信:实现 MCP 协议,以便在客户端和服务器之间进行安全的消息交换。

  • 消息处理:处理和处理客户端请求,发送适当的响应。

  • 错误处理和日志记录:记录服务器活动并确保顺利恢复错误。

  • 环境变量支持:使用.env文件安全地存储敏感信息。

  • API 测试和调试:支持使用 Postman 和测试脚本进行手动和自动测试。

安装

先决条件

  • Python 3.7 或更高版本

  • Google AI API 密钥

设置

  1. 克隆此存储库:

git clone https://github.com/yourusername/mcp-gemini-server.git
cd mcp-gemini-server
  1. 创建虚拟环境:

python -m venv venv
  1. 激活虚拟环境:

    • Windows: venv\Scripts\activate

    • macOS/Linux: source venv/bin/activate

  2. 安装依赖项:

pip install -r requirements.txt
  1. 使用您的 Gemini API 密钥在根目录中创建一个.env文件:

GEMINI_API_KEY=your_api_key_here

用法

  1. 启动服务器:

python server.py
  1. 默认情况下,服务器将在http://localhost:5000/上运行

  2. 使用 POST 方法向/mcp端点发送 MCP 请求

示例请求

import requests

url = 'http://localhost:5000/mcp'
payload = {
    'action': 'generate_text',
    'parameters': {
        'prompt': 'Write a short poem about AI',
        'temperature': 0.7
    }
}

response = requests.post(url, json=payload)
print(response.json())

API 参考

端点

  • GET /health :检查服务器是否正在运行

  • GET /list-models :列出可用的 Gemini 模型

  • POST /mcp :MCP 请求的主要端点

MCP 操作

1. 生成文本

使用 Gemini 生成文本内容。

参数:

  • prompt (必需):生成的文本提示

  • temperature (可选):控制随机性(0.0 到 1.0)

  • max_tokens (可选):要生成的最大令牌数

例子:

{
  "action": "generate_text",
  "parameters": {
    "prompt": "Write a short story about a robot",
    "temperature": 0.8,
    "max_tokens": 500
  }
}

2. 分析文本

分析文本内容。

参数:

  • text (必需):要分析的文本

  • analysis_type (可选):分析类型(“情绪”、“摘要”、“关键词”或“一般”)

例子:

{
  "action": "analyze_text",
  "parameters": {
    "text": "The weather today is wonderful! I love how the sun is shining.",
    "analysis_type": "sentiment"
  }
}

3. 聊天

与双子座进行对话。

参数:

  • messages (必需):具有“角色”和“内容”的消息对象数组

  • temperature (可选):控制随机性(0.0 到 1.0)

例子:

{
  "action": "chat",
  "parameters": {
    "messages": [
      {"role": "user", "content": "Hello, how are you?"},
      {"role": "assistant", "content": "I'm doing well! How can I help?"},
      {"role": "user", "content": "Tell me about quantum computing"}
    ],
    "temperature": 0.7
  }
}

错误处理

服务器返回适当的 HTTP 状态代码和错误消息:

  • 200 :请求成功

  • 400 :错误请求(缺少或无效参数)

  • 500 :服务器错误(API 问题等)

测试

使用附带的测试脚本来测试各种功能:

# Test all functionalities
python test_client.py

# Test specific functionality
python test_client.py text     # Test text generation
python test_client.py analyze  # Test text analysis
python test_client.py chat     # Test chat functionality

MCP 协议规范

这里实现的模型上下文协议遵循以下规范:

  1. 请求格式:

    • action :指定操作的字符串

    • parameters :包含特定于操作的参数的对象

  2. 响应格式:

    • result :包含操作结果的对象

    • error :解释任何错误的字符串(适用时)

执照

MIT 许可证

Related MCP Connectors

Related MCP Servers

  • A
    license
    Not graded
    quality
    B
    maintenance
    A Model Context Protocol server that enables Claude to collaborate with Google's Gemini AI models, providing tools for question answering, code review, brainstorming, test generation, and explanations.
    MIT
  • F
    license
    Not graded
    quality
    D
    maintenance
    A Model Context Protocol server that enables Claude to interact with Google's Gemini AI models, allowing users to ask Gemini questions directly from Claude.
    2
    -
  • -
    license
    Not graded
    quality
    Not graded
    maintenance
    A server that allows interaction with Google's Gemini AI through the Gemini CLI tool using the Model Context Protocol, providing a standardized interface for querying Gemini with various options and configurations.
    -

Appeared in Searches