Skip to main content
Glama
Gmail MCP Server

Gmail MCP Server

by shrutroup
README.md
# Gmail MCP Server

A Model Context Protocol (MCP) server that allows Claude Code to interact with your Gmail account for reading and deleting emails.

## Features

- **List Emails**: View your recent emails with summaries
- **Search Emails**: Search using Gmail query syntax (unread, from specific senders, etc.)
- **Read Emails**: Get full email content including body text
- **Delete Emails**: Permanently delete or move to trash
- **Trash Emails**: Move emails to trash (recoverable)

## Prerequisites

1. **Python 3.10+** installed
2. **Google Cloud Project** with Gmail API enabled
3. **OAuth 2.0 Credentials** from Google Cloud Console

## Setup Instructions

### Step 1: Install Dependencies

```bash
cd gmail-mcp-server
pip install -r requirements.txt
```

### Step 2: Set Up Google Cloud Project

1. Go to [Google Cloud Console](https://console.cloud.google.com/)
2. Create a new project (or select existing)
3. Enable the **Gmail API**:
   - Navigate to "APIs & Services" > "Library"
   - Search for "Gmail API"
   - Click "Enable"

### Step 3: Create OAuth 2.0 Credentials

1. Go to "APIs & Services" > "Credentials"
2. Click "Create Credentials" > "OAuth client ID"
3. Choose "Desktop app" as application type
4. Name it "Gmail MCP Server"
5. Click "Create"
6. Download the JSON file and save it as `credentials.json` in this directory

### Step 4: Configure Environment

```bash
cp .env.example .env
```

Edit `.env` and set the paths:
```
GMAIL_CREDENTIALS_PATH=~/git_repos/gmail-mcp-server/credentials.json
GMAIL_TOKEN_PATH=~/git_repos/gmail-mcp-server/token.json
```

### Step 5: Test the Server

Test with MCP Inspector:

```bash
npx @modelcontextprotocol/inspector ~/git_repos/gmail-mcp-server/server.py
```

On first run, it will open a browser for you to authenticate with Google.

### Step 6: Connect to Claude Code

Add the server to Claude Code:

```bash
claude mcp add --transport stdio gmail -- python3 ~/git_repos/gmail-mcp-server/server.py
```

Or manually edit `~/.claude.json`:

```json
{
  "mcpServers": {
    "gmail": {
      "command": "python3",
      "args": ["~/git_repos/gmail-mcp-server/server.py"],
      "env": {
        "GMAIL_CREDENTIALS_PATH": "~/git_repos/gmail-mcp-server/credentials.json",
        "GMAIL_TOKEN_PATH": "~/git_repos/gmail-mcp-server/token.json"
      }
    }
  }
}
```

## Available Tools

### `list_emails`
List recent emails from your inbox.

**Parameters:**
- `max_results` (optional): Number of emails to return (default: 10, max: 100)
- `query` (optional): Gmail search query

**Example:**
```
List my last 20 emails
```

### `search_emails`
Search emails using Gmail query syntax.

**Parameters:**
- `query`: Gmail search query
- `max_results` (optional): Number of results (default: 20, max: 100)

**Example queries:**
- `is:unread` - All unread emails
- `from:boss@company.com` - Emails from specific sender
- `subject:urgent` - Emails with "urgent" in subject
- `has:attachment` - Emails with attachments
- `newer_than:7d` - Emails from last 7 days

**Example:**
```
Search for unread emails from the last week
```

### `get_email`
Get full details of a specific email including body text.

**Parameters:**
- `message_id`: The Gmail message ID

**Example:**
```
Show me the full content of email ID 18c5a1b2f3d4e5f6
```

### `delete_email`
Permanently delete an email (cannot be recovered).

**Parameters:**
- `message_id`: The Gmail message ID

**Example:**
```
Permanently delete email ID 18c5a1b2f3d4e5f6
```

### `trash_email`
Move an email to Trash (can be recovered later).

**Parameters:**
- `message_id`: The Gmail message ID

**Example:**
```
Move email ID 18c5a1b2f3d4e5f6 to trash
```

### `delete_emails_by_query`
Delete multiple emails matching a Gmail search query (BULK DELETE).

**Parameters:**
- `query`: Gmail search query to find emails to delete
- `max_emails` (optional): Maximum number to delete (default: 100, max: 500)
- `permanent` (optional): If True, permanently delete. If False, move to Trash (default: False)

**Example queries:**
- `is:unread older_than:1y` - Delete unread emails older than 1 year
- `from:spam@example.com` - Delete all emails from a sender
- `subject:newsletter older_than:6m` - Delete old newsletters
- `has:attachment larger:10M` - Delete emails with large attachments
- `is:read older_than:2y` - Delete read emails older than 2 years

**Example:**
```
Delete all unread emails from last year
```

### `count_emails_by_query`
Count emails matching a query without deleting (useful for preview before bulk delete).

**Parameters:**
- `query`: Gmail search query

**Returns:**
- Count of matching emails and sample subjects

**Example:**
```
How many unread emails are older than 1 year?
```

## Usage in Claude Code

Once connected, you can ask Claude:

- "List my last 10 emails"
- "Search for unread emails"
- "Show me emails from john@example.com"
- "Delete email ID abc123"
- "Show me the full content of the first email"
- "Remove all emails that were unread in the last year"
- "Delete all emails from newsletter@example.com"
- "How many unread emails are older than 6 months?" (preview before deleting)
- "Trash all read emails older than 2 years"

## Troubleshooting

### Authentication Issues
- Make sure `credentials.json` exists and has correct OAuth credentials
- Delete `token.json` and re-authenticate if you get token errors

### Server Not Found
- Use absolute paths in Claude Code configuration
- Verify Python 3 is in your PATH: `which python3`

### Permission Errors
- Check that Gmail API is enabled in Google Cloud Console
- Verify OAuth consent screen is configured
- Ensure you granted all requested permissions during authentication

## Security Notes

- Never commit `credentials.json` or `token.json` to version control
- The `.gitignore` file excludes these by default
- OAuth tokens are stored locally and refresh automatically
- Use "Desktop app" OAuth type for maximum security

## License

MIT