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.
Clone the repository:
git clone https://github.com/yourusername/mcp-gemini-server.git
cd mcp-gemini-server
Create a virtual environment:
python -m venv venv
Activate the virtual environment:
venv\Scripts\activate
source venv/bin/activate
Install dependencies:
pip install -r requirements.txt
Create a .env
file in the root directory with your Gemini API key:
GEMINI_API_KEY=your_api_key_here
Start the server:
python server.py
The server will run on http://localhost:5000/
by default
Send MCP requests to the /mcp
endpoint using POST method
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())
GET /health
: Check if the server is runningGET /list-models
: List available Gemini modelsPOST /mcp
: Main endpoint for MCP requestsGenerate text content with Gemini.
Parameters:
prompt
(required): The text prompt for generationtemperature
(optional): Controls randomness (0.0 to 1.0)max_tokens
(optional): Maximum tokens to generateExample:
{
"action": "generate_text",
"parameters": {
"prompt": "Write a short story about a robot",
"temperature": 0.8,
"max_tokens": 500
}
}
Analyze text content.
Parameters:
text
(required): The text to analyzeanalysis_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"
}
}
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
}
}
The server returns appropriate HTTP status codes and error messages:
200
: Successful request400
: Bad request (missing or invalid parameters)500
: Server error (API issues, etc.)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
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.
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.
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"
]
}
}
}
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.
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.
To add this MCP server to Claude Desktop:
1. Find your configuration file:
~/Library/Application Support/Claude/claude_desktop_config.json
%APPDATA%\Claude\claude_desktop_config.json
~/.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