🤖 MCP服务器Gemini
  ](https://nodejs.org/)
A. 最先进的模型上下文协议(MCP)服务器 它提供了与谷歌Gemini人工智能模型的无缝集成。该服务器使Claude Desktop和其他兼容MCP的客户端能够充分利用Gemini先进的人工智能功能。
✨ 特性
🧠 最新Gemini型号
- 双子座2.5 Pro -最有能力的复杂推理思维模型
- 双子座2.5闪光灯 -具有最佳性价比的快速思维模型
- Gemini 2.0系列 -具有先进功能的最新一代型号
- Gemini 1.5系列 -经过验证的可靠模型,适用于生产使用
🚀 高性能
- 🧠 思维模型 -Gemini 2.5系列,逐步推理
- 🔍 谷歌搜索暂停 -实时网络信息集成
- 📊 JSON模式 -带模式验证的结构化输出
- 🎯 系统说明 -行为定制和控制
- 👁️ 视觉支持 -图像分析和多模式功能
- 💬 对话记忆 -跨交互的上下文保护
🛠️ 生产就绪
- TypeScript -全型安全与现代化发展
- 全面的错误处理 -强大的错误管理和恢复
- 速率限制 -防止API滥用的内置保护
- 详细日志记录 -全面监控与调试
- 输入验证 -使用Zod进行安全参数验证
- 重试逻辑 -指数回退自动重试
🚀 快速开始
先决条件
安装
选项1:全局安装(推荐)
npm install -g mcp-server-gemini方案2:地方发展
git clone https://github.com/gurr-i/mcp-server-gemini-pro.git
cd mcp-server-gemini-pro
npm install
npm run build配置
1.设置API密钥
选项A:环境变量
export GEMINI_API_KEY="your_api_key_here"选项B:.env文件
echo "GEMINI_API_KEY=your_api_key_here" > .env2.配置克劳德桌面
添加到您的 claude_desktop_config.json:
对于全局安装:
{
"mcpServers": {
"gemini": {
"command": "mcp-server-gemini",
"env": {
"GEMINI_API_KEY": "your_api_key_here"
}
}
}
}对于本地安装:
{
"mcpServers": {
"gemini": {
"command": "node",
"args": ["/path/to/mcp-server-gemini-pro/dist/enhanced-stdio-server.js"],
"env": {
"GEMINI_API_KEY": "your_api_key_here"
}
}
}
}3.重新启动克劳德桌面
完全关闭并重新启动Claude Desktop以使更改生效。
💡 使用示例
配置后,您可以使用自然语言通过Claude Desktop使用Gemini:
基本文本生成
"Use Gemini to explain quantum computing in simple terms"
"Generate a creative story about AI using Gemini 2.5 Pro"高级功能
"Use Gemini with JSON mode to extract key points from this text"
"Use Gemini with grounding to get the latest news about AI"
"Generate a Python function using Gemini's thinking capabilities"图像分析
"Analyze this image with Gemini" (attach image)
"What's in this screenshot using Gemini vision?"开发任务
"Use Gemini to review this code and suggest improvements"
"Generate comprehensive tests for this function using Gemini"⚙️ 配置
环境变量
服务器可以使用环境变量或 .env 文件:
所需配置
# Google AI Studio API Key (required)
GEMINI_API_KEY=your_api_key_here可选配置
# Logging level (default: info)
# Options: error, warn, info, debug
LOG_LEVEL=info
# Enable performance metrics (default: false)
ENABLE_METRICS=false
# Rate limiting configuration
RATE_LIMIT_ENABLED=true # Enable/disable rate limiting (default: true)
RATE_LIMIT_REQUESTS=100 # Max requests per window (default: 100)
RATE_LIMIT_WINDOW=60000 # Time window in ms (default: 60000 = 1 minute)
# Request timeout in milliseconds (default: 30000 = 30 seconds)
REQUEST_TIMEOUT=30000
# Environment mode (default: production)
NODE_ENV=production环境设置
开发环境
# .env for development
GEMINI_API_KEY=your_api_key_here
NODE_ENV=development
LOG_LEVEL=debug
RATE_LIMIT_ENABLED=false
REQUEST_TIMEOUT=60000生产环境
# .env for production
GEMINI_API_KEY=your_api_key_here
NODE_ENV=production
LOG_LEVEL=warn
RATE_LIMIT_ENABLED=true
RATE_LIMIT_REQUESTS=100
RATE_LIMIT_WINDOW=60000
REQUEST_TIMEOUT=30000
ENABLE_METRICS=trueClaude桌面配置
配置文件位置
| 操作系统 | 路径 |
|---|---|
| macOS | ~/Library/Application Support/Claude/claude_desktop_config.json |
| 视窗 | %APPDATA%\Claude\claude_desktop_config.json |
| Linux | ~/.config/Claude/claude_desktop_config.json |
基本配置
{
"mcpServers": {
"gemini": {
"command": "mcp-server-gemini",
"env": {
"GEMINI_API_KEY": "your_api_key_here"
}
}
}
}高级配置
{
"mcpServers": {
"gemini": {
"command": "mcp-server-gemini",
"env": {
"GEMINI_API_KEY": "your_api_key_here",
"LOG_LEVEL": "info",
"RATE_LIMIT_REQUESTS": "200",
"REQUEST_TIMEOUT": "45000"
}
}
}
}本地开发配置
{
"mcpServers": {
"gemini": {
"command": "node",
"args": ["/path/to/mcp-server-gemini-pro/dist/enhanced-stdio-server.js"],
"cwd": "/path/to/mcp-server-gemini-pro",
"env": {
"GEMINI_API_KEY": "your_api_key_here",
"NODE_ENV": "development",
"LOG_LEVEL": "debug"
}
}
}
}🛠️ 可用工具
| 工具 | 描述 | 主要功能 |
|---|---|---|
| generate_text | 生成具有高级功能的文本 | 思维模型、JSON模式、基础 |
| 分析图像 | 使用视觉模型分析图像 | 多模态理解,详细分析 |
| count_tokes | 计算代币以进行成本估算 | 所有型号的准确代币计数 |
| list_models | 列出所有可用的Gemini型号 | 实时型号可用性和功能 |
| embed_text | 生成文本嵌入 | 高质量矢量表示 |
| 获取帮助 | 获取使用帮助和文档 | 通过示例进行自我记录 |
📊 模型比较
| 模型 | 上下文窗口 | 功能 | 最佳选择 | 速度 |
|---|---|---|---|---|
| 双子座-2.5-pro | 2M令牌 | 思考、JSON、接地 | 复杂推理、编码 | 较慢 |
| 双子座-2.5-flash ⭐ | 1M代币 | 思考、JSON、接地 | 通用 | 快速 |
| 双晶-2.5-长石 | 1M令牌 | 思考,JSON | 高吞吐量任务 | 最快 |
| 双子座2.0闪光 | 1M令牌 | JSON,接地 | 标准任务 | 快速 |
| 双子座-2.0-flash-lite | 1M令牌 | JSON | 简单任务 | 最快 |
| gemini-2.0-实验 | 2M令牌 | JSON,接地 | 实验功能 | 中等 |
| 双子座-1.5-pro | 2M令牌 | JSON | 传统支持 | 中等 |
| 双子座-1.5-flash | 1M令牌 | JSON | 传统支持 | 快速 |
🔧 发展
先决条件
设置
# Clone the repository
git clone https://github.com/gurr-i/mcp-server-gemini-pro.git
cd mcp-server-gemini-pro
# Install dependencies
npm install
# Set up environment variables
cp .env.example .env
# Edit .env and add your GEMINI_API_KEY可用脚本
发展
npm run dev # Start development server with hot reload
npm run dev:watch # Start with file watching (nodemon)
npm run build # Build for production
npm run build:watch # Build with watch mode
npm run clean # Clean build directory测试
npm test # Run all tests
npm run test:watch # Run tests in watch mode
npm run test:coverage # Run tests with coverage report
npm run test:integration # Run integration tests (requires API key)代码质量
npm run lint # Lint TypeScript code
npm run lint:fix # Fix linting issues automatically
npm run format # Format code with Prettier
npm run format:check # Check code formatting
npm run type-check # Run TypeScript type checking
npm run validate # Run all quality checks (lint + test + type-check)发布与分发
npm run prepack # Prepare package for publishing
npm run release # Build, validate, and publish to npm项目结构
mcp-server-gemini/
├── src/ # Source code
│ ├── config/ # Configuration management
│ │ └── index.ts # Environment config with Zod validation
│ ├── utils/ # Utility modules
│ │ ├── logger.ts # Structured logging system
│ │ ├── errors.ts # Custom error classes & handling
│ │ ├── validation.ts # Input validation with Zod
│ │ └── rateLimiter.ts # Rate limiting implementation
│ ├── enhanced-stdio-server.ts # Main MCP server implementation
│ └── types.ts # TypeScript type definitions
├── tests/ # Test suite
│ ├── unit/ # Unit tests
│ │ ├── config.test.ts # Configuration tests
│ │ ├── validation.test.ts # Validation tests
│ │ └── errors.test.ts # Error handling tests
│ ├── integration/ # Integration tests
│ │ └── gemini-api.test.ts # Real API integration tests
│ └── setup.ts # Test setup and utilities
├── docs/ # Documentation
│ ├── api.md # API reference
│ ├── configuration.md # Configuration guide
│ └── troubleshooting.md # Troubleshooting guide
├── scripts/ # Build and utility scripts
│ ├── build.sh # Production build script
│ ├── dev.sh # Development script
│ └── test.sh # Test execution script
├── .github/workflows/ # GitHub Actions CI/CD
│ ├── ci.yml # Continuous integration
│ └── release.yml # Automated releases
├── dist/ # Built output (generated)
├── coverage/ # Test coverage reports (generated)
└── node_modules/ # Dependencies (generated)🧪 测试
测试套件概述
该项目包括全面的测试,包括单元测试、集成测试和代码覆盖率报告。
运行测试
所有测试
npm test # Run all tests (unit tests only by default)
npm run test:watch # Run tests in watch mode for development
npm run test:coverage # Run tests with coverage report单元测试
npm test -- --testPathPattern=unit # Run only unit tests
npm test -- --testNamePattern="config" # Run specific test suites集成测试
集成测试需要有效的 GEMINI_API_KEY 并进行真正的API调用:
# Set API key and run integration tests
GEMINI_API_KEY=your_api_key_here npm run test:integration
# Or set in .env file and run
npm run test:integration测试覆盖率
npm run test:coverage # Generate coverage report
open coverage/lcov-report/index.html # View coverage report (macOS)测试结构
单元测试(tests/unit/)
- 配置测试:环境变量验证、配置加载
- 验证测试:输入验证、模式验证、净化
- 错误处理测试:自定义错误类、错误恢复、重试逻辑
- 公用设施测试:记录器、速率限制器、辅助功能
集成测试(tests/integration/)
- Gemini API测试:用于测试连接和功能的真实API调用
- 模型测试:验证所有支持的模型是否正常工作
- 特性测试:JSON模式、基础、嵌入、令牌计数
写作测试
测试文件结构
// tests/unit/example.test.ts
import { describe, it, expect, beforeEach, afterEach } from '@jest/globals';
import { YourModule } from '../../src/your-module.js';
describe('YourModule', () => {
beforeEach(() => {
// Setup before each test
});
afterEach(() => {
// Cleanup after each test
});
it('should do something', () => {
// Test implementation
expect(result).toBe(expected);
});
});定制配对器
测试套件包括自定义Jest匹配器:
expect(response).toBeValidMCPResponse(); // Validates MCP response format测试配置
测试配置在 jest.config.js 与:
- TypeScript支持:完整的ES模块和TypeScript编译
- 覆盖阈值:要求最低70%的覆盖率
- 测试超时:30秒用于集成测试
- 安装文件:自动测试环境设置
🐳 Docker部署
使用Docker
构建并运行
# Build the Docker image
docker build -t mcp-server-gemini .
# Run the container
docker run -d \
--name mcp-server-gemini \
-e GEMINI_API_KEY=your_api_key_here \
-e LOG_LEVEL=info \
mcp-server-gemini使用Docker Compose
# Create .env file with your API key
echo "GEMINI_API_KEY=your_api_key_here" > .env
# Start the service
docker-compose up -d
# View logs
docker-compose logs -f
# Stop the service
docker-compose downDocker开发
# Start development environment
docker-compose --profile dev up
# This mounts source code for live reloading特定于环境的部署
生产部署
# Production build
docker build --target production -t mcp-server-gemini:prod .
# Run with production settings
docker run -d \
--name mcp-server-gemini-prod \
--restart unless-stopped \
-e GEMINI_API_KEY=your_api_key_here \
-e NODE_ENV=production \
-e LOG_LEVEL=warn \
-e RATE_LIMIT_ENABLED=true \
-e ENABLE_METRICS=true \
mcp-server-gemini:prod健康检查
# Check container health
docker ps
docker logs mcp-server-gemini
# Manual health check
docker exec mcp-server-gemini node -e "console.log('Health check passed')"🚀 部署选项
1.npm全局安装
# Install globally
npm install -g mcp-server-gemini
# Run directly
GEMINI_API_KEY=your_key mcp-server-gemini2.本地安装
# Clone and build
git clone https://github.com/gurr-i/mcp-server-gemini-pro.git
cd mcp-server-gemini-pro
npm install
npm run build
# Run locally
GEMINI_API_KEY=your_key npm start3.Docker部署
# Using Docker Hub (when published)
docker run -e GEMINI_API_KEY=your_key mcp-server-gemini-pro:latest
# Using local build
docker build -t mcp-server-gemini-pro .
docker run -e GEMINI_API_KEY=your_key mcp-server-gemini-pro4.流程经理(PM2)
# Install PM2
npm install -g pm2
# Create ecosystem file
cat > ecosystem.config.js Made with ❤️ By Gurveer for the AI development community