Content
# Google Search AI Agent
This project is a sample code for an AI agent using the Google Search API. It is executed using the Python package manager, uv.
## Project Structure
### Host (AI Agent)
1. **agent_chat_with_google_search.py**
* A CLI-based chat that answers based on the mcp server's fetch and Google search.
### MCP Server
1. **server_google_search.py**
* A Google search server using the official Google API. It returns the top 5 Google search results.
## Prerequisites
1. Python 3.12 or higher
2. uv package manager
3. Google Search API key
4. OpenAI API key
## Environment Setup Steps
### 1. Install uv
uv is a Python package manager that makes it easy to manage dependencies.
```bash
# Install uv using curl
curl -LsSf https://astral.sh/uv/install.sh | sh
```
### 2. Configure Google API
1. Go to [Google Cloud Console](https://console.cloud.google.com) and create or select a project.
2. Enable the "Custom Search API" from "API & Services" → "Library".
3. Create an API key by going to "API & Services" → "Credentials" → "Create Credentials" → "API Key" (this will be your `GOOGLE_CSE_API_KEY`).
4. Visit [Programmable Search Engine](https://programmablesearchengine.google.com/about/) and create a search engine.
5. Copy the search engine ID (cx value) from the search engine settings page (this will be your `GOOGLE_CSE_ID`).
*Note: The Google API is free for up to 100 queries per day; beyond that, it costs $5 per 1,000 requests (up to a maximum of 10,000 requests per day).*
### 3. Obtain OpenAI API Key
1. Create an account on [OpenAI's website](https://platform.openai.com/).
2. Issue an API key (note that this is a paid service).
### 4. Set Environment Variables
Create a `.env` file in the root directory of the project, referencing the `.env.sample` file, and set the following content:
```env
OPENAI_API_KEY=your_OpenAI_API_key
GOOGLE_CSE_API_KEY=your_Google_API_key
GOOGLE_CSE_ID=your_search_engine_ID
```
## MCP Server Configuration
In the agent file, the MCP server configuration is defined as follows:
```python
RAW_CONFIG: Dict[str, dict] = {
"fetch": {"command": "uvx", "args": ["mcp-server-fetch"]},
"google_search": {
"command": "uv",
"args": ["--directory", "/path/to/your/project/servers/src", "run", "server_google_search.py"],
},
}
```
**Note**: When using it, you need to change the `/path/to/your/project/servers/src` part to the path that matches your environment.
## How to Run
Execute the following command in the root directory of the project:
### Run Google Search Chatbot
```bash
uv --directory "/path/to/your/project/host/src" run mcp_host_tutorial_openai.py
```
**Important**: Before running the above command, make sure to modify the MCP server configuration path in the agent file (the `--directory` argument in RAW_CONFIG) to match your environment.
## Common Issues and Solutions
### If the API Key is Not Recognized
- Ensure that the `.env` file is correctly placed in the root directory of the project.
- Check that there are no extra spaces or quotes in the API key within the file.
### If You Reach Google API Quota Limits
- Charges will apply if you exceed the daily free quota (100 requests).
- Check the quota and usage in the Google Cloud Console.
Connection Info
You Might Also Like
awesome-mcp-servers
A collection of MCP servers.
git
A Model Context Protocol server for Git automation and interaction.
Appwrite
Build like a team of hundreds
agent-bridge
A local bridge for bidirectional collaboration between Claude Code and Codex.
UnrealClientProtocol
Lightweight UE5 plugin that exposes Unreal Engine's reflection system over...
github-global
New 2026 Programming Navigation AI Programming Practice Project: A GitHub...