Skip to content
kamaljaiPublic

About

No description, website, or topics provided.

Resources

Stars

1 star

Watchers

0 watching

Forks

Latest commit

 

History

5 Commits

Folders and files

Repository files navigation

Email MCP Gateway

A full-stack web application that acts as a unified productivity gateway, allowing users to connect multiple email and calendar accounts (Google, Microsoft Outlook) and expose all functionality through a standardised MCP (Model Context Protocol) server endpoint.

Any MCP-compatible AI client — Claude Desktop, Cursor, VS Code, Windsurf, or a custom client — can query and act on email, calendar, and tasks via 23 tools.


Architecture

┌────────────────────────────────────────────────────────┐
│                   MCP Client (AI)                      │
│         Claude Desktop / Cursor / VS Code              │
└───────────────────────┬────────────────────────────────┘
                        │ HTTP SSE (Bearer token)
┌───────────────────────▼────────────────────────────────┐
│                nginx reverse proxy :80                 │
├──────────────────────────┬─────────────────────────────┤
│  Angular SPA :4200       │   FastAPI backend :8000     │
│  (TypeScript/Material)   │   /api/v1/*  REST API       │
│                          │   /mcp/sse   MCP Server     │
└──────────────────────────┴──────────┬──────────────────┘
                                      │
                              ┌───────▼──────┐
                              │ PostgreSQL 16 │
                              └──────────────┘
                                      │
                       ┌─────────────┴──────────────┐
                  Google APIs               Microsoft Graph
                  Gmail, Calendar           Mail, Calendar,
                  Tasks                     To-Do

Quick Start

Prerequisites

  • Docker and Docker Compose v2
  • Google OAuth 2.0 credentials
  • Microsoft Azure App Registration credentials

1. Clone and configure

git clone <repo-url>
cd EmailApp
cp .env.example .env

Edit .env with your credentials (see OAuth Registration sections below).

Generate the required secret keys:

# App secret key
openssl rand -hex 32

# Fernet encryption key for tokens
python -c "from cryptography.fernet import Fernet; print(Fernet.generate_key().decode())"

2. Start the stack

docker compose up --build

The first startup runs Alembic migrations automatically. Access the app at http://localhost.

3. First login

Navigate to http://localhost → click Continue with Google or Continue with Microsoft.


OAuth App Registration

Google

  1. Go to Google Cloud Console → APIs & Services → Credentials
  2. Create OAuth 2.0 Client ID → Web application
  3. Add Authorised redirect URI: http://localhost/api/v1/auth/google/callback
  4. Enable APIs: Gmail API, Google Calendar API, Tasks API
  5. Copy Client ID and Secret to .env

Required scopes (configured automatically):

  • gmail.readonly, gmail.send, gmail.compose, gmail.modify
  • calendar.readonly, calendar.events
  • tasks.readonly, tasks

Microsoft Azure

  1. Go to Azure Portal → Azure Active Directory → App Registrations → New
  2. Set redirect URI: http://localhost/api/v1/auth/microsoft/callback (Web platform)
  3. Under Certificates & Secrets, create a new client secret
  4. Under API Permissions, add:
    • Mail.Read, Mail.Send, Mail.ReadWrite
    • Calendars.Read, Calendars.ReadWrite
    • Tasks.Read, Tasks.ReadWrite
    • openid, email, profile, offline_access
  5. Copy Application (client) ID, client secret, and Directory (tenant) ID to .env

MCP Client Configuration

After logging in and generating an API token at /tokens, use the MCP Setup Guide page (/mcp-guide) for auto-populated config snippets, or use the templates below.

Claude Desktop

~/Library/Application Support/Claude/claude_desktop_config.json (macOS) %APPDATA%\Claude\claude_desktop_config.json (Windows)

{
  "mcpServers": {
    "email-mcp-gateway": {
      "url": "http://localhost/mcp/sse",
      "headers": {
        "Authorization": "Bearer mcp_live_YOUR_TOKEN"
      }
    }
  }
}

Cursor

.cursor/mcp.json or global Cursor settings:

{
  "mcpServers": {
    "email-mcp-gateway": {
      "url": "http://localhost/mcp/sse",
      "headers": {
        "Authorization": "Bearer mcp_live_YOUR_TOKEN"
      }
    }
  }
}

VS Code

settings.json:

{
  "mcp.servers": {
    "email-mcp-gateway": {
      "type": "sse",
      "url": "http://localhost/mcp/sse",
      "headers": {
        "Authorization": "Bearer mcp_live_YOUR_TOKEN"
      }
    }
  }
}

MCP Tools Reference

Email (8 tools)

Tool Description
search_emails Full-text search with date/folder filters
get_email Fetch full email body by ID
list_emails Paginated email listing
send_email Compose and send HTML or plain-text email
reply_to_email Reply or reply-all to a thread
move_email Move to a folder/label
delete_email Move to trash
list_folders List all mailbox folders/labels

Calendar (7 tools)

Tool Description
list_calendar_events Events in a date range
search_calendar_events Search by title/description
get_calendar_event Full event details
create_calendar_event Create with attendees and timezone
update_calendar_event Update any fields
delete_calendar_event Delete/cancel event
list_calendars List all calendars

Tasks (8 tools)

Tool Description
list_tasks List active/completed tasks
search_tasks Search by title or notes
get_task Full task details
create_task Create with due date
update_task Update any fields
complete_task Mark as complete
delete_task Delete a task
list_task_lists List all task lists

All tools accept an optional account_email parameter to target a specific connected account.


Environment Variables

Variable Description Required
APP_SECRET_KEY JWT signing secret (32+ char hex) Yes
TOKEN_ENCRYPTION_KEY Fernet key for OAuth token encryption Yes
DATABASE_URL PostgreSQL connection string Yes
FRONTEND_URL Frontend origin for CORS Yes
GOOGLE_CLIENT_ID Google OAuth client ID For Google auth
GOOGLE_CLIENT_SECRET Google OAuth client secret For Google auth
MICROSOFT_CLIENT_ID Azure app client ID For Microsoft auth
MICROSOFT_CLIENT_SECRET Azure client secret For Microsoft auth
MICROSOFT_TENANT_ID Azure tenant ID (common for multi-tenant) For Microsoft auth

Development

Backend (standalone)

cd backend
uv venv && source .venv/bin/activate
uv pip install -e .
# Start local postgres first, then:
alembic upgrade head
uvicorn app.main:app --reload --port 8000

API docs: http://localhost:8000/api/docs

Frontend (standalone)

cd frontend
npm install
npm start   # http://localhost:4200

Security Notes

  • OAuth access and refresh tokens are encrypted at rest using Fernet (AES-128-CBC + HMAC-SHA256)
  • MCP API tokens are stored as SHA-256 hashes with HMAC comparison — the plaintext is shown only once
  • OAuth CSRF states expire after 10 minutes
  • MCP endpoints are rate-limited to 100 requests/minute per token
  • CORS is restricted to the configured FRONTEND_URL
  • Background scheduler automatically refreshes tokens expiring within 5 minutes

Disclaimer

THIS SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED. USE AT YOUR OWN RISK.

  • This application handles sensitive authentication credentials (OAuth tokens, API keys). Ensure you understand the security implications before deploying.
  • Never commit .env files or real credentials to version control. Always use .env.example as a template.
  • The authors and contributors are not responsible for any data loss, security breaches, or damages arising from the use of this software.
  • This is a demonstration/reference implementation. Additional security hardening (HTTPS, secret management, etc.) is required for production deployments.

Production Checklist

Before deploying to production:

  • Change all default secrets (APP_SECRET_KEY, TOKEN_ENCRYPTION_KEY)
  • Use strong, unique passwords for database and services
  • Enable HTTPS/TLS for all connections
  • Review and restrict CORS origins
  • Use proper secret management (AWS Secrets Manager, HashiCorp Vault, etc.)
  • Enable firewall rules and network segmentation
  • Set DEBUG=false in environment
  • Review OAuth redirect URIs and remove localhost
  • Audit log retention and monitoring
  • Regular security updates for dependencies

License

MIT

About

No description, website, or topics provided.

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages