Skip to content

Gmail SecretaryIMAP/SMTP Client for AI Agents

A production-grade Gmail client engineered for LLM orchestration with RFC-compliant CONDSTORE, IDLE, and native Gmail extensions

Gmail Secretary

Quick Start ​

Install via Docker (recommended):

yaml
# docker-compose.yml
services:
  workspace-secretary:
    image: ghcr.io/johnneerdael/gmail-secretary-map:latest
    container_name: workspace-secretary
    restart: always
    ports:
      - "8000:8000"
    volumes:
      - ./config:/app/config
    environment:
      - LOG_LEVEL=INFO

Important: Generate a unique bearer token for security:

bash
# macOS/Linux
uuidgen

# Windows PowerShell
[guid]::NewGuid().ToString()

Add to your config.yaml:

yaml
bearer_auth:
  enabled: true
  token: "your-generated-uuid-here"

Then start with: docker compose up -d

See Getting Started for complete installation instructions.

Why Gmail Secretary MCP? ​

Traditional email clients are built for humans. Gmail Secretary MCP is built for AI assistants.

  • Instant reads: SQLite cache means sub-millisecond email queries
  • Persistent connections: Engine maintains IMAP connection (no per-request reconnect)
  • Token-efficient: Bulk email fetching with smart truncation (700 chars) for fast triage
  • Context-rich: Full thread history, attachment content, and calendar context in one tool call
  • Intelligence signals: VIP detection, urgency markers, question detectionβ€”not hardcoded decisions
  • Agentic workflows: Compose specialized agents (Triage, Scheduler, Intelligence Clerk) using atomic tools

Example Workflows ​

Daily Briefing

"Give me my daily briefingβ€”what emails need my attention today?"

The AI uses get_daily_briefing() to fetch:

  • Today's calendar events
  • Email candidates with 5 priority signals (VIP, urgent, questions, deadlines, meetings)
  • Intelligent summary prioritizing your VIP senders and time-sensitive requests

Smart Scheduling

"I received a meeting invite from John for tomorrow at 2 PM. Check my calendar and if I'm free during working hours, draft an acceptance."

The AI:

  1. Checks calendar availability with check_calendar()
  2. Validates time is within your working_hours
  3. Uses create_draft_reply() to prepare response
  4. Shows you the draft for confirmation

Document Intelligence

"Find the invoice PDF sent by Accounting last week, read it, and tell me the total amount."

The AI:

  1. Searches with search_emails(keyword='invoice', from='accounting')
  2. Extracts PDF text with get_attachment_content()
  3. Parses and presents the total

What's New in v4.8.0 ​

Database Layer Refactoring & Bug Fixes:

  • πŸ—„οΈ Shared Query Modules: All SQL queries extracted to db/queries/ modules (56 functions total)
  • πŸ“‰ ~1,070 Lines Removed: Eliminated duplicate SQL between engine and web layers
  • πŸ”§ Bug Fixes:
    • Calendar page no longer returns 500 error (missing strftime filter)
    • Email sync works correctly (boolean type mismatch fixed)
    • Embedding sync no longer errors (missing methods added)
    • CSRF tokens now sent correctly with HTMX requests

See the Architecture Documentation for the new database layer structure.

What's New in v4.7.x ​

Critical Bug Fixes:

  • πŸ” OAuth2 Token Persistence: Refreshed tokens now properly saved to disk (v4.7.2)
  • 🧠 Embedding Dimensions: Fixed config path for embedding dimensions (v4.7.1)
  • πŸ—„οΈ Shared Database Layer: New workspace_secretary/db/ module for unified access

What's New in v4.6.0 ​

Calendar Caching & Offline-First Architecture:

  • ⚑ Instant Calendar Reads: 10x faster queries via intelligent caching
  • 🌐 Offline-First Events: Create/edit/delete events without internet
  • πŸ”„ Background Worker: Autonomous sync daemon with incremental updates
  • 🏷️ Status Badges: Visual indicators for pending sync and conflicts
  • βš™οΈ Calendar Selection: Choose which calendars to display in settings

