Gmail MCP Server
by adel-noufal
README.md
# Gmail MCP Server ๐ฌ
A local **Model Context Protocol (MCP)** server that connects AI assistants (like Claude) directly to your Gmail account. It lets an AI read your inbox, save drafts, and send emails โ but **only when you explicitly approve it**.
---
## ๐ค What Does This Server Do?
This server acts as a **secure bridge** between an AI assistant and your Gmail. Instead of giving an AI full access to your email, it exposes exactly 3 controlled tools:
| Tool | What It Does | Can It Send Email? |
|------|--------------|--------------------|
| `list_recent_emails` | Reads the **last 10 emails** in your inbox (subject, sender, date only โ no body) | โ No |
| `draft_reply` | Creates a **draft** in your Gmail Drafts folder | โ No โ structurally impossible |
| `send_confirmed_email` | **Sends** an email via Gmail | โ
Yes โ but only if you provide a secret token |
### ๐ Why Is It Safe?
- `list_recent_emails` uses **read-only** Gmail scope. It cannot modify anything.
- `draft_reply` calls `drafts.create` โ the Gmail API endpoint for creating drafts. The code **does not contain** `messages.send` anywhere. An AI cannot accidentally send an email through this tool.
- `send_confirmed_email` requires a **secret token** that only you know. Without it, the request is blocked in Python code before Gmail is ever contacted.
---
## โ
Prerequisites
Before starting, make sure you have:
- **Python 3.10 or newer** โ check with `python --version`
- **Node.js** โ needed to run the MCP Inspector. Download from https://nodejs.org
- A **Google account** (the Gmail you want to connect)
- A **Google Cloud project** with the Gmail API enabled (see Step 1 below)
---
## ๐ Setup Guide
### Step 1 โ Enable Gmail API on Google Cloud (One-Time Setup)
This gives your server permission to talk to Gmail.
1. Go to https://console.cloud.google.com
2. Click **"Select a project"** โ **"New Project"** โ give it any name โ **Create**
3. In the left menu go to **APIs & Services โ Library**
4. Search for **"Gmail API"** โ click it โ click **Enable**
5. Go to **APIs & Services โ OAuth consent screen**
- Choose **External** โ click **Create**
- Fill in an App name (e.g. `Gmail MCP`) and your email โ click **Save and Continue** through all steps
- On the **Test Users** page โ click **"+ Add Users"** โ add your Gmail address โ **Save**
6. Go to **APIs & Services โ Credentials**
- Click **"+ Create Credentials"** โ choose **"OAuth 2.0 Client ID"**
- Application type: **Desktop app** โ click **Create**
- Click **"Download JSON"** on the newly created credential
7. Rename the downloaded file to `credentials.json` and place it in this project folder:
```
gmail-mcp-server/
โโโ credentials.json โ put it here
```
> โ ๏ธ `credentials.json` is in `.gitignore` and will never be committed to GitHub. Keep it private.
---
### Step 2 โ Clone the Repository
```bash
git clone https://github.com/adel-noufal/Gmail-Mcp-Server-Automation.git
cd Gmail-Mcp-Server-Automation
```
---
### Step 3 โ Create a Python Virtual Environment
```powershell
# Create the virtual environment
python -m venv .venv
# Activate it (Windows PowerShell)
.\.venv\Scripts\Activate.ps1
# If you get a permissions error, run this first:
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser
```
> On Mac/Linux: `source .venv/bin/activate`
---
### Step 4 โ Install Dependencies
```powershell
pip install -r requirements.txt
```
This installs the MCP library, Google API client, and authentication libraries.
---
### Step 5 โ Set Your Secret Approval Token
This token is your personal key that allows the `send_confirmed_email` tool to actually send. **Choose any secret string you like.**
```powershell
# Windows PowerShell
$env:MCP_APPROVAL_TOKEN="your_secret_token_here"
```
> On Mac/Linux: `export MCP_APPROVAL_TOKEN="your_secret_token_here"`
> โ ๏ธ You must set this every time you open a new terminal. To make it permanent, add it to your system environment variables.
---
### Step 6 โ Run the Server with MCP Inspector
The **MCP Inspector** is a browser-based tool that lets you test your server visually without needing to connect a full AI assistant.
```powershell
npx -y @modelcontextprotocol/inspector .venv\Scripts\python server.py
```
> On Mac/Linux: `npx -y @modelcontextprotocol/inspector .venv/bin/python server.py`
Then open your browser and go to:
```
http://localhost:5173
```
---
### Step 7 โ Authenticate with Google (First Run Only)
When you run the server for the first time, a **browser window will open automatically** asking you to log in to Google.
1. Select your Google account
2. You may see a warning "Google hasn't verified this app" โ click **"Advanced"** โ **"Go to [App Name] (unsafe)"**
3. Click **Allow** on the permissions screen
4. The browser will show "Authentication successful" โ you can close it
A `token.json` file is now saved locally. **You will not need to log in again** unless you delete this file.
---
## ๐งช Testing the Tools
Once the Inspector is open at `http://localhost:5173`, you'll see all 3 tools listed. Here's how to test each one:
### Tool 1 โ `list_recent_emails`
- Click the tool โ click **Run** (no parameters needed)
- You'll see a list of your last 10 inbox emails with subject, sender, and date
### Tool 2 โ `draft_reply`
Fill in the parameters:
```
to: friend@example.com
subject: Hello from MCP!
body: This is a test draft created by my Gmail MCP server.
```
- Click **Run**
- Open Gmail โ go to **Drafts** โ you'll see the email sitting there, unsent โ
### Tool 3 โ `send_confirmed_email`
**With a wrong token (to test the security gate):**
```
to: friend@example.com
subject: Test
body: Hello!
approval_token: wrongtoken
```
- Click **Run** โ you'll get `"status": "blocked"` โ
The gate works.
**With the correct token (to actually send):**
```
to: friend@example.com
subject: Hello from MCP!
body: This email was sent by my Gmail MCP server!
approval_token: your_secret_token_here
```
- Click **Run** โ email is sent โ
---
## โ๏ธ Connect to Claude or Another AI Assistant
To connect this server to an AI assistant like Claude, add this block to your MCP configuration file:
```json
{
"mcpServers": {
"gmail-mcp": {
"command": "C:\\path\\to\\gmail-mcp-server\\.venv\\Scripts\\python.exe",
"args": [
"C:\\path\\to\\gmail-mcp-server\\server.py"
],
"env": {
"MCP_APPROVAL_TOKEN": "your_secret_token_here"
}
}
}
}
```
Replace `C:\\path\\to\\gmail-mcp-server` with the actual path to your project folder.
---
## ๐ Project Structure
```
gmail-mcp-server/
โโโ server.py # Main MCP server โ defines all 3 tools
โโโ auth.py # Handles Google OAuth 2.0 login
โโโ requirements.txt # Python dependencies
โโโ credentials.json.template # Template showing the credentials.json format
โโโ .gitignore # Excludes credentials.json and token.json from git
โโโ LICENSE # MIT License
โโโ README.md # This file
```
> `credentials.json` and `token.json` are **never committed** โ they contain your private Google credentials.
---
## โ Troubleshooting
| Problem | Solution |
|---------|----------|
| `credentials.json not found` | Make sure you downloaded and renamed the file from Google Cloud Console and placed it in the project root |
| `Execution policy` error on `.Activate.ps1` | Run `Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser` |
| Browser doesn't open for Google login | Run `python auth.py` manually to trigger the login flow |
| `MCP_APPROVAL_TOKEN not set` error | You forgot Step 5 โ set the environment variable before starting the server |
| Inspector shows no tools | Make sure you're running the command from inside the project folder with the venv activated |
---
## ๐ License
This project is licensed under the MIT License โ see the [LICENSE](LICENSE) file for details.
This server cannot be deployed
Maintenance
ActivityMaintained
ResponsivenessNo issues