added new gemini creations
This commit is contained in:
@@ -0,0 +1 @@
|
||||
GOOGLE_API_KEY=
|
||||
@@ -0,0 +1,47 @@
|
||||
# Gemini MCP Integration
|
||||
|
||||
This directory contains a boilerplate setup for integrating Google Gemini with the Model Context Protocol (MCP).
|
||||
|
||||
## Structure
|
||||
|
||||
- **`simple_server.py`**: A simple MCP server using `FastMCP` that exposes two tools (`add_numbers`, `get_current_time`).
|
||||
- **`gemini_client.py`**: A Python script that acts as an MCP client. It connects to `simple_server.py`, lists the available tools, and provides guidance on integrating them with the `google-genai` SDK.
|
||||
- **`setup.sh`**: A provision script that sets up the Python virtual environment and installs dependencies.
|
||||
- **`requirements.txt`**: List of Python dependencies.
|
||||
|
||||
## Prerequisites
|
||||
|
||||
- Python 3
|
||||
- Node.js & npm (for MCP Inspector)
|
||||
|
||||
## Setup
|
||||
|
||||
1. Run the setup script:
|
||||
```bash
|
||||
./setup.sh
|
||||
```
|
||||
2. Activate the virtual environment:
|
||||
```bash
|
||||
source venv/bin/activate
|
||||
```
|
||||
3. Set your Google API Key in `.env`:
|
||||
```bash
|
||||
# Edit .env file
|
||||
GOOGLE_API_KEY=your_api_key_here
|
||||
```
|
||||
|
||||
## Running the Example
|
||||
|
||||
Run the client script to see the connection to the server and the list of tools:
|
||||
|
||||
```bash
|
||||
python gemini_client.py
|
||||
```
|
||||
|
||||
## Using MCP Inspector
|
||||
|
||||
You can use the official MCP Inspector to interact with the `simple_server.py` visually.
|
||||
|
||||
```bash
|
||||
npx @modelcontextprotocol/inspector python simple_server.py
|
||||
```
|
||||
@@ -0,0 +1,62 @@
|
||||
import asyncio
|
||||
import os
|
||||
import sys
|
||||
from mcp import ClientSession, StdioServerParameters
|
||||
from mcp.client.stdio import stdio_client
|
||||
from google import genai
|
||||
from dotenv import load_dotenv
|
||||
|
||||
# Load environment variables
|
||||
load_dotenv()
|
||||
|
||||
async def main():
|
||||
print("Starting MCP Client...")
|
||||
|
||||
# Check for API Key
|
||||
api_key = os.getenv("GOOGLE_API_KEY")
|
||||
if not api_key:
|
||||
print("Warning: GOOGLE_API_KEY is not set in .env. Gemini calls will fail.")
|
||||
else:
|
||||
print("GOOGLE_API_KEY found.")
|
||||
|
||||
# Define the server parameters
|
||||
# We run the simple_server.py we created earlier
|
||||
server_script = os.path.join(os.path.dirname(__file__), "simple_server.py")
|
||||
|
||||
server_params = StdioServerParameters(
|
||||
command=sys.executable, # Use the current python interpreter
|
||||
args=[server_script],
|
||||
env=None
|
||||
)
|
||||
|
||||
print(f"Connecting to MCP server at: {server_script}")
|
||||
|
||||
try:
|
||||
async with stdio_client(server_params) as (read, write):
|
||||
async with ClientSession(read, write) as session:
|
||||
# Initialize the session
|
||||
await session.initialize()
|
||||
|
||||
# List available tools
|
||||
result = await session.list_tools()
|
||||
tools = result.tools
|
||||
|
||||
print(f"\nSuccessfully connected! Found {len(tools)} tools:")
|
||||
for tool in tools:
|
||||
print(f" - {tool.name}: {tool.description}")
|
||||
print(f" Schema: {tool.inputSchema}")
|
||||
|
||||
print("\n--- Integration with Gemini (google-genai) ---")
|
||||
print("To integrate these tools with Gemini using the new SDK, you would:")
|
||||
print("1. Convert the MCP tool schemas to Gemini tool definitions.")
|
||||
print("2. Initialize the Gemini client:")
|
||||
print(" from google import genai")
|
||||
print(" client = genai.Client(api_key=api_key)")
|
||||
print("3. Pass the tools to the generate_content call.")
|
||||
print("4. Handle the tool calls returned by the model.")
|
||||
|
||||
except Exception as e:
|
||||
print(f"Error connecting to MCP server: {e}")
|
||||
|
||||
if __name__ == "__main__":
|
||||
asyncio.run(main())
|
||||
@@ -0,0 +1,3 @@
|
||||
mcp
|
||||
google-genai
|
||||
python-dotenv
|
||||
Executable
+48
@@ -0,0 +1,48 @@
|
||||
#!/bin/bash
|
||||
|
||||
# Exit on error
|
||||
set -e
|
||||
|
||||
echo "Provisioning MCP environment..."
|
||||
|
||||
# Check for Python 3
|
||||
if ! command -v python3 &> /dev/null; then
|
||||
echo "Python 3 could not be found. Please install Python 3."
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Create Virtual Environment
|
||||
if [ ! -d "venv" ]; then
|
||||
echo "Creating virtual environment..."
|
||||
python3 -m venv venv
|
||||
else
|
||||
echo "Virtual environment already exists."
|
||||
fi
|
||||
|
||||
# Activate venv
|
||||
source venv/bin/activate
|
||||
|
||||
# Upgrade pip
|
||||
echo "Upgrading pip..."
|
||||
pip install --upgrade pip
|
||||
|
||||
# Install dependencies
|
||||
if [ -f "requirements.txt" ]; then
|
||||
echo "Installing dependencies from requirements.txt..."
|
||||
pip install -r requirements.txt
|
||||
else
|
||||
echo "requirements.txt not found!"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Create .env from template if it doesn't exist
|
||||
if [ ! -f ".env" ]; then
|
||||
echo "Creating .env file..."
|
||||
touch .env
|
||||
echo "GOOGLE_API_KEY=" >> .env
|
||||
echo "Please add your GOOGLE_API_KEY to mcp/.env"
|
||||
fi
|
||||
|
||||
echo "Setup complete! To start working:"
|
||||
echo "cd mcp"
|
||||
echo "source venv/bin/activate"
|
||||
@@ -0,0 +1,20 @@
|
||||
from mcp.server.fastmcp import FastMCP
|
||||
import asyncio
|
||||
|
||||
# Initialize FastMCP server
|
||||
mcp = FastMCP("Simple Demo Server")
|
||||
|
||||
@mcp.tool()
|
||||
def add_numbers(a: int, b: int) -> int:
|
||||
"""Add two numbers together."""
|
||||
return a + b
|
||||
|
||||
@mcp.tool()
|
||||
def get_current_time() -> str:
|
||||
"""Get the current local time."""
|
||||
from datetime import datetime
|
||||
return datetime.now().isoformat()
|
||||
|
||||
if __name__ == "__main__":
|
||||
# Run the server over stdio
|
||||
mcp.run()
|
||||
Reference in New Issue
Block a user