|
| 1 | +#🚀 MCP Client with Gemini AI |
| 2 | + |
| 3 | +[](https://youtu.be/GAPncIfnDwg) |
| 4 | + |
| 5 | +[📢 Subscribe to The AI Language!](https://youtube.com/@theailanguage?sub_confirmation=1) |
| 6 | + |
| 7 | +Before we begin, if you enjoy learning about AI, coding, and automation, please**like this video and subscribe** to the channel. It really helps us bring more tutorials your way! Now, let’s get started! |
| 8 | + |
| 9 | +--- |
| 10 | + |
| 11 | +##📌**Features** |
| 12 | +✅ Connects to an MCP server (Python or Node.js) |
| 13 | +✅ Sends queries to**Google Gemini AI** |
| 14 | +✅ Lets**Gemini call external tools** from the MCP server |
| 15 | +✅ Executes MCP tool commands and**returns the results** |
| 16 | +✅**Maintains conversation history**, so Gemini**remembers past queries** |
| 17 | + |
| 18 | +--- |
| 19 | + |
| 20 | +##📦**Installation** |
| 21 | +**1️⃣ Install the required dependencies using`uv` (Universal Virtualenv):** |
| 22 | +```bash |
| 23 | +uv add mcp python-dotenv google-genai |
| 24 | +``` |
| 25 | + |
| 26 | +**2️⃣ Clone this repository:** |
| 27 | +```bash |
| 28 | +git clone https://github.com/your-username/mcp-client-gemini.git |
| 29 | +cd mcp-client-gemini |
| 30 | +``` |
| 31 | + |
| 32 | +**3️⃣ Set up the project and virtual environment:** |
| 33 | +```bash |
| 34 | +uv init mcp-client |
| 35 | +cd mcp-client |
| 36 | +uv venv |
| 37 | +``` |
| 38 | + |
| 39 | +**4️⃣ Activate the virtual environment:** |
| 40 | +```bash |
| 41 | +# On Windows: |
| 42 | +.venv\Scripts\activate |
| 43 | + |
| 44 | +# On MacOS/Linux: |
| 45 | +source .venv/bin/activate |
| 46 | +``` |
| 47 | + |
| 48 | +--- |
| 49 | + |
| 50 | +##🔑**Setting Up the API Key** |
| 51 | +To use**Google Gemini AI**, you need an**API key**. |
| 52 | + |
| 53 | +**1️⃣ Create a`.env` file:** |
| 54 | +```bash |
| 55 | +touch .env |
| 56 | +``` |
| 57 | + |
| 58 | +**2️⃣ Add your API key inside`.env`:** |
| 59 | +``` |
| 60 | +GEMINI_API_KEY=your_api_key_here |
| 61 | +``` |
| 62 | + |
| 63 | +**3️⃣ Make sure`.env` is ignored in Git:** |
| 64 | +```bash |
| 65 | +echo".env">> .gitignore |
| 66 | +``` |
| 67 | + |
| 68 | +--- |
| 69 | + |
| 70 | +##🚀**Running the MCP Client** |
| 71 | +**Start the MCP client and connect it to an MCP server:** |
| 72 | +```bash |
| 73 | +uv run client.py path/to/server.py# Use a Python server |
| 74 | +uv run client.py path/to/server.js# Use a Node.js server |
| 75 | +``` |
| 76 | + |
| 77 | +Example (if using a**weather server**): |
| 78 | +```bash |
| 79 | +uv run client.py ./server/weather.py |
| 80 | +``` |
| 81 | + |
| 82 | +--- |
| 83 | + |
| 84 | +##🔧**How It Works** |
| 85 | +1️⃣ The user enters a query (e.g.,`"Create a file named test.txt"`). |
| 86 | +2️⃣ The MCP client sends the query to**Gemini AI**. |
| 87 | +3️⃣**Gemini AI checks available MCP tools** and calls the correct one. |
| 88 | +4️⃣ The MCP client**executes the command** and returns the result. |
| 89 | +5️⃣ Gemini**remembers past interactions** and adjusts responses accordingly. |
| 90 | + |
| 91 | +--- |
| 92 | + |
| 93 | +##📁**Project Structure** |
| 94 | +``` |
| 95 | +mcp-client-gemini/ |
| 96 | +│── client.py # MCP Client (Main script) |
| 97 | +│── .env # Stores API Key (ignored in Git) |
| 98 | +│── README.md # Documentation |
| 99 | +│── requirements.txt # Dependencies (optional) |
| 100 | +│── server/ # Folder for MCP server scripts (e.g., weather.py) |
| 101 | +│── .gitignore # Ignores sensitive files |
| 102 | +│── LICENSE # License file |
| 103 | +``` |
| 104 | + |
| 105 | +--- |
| 106 | + |
| 107 | +##🎯**Contributing** |
| 108 | +Feel free to submit issues or contribute improvements via pull requests. |
| 109 | + |
| 110 | +--- |
| 111 | + |
| 112 | +##📄**License** |
| 113 | +This project is licensed under the**MIT License**. |
| 114 | + |
| 115 | +--- |