Skip to main content
Glama
binaykushwaha1244

MCP Web Search Assistant

README.md
# MCP Web Search Assistant with Gemini

A simple MCP-based web search assistant that uses **Google Gemini** as the Large Language Model (LLM) and a local **MCP server** to perform web searches through DuckDuckGo.

This project demonstrates how an LLM can interact with external tools using a client-server architecture.

---

## Features

- Uses Google Gemini for natural-language understanding and responses.
- Uses an MCP-style client/server architecture for tool integration.
- Performs web searches using DuckDuckGo.
- Uses a local Flask server to handle tool requests.
- Runs the MCP server and client separately.
- Uses an environment variable to securely provide the Gemini API key.

---

## Architecture

```text
                         User
                           |
                           v
                  +----------------+
                  |  ask_claude.py |
                  |  Client App    |
                  +----------------+
                           |
                           v
                  +-------------------+
                  | ClaudeClient       |
                  | Gemini Integration |
                  +-------------------+
                           |
                           v
                     Gemini API
                           |
                           | Tool Call
                           v
                  +----------------+
                  |   MCP Server   |
                  | mcp_server.py  |
                  +----------------+
                           |
                           v
                  +----------------+
                  |   DuckDuckGo   |
                  |   Web Search   |
                  +----------------+
                           |
                           v
                    Search Results
                           |
                           v
                        Gemini
                           |
                           v
                    Final Response
```

### Request Flow

1. The user enters a question through the client application.
2. The client sends the question to Google Gemini.
3. Gemini determines whether external web information is required.
4. If a web search is required, Gemini requests the `fetch_web_content` tool.
5. The client sends the tool request to the local MCP server.
6. The MCP server performs the search using DuckDuckGo.
7. Search results are returned to the client.
8. Gemini uses the retrieved information to generate the final response.

---

## Project Structure

```text
MCP/
│
├── mcp_integration.py
├── mcp_server.py
├── claude_mcp_client.py
├── ask_claude.py
├── requirements.txt
├── .gitignore
└── README.md
```

### File Description

| File | Description |
|------|-------------|
| `mcp_integration.py` | Handles Gemini integration, MCP tool definitions, and web-search functionality. |
| `mcp_server.py` | Runs the local Flask-based MCP server and exposes the tool endpoint. |
| `claude_mcp_client.py` | Client responsible for communicating with Gemini and the MCP server. |
| `ask_claude.py` | Command-line interface used to interact with the assistant. |
| `requirements.txt` | Contains the required Python dependencies. |
| `.gitignore` | Prevents sensitive and unnecessary files from being committed. |
| `README.md` | Project documentation and setup instructions. |

> **Note:** Some filenames and class names retain the original Claude-based naming from the tutorial, although the project now uses Google Gemini.

---

## Requirements

Before running the project, make sure you have:

- Python 3.10 or higher
- pip
- A Google Gemini API key
- An active internet connection

---

## Installation

### 1. Clone the Repository

```bash
git clone <YOUR_GITHUB_REPOSITORY_URL>
cd MCP
```

### 2. Install Dependencies

```bash
pip install -r requirements.txt
```

If the Google GenAI SDK is not installed:

```bash
pip install google-genai
```

---

## Gemini API Key Setup

This project requires a Google Gemini API key.

Set the API key as an environment variable in PowerShell:

```powershell
$env:GEMINI_API_KEY="YOUR_GEMINI_API_KEY"
```

Verify that the key is available without displaying the actual key:

```powershell
python -c "import os; print('Gemini key found:', bool(os.environ.get('GEMINI_API_KEY')))"
```

Expected output:

```text
Gemini key found: True
```

> **Security:** Never add your actual Gemini API key to the source code, README, or GitHub repository.

---

# Running the Project

The application uses **two PowerShell terminals**.

## Terminal 1 — Start the MCP Server

Open the first PowerShell terminal and navigate to the project directory:

```powershell
cd D:\Projects\MCP
```

Set the Gemini API key:

```powershell
$env:GEMINI_API_KEY="YOUR_GEMINI_API_KEY"
```

Verify the key:

```powershell
python -c "import os; print('Gemini key found:', bool(os.environ.get('GEMINI_API_KEY')))"
```

Start the MCP server:

```powershell
python mcp_server.py
```

The server will run on:

```text
http://localhost:5001
```

Keep **Terminal 1 running**.

---

## Terminal 2 — Start the Client

Open a second PowerShell terminal.

Navigate to the project:

```powershell
cd D:\Projects\MCP
```

Set the Gemini API key:

```powershell
$env:GEMINI_API_KEY="YOUR_GEMINI_API_KEY"
```

Verify the key:

```powershell
python -c "import os; print('Gemini key found:', bool(os.environ.get('GEMINI_API_KEY')))"
```

Start the client:

```powershell
python ask_claude.py
```

You can now enter questions for the assistant.

### Example

```text
What is the latest information about Python?
```

---

## Example Workflow

```text
User Question
      |
      v
ask_claude.py
      |
      v
ClaudeClient
      |
      v
Gemini API
      |
      | Tool required
      v
MCP Server
      |
      v
DuckDuckGo
      |
      v
Search Results
      |
      v
Gemini
      |
      v
Final Answer
```

---

## MCP Server Endpoints

The MCP server runs locally on port `5001`.

### Health Check

```text
GET /health
```

Used to verify that the MCP server is running.

### Tool Call

```text
POST /tool_call
```

Used by the client to request the web-search tool.

Example request:

```json
{
  "name": "fetch_web_content",
  "parameters": {
    "query": "Python programming language"
  }
}
```

---

## Technologies Used

- **Python** — Core programming language
- **Google Gemini** — Large Language Model
- **Google GenAI SDK** — Gemini API integration
- **Flask** — Local MCP server
- **DuckDuckGo** — Web search
- **Requests** — HTTP communication
- **MCP-style Client/Server Architecture** — Tool integration

---

## Security

The Gemini API key is provided through an environment variable rather than being stored directly in the source code.

Recommended `.gitignore`:

```gitignore
__pycache__/
*.pyc
.env
.venv/
venv/
```

Never commit API keys, passwords, tokens, or other credentials to GitHub.

---

## Troubleshooting

### Gemini API Key Not Found

Run:

```powershell
python -c "import os; print(bool(os.environ.get('GEMINI_API_KEY')))"
```

Expected:

```text
True
```

If it returns `False`, set the key again:

```powershell
$env:GEMINI_API_KEY="YOUR_GEMINI_API_KEY"
```

> PowerShell environment variables set using `$env:` apply only to the current terminal session. If you open a second terminal, set the variable there as well.

### MCP Server Not Responding

Make sure the MCP server is running in Terminal 1:

```powershell
python mcp_server.py
```

The server should be available at:

```text
http://localhost:5001
```

### Port Already in Use

If port `5001` is already being used, stop the existing server process or configure the application to use another available port.

---

## Future Improvements

- Rename the remaining Claude-based filenames and classes to Gemini-based names.
- Add additional MCP tools.
- Improve conversation history management.
- Add more robust error handling and retry mechanisms.
- Support additional search providers.
- Add a web-based user interface.

---

## Output
![alt text](image.png)
![alt text](image-1.png)


## License

This project is licensed under the **MIT License**.

You are free to use, modify, distribute, and reuse this project, subject to the terms of the license.

### MIT License

Copyright (c) 2026

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

Maintenance

ActivityMaintained
ResponsivenessNo issues