Post

MCP Email Server Setup Guide

MCP Email Server Setup Guide

MCP Email Server Setup Guide

This guide provides a step-by-step installation of an MCP (Model Context Protocol) Email Server on Ubuntu using Node.js and AWS SES.


Step 1: Install Required Dependencies

Before starting, ensure your system is up-to-date and install Node.js along with npm.

1
2
sudo apt update && sudo apt upgrade -y
sudo apt install -y nodejs npm

Verify the installations:

1
2
node -v
npm -v

Step 2: Set Up the MCP Server Directory

Create and navigate to the project directory:

1
mkdir mcp-server && cd mcp-server

Step 3: Initialize the Node.js Project

Initialize a new Node.js project:

1
npm init -y

Step 4: Install Necessary Packages

Install the required packages:

1
npm install express nodemailer dotenv

Step 5: Configure Environment Variables

Create a .env file to store environment variables:

1
nano .env

Add the following content:

1
2
3
4
5
SMTP_HOST=email-smtp.us-east-1.amazonaws.com  #Update to your region
SMTP_PORT=587
SMTP_USER=your-smtp-username
SMTP_PASS=your-smtp-password
EMAIL_FROM=[email protected]

Save & exit (CTRL + X, then Y, then ENTER).


Step 6: Develop the MCP Email Server

Create a server.js file:

1
nano server.js

Paste the following code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
require('dotenv').config();
const express = require('express');
const nodemailer = require('nodemailer');

const app = express();
app.use(express.json());

const transporter = nodemailer.createTransport({
    host: process.env.SMTP_HOST,
    port: process.env.SMTP_PORT,
    auth: {
        user: process.env.SMTP_USER,
        pass: process.env.SMTP_PASS
    }
});

app.post('/send-email', async (req, res) => {
    const { to, subject, text } = req.body;

    if (!to || !subject || !text) {
        return res.status(400).json({ error: "Missing required fields: to, subject, text" });
    }

    try {
        const mailOptions = {
            from: process.env.EMAIL_FROM,
            to,
            subject,
            text
        };

        await transporter.sendMail(mailOptions);
        res.status(200).json({ message: "Email sent successfully!" });
    } catch (error) {
        res.status(500).json({ error: "Failed to send email", details: error.message });
    }
});

const PORT = process.env.PORT || 3001;
app.listen(PORT, () => {
    console.log(`MCP Email Server running on port ${PORT}`);
});

Save & exit (CTRL + X, then Y, then ENTER).


Step 7: Run the MCP Email Server

Start the server:

1
node server.js

You should see:

1
MCP Email Server running on port 3001

Step 8: Install PM2 for Process Management

Install PM2 globally to manage the server process:

1
sudo npm install -g pm2

Step 9: Run the Server with PM2

Start the server using PM2:

1
pm2 start server.js --name mcp-server

Save the PM2 process list:

1
pm2 save

Set PM2 to start on system boot:

1
pm2 startup

Step 10: Test the MCP Email Server

Use curl to test the email endpoint:

1
2
3
4
5
6
7
curl -X POST http://localhost:3001/send-email \
-H "Content-Type: application/json" \
-d '{
  "to": "[email protected]",
  "subject": "Test Email",
  "text": "Hello! This is a test email from the MCP server."
}'

If configured correctly, you should receive the test email.


Note: Ensure that your AWS SES credentials are correctly set in the .env file and that the email addresses used are verified in your AWS SES account.

This post is licensed under CC BY 4.0 by the author.