gmail-send-email
Send emails through Gmail by specifying recipients, subject, and body content. Supports CC and BCC for managing email distribution.
Instructions
Send an email using Gmail
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| bcc | No | BCC email addresses (comma-separated) | |
| body | Yes | Email body content | |
| cc | No | CC email addresses (comma-separated) | |
| subject | Yes | Email subject | |
| to | Yes | Recipient email address |
Input Schema (JSON Schema)
{
"properties": {
"bcc": {
"description": "BCC email addresses (comma-separated)",
"type": "string"
},
"body": {
"description": "Email body content",
"type": "string"
},
"cc": {
"description": "CC email addresses (comma-separated)",
"type": "string"
},
"subject": {
"description": "Email subject",
"type": "string"
},
"to": {
"description": "Recipient email address",
"type": "string"
}
},
"required": [
"to",
"subject",
"body"
],
"type": "object"
}
Implementation Reference
- src/gmail.ts:168-211 (handler)The handler function that executes the gmail-send-email tool: authenticates with Gmail API, constructs the raw email, sends it, and returns success/error response.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 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)Registration of the gmail-send-email tool on the MCP server, linking 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 authentication client for Gmail API access, used by the sendEmail handler.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; }