Skip to main content
Glama
MCP IMAP Server

MCP IMAP Server

by qq7te

MCP IMAP Server

A lightweight Model Context Protocol (MCP) server that exposes your IMAP mailbox through tools, enabling AI assistants to interact with your email.

Features

  • šŸ” Search emails with flexible criteria (sender, subject, date range, text, etc.)

  • šŸ“¬ List mailboxes and folders in your IMAP account

  • šŸ“§ Retrieve messages with full headers, body content, and attachment metadata

  • šŸ“Ž Download attachments (base64 or temporary one-time URL)

  • āœ… Mark messages as seen/unseen

  • šŸ—‘ļø Mark messages as deleted (\\Deleted) in batches of up to 10 (no expunge)

  • šŸ”’ Secure - uses standard IMAP authentication

  • 🌐 Flexible deployment - supports both local (stdio) and remote (HTTP) modes

Related MCP server: imap-mcp

Installation

Prerequisites

  • Python 3.10 or higher

  • An IMAP-enabled email account (Gmail, Outlook, etc.)

Setup

  1. Clone this repository:

git clone <repository-url>
cd mcp-imap-server
  1. Create and activate a virtual environment:

python3 -m venv .venv
source .venv/bin/activate  # On Windows: .venv\Scripts\activate
  1. Install the package:

pip install -U pip
pip install -e .

Configuration

Environment Variables

Copy .env.example to .env and configure your IMAP settings:

cp .env.example .env

Required Settings

  • IMAP_HOST - Your IMAP server hostname (e.g., imap.gmail.com)

  • IMAP_USERNAME - Your email address or username

  • IMAP_PASSWORD - Your password or app-specific password

Optional Settings

  • IMAP_PORT - IMAP server port (default: 993)

  • IMAP_SSL - Use SSL/TLS connection (default: true)

  • IMAP_STARTTLS - Use STARTTLS (default: false)

  • IMAP_TIMEOUT_SECONDS - Connection timeout (default: 30)

  • MCP_ATTACHMENT_URL_HOST - Host/IP used when returning temporary attachment URLs (must resolve to 192.168.x.x; required for serve=true if auto-detection cannot find one)

  • MCP_ATTACHMENT_URL_PORT - Port used in temporary attachment URLs (default: same as MCP_PORT)

  • MCP_ATTACHMENT_URL_SCHEME - URL scheme for temporary attachment URLs (http or https, default: http)

  • MCP_ATTACHMENT_URL_TTL_SECONDS - Token TTL in seconds for temporary attachment URLs (default: 300)

  • MCP_ATTACHMENT_STORAGE_DIR - Directory for temporary attachment files

Email Provider Examples

Gmail:

IMAP_HOST=imap.gmail.com
IMAP_USERNAME=your-email@gmail.com
IMAP_PASSWORD=your-app-password

Note: Gmail requires app-specific passwords when 2FA is enabled.

Outlook/Office 365:

IMAP_HOST=outlook.office365.com
IMAP_USERNAME=your-email@outlook.com
IMAP_PASSWORD=your-password

Usage

The easiest way to run the server is using Docker:

  1. Build the Docker image:

docker build -t mcp-imap-server .
  1. Run with environment variables:

docker run -d \
  --name mcp-imap-server \
  -p 8993:8993 \
  -e IMAP_HOST=imap.gmail.com \
  -e IMAP_USERNAME=your-email@gmail.com \
  -e IMAP_PASSWORD=your-app-password \
  mcp-imap-server
  1. Or use docker-compose with your .env file:

docker-compose up -d

The server will be available at http://localhost:8993/mcp

To view logs:

docker logs -f mcp-imap-server

To stop:

docker-compose down
# or
docker stop mcp-imap-server

Running as HTTP Server (Native)

This mode allows AI clients on other machines (like Warp) to connect to your email server:

source .venv/bin/activate

# Listen on all interfaces
MCP_HOST=0.0.0.0 MCP_PORT=8993 mcp-imap-server

# Or listen on localhost only (more secure)
MCP_HOST=127.0.0.1 MCP_PORT=8993 mcp-imap-server

