Skip to main content
Glama
adel-noufal

Gmail MCP Server

by adel-noufal

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.


Related MCP server: Gmail MCP Server

βœ… 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

git clone https://github.com/adel-noufal/Gmail-Mcp-Server-Automation.git
cd Gmail-Mcp-Server-Automation

Step 3 β€” Create a Python Virtual Environment

# 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

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.

# 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.

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:

{
  "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 file for details.

Related MCP Connectors

Related MCP Servers