Gemini
HTTP-SSEMCP server enabling AI assistants to interact with Google's Gemini API
MCP server enabling AI assistants to interact with Google's Gemini API
A server implementation of the Model Context Protocol (MCP) to enable AI assistants like Claude to interact with Google's Gemini API.
This project implements a server that follows the Model Context Protocol, allowing AI assistants to communicate with Google's Gemini models. With this MCP server, AI assistants can request text generation, text analysis, and maintain chat conversations through the Gemini API.
.env file for storing sensitive information securely.git clone https://github.com/yourusername/mcp-gemini-server.git cd mcp-gemini-server
python -m venv venv
Activate the virtual environment:
venv\Scripts\activatesource venv/bin/activateInstall dependencies:
pip install -r requirements.txt
.env file in the root directory with your Gemini API key:GEMINI_API_KEY=your_api_key_here
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.)Use the included test script to test various functionalities:
# 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
The Model Context Protocol implemented here follows these specifications:
Request Format:
action: String specifying the operationparameters: Object containing action-specific parametersResponse Format:
result: Object containing the operation resulterror: String explaining any error (when applicable)MIT License