The MCP endpoint will be available at:

  • Local: http://127.0.0.1:8993/mcp

  • Remote: http://<server-ip>:8993/mcp

You can test the server with:

curl http://localhost:8993/health

Running in stdio Mode (Local Process)

For local AI clients that spawn the server as a subprocess:

source .venv/bin/activate
MCP_TRANSPORT=stdio mcp-imap-server

Connecting with Warp

In Warp, configure the MCP server by adding to your MCP settings:

{
  "mcpServers": {
    "imap": {
      "url": "http://<server-ip>:8993/mcp"
    }
  }
}

Available Tools

The server exposes the following MCP tools:

list_mailboxes

Returns all available mailboxes/folders in your IMAP account.

Returns: List of mailboxes with names, delimiters, and flags.

search_messages

Search for messages with various criteria.

Parameters:

  • mailbox (str, default: "INBOX") - Mailbox to search in

  • from_ (str, optional) - Filter by sender email

  • to (str, optional) - Filter by recipient email

  • subject (str, optional) - Filter by subject text

  • text (str, optional) - Search in message body

  • unseen (bool, optional) - Filter by read/unread status

  • since (str, optional) - Messages since date (YYYY-MM-DD)

  • before (str, optional) - Messages before date (YYYY-MM-DD)

  • limit (int, default: 20) - Maximum number of results

Returns: List of message summaries with UID, subject, sender, date, flags, and size.

get_message

Retrieve full message content by UID.

Parameters:

  • uid (int, required) - Message UID

  • mailbox (str, default: "INBOX") - Mailbox containing the message

  • include_body (bool, default: true) - Include message body

  • include_html (bool, default: false) - Include HTML body

  • max_body_chars (int, default: 20000) - Maximum body length

Returns: Full message with headers, body, flags, and attachment metadata.

download_attachment

Download a message attachment by UID.

Parameters:

  • uid (int, required) - Message UID

  • mailbox (str, default: "INBOX") - Mailbox containing the message

  • attachment_index (int, default: 0) - 0-based index among attachments

  • filename (str, optional) - Exact filename match (preferred when known)

  • offset_bytes (int, default: 0) - Start offset into the attachment payload (for chunked downloads)

  • max_bytes (int, default: 10000000) - If > 0, truncate returned bytes to this limit

  • serve (bool, default: false) - If true, return a temporary one-time URL (no content_base64)

Returns:

  • With serve=false (default): attachment metadata plus content_base64

  • With serve=true: attachment metadata plus:

    • temporary_download_url

    • temporary_download_url_expires_at

    • temporary_download_url_ttl_seconds

    • temporary_download_url_single_use=true

set_seen

Mark a message as read or unread.

Parameters:

  • uid (int, required) - Message UID

  • mailbox (str, default: "INBOX") - Mailbox containing the message

  • seen (bool, default: true) - Mark as seen (true) or unseen (false)

Returns: Updated message flags.

mark_deleted

Mark up to 10 messages as deleted by setting the IMAP \\Deleted flag.

This tool does not expunge messages. It only marks them so you can review or undo from your IMAP client before any permanent deletion.

Parameters:

  • uids (list[int], required) - Message UIDs to mark as deleted (max 10)

  • mailbox (str, default: "INBOX") - Mailbox containing the messages

Returns: Mailbox name, marked count, expunged=false, and per-message updated flags.

Security Considerations

  • āš ļø App-Specific Passwords: Use app-specific passwords instead of your main account password when possible

  • šŸ”’ Firewall: If running in HTTP mode on 0.0.0.0, ensure your firewall restricts access appropriately

  • 🌐 Network: For remote access, consider using a VPN or SSH tunnel instead of exposing the server directly to the internet

  • šŸ“ Environment Variables: Never commit your .env file - it's already in .gitignore

  • šŸ”‘ Read-Only by Default: Most operations are read-only except flag updates (set_seen, mark_deleted); mark_deleted does not expunge

Development

Project Structure

