MCP Email Server with SendGrid

MCP Email Server with SendGrid

An MCP server that enables AI applications to send text, HTML, and template-based emails using SendGrid. It supports file attachments, CC/BCC recipients, and easy deployment via Python or Docker.

Category
访问服务器

README

✉️ MCP Email Server with SendGrid

A Model Context Protocol (MCP) server that provides email sending capabilities through SendGrid. This server enables AI applications like Claude Desktop, Langflow, and other MCP clients to send emails programmatically.

🚀 Features

  • 📧 Basic Email Sending - Send text or HTML emails
  • 🎨 Template Support - Use SendGrid dynamic templates
  • 📎 File Attachments - Attach files to emails
  • 👥 CC/BCC Support - Send copies to multiple recipients
  • 🐳 Docker Ready - Containerized deployment
  • 🔒 Secure - Environment-based configuration

📋 Prerequisites

  • Python 3.10 or higher
  • SendGrid account and API key (Sign up here)
  • Verified sender email address in SendGrid
  • Docker (optional, for containerized deployment)

🛠️ Installation

Option 1: Local Python Setup

  1. Clone or navigate to the repository:

    cd d:\repos\mcp_mail
    
  2. Create and activate virtual environment:

    python -m venv venv
    .\venv\Scripts\activate  # Windows
    
  3. Install dependencies:

    pip install -e .
    
  4. Configure environment variables:

    copy .env.example .env
    

    Edit .env and add your credentials:

    SENDGRID_API_KEY=your_actual_sendgrid_api_key
    DEFAULT_FROM_EMAIL=your-verified-email@example.com
    DEFAULT_FROM_NAME=Your Name
    

Option 2: Docker Deployment

  1. Configure environment:

    copy .env.example .env
    # Edit .env with your credentials
    
  2. Build and run with Docker Compose:

    docker-compose up -d
    

    Or build manually:

    docker build -t mcp-email-server .
    docker run --env-file .env mcp-email-server
    

🔧 Configuration

