gmail-send-email
Send emails through Gmail by specifying recipients, subject, and body content. This tool enables email dispatch with optional CC and BCC fields for communication management.
Instructions
Send an email using Gmail
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| to | Yes | Recipient email address | |
| subject | Yes | Email subject | |
| body | Yes | Email body content | |
| cc | No | CC email addresses (comma-separated) | |
| bcc | No | BCC email addresses (comma-separated) |
Implementation Reference
- src/gmail.ts:168-211 (handler)Main handler function that constructs and sends an email via the Gmail API using base64url encoded raw email format and OAuth2 authentication.export async function sendEmail(params: z.infer<typeof sendEmailSchema>) { try { const auth = createGmailAuth(); const gmail = google.gmail({ version: "v1", auth }); // Create email content const email = []; email.push(`To: ${params.to}`); if (params.cc) email.push(`Cc: ${params.cc}`); if (params.bcc) email.push(`Bcc: ${params.bcc}`); email.push(`Subject: ${params.subject}`); email.push(""); email.push(params.body); const rawEmail = Buffer.from(email.join("\n")).toString("base64url"); const response = await gmail.users.messages.send({ userId: "me", requestBody: { raw: rawEmail, }, }); return { content: [ { type: "text" as const, text: `# Email Sent Successfully β \n\nMessage ID: \`${response.data.id}\` \nThread ID: \`${response.data.threadId}\` \nTo: ${params.to} \nSubject: ${params.subject}`, }, ], }; } catch (error) { return { content: [ { type: "text" as const, text: `Error sending email: ${ error instanceof Error ? error.message : String(error) }`, }, ], }; } }
- src/gmail.ts:5-11 (schema)Zod schema defining the input parameters for the gmail-send-email tool.export const sendEmailSchema = z.object({ to: z.string().describe("Recipient email address"), subject: z.string().describe("Email subject"), body: z.string().describe("Email body content"), cc: z.string().optional().describe("CC email addresses (comma-separated)"), bcc: z.string().optional().describe("BCC email addresses (comma-separated)"), });
- src/index.ts:182-189 (registration)Tool registration in the MCP server, linking the name, description, schema, and handler.server.tool( "gmail-send-email", "Send an email using Gmail", sendEmailSchema.shape, async (params) => { return await sendEmail(params); } );
- src/gmail.ts:134-165 (helper)Helper function to create and configure OAuth2 client for Gmail API authentication using environment variables.function createGmailAuth() { const clientId = process.env.GOOGLE_CLIENT_ID; const clientSecret = process.env.GOOGLE_CLIENT_SECRET; const redirectUri = process.env.GOOGLE_REDIRECT_URI || "http://localhost:3000/oauth2callback"; if (!clientId || !clientSecret) { throw new Error( "GOOGLE_CLIENT_ID and GOOGLE_CLIENT_SECRET are required. Run oauth-setup.js to configure." ); } const oauth2Client = new google.auth.OAuth2( clientId, clientSecret, redirectUri ); const accessToken = process.env.GOOGLE_ACCESS_TOKEN; const refreshToken = process.env.GOOGLE_REFRESH_TOKEN; if (!accessToken || !refreshToken) { throw new Error("OAuth2 tokens missing. Run oauth-setup.js to get tokens."); } oauth2Client.setCredentials({ access_token: accessToken, refresh_token: refreshToken, }); return oauth2Client; }