feat: Optimize for Claude Code developer assistant role

Major enhancements for Claude Code integration:

Temperature Optimization:
- Chat: 0.5 (balanced accuracy/creativity for development discussions)
- Code Analysis: 0.2 (high precision for code reviews and debugging)

Enhanced Developer Context:
- Rewritten system prompt focusing on Claude Code augmentation
- Emphasizes precision, best practices, and actionable solutions
- Positions Gemini as an extension for large context tasks

Claude Code-Centric Documentation:
- README completely rewritten for Claude Code users
- Clear configuration instructions with file paths
- Practical examples for common development scenarios
- Quick start guide with natural language usage

Key improvements:
- Lower temperatures for more accurate, deterministic responses
- Developer-first approach in all interactions
- Clear positioning as Claude's extended context handler
- Comprehensive setup guide for Claude Desktop integration

The server is now fully optimized to act as a specialized developer
assistant that seamlessly extends Claude Code's capabilities.

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
Fahad
2025-06-08 20:00:29 +04:00
parent 4d2ad48638
commit 50fec40f13
3 changed files with 241 additions and 135 deletions

248
README.md
View File

@@ -1,157 +1,169 @@
# Gemini MCP Server # Gemini MCP Server for Claude Code
A Model Context Protocol (MCP) server that enables integration with Google's Gemini models, optimized for Gemini 2.5 Pro Preview with 1M token context window. A specialized Model Context Protocol (MCP) server that extends Claude Code's capabilities with Google's Gemini 2.5 Pro Preview, featuring a massive 1M token context window for handling large codebases and complex analysis tasks.
## How It Works with Claude ## 🎯 Purpose
Once configured, Claude automatically discovers this server's capabilities. You can use natural language to invoke Gemini: This server acts as a developer assistant that augments Claude Code when you need:
- "Ask Gemini about..." - Analysis of files too large for Claude's context window
- "Use Gemini to analyze this file..." - Deep architectural reviews across multiple files
- "Have Gemini review this code..." - Extended thinking and complex problem solving
- Performance analysis of large codebases
- Security audits requiring full codebase context
See [MCP_DISCOVERY.md](MCP_DISCOVERY.md) for detailed information about how Claude discovers and uses MCP servers. ## 🚀 Quick Start for Claude Code
## Features ### 1. Configure in Claude Desktop
- **Chat with Gemini**: Send prompts to Gemini 2.5 Pro Preview by default Add to your Claude Desktop configuration file:
- **Analyze Code**: Process large codebases with Gemini's 1M token context window
- **File Reading**: Automatically read and analyze multiple files
- **List Models**: View all available Gemini models
- **Configurable Parameters**: Adjust temperature, max tokens, and model selection
- **System Prompts**: Support for system prompts to set context
- **Developer Context**: Automatically uses developer-focused system prompt for Claude Code integration
## Installation **macOS**: `~/Library/Application Support/Claude/claude_desktop_config.json`
**Windows**: `%APPDATA%\Claude\claude_desktop_config.json`
1. Clone this repository
2. Create a virtual environment:
```bash
python3 -m venv venv
source venv/bin/activate
```
3. Install dependencies:
```bash
pip install -r requirements.txt
```
## Configuration
Set your Gemini API key as an environment variable:
```bash
export GEMINI_API_KEY="your-api-key-here"
```
## Usage
### For Claude Desktop
Add this configuration to your Claude Desktop config file:
```json ```json
{ {
"mcpServers": { "mcpServers": {
"gemini": { "gemini": {
"command": "/path/to/venv/bin/python", "command": "/path/to/gemini-mcp-server/venv/bin/python",
"args": ["/path/to/gemini_server.py"], "args": ["/path/to/gemini-mcp-server/gemini_server.py"],
"env": { "env": {
"GEMINI_API_KEY": "your-api-key-here" "GEMINI_API_KEY": "your-gemini-api-key-here"
} }
} }
} }
} }
``` ```
### Direct Usage ### 2. Restart Claude Desktop
Run the server: After adding the configuration, restart Claude Desktop. You'll see "gemini" in the MCP servers list.
```bash
source venv/bin/activate ### 3. Start Using Natural Language
export GEMINI_API_KEY="your-api-key-here"
python gemini_server.py Just talk to Claude naturally:
- "Use Gemini to analyze this large file..."
- "Ask Gemini to review the architecture of these files..."
- "Have Gemini check this codebase for security issues..."
## 💻 Developer-Optimized Features
### Automatic Developer Context
When no custom system prompt is provided, Gemini automatically operates with deep developer expertise, focusing on:
- Clean code principles
- Performance optimization
- Security best practices
- Architectural patterns
- Testing strategies
- Modern development practices
### Optimized Temperature Settings
- **General chat**: 0.5 (balanced accuracy with some creativity)
- **Code analysis**: 0.2 (high precision for code review)
### Large Context Window
- Handles up to 1M tokens (~4M characters)
- Perfect for analyzing entire codebases
- Maintains context across multiple large files
## 🛠️ Available Tools
### `chat`
General-purpose developer conversations with Gemini.
**Example uses:**
```
"Ask Gemini about the best approach for implementing a distributed cache"
"Use Gemini to explain the tradeoffs between different authentication strategies"
``` ```
## Available Tools ### `analyze_code`
Specialized tool for analyzing large files or multiple files that exceed Claude's limits.
### chat **Example uses:**
Send a prompt to Gemini and receive a response. ```
"Use Gemini to analyze /src/core/engine.py and identify performance bottlenecks"
"Have Gemini review these files together: auth.py, users.py, permissions.py"
```
Parameters: ### `list_models`
- `prompt` (required): The prompt to send to Gemini Lists available Gemini models (defaults to 2.5 Pro Preview).
- `system_prompt` (optional): System prompt for context
- `max_tokens` (optional): Maximum tokens in response (default: 8192)
- `temperature` (optional): Temperature for randomness 0-1 (default: 0.7)
- `model` (optional): Model to use (default: gemini-2.5-pro-preview-06-05)
### analyze_code ## 📋 Installation
Analyze code files or snippets with Gemini's massive context window. Perfect for when Claude hits token limits.
Parameters: 1. Clone the repository:
- `files` (optional): List of file paths to analyze ```bash
- `code` (optional): Direct code content to analyze git clone https://github.com/BeehiveInnovations/gemini-mcp-server.git
- `question` (required): Question or analysis request about the code cd gemini-mcp-server
- `system_prompt` (optional): System prompt for context
- `max_tokens` (optional): Maximum tokens in response (default: 8192)
- `temperature` (optional): Temperature for randomness 0-1 (default: 0.3 for code)
- `model` (optional): Model to use (default: gemini-2.5-pro-preview-06-05)
Note: You must provide either `files` or `code` (or both).
### list_models
List all available Gemini models that support content generation.
## Usage Examples
### From Claude Code
When working with large files in Claude Code, you can use the Gemini server like this:
1. **Analyze a large file**:
```
Use the gemini tool to analyze this file: /path/to/large/file.py
Question: What are the main design patterns used in this code?
``` ```
2. **Analyze multiple files**: 2. Create virtual environment:
``` ```bash
Use gemini to analyze these files together: python3 -m venv venv
- /path/to/file1.py source venv/bin/activate # On Windows: venv\Scripts\activate
- /path/to/file2.py
- /path/to/file3.py
Question: How do these components interact with each other?
``` ```
3. **Extended thinking with Gemini**: 3. Install dependencies:
When Claude hits token limits, you can pass the entire context to Gemini for analysis. ```bash
pip install -r requirements.txt
```
## Models 4. Set your Gemini API key:
```bash
export GEMINI_API_KEY="your-api-key-here"
```
The server defaults to `gemini-2.5-pro-preview-06-05` (the latest and most capable model) which supports: ## 🔧 Advanced Configuration
- 1 million token context window
- Advanced reasoning capabilities
- Code understanding and analysis
Other available models: ### Custom System Prompts
- `gemini-1.5-pro-latest` - Stable Gemini 1.5 Pro Override the default developer prompt when needed:
- `gemini-1.5-flash` - Fast Gemini 1.5 Flash model ```python
- `gemini-2.0-flash` - Gemini 2.0 Flash {
- And many more (use `list_models` to see all available) "prompt": "Review this code",
"system_prompt": "You are a security expert. Focus only on vulnerabilities."
}
```
## Requirements ### Temperature Control
Adjust for your use case:
- `0.1-0.3`: Maximum precision (debugging, security analysis)
- `0.4-0.6`: Balanced (general development tasks)
- `0.7-0.9`: Creative solutions (architecture design, brainstorming)
- Python 3.8+ ### Model Selection
- Valid Google Gemini API key While defaulting to `gemini-2.5-pro-preview-06-05`, you can specify other models:
- `gemini-1.5-pro-latest`: Stable alternative
- `gemini-1.5-flash`: Faster responses
- Use `list_models` to see all available options
## Notes ## 🎯 Claude Code Integration Examples
- The Gemini 2.5 Pro preview models may have safety restrictions that block certain prompts ### When Claude hits token limits:
- If a model returns a blocked response, the server will indicate the finish reason ```
- The server estimates tokens as ~4 characters per token Claude: "This file is too large for me to analyze fully..."
- Maximum context window is 1 million tokens (~4 million characters) You: "Use Gemini to analyze the entire file and identify the main components"
- When no system prompt is provided, the server automatically uses a developer-focused prompt similar to Claude Code ```
## Tips for Claude Code Users ### For architecture reviews:
```
You: "Use Gemini to analyze all files in /src/core/ and create an architecture diagram"
```
1. When Claude says a file is too large, use the `analyze_code` tool with the file path ### For performance optimization:
2. For architectural questions spanning multiple files, pass all relevant files to `analyze_code` ```
3. Use lower temperatures (0.1-0.3) for code analysis and higher (0.7-0.9) for creative tasks You: "Have Gemini profile this codebase and suggest the top 5 performance improvements"
4. The default model (2.5 Pro Preview) is optimized for large context understanding ```
## 📝 Notes
- Gemini 2.5 Pro Preview may occasionally block certain prompts due to safety filters
- The server automatically falls back gracefully when this happens
- Token estimation: ~4 characters per token
- All file paths should be absolute paths
## 🤝 Contributing
This server is designed specifically for Claude Code users. Contributions that enhance the developer experience are welcome!
## 📄 License
MIT License - feel free to customize for your development workflow.

