Gemini MCP server

Integrates with Google's Gemini API to enable text generation, analysis, and chat conversations through a Flask-based RESTful server that securely manages API keys via environment variables.
Back to servers
Setup instructions
Provider
Amit Sharma
Release date
Mar 17, 2025
Language
Python

This MCP Gemini Server implements the Model Context Protocol, allowing AI assistants like Claude to interact with Google's Gemini API. The server enables text generation, analysis, and chat conversation capabilities through a simple API interface.

Installation

Prerequisites

  • Python 3.7 or higher
  • Google AI API key

Setup

  1. Clone the repository:

    git clone https://github.com/yourusername/mcp-gemini-server.git
    cd mcp-gemini-server
    
  2. Create a virtual environment:

    python -m venv venv
    
  3. Activate the virtual environment:

    • Windows:
      venv\Scripts\activate
      
    • macOS/Linux:
      source venv/bin/activate
      
  4. Install dependencies:

    pip install -r requirements.txt
    
  5. Create a .env file in the root directory with your Gemini API key:

    GEMINI_API_KEY=your_api_key_here
    

Usage

Starting the Server

  1. Start the server:

    python server.py
    
  2. The server will run on http://localhost:5000/ by default

  3. Send MCP requests to the /mcp endpoint using POST method

Example Request

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 Reference

Endpoints

  • GET /health: Check if the server is running
  • GET /list-models: List available Gemini models
  • POST /mcp: Main endpoint for MCP requests

MCP Actions

Generate Text

Generate text content with Gemini.

Parameters:

  • prompt (required): The text prompt for generation
  • temperature (optional): Controls randomness (0.0 to 1.0)
  • max_tokens (optional): Maximum tokens to generate

Example:

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

Analyze Text

Analyze text content.

Parameters:

  • text (required): The text to analyze
  • analysis_type (optional): Type of analysis ('sentiment', 'summary', 'keywords', or 'general')

Example:

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

Chat

Have a conversation with Gemini.

Parameters:

  • messages (required): Array of message objects with 'role' and 'content'
  • temperature (optional): Controls randomness (0.0 to 1.0)

Example:

{
  "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
  }
}

Error Handling

The server returns appropriate HTTP status codes and error messages:

  • 200: Successful request
  • 400: Bad request (missing or invalid parameters)
  • 500: Server error (API issues, etc.)

Testing

You can use the included test script to verify functionality:

# 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

How to install this MCP server

For Claude Code

To add this MCP server to Claude Code, run this command in your terminal:

claude mcp add-json "gemini" '{"command":"python","args":["server.py"]}'

See the official Claude Code MCP documentation for more details.

For Cursor

There are two ways to add an MCP server to Cursor. The most common way is to add the server globally in the ~/.cursor/mcp.json file so that it is available in all of your projects.

If you only need the server in a single project, you can add it to the project instead by creating or adding it to the .cursor/mcp.json file.

Adding an MCP server to Cursor globally

To add a global MCP server go to Cursor Settings > Tools & Integrations and click "New MCP Server".

When you click that button the ~/.cursor/mcp.json file will be opened and you can add your server like this:

{
    "mcpServers": {
        "gemini": {
            "command": "python",
            "args": [
                "server.py"
            ]
        }
    }
}

Adding an MCP server to a project

To add an MCP server to a project you can create a new .cursor/mcp.json file or add it to the existing one. This will look exactly the same as the global MCP server example above.

How to use the MCP server

Once the server is installed, you might need to head back to Settings > MCP and click the refresh button.

The Cursor agent will then be able to see the available tools the added MCP server has available and will call them when it needs to.

You can also explicitly ask the agent to use the tool by mentioning the tool name and describing what the function does.

For Claude Desktop

To add this MCP server to Claude Desktop:

1. Find your configuration file:

  • macOS: ~/Library/Application Support/Claude/claude_desktop_config.json
  • Windows: %APPDATA%\Claude\claude_desktop_config.json
  • Linux: ~/.config/Claude/claude_desktop_config.json

2. Add this to your configuration file:

{
    "mcpServers": {
        "gemini": {
            "command": "python",
            "args": [
                "server.py"
            ]
        }
    }
}

3. Restart Claude Desktop for the changes to take effect

Want to 10x your AI skills?

Get a free account and learn to code + market your apps using AI (with or without vibes!).

Nah, maybe later