Gmail & PostgreSQL MCP Server
Provides AI agents with unified access to Gmail for email management (send, read, list, labels) and PostgreSQL databases for executing queries and schema inspection.
README
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.
🚀 Two Modes Available
1. stdio Mode (for Claude Desktop)
- Direct integration with Claude Desktop
- Uses standard input/output
- No network configuration needed
2. HTTP/SSE Mode (for n8n, remote agents)
- HTTP server with Server-Sent Events (SSE)
- Can be deployed to VPS
- Accessible via URL
- Perfect for n8n AI Agent with MCP Client connector
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:
npm install
- Build the project:
npm run build
Configuration
Step 1: Create .env file
Copy the example file and fill in your credentials:
cp .env.example .env
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.js- Or use Google's OAuth 2.0 Playground:
- Go to https://developers.google.com/oauthplayground/
- 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:
GMAIL_CLIENT_ID=your_client_id_from_step_5
GMAIL_CLIENT_SECRET=your_client_secret_from_step_5
GMAIL_REFRESH_TOKEN=your_refresh_token_from_step_6
Step 3: Configure PostgreSQL
Add your PostgreSQL connection details to .env:
POSTGRES_HOST=localhost # or your database host
POSTGRES_PORT=5432 # default PostgreSQL port
POSTGRES_DATABASE=your_db_name # your database name
POSTGRES_USER=your_db_user # your database user
POSTGRES_PASSWORD=your_password # your database password
POSTGRES_SSL=false # set to true if using SSL
Usage
For n8n (HTTP Mode) - RECOMMENDED
Start the HTTP server:
# Development
npm run dev:http
# Production
npm run build
npm run start:http
Server will be available at: http://localhost:3000/sse
See N8N_SETUP.md for complete n8n integration guide.
For Claude Desktop (stdio Mode)
# Development
npm run dev
# Production
npm start
Configure in Claude Desktop's config file (see Configuration section below).
Available Tools
Gmail Tools
gmail_list_emails: List emails from inbox with optional filters- Parameters:
query,maxResults,labelIds
- Parameters:
gmail_read_email: Read a specific email by message ID- Parameters:
messageId,format
- Parameters:
gmail_send_email: Send an email- Parameters:
to,subject,body,htmlBody(optional),cc(optional),bcc(optional)
- Parameters:
gmail_get_labels: Get all Gmail labels
PostgreSQL Tools
postgres_query: Execute SELECT queries (read-only)- Parameters:
query,params(optional array)
- Parameters:
postgres_execute: Execute write operations (INSERT, UPDATE, DELETE)- Parameters:
query,params(optional array)
- Parameters:
postgres_get_tables: List all tables in the database- Parameters:
schema(optional, default: 'public')
- Parameters:
postgres_get_table_schema: Get schema information for a table- Parameters:
tableName,schema(optional, default: 'public')
- Parameters:
Deployment to VPS
- Transfer files to your VPS:
scp -r . user@your-vps-ip:/path/to/mcp-server/
- SSH into your VPS and install:
ssh user@your-vps-ip
cd /path/to/mcp-server
npm install
npm run build
-
Create
.envfile on VPS with your credentials -
Run as a service (using PM2 or systemd):
With PM2:
npm install -g pm2
pm2 start npm --name "mcp-server" -- start
pm2 save
With systemd, create /etc/systemd/system/mcp-server.service:
[Unit]
Description=MCP Gmail & PostgreSQL Server
After=network.target
[Service]
Type=simple
User=your-user
WorkingDirectory=/path/to/mcp-server
Environment=NODE_ENV=production
ExecStart=/usr/bin/node dist/index.js
Restart=always
[Install]
WantedBy=multi-user.target
Then:
sudo systemctl enable mcp-server
sudo systemctl start mcp-server
Project Structure
.
├── src/
│ ├── index.ts # Main server entry point
│ └── services/
│ ├── gmail.service.ts # Gmail integration
│ └── postgres.service.ts # PostgreSQL integration
├── dist/ # Compiled JavaScript
├── .env # Environment variables (not in git)
├── .env.example # Example environment file
├── package.json
├── tsconfig.json
└── README.md
Security Notes
- Never commit
.envfile to version control - Use 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
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.