What's New in v4.4.0 ​

Web UI Configuration:

  • πŸ“ Config Documentation: Added web UI configuration options to config.sample.yaml

What's New in v4.3.3 ​

Phase 4 Dashboard:

  • πŸ“Š Dashboard Stats: Email statistics overview (total, unread, today's count)
  • ⚑ Priority Queue: Priority email list with quick actions
  • ⌨️ Keyboard Shortcuts: j/k navigation, o to open, r to reply

What's New in v4.3.2 ​

halfvec Support for High Dimensions:

  • 🧠 Automatic Quantization: 16-bit halfvec type for dimensions > 2000
  • πŸ”§ HNSW Fix: Resolves "column cannot have more than 2000 dimensions" error
  • πŸ“‰ 50% Storage Savings: halfvec uses half the space with ~0.1% recall loss

What's New in v4.3.0 ​

Gemini Embeddings & Provider Fallback:

  • 🧠 Google Gemini Provider: Native google-genai SDK with task_type optimization
  • πŸ”„ Automatic Fallback: Switch providers on rate limit (Cohere β†’ Gemini or vice versa)
  • πŸ“Š L2 Normalization: Auto-normalizes Gemini vectors for dimensions β‰  3072
  • πŸ“– Detailed Guides: Copy-paste configs with rate limits, sync time estimates, batch calculators
ProviderFree Tier Sync (25k emails)Paid Tier Sync
Gemini Free~25 days (1k RPD limit)N/A
Gemini Tier 1N/A~17 minutes
Cohere TrialLimited (1k calls/month)Varies by plan

See Embeddings Guide for configuration details.

What's New in v4.2.7 ​

Gap Sync & Accurate Counters:

  • πŸ” Set Difference Sync: Compares IMAP UIDs vs DB UIDs to find exactly what's missing
  • πŸ“Š Real Progress: Counters reflect actual DB state, not estimates
  • πŸ”§ Gap Recovery: Fixes sync when emails were synced from both ends leaving a gap

What's New in v4.2.6 ​

Resumable Sync & Stability Fixes:

  • πŸ”„ Resume on Restart: Sync picks up where it left off using stored uidnext
  • πŸ“‘ IDLE Starts Immediately: Real-time email detection even during initial sync
  • πŸ›‘οΈ Embeddings Stability: Filters invalid texts, prevents 400 errors from empty emails
  • πŸ“Š Accurate Progress: Shows "Resuming (16000/26000 done, 10000 remaining)"

What's New in v4.2.5 ​

Lockstep Sync+Embed Architecture β€” Fixes critical race conditions:

  • πŸ”„ Lockstep Processing: Sync 50 emails β†’ embed those 50 β†’ repeat (no parallel race)
  • πŸ“Š Oldest-First Sync: Now processes all emails from oldest to newest UID
  • ⚑ Event Loop Fixes: Pool init runs in executor, httpx client reused with semaphore
  • πŸ›‘οΈ Race Condition Fixed: Embeddings can no longer exceed synced email count

What's New in v4.2.4 ​

Parallel Sync & Smart Scheduling:

  • ⚑ Parallel Folder Sync: Up to 5 concurrent IMAP connections via connection pool
  • πŸ”„ IDLE + Catch-up Strategy: Real-time INBOX push, 30-min periodic catch-up for other folders
  • πŸŽ›οΈ Configurable: MAX_SYNC_CONNECTIONS and SYNC_CATCHUP_INTERVAL env vars

What's New in v4.2.3 ​

Fix for IDLE Blocking Issues:

  • Threaded IDLE Operation:
    • Moved idle_start, idle_done, idle_check to a dedicated thread.
    • Prevents event loop blocking during IMAP IDLE.
  • Event-loop Safe Communication:
    • Uses loop.call_soon_threadsafe() for updates.
  • Updated Documentation:
    • Added architecture section for threaded IDLE pattern.

What's New in v4.2.2 ​

Bug Fixes & Documentation Overhaul:

  • πŸ”§ Database Init Fix: Engine now properly initializes database connection pool
  • 🐘 PostgreSQL Support: Docker image includes psycopg drivers out of the box
  • πŸ“ Sync Logging: INFO-level logs show sync progress ("Synced N emails from INBOX")
  • πŸ“š Docs Refresh: Simplified OAuth setup, added PostgreSQL guide, updated threading docs

What's New in v4.2.1 ​

Authentication UX Improvements β€” Easier setup with better error messages:

  • πŸ” Fixed OAuth Token Storage: auth_setup now saves client_id and client_secret in token.json (fixes "Missing client_id or client_secret" error)
  • πŸ“‹ Better Setup Instructions: Startup warning now shows both OAuth2 and App Password options
  • πŸ”’ Hardcoded Config Paths: Removed user-configurable output paths to prevent misconfiguration

What's New in v4.2.0 ​

Calendar API Passthrough β€” Simplified architecture with real-time calendar access:

  • πŸ—“οΈ Direct API Access: Calendar operations now go directly to Google Calendar API (no local caching)
  • ⚑ Real-Time Data: All calendar queries return live data, no sync staleness
  • 🧹 Simplified Architecture: Removed calendar database tables and sync complexity
  • πŸ”§ New Endpoints: Added 6 new calendar API endpoints for fine-grained control

New Calendar API Endpoints ​

EndpointPurpose
GET /api/calendar/listList all user's calendars
GET /api/calendar/{id}Get calendar details
GET /api/calendar/{id}/events/{event_id}Get single event
PATCH /api/calendar/{id}/events/{event_id}Update event
DELETE /api/calendar/{id}/events/{event_id}Delete event
POST /api/calendar/freebusyQuery free/busy for multiple calendars

Removed ​

  • calendar_cache.py β€” Calendar caching layer removed
  • gmail_client.py β€” Deprecated Gmail REST API client removed
  • CalendarSync class β€” No longer needed without caching
  • Calendar database tables (calendars, events, attendees)
  • calendar_cache_path config option (ignored if present)

What's New in v4.1.0 ​

CONDSTORE & IDLE Support β€” Efficient incremental sync with push notifications:

  • ⚑ CONDSTORE (RFC 7162): Skip sync when mailbox unchanged, fetch only changed flags via CHANGEDSINCE
  • πŸ“¬ IMAP IDLE (RFC 2177): Push-based notifications for instant new mail detection
  • 🏷️ Gmail Extensions: Native X-GM-MSGID, X-GM-THRID, X-GM-LABELS support
  • πŸ“Ž Attachment Metadata: has_attachments and attachment_filenames stored in database
  • πŸ”„ Debounced Sync: Mutations trigger 2-second debounced sync to batch rapid changes

Performance Improvements ​

ScenarioBeforeAfter
Unchanged mailboxFetch all UIDs, compareSkip entirely (HIGHESTMODSEQ)
Flag changes onlyRe-fetch entire messageFetch only changed flags
New mail detection5-minute poll intervalInstant via IDLE
Rapid mutationsSync per mutationSingle batched sync

What's New in v4.0.0 ​

Complete Architecture Rewrite β€” Read/Write Split for reliability and consistency:

  • πŸ”„ Engine Owns All Writes: Engine now uses DatabaseInterface for all database mutations
  • πŸ“– MCP is Read-Only: MCP reads directly from database, calls Engine API only for mutations
  • πŸ—„οΈ Unified Database: Both Engine and MCP use the same DatabaseInterface abstraction
  • πŸ“… Integrated Calendar Sync: Calendar operations now use direct API passthrough (no local caching)
  • 🧠 Auto Embeddings: Engine generates embeddings automatically after email sync (PostgreSQL)
  • πŸš€ Graceful Enrollment: Engine starts in "no account" mode, auto-connects when OAuth tokens appear

New Engine API Endpoints ​

EndpointPurpose
GET /api/calendar/eventsList calendar events in time range
GET /api/calendar/availabilityGet free/busy information
POST /api/email/setup-labelsCreate Secretary label hierarchy
POST /api/email/sendSend email via Gmail API
POST /api/email/draft-replyCreate draft reply in Gmail

See the Architecture Documentation for the complete technical overview.

What's New in v3.2.0 ​

Standardized Docker Paths & OAuth Consolidation:

  • πŸ“ Config Path: /app/config/config.yaml is now the default (Docker-first)
  • πŸ” Single OAuth Entry: auth_setup.py handles all OAuth flows (browser_auth removed)
  • πŸ“… Calendar Scope: OAuth now includes Gmail + Calendar scopes by default
  • πŸ› οΈ Token Output: --token-output flag for Docker-friendly token management

What's New in v3.1.0 ​

Single Container Architecture β€” Simplified deployment with supervisor-managed processes:

  • πŸ“¦ One Container: Engine + MCP server run together via supervisord
  • πŸ”Œ Unix Socket IPC: Internal communication between processes
  • ⚑ Persistent IMAP: Engine maintains connection (no per-request reconnect)
  • πŸ”„ Background Sync: Continuous incremental sync every 5 minutes
  • πŸ“ Better Docs: Added credentials.json example format

What's New in v3.0.0 ​

Dual-Process Architecture β€” Complete separation of concerns for reliability:

  • πŸ”„ Engine + MCP Split: Independent sync daemon + MCP server
  • πŸ“… Calendar Sync: Full calendar synchronization with local SQLite cache
  • 🧠 Semantic Search: Optional PostgreSQL + pgvector backend with embeddings
  • ⚑ Database Options: SQLite (default) or PostgreSQL with pgvector for AI features
  • 🚫 IMAP-Only: Removed deprecated API mode (oauth_mode config removed)

Database Options ​

BackendWhen to UseFeatures
SQLite (default)Simple deployment, single userFTS5 keyword search, WAL mode
PostgreSQL + pgvectorAI features neededSemantic search, embeddings, similarity matching

See the Architecture Documentation for technical details.

What's New in v2.2.0 ​

RFC 5256 Threading Support β€” Full conversation threading with automatic backfill:

  • 🧡 Server-Side Threading: Uses IMAP THREAD command (RFC 5256) when available
  • πŸ”„ Automatic Backfill: Existing emails get thread headers populated on first sync
  • πŸ“Š Thread Data Model: in_reply_to, references, thread_root_uid stored in SQLite
  • ⚑ Cache-First Threads: get_email_thread queries local cache instead of IMAP

See the Threading Guide for details.

Previous Releases ​

v2.1.0 and earlier

v2.1.0 ​

  • πŸ“š Comprehensive v2.0 documentation overhaul
  • πŸ”§ Fixed sync direction (newest-first for immediate usability)
  • πŸ› Cache update fixes for triage tools

v2.0.0 - Local-First Architecture ​

  • ⚑ SQLite Cache: Email queries hit local databaseβ€”instant response times
  • πŸ”„ Background Sync: Continuous incremental sync keeps cache fresh
  • πŸ’Ύ Persistent Storage: Cache survives restarts
  • πŸ“Š RFC-Compliant: Proper UIDVALIDITY/UIDNEXT tracking per RFC 3501/4549

v1.1.0 ​

  • Third-party OAuth Support (Thunderbird/GNOME credentials)
  • SMTP with XOAUTH2
  • Calendar independent of email backend

v0.2.0 ​

  • Timezone-aware scheduling
  • Working hours constraints
  • Intelligent email signals
  • VIP sender detection

See Migration Guide for upgrading from earlier versions.

Community & Support ​


Built with ❀️ for the AI-native future

Licensed under MIT β€’ Β© 2024-present John Neerdael

Released under the MIT License.