pbanavara/cal-mcp-server
If you are the rightful owner of cal-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 Monitor Server is designed to monitor Gmail for new emails and provide real-time notifications to connected clients using the Model Context Protocol.
MCP Email Monitor Server
A Model Context Protocol (MCP) server that monitors Gmail for new emails and sends real-time notifications to connected clients (like Cursor IDE).
š Features
- Gmail API Integrations: Monitors Gmail inbox for new messages
- MCP Protocol: Implements the Model Context Protocol for client communication
- Real-time Notifications: Sends instant notifications when new emails arrive
- Token Management: Reads OAuth tokens from the web app
- Webhook Support: Handles Gmail push notifications
- Graceful Shutdown: Proper cleanup on server termination
š Project Structure
mcp-server/
āāā src/
ā āāā types.ts # TypeScript type definitions
ā āāā token-manager.ts # JWT token management
ā āāā gmail-monitor.ts # Gmail API monitoring
ā āāā mcp-server-remote.ts # Remote MCP server with HTTP transport
ā āāā jwt-auth-middleware.ts # JWT authentication middleware
ā āāā webhook-server.ts # Gmail webhook handler
āāā scripts/
ā āāā test-jwt-auth.js # JWT authentication testing
ā āāā test-remote-mcp-client.js # Remote MCP client testing
ā āāā test-token-refresh.js # Token refresh testing
āāā dist/ # Compiled JavaScript
āāā package.json # Dependencies and scripts
āāā tsconfig.json # TypeScript configuration
āāā README.md # This file
š ļø Prerequisites
- Node.js 18+
- npm or yarn
- Google Cloud Console project with Gmail API enabled
- OAuth tokens from the web app
š¦ Installation
-
Navigate to the MCP server directory:
cd mcp-server
-
Install dependencies:
npm install
-
Build the project:
npm run build
š§ Configuration
Environment Variables
Create a .env
file in the mcp-server
directory:
# Webhook server port (for Gmail notifications)
WEBHOOK_PORT=3002
# Google Cloud project settings
GOOGLE_CLOUD_PROJECT_ID=your-project-id
GMAIL_WATCH_TOPIC=projects/your-project-id/topics/gmail-notifications
Google Cloud Setup
The Gmail watch API requires a Google Cloud Pub/Sub topic and subscription:
-
Create a Pub/Sub topic:
gcloud pubsub topics create gmail-notifications
-
Create a subscription:
gcloud pubsub subscriptions create gmail-webhook \ --topic=gmail-notifications \ --push-endpoint=http://your-domain.com/webhook/gmail
-
Update the topic name in
gmail-monitor.ts
:topicName: 'projects/your-project-id/topics/gmail-notifications'
š Usage
Starting the MCP Server
-
Ensure you have JWT tokens from the web app:
# Check if JWT tokens exist ls ../mcp-webapp/tokens/jwt_tokens.json
-
Start the MCP server:
npm start
-
For development:
npm run dev
Starting the Webhook Server
The webhook server handles Gmail push notifications:
npm run webhook
Available Scripts
npm run build # Build TypeScript to JavaScript
npm run start # Start the MCP server
npm run dev # Start in development mode (mcp-server-remote.ts)
npm run watch # Watch for changes and rebuild
npm run clean # Clean build artifacts
npm run webhook # Start webhook server
š MCP Protocol
The server implements the MCP protocol with the following capabilities:
Notifications
- Subscribe: Clients can subscribe to email notifications
- Unsubscribe: Clients can unsubscribe from notifications
- Notify: Server sends notifications when new emails arrive
Notification Format
{
"type": "email_received",
"data": {
"messageId": "gmail-message-id",
"subject": "Email Subject",
"sender": "sender@example.com",
"receivedAt": "2024-01-01T12:00:00.000Z",
"snippet": "Email snippet..."
}
}
š§ Gmail Monitoring
How It Works
- Initialization: Server reads OAuth tokens from web app storage
- Watch Setup: Starts Gmail watch API with Pub/Sub topic
- Message Detection: Monitors for new messages via webhook or polling
- Notification: Sends MCP notifications to connected clients
Monitoring Methods
- Webhook (Recommended): Real-time push notifications via Pub/Sub
- Polling (Fallback): Periodic checks every 30 seconds
š Authentication
The server uses JWT authentication and reads Google OAuth tokens from the web app's storage:
- JWT Token: Required for MCP server authentication
- Google Token Location:
../mcp-webapp/tokens/jwt_tokens.json
- Token Format: Google OAuth 2.0 tokens with refresh capability
- Auto-refresh: Handled by the Google APIs library
š Troubleshooting
Common Issues
-
"No tokens available"
- Ensure you've authenticated in the web app first
- Check that
jwt_tokens.json
exists in the web app tokens directory
-
"Token is expired"
- Re-authenticate in the web app
- The server will automatically use refreshed tokens
-
"Failed to start Gmail watch"
- Check Google Cloud Pub/Sub setup
- Verify topic name and permissions
- Ensure webhook endpoint is publicly accessible
-
"Webhook not receiving notifications"
- Check webhook server is running
- Verify Pub/Sub subscription configuration
- Test webhook endpoint accessibility
Debug Mode
Enable debug logging:
DEBUG=* npm run dev
š Integration with Web App
The MCP server integrates with the web app:
- Token Sharing: Reads OAuth tokens from web app storage
- Launch Integration: Web app can launch the MCP server
- Status Monitoring: Web app can check MCP server status
š API Reference
Server Status
interface ServerStatus {
isRunning: boolean;
clientCount: number;
gmailStatus: {
isWatching: boolean;
currentHistoryId: string | null;
watchExpiration: string | null;
};
}
Gmail Monitor Status
interface GmailStatus {
isWatching: boolean;
currentHistoryId: string | null;
watchExpiration: string | null;
}
š Deployment
Local Development
npm run dev
Production
npm run build
npm start
Docker (Future)
FROM node:18-alpine
WORKDIR /app
COPY package*.json ./
RUN npm ci --only=production
COPY dist ./dist
CMD ["npm", "start"]
š® Future Enhancements
- Meeting Detection: AI-powered meeting email detection
- Email Filtering: Configurable email filters
- Multiple Accounts: Support for multiple Gmail accounts
- Analytics: Email monitoring statistics
- Web Dashboard: Real-time monitoring dashboard
š License
This project is licensed under the MIT License.
Built for seamless email monitoring and real-time notifications in development environments