#!/usr/bin/env node import { StdioServerTransport } from '@modelcontextprotocol/sdk/server/stdio.js'; import { SendGridMCPServer } from './server.js'; // Validate environment const SENDGRID_API_KEY = process.env.SENDGRID_API_KEY; if (!SENDGRID_API_KEY) { console.error('āŒ ERROR: SENDGRID_API_KEY environment variable is required'); console.error(''); console.error('Get your API key from:'); console.error(' 1. Log in to SendGrid: https://app.sendgrid.com/'); console.error(' 2. Navigate to Settings > API Keys'); console.error(' 3. Create a new API key with Full Access or Mail Send permissions'); console.error(' 4. Copy the key and set: export SENDGRID_API_KEY=your_key_here'); console.error(''); console.error('Required permissions:'); console.error(' - Mail Send (for transactional emails)'); console.error(' - Marketing Campaigns (for campaigns, lists, contacts)'); console.error(' - Template Engine (for templates)'); console.error(' - Suppressions (for bounce/spam management)'); process.exit(1); } // Create server instance const server = new SendGridMCPServer({ apiKey: SENDGRID_API_KEY, }); // Graceful shutdown let isShuttingDown = false; const shutdown = async (signal: string) => { if (isShuttingDown) return; isShuttingDown = true; console.error(`\nšŸ“” Received ${signal}, shutting down SendGrid MCP server...`); try { await server.close(); console.error('āœ… Server closed gracefully'); process.exit(0); } catch (error) { console.error('āŒ Error during shutdown:', error); process.exit(1); } }; process.on('SIGINT', () => shutdown('SIGINT')); process.on('SIGTERM', () => shutdown('SIGTERM')); // Start server const transport = new StdioServerTransport(); server.connect(transport).catch((error) => { console.error('āŒ Failed to start SendGrid MCP server:', error); process.exit(1); }); console.error('šŸš€ SendGrid MCP Server running on stdio'); console.error('šŸ“§ Ready to handle email operations');