SendGrid Setup

  1. Create a SendGrid account at sendgrid.com

  2. Generate an API key:

    • Go to Settings → API Keys
    • Click "Create API Key"
    • Select "Full Access" or "Restricted Access" with Mail Send permissions
    • Copy the API key (you'll only see it once!)
  3. Verify sender email:

    • Go to Settings → Sender Authentication
    • Verify the email address you'll use as the sender
    • This is required by SendGrid to prevent spam

Environment Variables

Variable Required Description
SENDGRID_API_KEY Yes Your SendGrid API key
DEFAULT_FROM_EMAIL Yes Default sender email (must be verified in SendGrid)
DEFAULT_FROM_NAME No Default sender name displayed to recipients

📖 Usage

Running the Server

Local:

python server.py

Docker:

docker-compose up

The server communicates via stdio (standard input/output) using the MCP protocol.

Available MCP Tools

1. send_email

Send a basic email with text or HTML content.

Parameters:

  • to_email (required): Recipient email address
  • subject (required): Email subject line
  • body (required): Email content (text or HTML)
  • from_email (optional): Sender email (uses DEFAULT_FROM_EMAIL if not provided)
  • from_name (optional): Sender name
  • cc_emails (optional): List of CC recipients
  • bcc_emails (optional): List of BCC recipients
  • is_html (optional): Set to true for HTML emails (default: false)

Example:

{
  "to_email": "recipient@example.com",
  "subject": "Hello from MCP",
  "body": "<h1>Welcome!</h1><p>This is a test email.</p>",
  "is_html": true
}

2. send_email_with_template

Send an email using a SendGrid dynamic template.

Parameters:

  • to_email (required): Recipient email address
  • template_id (required): SendGrid template ID
  • dynamic_data (required): Dictionary of template variables
  • from_email (optional): Sender email
  • from_name (optional): Sender name
  • subject (optional): Override template subject

Example:

{
  "to_email": "user@example.com",
  "template_id": "d-1234567890abcdef",
  "dynamic_data": {
    "username": "John",
    "action_url": "https://example.com/verify"
  }
}

3. send_email_with_attachments

Send an email with file attachments.

Parameters:

  • to_email (required): Recipient email address
  • subject (required): Email subject line
  • body (required): Email content
  • attachment_paths (required): List of file paths to attach
  • from_email (optional): Sender email
  • from_name (optional): Sender name
  • is_html (optional): HTML email flag

Example:

{
  "to_email": "recipient@example.com",
  "subject": "Report Attached",
  "body": "Please find the report attached.",
  "attachment_paths": ["/path/to/report.pdf", "/path/to/data.csv"]
}

🔌 Integration Examples

Claude Desktop

Add to your Claude Desktop config file (claude_desktop_config.json):

{
  "mcpServers": {
    "email": {
      "command": "python",
      "args": ["d:\\repos\\mcp_mail\\server.py"],
      "env": {
        "SENDGRID_API_KEY": "your_api_key",
        "DEFAULT_FROM_EMAIL": "your-email@example.com",
        "DEFAULT_FROM_NAME": "Your Name"
      }
    }
  }
}

Or using Docker:

{
  "mcpServers": {
    "email": {
      "command": "docker",
      "args": ["run", "-i", "--rm", "--env-file", "d:\\repos\\mcp_mail\\.env", "mcp-email-server"]
    }
  }
}

Langflow Integration

  1. Install an MCP client component in Langflow (if available) or use a custom Python component
  2. Configure the MCP server connection with the email server's stdio transport
  3. Call the email tools from your Langflow flows

See examples/langflow_integration.md for detailed instructions.

Python Client Example

from mcp import ClientSession, StdioServerParameters
from mcp.client.stdio import stdio_client

server_params = StdioServerParameters(
    command="python",
    args=["d:/repos/mcp_mail/server.py"],
    env={
        "SENDGRID_API_KEY": "your_key",
        "DEFAULT_FROM_EMAIL": "your@email.com"
    }
)

async with stdio_client(server_params) as (read, write):
    async with ClientSession(read, write) as session:
        await session.initialize()
        
        # Call the send_email tool
        result = await session.call_tool("send_email", {
            "to_email": "recipient@example.com",
            "subject": "Test Email",
            "body": "Hello from MCP!",
            "is_html": False
        })
        
        print(result)

🧪 Testing

Run the test script to verify functionality:

python test_server.py

This will test the email sending functions with mocked SendGrid responses.

🐛 Troubleshooting

"SendGrid client not initialized"

  • Ensure SENDGRID_API_KEY is set in your .env file
  • Verify the API key is valid and has Mail Send permissions

"403 Forbidden" error

  • Your sender email address must be verified in SendGrid
  • Go to SendGrid Settings → Sender Authentication

"No from_email provided"

  • Set DEFAULT_FROM_EMAIL in your .env file, or
  • Provide from_email parameter in each request

Attachments not working

  • Ensure the file paths are absolute and accessible
  • Check file permissions
  • Verify files exist at the specified paths

📚 Resources

📄 License

MIT License - feel free to use this in your projects!

🤝 Contributing

Contributions welcome! Feel free to submit issues or pull requests.

推荐服务器

Baidu Map

Baidu Map

百度地图核心API现已全面兼容MCP协议,是国内首家兼容MCP协议的地图服务商。

官方
精选
JavaScript
Playwright MCP Server

Playwright MCP Server

一个模型上下文协议服务器,它使大型语言模型能够通过结构化的可访问性快照与网页进行交互,而无需视觉模型或屏幕截图。

官方
精选
TypeScript
Magic Component Platform (MCP)

Magic Component Platform (MCP)

一个由人工智能驱动的工具,可以从自然语言描述生成现代化的用户界面组件,并与流行的集成开发环境(IDE)集成,从而简化用户界面开发流程。

官方
精选
本地
TypeScript
Audiense Insights MCP Server

Audiense Insights MCP Server

通过模型上下文协议启用与 Audiense Insights 账户的交互,从而促进营销洞察和受众数据的提取和分析,包括人口统计信息、行为和影响者互动。

官方
精选
本地
TypeScript
VeyraX

VeyraX

一个单一的 MCP 工具,连接你所有喜爱的工具:Gmail、日历以及其他 40 多个工具。

官方
精选
本地
graphlit-mcp-server

graphlit-mcp-server

模型上下文协议 (MCP) 服务器实现了 MCP 客户端与 Graphlit 服务之间的集成。 除了网络爬取之外,还可以将任何内容(从 Slack 到 Gmail 再到播客订阅源)导入到 Graphlit 项目中,然后从 MCP 客户端检索相关内容。

官方
精选
TypeScript
Kagi MCP Server

Kagi MCP Server

一个 MCP 服务器,集成了 Kagi 搜索功能和 Claude AI,使 Claude 能够在回答需要最新信息的问题时执行实时网络搜索。

官方
精选
Python
e2b-mcp-server

e2b-mcp-server

使用 MCP 通过 e2b 运行代码。

官方
精选
Neon MCP Server

Neon MCP Server

用于与 Neon 管理 API 和数据库交互的 MCP 服务器

官方
精选
Exa MCP Server

Exa MCP Server

模型上下文协议(MCP)服务器允许像 Claude 这样的 AI 助手使用 Exa AI 搜索 API 进行网络搜索。这种设置允许 AI 模型以安全和受控的方式获取实时的网络信息。

官方
精选