mcp-imap-server
Enables LLMs to read, search, and manage emails via IMAP with secure, read-only access to email accounts.
README
MCP IMAP Server
A Model Context Protocol (MCP) server that enables Claude and other LLM applications to read emails via the IMAP protocol. This server provides secure, read-only access to email accounts from popular providers like Gmail, Outlook, Yahoo, and custom IMAP servers.
Features
- š Secure Authentication: Support for username/password authentication with secure credential handling
- š§ Email Operations: Search, read, and manage emails with comprehensive filtering options
- š Folder Management: List and navigate email folders/mailboxes
- š·ļø Email Status: Mark emails as read/unread
- š Advanced Search: Search by sender, subject, date range, content, and read status
- š Provider Support: Pre-configured for Gmail, Outlook, Yahoo, iCloud, and custom IMAP servers
- š”ļø Security First: Read-only access, no email sending or deletion capabilities
- ā” Performance: Efficient connection pooling and error handling
Quick Start
Installation
git clone <repository-url>
cd mcp-imap-server
npm install
npm run build
Configuration
Create a .env file in the project root:
# Required: IMAP server configuration
IMAP_HOST=imap.gmail.com
IMAP_PORT=993
IMAP_USER=your-email@gmail.com
IMAP_PASSWORD=your-app-password
IMAP_TLS=true
# Optional: Connection settings
IMAP_AUTH_TIMEOUT=3000
IMAP_CONN_TIMEOUT=10000
IMAP_KEEPALIVE_INTERVAL=10000
IMAP_IDLE_INTERVAL=300000
IMAP_FORCE_NOOP=false
Popular Email Provider Settings
Gmail
IMAP_HOST=imap.gmail.com
IMAP_PORT=993
IMAP_TLS=true
Outlook/Hotmail
IMAP_HOST=outlook.office365.com
IMAP_PORT=993
IMAP_TLS=true
Yahoo Mail
IMAP_HOST=imap.mail.yahoo.com
IMAP_PORT=993
IMAP_TLS=true
iCloud Mail
IMAP_HOST=imap.mail.me.com
IMAP_PORT=993
IMAP_TLS=true
Running the Server
# Development mode
npm run dev
# Production mode
npm run build
npm start
MCP Tools
The server exposes the following tools to MCP clients:
search_emails
Search for emails based on various criteria.
Parameters:
sender(optional): Filter by sender email address or namesubject(optional): Filter by subject line (partial match)dateFrom(optional): Start date for date range filter (ISO format: YYYY-MM-DD)dateTo(optional): End date for date range filter (ISO format: YYYY-MM-DD)content(optional): Search within email content/body textunread(optional): Filter by read status (true for unread only, false for read only)folder(optional): Folder to search in (defaults to INBOX)limit(optional): Maximum number of emails to return (1-100)
Example:
{
"name": "search_emails",
"arguments": {
"sender": "notifications@github.com",
"unread": true,
"limit": 10
}
}
get_email
Retrieve the full content of a specific email by its message ID.
Parameters:
messageId(required): The message ID or UID of the email to retrievefolder(optional): Folder containing the email (defaults to INBOX)
Example:
{
"name": "get_email",
"arguments": {
"messageId": "12345",
"folder": "INBOX"
}
}
list_folders
List all available email folders/mailboxes in the account.
Parameters:
includeSpecial(optional): Include special folders like Sent, Drafts, etc.
Example:
{
"name": "list_folders",
"arguments": {
"includeSpecial": true
}
}
get_recent_emails
Get the most recent emails from a specific folder.
Parameters:
folder(optional): Folder to get emails from (defaults to INBOX)limit(optional): Number of recent emails to retrieve (1-50, default: 10)unreadOnly(optional): Only return unread emails
Example:
{
"name": "get_recent_emails",
"arguments": {
"folder": "INBOX",
"limit": 5,
"unreadOnly": true
}
}
mark_as_read
Mark an email as read or unread.
Parameters:
messageId(required): The message ID or UID of the email to markfolder(optional): Folder containing the email (defaults to INBOX)read(optional): Mark as read (true) or unread (false). Default: true
Example:
{
"name": "mark_as_read",
"arguments": {
"messageId": "12345",
"read": true
}
}
Security Considerations
Email Provider Setup
Gmail
- Enable 2-factor authentication
- Generate an App Password (not your regular password)
- Use the App Password as
IMAP_PASSWORD
Outlook/Hotmail
- Enable 2-factor authentication
- Generate an App Password
- Use the App Password as
IMAP_PASSWORD
Other Providers
Consult your email provider's documentation for IMAP access and app password setup.
Best Practices
- Never log credentials: The server is designed to never log sensitive information
- Use app passwords: Always use app-specific passwords, never your main account password
- Read-only access: The server only provides read operations for security
- Secure storage: Store credentials securely and never commit them to version control
- Network security: Always use TLS/SSL connections (enabled by default)
Development
Project Structure
src/
āāā config/ # Configuration management
āāā services/ # IMAP connection and management
āāā tools/ # MCP tool implementations
āāā types/ # TypeScript type definitions
āāā index.ts # Main server entry point
Available Scripts
npm run build # Build TypeScript to JavaScript
npm run dev # Run in development mode with auto-reload
npm start # Run the built server
npm test # Run tests
npm run lint # Lint the code
npm run typecheck # Type check without building
npm run clean # Clean build directory
Adding New Tools
- Define types in
src/types/index.ts - Add Zod validation schema
- Implement the tool logic in
src/tools/index.ts - Register the tool in
src/index.ts
Error Handling
The server includes comprehensive error handling for:
- Authentication failures: Clear messages for invalid credentials
- Connection issues: Timeout and network error handling
- IMAP protocol errors: Detailed error messages for debugging
- Validation errors: Input validation with helpful error messages
Troubleshooting
Common Issues
-
Authentication Failed
- Verify your email and app password are correct
- Ensure 2FA is enabled and you're using an app password
- Check if IMAP is enabled in your email provider settings
-
Connection Timeout
- Verify the IMAP host and port settings
- Check your firewall and network settings
- Try increasing the connection timeout values
-
TLS/SSL Errors
- Ensure
IMAP_TLS=truefor secure connections - Verify your email provider supports the specified port with TLS
- Ensure
Debug Mode
Set NODE_ENV=development to enable debug logging:
NODE_ENV=development npm run dev
Contributing
- Fork the repository
- Create a feature branch
- Make your changes with proper tests
- Ensure all tests pass and code is properly typed
- Submit a pull request
License
MIT License - see LICENSE file for details.
Support
For issues and questions:
- Check the troubleshooting section above
- Review your email provider's IMAP documentation
- Open an issue on GitHub with detailed error messages and configuration (without credentials)
Recommended Servers
playwright-mcp
A Model Context Protocol server that enables LLMs to interact with web pages through structured accessibility snapshots without requiring vision models or screenshots.
Magic Component Platform (MCP)
An AI-powered tool that generates modern UI components from natural language descriptions, integrating with popular IDEs to streamline UI development workflow.
Audiense Insights MCP Server
Enables interaction with Audiense Insights accounts via the Model Context Protocol, facilitating the extraction and analysis of marketing insights and audience data including demographics, behavior, and influencer engagement.
VeyraX MCP
Single MCP tool to connect all your favorite tools: Gmail, Calendar and 40 more.
graphlit-mcp-server
The Model Context Protocol (MCP) Server enables integration between MCP clients and the Graphlit service. Ingest anything from Slack to Gmail to podcast feeds, in addition to web crawling, into a Graphlit project - and then retrieve relevant contents from the MCP client.
Kagi MCP Server
An MCP server that integrates Kagi search capabilities with Claude AI, enabling Claude to perform real-time web searches when answering questions that require up-to-date information.
E2B
Using MCP to run code via e2b.
Neon Database
MCP server for interacting with Neon Management API and databases
Exa Search
A Model Context Protocol (MCP) server lets AI assistants like Claude use the Exa AI Search API for web searches. This setup allows AI models to get real-time web information in a safe and controlled way.
Qdrant Server
This repository is an example of how to create a MCP server for Qdrant, a vector search engine.