Skip to main content
Glama
MCP Gmail Server

MCP Gmail Server

README.md
# šŸ“§ MCP Gmail Server

A Model Context Protocol (MCP) server that enables Claude Desktop to read Gmail emails and create draft replies.

![Gmail MCP Demo](docs/images/architecture.png)

---

## šŸŽÆ What This Server Does

This MCP server connects Claude Desktop to your Gmail account, allowing you to:

- **Read unread emails** - Fetch and display unread emails from your inbox
- **Create draft replies** - Automatically draft responses to emails without sending them

---

## šŸ“‹ Table of Contents

- [Features](#-features)
- [Prerequisites](#-prerequisites)
- [Installation](#-installation)
- [Google Cloud Setup](#-google-cloud-setup)
- [Configuration](#%EF%B8%8F-configuration)
- [Usage](#-usage)
- [Example Prompts](#-example-prompts)
- [Troubleshooting](#-troubleshooting)
- [Project Structure](#-project-structure)
- [Security Notes](#-security-notes)
- [Available MCP Tools](#-available-mcp-tools)
- [Development](#-development)
- [Contributing](#-contributing)
- [License](#-license)
- [Acknowledgments](#-acknowledgments)
- [Contact](#-contact)

---

## ✨ Features

| Feature | Description |
|---------|-------------|
| šŸ“§ **Read Emails** | Fetch unread emails with sender, subject, and preview |
| šŸ“ **Draft Replies** | Create draft replies in Gmail (doesn't send automatically) |
| šŸ” **OAuth 2.0** | Secure authentication with Google |
| šŸ–„ļø **Claude Desktop** | Native integration with Claude Desktop app |

---

## šŸ“¦ Prerequisites

- **Python 3.10+** (Required - MCP doesn't support Python 3.9 or below)
- **Claude Desktop** installed on your computer
- **Google Cloud Project** with Gmail API enabled
- **Git** (optional, for cloning)

### Check Your Python Version

```bash
python --version
```

> Must show `Python 3.10.x` or higher.

---

## šŸš€ Installation

### Step 1: Clone the Repository

```bash
git clone https://github.com/TheMlengineer-py/MCP_Server.git
cd MCP_Server
```

### Step 2: Create Virtual Environment (Optional but Recommended)

```bash
python -m venv venv

# Windows
venv\Scripts\activate

# macOS/Linux
source venv/bin/activate
```

### Step 3: Install Dependencies

```bash
pip install mcp google-api-python-client google-auth-oauthlib google-auth-httplib2
```

Or use requirements.txt:

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

---

## šŸ”‘ Google Cloud Setup

### Step 1: Create a Google Cloud Project

1. Go to [Google Cloud Console](https://console.cloud.google.com/)
2. Click **"Create Project"**
3. Name your project (e.g., `mcp-gmail-local`)
4. Click **"Create"**

### Step 2: Enable Gmail API

1. Go to **APIs & Services** → **Library**
2. Search for **"Gmail API"**
3. Click **"Enable"**

### Step 3: Configure OAuth Consent Screen

1. Go to **APIs & Services** → **OAuth consent screen**
2. Select **"External"** → Click **"Create"**
3. Fill in required fields:
   - **App name:** `Local MCP Gmail`
   - **User support email:** Your email
   - **Developer contact:** Your email
4. Click **"Save and Continue"**
5. Add scopes:
   - `https://www.googleapis.com/auth/gmail.readonly`
   - `https://www.googleapis.com/auth/gmail.compose`
6. Add your email as a **Test User**
7. Click **"Save and Continue"**

### Step 4: Create OAuth Credentials

1. Go to **APIs & Services** → **Credentials**
2. Click **"+ Create Credentials"** → **"OAuth client ID"**
3. Application type: **"Desktop app"**
4. Name: `Local MCP Gmail Client`
5. Click **"Create"**
6. Click **"Download JSON"**
7. Rename the file to `credentials.json`
8. Move it to the `MCP_Server/` folder

### Step 5: Authenticate

Run this command to generate `token.json`:

```bash
cd MCP_Server
python -c "from app.gmail_auth import get_gmail_service; get_gmail_service(); print('Auth complete')"
```

> A browser window will open. Sign in and grant permissions.

---

## āš™ļø Configuration

### Claude Desktop Config

Locate your Claude Desktop configuration file:

| OS | Path |
|----|------|
| **Windows** | `%APPDATA%\Claude\claude_desktop_config.json` |
| **macOS** | `~/Library/Application Support/Claude/claude_desktop_config.json` |
| **Linux** | `~/.config/Claude/claude_desktop_config.json` |

### Configuration File

Add this configuration (adjust paths for your system):

#### Windows

```json
{
  "mcpServers": {
    "gmail": {
      "command": "C:\\Users\\YourUsername\\AppData\\Local\\Programs\\Python\\Python313\\python.exe",
      "args": [
        "C:\\Users\\YourUsername\\MCP_Gmail\\MCP_Server\\app\\server.py"
      ]
    }
  }
}
```

#### macOS/Linux

```json
{
  "mcpServers": {
    "gmail": {
      "command": "python3",
      "args": [
        "/Users/YourUsername/MCP_Gmail/MCP_Server/app/server.py"
      ]
    }
  }
}
```

### Verify Your Python Path

```bash
# Windows (Git Bash)
which python

# macOS/Linux
which python3
```

> Use the full path in your config.

---

## šŸŽ® Usage

### Start Claude Desktop

1. Save your `claude_desktop_config.json`
2. **Completely close** Claude Desktop (check system tray)
3. **Reopen** Claude Desktop
4. Look for the Gmail MCP server in the tools

### Verify Connection

In Claude Desktop, you should see the Gmail MCP server connected. If you see an error, check the [Troubleshooting](#-troubleshooting) section.

---

## šŸ’¬ Example Prompts

### Reading Emails

> **"Show me my unread emails"**

Claude will fetch and display your unread emails with:
- Sender name
- Subject line
- Email preview snippet

> **"Check my Gmail inbox"**

> **"What emails do I have?"**

> **"List my latest unread messages"**

---

### Creating Draft Replies

> **"Draft a reply to the Usertesting email saying I'm interested in the test opportunity"**

Claude will:
1. Find the email
2. Create a draft reply
3. Save it to your Gmail Drafts folder

> **"Create a draft reply to the first email thanking them for reaching out"**

> **"Reply to the Airtm email explaining I haven't had time to use the service yet"**

> **"Draft a response to Vaultleap asking for more information about their product"**

---

### Combined Workflows

> **"Show me my unread emails and draft a polite decline to any promotional emails"**

> **"Check my inbox and summarize what needs my attention"**

---

## šŸ”§ Troubleshooting

### Server Disconnected Error

**Check the logs:**

| OS | Log Location |
|----|--------------|
| **Windows** | `%APPDATA%\Claude\logs\mcp-server-gmail.log` |
| **macOS** | `~/Library/Application Support/Claude/logs/` |

---

### Common Issues

#### 1. `ModuleNotFoundError: No module named 'mcp'`

**Cause:** MCP not installed in the Python version Claude is using.

**Fix:**

```bash
# Use the same Python as in your config
/path/to/python -m pip install mcp
```

---

#### 2. `ModuleNotFoundError: No module named 'app'`

**Cause:** Missing `__init__.py` files.

**Fix:** Create these files:

```bash
touch MCP_Server/__init__.py
touch MCP_Server/app/__init__.py
```

---

#### 3. `MCP requires Python >=3.10`

**Cause:** Using Python 3.9 or older.

**Fix:** Install Python 3.10+ and update your config path.

---

#### 4. `Expecting value: line 1 column 1 (char 0)`

**Cause:** Empty or corrupted `token.json` or `credentials.json`.

**Fix:**

```bash
# Delete and re-authenticate
rm token.json
python -c "from app.gmail_auth import get_gmail_service; get_gmail_service()"
```

---

#### 5. OAuth Error - Browser Won't Open

**Fix:** Manually copy the URL from terminal and paste in browser.

---

### Test Your Setup

Run this diagnostic:

```bash
cd MCP_Server
python tests/test_server.py
```

**Expected output:**

```
[OK] Path configured
[OK] MCP imported successfully
[OK] gmail_auth imported successfully
[OK] gmail_client imported successfully
[OK] Gmail authentication successful

=== All checks passed! Server should work. ===
```

---

## šŸ“ Project Structure

```
MCP_Server/
ā”œā”€ā”€ .github/
│   └── workflows/
│       └── deploy.yml           # CI/CD pipeline
ā”œā”€ā”€ app/
│   ā”œā”€ā”€ __init__.py
│   ā”œā”€ā”€ server.py                # MCP server entry point
│   ā”œā”€ā”€ gmail_auth.py            # OAuth 2.0 authentication
│   └── gmail_client.py          # Gmail API operations
ā”œā”€ā”€ tests/
│   ā”œā”€ā”€ __init__.py
│   ā”œā”€ā”€ conftest.py              # Pytest configuration
│   └── test_server.py           # Test suite
ā”œā”€ā”€ docs/
│   └── images/                  # Screenshots & diagrams
ā”œā”€ā”€ credentials.json             # Google OAuth credentials (DO NOT COMMIT)
ā”œā”€ā”€ token.json                   # OAuth token (DO NOT COMMIT)
ā”œā”€ā”€ requirements.txt             # Python dependencies
ā”œā”€ā”€ .gitignore                   # Git ignore rules
ā”œā”€ā”€ LICENSE                      # MIT License
└── README.md                    # This file
```

---

## šŸ”’ Security Notes

> āš ļø **Never commit these files:**

- `credentials.json`
- `token.json`

**Add to your `.gitignore`:**

```
credentials.json
token.json
*.pyc
__pycache__/
venv/
.env
```

---

## šŸ“Š Available MCP Tools

| Tool | Description | Gmail Scope |
|------|-------------|-------------|
| `get_unread_emails` | Fetch unread emails from inbox | `gmail.readonly` |
| `create_draft_reply` | Create a draft reply to an email | `gmail.compose` |

---

## šŸ› ļø Development

### Run Server Manually

```bash
cd MCP_Server
python app/server.py
```

> The server communicates via stdin/stdout using JSON-RPC (MCP protocol).

### Run Tests

```bash
# Manual test
python tests/test_server.py

# Pytest
python -m pytest tests/ -v
```

---

## šŸ“ Requirements

```txt
mcp>=1.0.0
google-api-python-client>=2.0.0
google-auth-oauthlib>=1.0.0
google-auth-httplib2>=0.1.0
```

---

## šŸ¤ Contributing

1. Fork the repository
2. Create a feature branch (`git checkout -b feature/amazing-feature`)
3. Commit your changes (`git commit -m 'Add amazing feature'`)
4. Push to the branch (`git push origin feature/amazing-feature`)
5. Open a Pull Request

---

## šŸ“„ License

This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.

---

## šŸ™ Acknowledgments

- [Anthropic](https://anthropic.com) for Claude and the MCP protocol
- [Google](https://developers.google.com/gmail/api) for the Gmail API

---

## šŸ“§ Contact

**Dave** - dave.resume.job@gmail.com

**Project Link:** [https://github.com/TheMlengineer-py/MCP_Server](https://github.com/TheMlengineer-py/MCP_Server)

---

<div align="center">

**Made with ā¤ļø for the Claude Desktop community**

</div>