gmail-send-email
Send emails through Gmail by specifying recipients, subject, and body content. Use this tool to automate email dispatch from the MCP Server Boilerplate.
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)The core handler function that implements the gmail-send-email tool logic using Google Gmail API to send an email.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 sendEmail function.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 'gmail-send-email' to the sendEmail handler and schema.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 authentication client for accessing Gmail API.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; }