Provides tools for sending emails, reading emails, listing emails with filters, and managing Gmail labels through the Gmail API.
Enables execution of SQL queries (SELECT, INSERT, UPDATE, DELETE), database schema inspection, and table management for PostgreSQL databases.
MCP Gmail & PostgreSQL Server
A unified Model Context Protocol (MCP) server that provides AI agents with read and write access to Gmail and PostgreSQL databases.
Features
Gmail Integration: Send emails, read emails, list emails, and manage labels
PostgreSQL Integration: Execute queries, read/write operations, and schema inspection
Modular Architecture: Cleanly separated services for easy maintenance
Environment-based Configuration: Secure credential management via
.envfile
Prerequisites
Node.js 18+
npm or yarn
Gmail API credentials (Google Cloud Console)
PostgreSQL database
Installation
Clone and install dependencies:
Build the project:
Configuration
Step 1: Create .env file
Copy the example file and fill in your credentials:
Step 2: Configure Gmail
Get Gmail API Credentials
Go to Google Cloud Console
Create a new project or select an existing one
Enable the Gmail API:
Navigate to "APIs & Services" > "Library"
Search for "Gmail API" and click "Enable"
Create OAuth 2.0 credentials:
Go to "APIs & Services" > "Credentials"
Click "Create Credentials" > "OAuth client ID"
Choose "Desktop app" as application type
Download the credentials JSON file
Get your Client ID and Client Secret from the credentials
Get a Refresh Token:
Run this script to authorize and get a refresh token:
node scripts/get-gmail-token.jsOr use Google's OAuth 2.0 Playground:
Click the gear icon (⚙️) and check "Use your own OAuth credentials"
Enter your Client ID and Client Secret
In the left panel, select "Gmail API v1" and check the scopes:
https://www.googleapis.com/auth/gmail.readonlyhttps://www.googleapis.com/auth/gmail.sendhttps://www.googleapis.com/auth/gmail.modify
Click "Authorize APIs" and sign in
Click "Exchange authorization code for tokens"
Copy the "Refresh token" value
Add to
.env:
Step 3: Configure PostgreSQL
Add your PostgreSQL connection details to .env:
Usage
Development Mode
Production Mode
The server runs on stdio and communicates via JSON-RPC, following the MCP protocol.
Available Tools
Gmail Tools
gmail_list_emails: List emails from inbox with optional filtersParameters:
query,maxResults,labelIds
gmail_read_email: Read a specific email by message IDParameters:
messageId,format
gmail_send_email: Send an emailParameters:
to,subject,body,htmlBody(optional),cc(optional),bcc(optional)
gmail_get_labels: Get all Gmail labels
PostgreSQL Tools
postgres_query: Execute SELECT queries (read-only)Parameters:
query,params(optional array)
postgres_execute: Execute write operations (INSERT, UPDATE, DELETE)Parameters:
query,params(optional array)
postgres_get_tables: List all tables in the databaseParameters:
schema(optional, default: 'public')
postgres_get_table_schema: Get schema information for a tableParameters:
tableName,schema(optional, default: 'public')
Deployment to VPS
Transfer files to your VPS:
SSH into your VPS and install:
Create with your credentials
Run as a service (using PM2 or systemd):
With PM2:
With systemd, create /etc/systemd/system/mcp-server.service:
Then:
Project Structure
Security Notes
Never commit
.envfile to version controlUse strong database passwords
Keep Gmail OAuth credentials secure
Consider using environment-specific credentials for production
Regularly rotate refresh tokens and passwords
Troubleshooting
Gmail not working
Verify your refresh token is valid and not expired
Check that all required OAuth scopes are granted
Ensure
GMAIL_CLIENT_IDandGMAIL_CLIENT_SECRETare correct
PostgreSQL connection issues
Verify database credentials are correct
Check if PostgreSQL is running and accessible
For remote connections, ensure firewall rules allow connections
If using SSL, set
POSTGRES_SSL=truein.env
License
MIT
This server cannot be installed
remote-capable server
The server can be hosted and run remotely because it primarily relies on remote services or has no dependency on the local environment.
Provides AI agents with unified access to Gmail for email management (send, read, list, labels) and PostgreSQL databases for executing queries and schema inspection.