A Model Context Protocol (MCP) client implementation that connects to MCP servers and uses Google's Gemini 2.0 Flash model for intelligent function calling.
This project provides an MCP client that can integrate with any MCP-compatible server. It uses Google's Gemini 2.0 Flash model to interpret user queries and intelligently call server-defined tools through the MCP protocol.
- Connect to any MCP server (Python or Node.js)
- Automatic conversion of MCP tools to Gemini function declarations
- Native function calling using Gemini's built-in capabilities
- Smart context handling using Gemini's chat interface
- Interactive command-line interface
- Helpful system instructions for balanced tool usage
- Google Cloud project with Gemini API enabled
Clone the repository and set up the environment using uv:
# Clone the repository
git clone https://github.com/yourusername/gemini-mcp-client.git
cd gemini-mcp-client
# Create and activate virtual environment with uv
uv venv
# On Windows:
.venv\Scripts\activate
# On Unix or MacOS:
source .venv/bin/activate
# Install dependencies using uv sync
uv sync
About
uv sync
: Theuv sync
command installs all dependencies specified in your project's pyproject.toml file. Unlike traditional pip install methods,uv sync
is significantly faster as it resolves dependencies in parallel and caches them efficiently. It ensures that all your dependencies are installed with the exact versions specified in your project, maintaining consistency across different environments.
You'll need to set up Google Cloud authentication for your project:
# Create .env file
touch .env
Add your Google Cloud project details to the .env file:
GOOGLE_CLOUD_PROJECT=your-project-id
GOOGLE_CLOUD_LOCATION=us-central1
Add .env to your .gitignore:
echo ".env" >> .gitignore
Make sure you authenticate with Google Cloud:
gcloud auth application-default login
To run your client with any MCP server:
# For Python servers
python client.py path/to/server.py
# For Node.js servers
python client.py path/to/build/index.js
The client will:
- Connect to the specified server
- List available tools
- Start an interactive chat session where you can:
- Enter queries
- See tool executions
- Get responses from Gemini
To test this client, you'll need an MCP server to connect to. Follow these steps:
- Set up a test server by following the official MCP server quickstart guide: MCP Server Quickstart
- This will walk you through creating a simple weather server that provides forecast tools
- Once you've completed the server setup, you can connect the Gemini client to it:
# Make sure your virtual environment is activated
uv run client_gemini_function_call.py path/to/your/server/weather.py
This repository provides two different approaches to implementing an MCP client with Gemini:
The first approach uses a simple prompting strategy to extract tool calls:
uv run client_gemini.py path/to/your/server.py
Key features:
- Formats tool descriptions directly in the prompt
- Uses custom regex patterns to extract tool calls from Gemini's response
- Manually handles the conversation context
- Uses "generate_content" implementation in google's genai SDK.
The second approach leverages Gemini's built-in function calling capabilities:
uv run client_gemini_function_call.py path/to/your/server.py
Key features:
- Converts MCP tools to Gemini's function declarations format
- Uses Gemini's native function calling API
- Automatically handles tool execution and context management
- Provides more reliable tool integration
- Uses the "chat" implementation in google's genai SDK.
When you submit a query:
- The client gets the list of available tools from the server
- Your query is sent to Gemini along with tool descriptions
- Gemini decides which tools (if any) to use based on your query
- The client executes any requested tool calls through the server
- Results are sent back to Gemini
- Gemini provides a natural language response
- The response is displayed to you
The client consists of several key components:
- Server Connection: Handles connecting to MCP servers and listing available tools
- Tool Conversion: Automatically converts MCP tool schemas to Gemini function declarations
- Query Processing: Manages the flow of requests and responses between you, Gemini, and tools
- Interactive Interface: Provides a simple command-line interface for interacting with the system
- Ensure you've run
gcloud auth application-default login
- Verify your project ID and location are correct
- Check that your Google Cloud project has the necessary API enabled
- Double-check the path to your server script is correct
- Use the absolute path if the relative path isn't working
- For Windows users, make sure to use forward slashes (/) or escaped backslashes (\) in the path
FileNotFoundError
: Check your server pathConnection refused
: Ensure the server is running and the path is correctTool execution failed
: Verify any required environment variables for tools are set