View File

@@ -22,18 +22,28 @@ DEFAULT_MODEL = "gemini-2.5-pro-preview-06-05"
MAX_CONTEXT_TOKENS = 1000000 # 1M tokens MAX_CONTEXT_TOKENS = 1000000 # 1M tokens
# Developer-focused system prompt for Claude Code usage # Developer-focused system prompt for Claude Code usage
DEVELOPER_SYSTEM_PROMPT = """You are an expert software developer and code analyst, similar to Claude Code. DEVELOPER_SYSTEM_PROMPT = """You are an expert software developer assistant working alongside Claude Code. Your role is to extend Claude's capabilities when handling large codebases or complex analysis tasks.
You excel at:
- Writing clean, efficient, and well-documented code
- Debugging and solving complex programming problems
- Explaining technical concepts clearly
- Following best practices and design patterns
- Providing thoughtful code reviews and suggestions
- Understanding system architecture and design
- Helping with testing strategies and implementation
- Optimizing performance and identifying bottlenecks
You should be direct, helpful, and focused on practical solutions. When analyzing code, provide actionable insights and concrete improvements. Always consider the broader context and long-term maintainability.""" Core competencies:
- Deep understanding of software architecture and design patterns
- Expert-level debugging and root cause analysis
- Performance optimization and scalability considerations
- Security best practices and vulnerability identification
- Clean code principles and refactoring strategies
- Comprehensive testing approaches (unit, integration, e2e)
- Modern development practices (CI/CD, DevOps, cloud-native)
- Cross-platform and cross-language expertise
Your approach:
- Be precise and technical, avoiding unnecessary explanations
- Provide actionable, concrete solutions with code examples
- Consider edge cases and potential issues proactively
- Focus on maintainability, readability, and long-term sustainability
- Suggest modern, idiomatic solutions for the given language/framework
- When reviewing code, prioritize critical issues first
- Always validate your suggestions against best practices
Remember: You're augmenting Claude Code's capabilities, especially for tasks requiring extensive context or deep analysis that might exceed Claude's token limits."""
class GeminiChatRequest(BaseModel): class GeminiChatRequest(BaseModel):
@@ -41,7 +51,7 @@ class GeminiChatRequest(BaseModel):
prompt: str = Field(..., description="The prompt to send to Gemini") prompt: str = Field(..., description="The prompt to send to Gemini")
system_prompt: Optional[str] = Field(None, description="Optional system prompt for context") system_prompt: Optional[str] = Field(None, description="Optional system prompt for context")
max_tokens: Optional[int] = Field(8192, description="Maximum number of tokens in response") max_tokens: Optional[int] = Field(8192, description="Maximum number of tokens in response")
temperature: Optional[float] = Field(0.7, description="Temperature for response randomness (0-1)") temperature: Optional[float] = Field(0.5, description="Temperature for response randomness (0-1, default 0.5 for balanced accuracy/creativity)")
model: Optional[str] = Field(DEFAULT_MODEL, description=f"Model to use (defaults to {DEFAULT_MODEL})") model: Optional[str] = Field(DEFAULT_MODEL, description=f"Model to use (defaults to {DEFAULT_MODEL})")
@@ -52,7 +62,7 @@ class CodeAnalysisRequest(BaseModel):
question: str = Field(..., description="Question or analysis request about the code") question: str = Field(..., description="Question or analysis request about the code")
system_prompt: Optional[str] = Field(None, description="Optional system prompt for context") system_prompt: Optional[str] = Field(None, description="Optional system prompt for context")
max_tokens: Optional[int] = Field(8192, description="Maximum number of tokens in response") max_tokens: Optional[int] = Field(8192, description="Maximum number of tokens in response")
temperature: Optional[float] = Field(0.3, description="Temperature for response randomness (0-1)") temperature: Optional[float] = Field(0.2, description="Temperature for code analysis (0-1, default 0.2 for high accuracy)")
model: Optional[str] = Field(DEFAULT_MODEL, description=f"Model to use (defaults to {DEFAULT_MODEL})") model: Optional[str] = Field(DEFAULT_MODEL, description=f"Model to use (defaults to {DEFAULT_MODEL})")
@@ -128,8 +138,8 @@ async def handle_list_tools() -> List[Tool]:
}, },
"temperature": { "temperature": {
"type": "number", "type": "number",
"description": "Temperature for response randomness (0-1)", "description": "Temperature for response randomness (0-1, default 0.5 for balanced accuracy/creativity)",
"default": 0.7, "default": 0.5,
"minimum": 0, "minimum": 0,
"maximum": 1 "maximum": 1
}, },
@@ -172,8 +182,8 @@ async def handle_list_tools() -> List[Tool]:
}, },
"temperature": { "temperature": {
"type": "number", "type": "number",
"description": "Temperature for response randomness (0-1)", "description": "Temperature for code analysis (0-1, default 0.2 for high accuracy)",
"default": 0.3, "default": 0.2,
"minimum": 0, "minimum": 0,
"maximum": 1 "maximum": 1
}, },

