ptbsare/email-mcp-server
If you are the rightful owner of email-mcp-server and would like to certify it and/or have it hosted online, please leave a comment on the right or send an email to henry@mcphub.com.
The MCP Email Server is a Python-based server that allows an LLM to interact with an email account using POP3 and SMTP protocols, secured with TLS encryption.
MCP Email Server
This Python script implements an MCP (Model Context Protocol) server using the mcp library (FastMCP) that allows an LLM (like Claude) to interact with an email account using POP3 (for reading/deleting emails) and SMTP (for sending emails), both secured with TLS encryption. It uses python-dotenv to load configuration from a .env file.
Features
- Poll Emails: Polls the inbox for email headers (
pollEmails). - Fetch Full Emails: Retrieves specific emails (headers and body) by ID (
getEmailsById). - Delete Emails: Deletes specific emails by ID (
deleteEmailsById). - Send Emails: Sends plain text (
sendTextEmail) or HTML formatted (sendHtmlEmail) emails. - Secure Connections: Uses POP3 over SSL (default port 995). For SMTP, supports both STARTTLS (explicit TLS, default port 587) and direct SSL/TLS (implicit TLS, default port 465), configurable via environment variable.
- Configuration: Loads credentials and server details from environment variables or a
.envfile.
Tools Provided
The server exposes the following tools (defined using @mcp.tool()) to the connected LLM:
-
pollEmails()- Description: Returns the message ID and key headers (Subject, From, Date, Message-ID) of all emails currently in the selected mailbox.
- Inputs: None.
- Returns:
list[dict]- A list of dictionaries, each containingidand headers. Example:[{"id": 1, "Subject": "Hello", "From": "...", "Date": "...", "Message-ID": "..."}]
-
getEmailsById(ids: list)- Description: Returns the full details (ID, headers, parsed body) of the specified emails. It attempts to parse the body, preferring HTML over plain text if available.
- Inputs:
ids(list[int]): A list of email IDs to retrieve (IDs correspond to the order returned bypollEmailsat the time of polling).
- Returns:
list[dict]- A list of dictionaries, each containingid,headers(dict), andbody(str). If an ID is invalid or fetching fails, anerrorkey will be present. Example:[{"id": 1, "headers": {"Subject": "..."}, "body": "..."}]
-
deleteEmailsById(ids: list)- Description: Marks specified emails for deletion based on their ID. Important: Deleting emails invalidates the current ID sequence. It's recommended to perform deletions after all necessary read operations in a given session. The actual deletion occurs when the POP3 connection is closed after the command.
- Inputs:
ids(list[int]): A list of email IDs to delete.
- Returns:
dict- A dictionary indicating which IDs were marked for deletion and which failed. Example:{"deleted": [1, 3], "failed": {2: "Error message"}}
-
sendTextEmail(fromAddress: str, toAddresses: list, subject: str, body: str)- Description: Sends a plain text email via SMTP.
- Inputs:
fromAddress(str): The email address to send from. Note: Sending may fail if this doesn't match the authenticated user, depending on SMTP server policy.toAddresses(list[str]): A list of recipient email addresses.subject(str): The email subject line.body(str): The plain text content of the email body.
- Returns:
dict- Status indication. Example:{"status": "success"}or{"error": "..."}
-
sendHtmlEmail(fromAddress: str, toAddresses: list, subject: str, body: str)- Description: Sends an HTML formatted email via SMTP.
- Inputs:
fromAddress(str): The email address to send from. Note: Sending may fail if this doesn't match the authenticated user, depending on SMTP server policy.toAddresses(list[str]): A list of recipient email addresses.subject(str): The email subject line.body(str): The HTML content of the email body.
- Returns:
dict- Status indication. Example:{"status": "success"}or{"error": "..."}
Setup and Installation
-
Prerequisites:
- Python 3.12+ installed (check
pyproject.tomlfor specific version, e.g., >=3.12). - uv installed (recommended for environment management and running).
- Python 3.12+ installed (check
-
Environment Setup & Dependencies:
- Navigate to the
email-mcp-serverdirectory in your terminal. - Create and activate a virtual environment using
uv:uv venv source .venv/bin/activate # Linux/macOS # .venv\Scripts\activate # Windows - Install dependencies using
uv(it readspyproject.tomland installsmcpandpython-dotenv):(This installs the package in editable mode, which is good practice for development).uv pip install -e . # or 'uv pip sync' if uv.lock exists
- Navigate to the
-
Configuration (
.envfile):- Create a file named
.envin theemail-mcp-serverdirectory. - Add your email credentials and server details to this file:
EMAIL_USER=YourEmailUsername@example.com EMAIL_PASS=YourEmailAppPasswordOrRegularPassword POP3_SERVER=pop.example.com POP3_PORT=995 # Optional, defaults to 995 # SMTP Configuration (Choose ONE method: STARTTLS or SSL) SMTP_SERVER=smtp.example.com # For STARTTLS (usually port 587, default method): SMTP_PORT=587 # Optional, defaults to 587 if SMTP_USE_SSL is false/unset # SMTP_USE_SSL=false # Optional, defaults to false # OR For direct SSL (usually port 465): # SMTP_PORT=465 # Optional, defaults to 465 if SMTP_USE_SSL is true # SMTP_USE_SSL=true - Security: Ensure the
.envfile is kept secure and never committed to version control (add.envto your.gitignorefile).
- Create a file named
-
Configuration (Claude Desktop):
- Add this server to your Claude Desktop developer configuration file (
developer_config.json). - Replace
/path/to/email-mcp-serverwith the full, absolute path to theemail-mcp-serverdirectory on your system. - Note: The
envsection in the JSON is now less critical if you use a.envfile, but it can still be used to override.envvariables if needed.
{ "mcpServers": { "email-mcp-server": { "command": "uv", "args": [ "--directory", "/path/to/email-mcp-server", "run", "main.py" ], "env": { # These can override .env file settings if needed # "EMAIL_USER": "...", # "EMAIL_PASS": "...", # "POP3_SERVER": "...", # "POP3_PORT": "...", # "SMTP_SERVER": "...", # "SMTP_PORT": "...", # "SMTP_USE_SSL": "false" # or "true" } } } } - Add this server to your Claude Desktop developer configuration file (
-
Restart Claude Desktop: After modifying the configuration, restart Claude Desktop for the changes to take effect. The server should connect automatically.
Important Notes
- .env File: Using a
.envfile is the recommended way to manage credentials for this server. - App Passwords: If your email provider uses Two-Factor Authentication (2FA), you will likely need to generate an "App Password" specifically for this server instead of using your regular account password. Use this App Password in your
.envfile forEMAIL_PASS. Consult your email provider's documentation (e.g., Gmail, Outlook). - Security: Ensure the
.envfile and potentially thedeveloper_config.jsonfile are kept secure. Add.envto your.gitignore. - Error Handling: The server uses the
mcplibrary's error handling. Connection errors or tool execution failures should result in appropriate MCP error responses. - Email IDs: POP3 email IDs are typically sequential numbers assigned by the server for the current session. They are only valid for the duration of that session and will change if emails are deleted. Use
pollEmailsto get current IDs before usinggetEmailsByIdordeleteEmailsById.