Labsco
Cam10001110101 logo

Email Processing

β˜… 14

from Cam10001110101

An email processing server that uses MongoDB for semantic search and SQLite for efficient storage and retrieval.

πŸ”₯πŸ”₯πŸ”₯βœ“ VerifiedFreeQuick setup

MseeP.ai Security Assessment Badge

Email Processing MCP Server

A cross-platform MCP server that processes Microsoft Outlook emails, generates vector embeddings using Ollama, and provides semantic search capabilities. Works on Windows, macOS, and any platform via Microsoft Graph API.

The server complies with the Model Context Protocol (MCP) 2025-06-18 specification and uses the official MCP SDK.

Features

Core Capabilities

  • Process emails from Outlook with date range filtering
  • Store emails in SQLite database with proper connection management
  • Generate vector embeddings using Ollama (nomic-embed-text)
  • Semantic search across email content via MongoDB vector store
  • Multi-mailbox and multi-account support
  • Support for Inbox, Sent Items, and optionally Deleted Items folders

Cross-Platform Support

  • Windows: Native Outlook COM automation via pywin32
  • macOS: AppleScript integration with Outlook for Mac
  • Any Platform: Microsoft Graph API for cloud-based access (Windows, macOS, Linux, containers)

MCP 2025-06-18 Compliance

  • Structured Tool Results: Tools return properly typed, validated Pydantic models
  • HTTP Transport: Supports both STDIO and HTTP (Streamable HTTP) transports
  • Protocol Negotiation: Declares protocol version during handshake
  • Enhanced Metadata: Tool titles and descriptions for better UI integration

Available Tools (12+)

CategoryTools
Email Processingprocess_emails
Search & Analysissearch_emails, analyze_email_sentiment, find_actionable_items
Data Exportexport_email_data (CSV, JSON, HTML, Excel)
Folder Managementlist_outlook_folders, get_folder_statistics, organize_emails_by_rules
Contact Managementextract_contacts
Statisticsget_email_statistics, check_data_consistency

Available Tools

1. process_emails

Process emails from a specified date range and return structured results:

Input:

Copy & paste β€” that's it
{
  "start_date": "2024-01-01",    # ISO format date (YYYY-MM-DD)
  "end_date": "2024-02-15",      # ISO format date (YYYY-MM-DD)
  "mailboxes": ["All"]           # List of mailbox names or ["All"] for all mailboxes
}

Output (Structured):

Copy & paste β€” that's it
{
  "success": true,
  "processed_count": 150,
  "retrieved_count": 200,
  "stored_count": 180,
  "failed_count": 20,
  "message": "Successfully processed 150 emails (retrieved: 200, stored: 180, failed: 20)",
  "error": null
}

The tool will:

  1. Connect to specified Outlook mailboxes
  2. Retrieve emails from Inbox and Sent Items folders (and Deleted Items if enabled)
  3. Store emails in SQLite database
  4. Generate embeddings using Ollama
  5. Store embeddings in MongoDB for semantic search
  6. Return structured results with detailed statistics

Architecture

Provider-Based Connector Design

The server uses a provider-based abstraction for cross-platform email access:

Copy & paste β€” that's it
src/connectors/
β”œβ”€β”€ base.py               # OutlookConnectorBase (abstract interface)
β”œβ”€β”€ factory.py            # create_connector() with auto-detection
β”œβ”€β”€ windows_connector.py  # Windows COM via pywin32
β”œβ”€β”€ mac_connector.py      # macOS AppleScript via osascript
└── graph_connector.py    # Microsoft Graph API (cross-platform)

All connectors implement the same interface, returning standardized EmailMetadata objects regardless of platform.

Dual-Database Architecture

The server uses a hybrid storage approach:

SQLite Database:

  • Primary email storage and metadata
  • Full-text search capabilities
  • Processing status tracking
  • Date range and folder filtering
  • Fast structured queries

MongoDB:

  • Vector embeddings storage (768 dimensions)
  • Semantic similarity search
  • Metadata stored alongside embeddings
  • Enables AI-powered search

Error Handling

The server provides detailed error messages for common issues:

  • Invalid date formats
  • Connection issues with Outlook
  • MongoDB errors
  • Embedding generation failures with retry logic
  • SQLite storage errors
  • Ollama server connection issues with automatic retries

Resource Management

The server implements proper resource management to prevent issues:

  • Database connections (SQLite and MongoDB) are kept open during the server's lifetime to prevent "Cannot operate on a closed database" errors
  • Connections are only closed when the server shuts down, using an atexit handler
  • Destructors and context managers are used as a fallback to ensure connections are closed when objects are garbage collected
  • Connection management is designed to balance resource usage with operational reliability
  • Robust retry logic for external services like Ollama to handle temporary connection issues

MCP 2025-06-18 Compliance

This server has been upgraded to comply with the MCP 2025-06-18 specification:

Protocol Features

  • Protocol Version: Declares protocolVersion: "2025-06-18" during handshake
  • Structured Output: All tools return typed, validated Pydantic models
  • HTTP Transport: Supports Streamable HTTP with proper header validation
  • Tool Metadata: Enhanced tool descriptions with titles and schemas
  • Error Handling: Structured error responses with detailed information

Transport Support

  • STDIO: Traditional stdin/stdout communication (default)
  • HTTP: Streamable HTTP on localhost:8000/mcp with header validation
  • Protocol Headers: Validates MCP-Protocol-Version and Origin headers
  • Single-Message JSON-RPC: No batch request support per 2025-06-18 spec

Structured Output

The process_emails tool returns a structured ProcessEmailsResult with:

  • success: Boolean indicating operation success
  • processed_count: Number of emails successfully processed
  • retrieved_count: Total emails retrieved from Outlook
  • stored_count: Number of emails stored in SQLite
  • failed_count: Number of emails that failed processing
  • message: Human-readable status message
  • error: Error details if operation failed

Security Notes

  • The server only processes emails from specified mailboxes
  • All data is stored locally (SQLite) and in MongoDB
  • No external API calls except to local Ollama server (and Microsoft Graph if using that provider)
  • Requires explicit user approval for email processing
  • No sensitive email data is exposed through the MCP interface
  • HTTP transport binds only to localhost for security
  • Graph API uses OAuth 2.0 client credentials flow with Azure AD
  • Store Azure AD credentials securely (environment variables, not in code)

Debugging

If you encounter issues:

  1. Verify emails were successfully processed (check process_emails response)
  2. Ensure Ollama server is running for embedding generation
  3. Check that the SQLite database is accessible
  4. Verify MongoDB connection is working properly
  5. Use check_data_consistency tool to verify SQLite/MongoDB sync

Platform-Specific Debugging

Windows:

  • Ensure Outlook is running and accessible
  • Check that pywin32 is installed: pip show pywin32
  • Verify COM automation works: python -c "import win32com.client; print('OK')"

macOS:

  • Ensure Outlook for Mac is installed (not just the "New Outlook" web app)
  • Test AppleScript access: osascript -e 'tell application "Microsoft Outlook" to get name'
  • Grant Terminal/IDE permission in System Preferences β†’ Security & Privacy β†’ Automation

Graph API:

  • Verify Azure AD app has correct permissions (Mail.Read, User.Read.All)
  • Check admin consent was granted
  • Test token acquisition: credentials are logged at startup
  • Verify GRAPH_USER_EMAILS is set correctly ("All" or comma-separated emails)

Upcoming Features

  • Email summarization using LLMs
  • Automatic email categorization
  • Customizable email reports
  • Outlook drafting email responses
  • Outlook rule suggestions
  • Expanded database options with Neo4j and ChromaDB integration