84
test_optimized.py Normal file
View File

@@ -0,0 +1,84 @@
#!/usr/bin/env python3
"""
Test script for optimized Claude Code settings
"""
import os
import asyncio
from gemini_server import configure_gemini, handle_call_tool
async def test_optimized_settings():
"""Test the optimized temperature and developer settings"""
print("Testing Optimized Claude Code Settings...")
print("-" * 50)
# Test configuration
try:
configure_gemini()
print("✓ Gemini API configured successfully")
except Exception as e:
print(f"✗ Failed to configure Gemini API: {e}")
return
# Test 1: Default chat temperature (should be 0.5)
print("\n1. Testing chat with default temperature (0.5)...")
result = await handle_call_tool("chat", {
"prompt": "Explain the concept of dependency injection in one paragraph. Be concise but thorough."
})
print("Response preview (should be balanced - accurate but not robotic):")
print(result[0].text[:300] + "..." if len(result[0].text) > 300 else result[0].text)
# Test 2: Code analysis with low temperature (0.2)
print("\n2. Testing code analysis with default low temperature (0.2)...")
code = '''
async def fetch_user_data(user_id: str, cache=None):
if cache and user_id in cache:
return cache[user_id]
response = await http_client.get(f"/api/users/{user_id}")
user_data = response.json()
if cache:
cache[user_id] = user_data
return user_data
'''
result = await handle_call_tool("analyze_code", {
"code": code,
"question": "Identify potential issues and suggest improvements"
})
print("Response preview (should be precise and technical):")
print(result[0].text[:400] + "..." if len(result[0].text) > 400 else result[0].text)
# Test 3: Creative task with higher temperature
print("\n3. Testing creative task with custom higher temperature...")
result = await handle_call_tool("chat", {
"prompt": "Suggest 3 innovative ways to implement a rate limiter",
"temperature": 0.8
})
print("Response preview (should be more creative):")
print(result[0].text[:400] + "..." if len(result[0].text) > 400 else result[0].text)
# Test 4: Verify developer context is applied
print("\n4. Testing developer context (no system prompt)...")
result = await handle_call_tool("chat", {
"prompt": "What's the time complexity of quicksort?",
"temperature": 0.3
})
print("Response (should be technical and developer-focused):")
print(result[0].text[:300] + "..." if len(result[0].text) > 300 else result[0].text)
print("\n" + "-" * 50)
print("Optimized settings test completed!")
if __name__ == "__main__":
# Check for API key
if not os.getenv("GEMINI_API_KEY"):
print("Error: GEMINI_API_KEY environment variable is not set")
print("Please set it with: export GEMINI_API_KEY='your-api-key'")
exit(1)
asyncio.run(test_optimized_settings())