Skip to main content
Glama
Gmail MCP Server

Gmail MCP Server

by saidulalam01
README.md
# Gmail MCP Server

Let your AI assistant manage your Gmail — search, read, send, label, archive, and more through natural conversation.

Works with **Claude Code**, **Claude Desktop**, **Cursor**, **Windsurf**, and any [MCP-compatible](https://modelcontextprotocol.io/) app.

![Python](https://img.shields.io/badge/python-3.10%2B-blue)
![License](https://img.shields.io/badge/license-MIT-green)
![MCP](https://img.shields.io/badge/protocol-MCP-purple)
![Tests](https://img.shields.io/badge/tests-36%20passed-brightgreen)

<p align="center">
  <img src="assets/demo.svg" alt="Demo" width="800">
</p>

---

## Setup

### 1. Clone & Install

```bash
git clone https://github.com/saidulalam01/gmail-mcp-server.git
cd gmail-mcp-server
python -m venv .venv
source .venv/bin/activate        # Windows: .venv\Scripts\activate
pip install -r requirements.txt
```

> No git? Click the green **Code** button on GitHub → **Download ZIP**.

### 2. Get Google Credentials

1. Go to [console.cloud.google.com](https://console.cloud.google.com/) and create a project
2. Go to **APIs & Services → Library** → search **Gmail API** → **Enable**
3. Go to **APIs & Services → OAuth consent screen** → choose **External** → fill in app name and your email → **Save and Continue**
4. On the **Scopes** page, add these three then save:
   ```
   https://www.googleapis.com/auth/gmail.modify
   https://www.googleapis.com/auth/gmail.compose
   https://www.googleapis.com/auth/gmail.labels
   ```
5. On the **Test users** page, add your Gmail address → save
6. Go to **APIs & Services → Credentials** → **+ Create Credentials → OAuth client ID** → type: **Desktop app** → **Create**
7. Download the JSON file and move it into the project folder

### 3. Authenticate

```bash
python gmail_mcp_server.py --auth
```

Your browser opens for Google sign-in. If you see "This app isn't verified", click **Advanced → Go to Gmail MCP Server (unsafe)** — this is normal for personal projects.

### 4. Connect to Your AI App

Get your paths first:

```bash
echo "$(pwd)/.venv/bin/python $(pwd)/gmail_mcp_server.py"
```

Then add to your app's config:

<details>
<summary><b>Claude Code</b> — <code>~/.claude/settings.json</code></summary>

```json
{
  "mcpServers": {
    "gmail": {
      "command": "/Users/yourname/gmail-mcp-server/.venv/bin/python",
      "args": ["/Users/yourname/gmail-mcp-server/gmail_mcp_server.py"]
    }
  }
}
```
</details>

<details>
<summary><b>Claude Desktop</b> — <code>~/Library/Application Support/Claude/claude_desktop_config.json</code></summary>

```json
{
  "mcpServers": {
    "gmail": {
      "command": "/Users/yourname/gmail-mcp-server/.venv/bin/python",
      "args": ["/Users/yourname/gmail-mcp-server/gmail_mcp_server.py"]
    }
  }
}
```

Windows config path: `%APPDATA%\Claude\claude_desktop_config.json`
</details>

<details>
<summary><b>Cursor / Windsurf / Other</b></summary>

Point your MCP client to the server as a stdio process using the two paths from above.
</details>

> **Use full absolute paths** — relative paths won't work because the AI app launches the server from its own directory.

### 5. Try It

- *"Show me my unread emails"*
- *"Read the latest email from Sarah"*
- *"Draft a reply saying I'll be there at 3pm"*
- *"Archive all emails from noreply"*

---

## Available Tools

| Tool | What It Does |
|------|-------------|
| `search_emails` | Search with Gmail syntax (`from:`, `is:unread`, `newer_than:2d`, etc.) |
| `read_email` | Read full email content |
| `list_labels` | List all labels |
| `create_label` | Create a new label |
| `apply_label` | Add a label to an email |
| `remove_label` | Remove a label |
| `archive_email` | Archive an email |
| `move_email` | Move between folders |
| `mark_as_read` | Mark as read |
| `mark_as_unread` | Mark as unread |
| `draft_reply` | Draft a threaded reply |
| `send_email` | Send a new email (CC/BCC supported) |
| `send_draft` | Send an existing draft |

You don't call these directly — just describe what you want and the AI picks the right tool.

---

## Troubleshooting

**Token expires every 7 days** — Your Google app is in "Testing" mode. Just re-run `python gmail_mcp_server.py --auth`. To fix permanently: switch to "Internal" in Google Cloud (requires Workspace), or publish the app.

**"Access blocked" during auth** — Add your email as a test user: Google Cloud → OAuth consent screen → Test users.

**"invalid_grant" or "token revoked"** — Delete `token.json` and re-run `python gmail_mcp_server.py --auth`.

**AI app can't find the server** — Make sure you're using full absolute paths in the config, not relative.

---

## Security

- `token.json` and credentials are **git-ignored** — never committed
- The server runs **locally over stdio** — no ports opened, no network exposure
- The server **cannot permanently delete** emails — only archive, label, and send
- To revoke access: [Google Account Permissions](https://myaccount.google.com/permissions) → remove the app

---

## Contributing

See [CONTRIBUTING.md](CONTRIBUTING.md). Run tests with `pip install -e ".[dev]" && pytest`.

## License

[MIT](LICENSE)