mcp-imap-server/
ā”œā”€ā”€ src/
│   └── mcp_imap_server/
│       ā”œā”€ā”€ __init__.py
│       ā”œā”€ā”€ __main__.py
│       ā”œā”€ā”€ server.py      # Main MCP server and tool definitions
│       ā”œā”€ā”€ config.py      # Configuration management
│       └── imap.py        # IMAP client wrapper
ā”œā”€ā”€ pyproject.toml
ā”œā”€ā”€ README.md
ā”œā”€ā”€ .env.example
└── .gitignore

Running Tests

To test the server functionality:

  1. Ensure your .env file is configured correctly

  2. Start the server in one terminal

  3. Use an MCP client or curl to test the endpoints

Troubleshooting

Connection Errors

  • Verify your IMAP credentials are correct

  • Check if your email provider requires app-specific passwords

  • Ensure the IMAP port (usually 993) is not blocked by your firewall

  • Try enabling debug logging by checking server output

Authentication Failures

  • Some providers (like Gmail) require enabling "Less secure app access" or using OAuth2

  • Use app-specific passwords when 2FA is enabled

  • Verify the username format (some providers need full email, others just the username)

Performance

  • Use the limit parameter in search_messages to reduce result size

  • Adjust max_body_chars in get_message to limit body content size

  • Consider using the unseen filter to only fetch unread messages

License

GPL-3.0-only – see the LICENSE file for details.

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

Support

If you encounter issues or have questions, please open an issue on the GitHub repository.

Tool Schema Changelog

Recent tool additions, removals, and schema changes observed during successful MCP inspections.

Loading tool schema changelog…

    Related MCP Connectors

    • Email infrastructure for AI agents — send, receive, search, and reply to email over MCP.

      https://www.primitive.dev/mcp
    • Email inboxes for AI agents: send, receive, reply, search, and manage threaded email over MCP.

      https://mcp.agentmail.to/mcp
    • Your IMAP mailbox as an MCP server: read, search and (if you allow it) organize mail. Open source.

      https://mcp4mail.online/mcp
    • Your agent needs a mailbox of its own — to receive, thread, draft and send, with attachments, without borrowing your personal inbox or your company's SMTP. **What you can ask for** • "Create an inbox for this agent and tell me its address." • "Read the new messages in this thread and draft a reply." • "Send this message with the attachment and wait for the response." • "Search this inbox for everything from that domain." • "Show delivery metrics and the events on this inbox." **How to use it** Point any MCP client at https://mcp.aisa.one/mail/mcp and sign in with OAuth — there is no key to create or paste. 49 tools: create and delete inboxes, list and read messages, raw message bodies, attachments, threads, drafts and draft attachments, send and reply, message search, inbox events, metrics, and list entries — reads and writes. **Why this rather than the source** A real inbox an agent owns, rather than an SMTP credential it borrows from a human. **It is also a door to the rest** The same login reaches 26 sources and 580+ operations. Find the contact elsewhere in the catalogue, then write to them from here — without adding a second server. **What it costs** Finding and inspecting an operation is free. Running one is billed per call at API prices, with no seat and no monthly minimum, and every call takes max_price_usd so an agent cannot overspend by accident. **Where else it reaches** https://mcp.aisa.one/sales/mcp finds the person to write to.

    Related MCP Servers

    • A
      license
      Not graded
      quality
      D
      maintenance
      A generic IMAP and SMTP MCP server that enables AI agents to interact with email accounts for reading, searching, and sending messages. It provides high-level tools for managing email workflows like daily digests and folder organization across any standard email provider.
      1
      MIT
    • A
      license
      B
      quality
      C
      maintenance
      An MCP server that exposes IMAP operations as tools for AI assistants, enabling email management including listing mailboxes, reading, searching, moving, flagging emails, and creating drafts.
      7
      47 npm
      MIT
    • A
      license
      Not graded
      quality
      D
      maintenance
      Enables secure mailbox access and email management via IMAP/SMTP, with tools for reading, searching, organizing, sending, and deleting emails, designed for local use and compatible with MCP clients like ChatGPT.
      1
      MIT
    • A
      license
      Not graded
      quality
      C
      maintenance
      Connects any IMAP/SMTP mailbox to AI agents via MCP, enabling email read, search, send, reply, and management through natural language.
      11 npm
      MIT