Skip to main content
Glama

send_message

Send email messages to recipients with options for CC, BCC, subject, and body content through the Gmail MCP server.

Instructions

Send an email message to specified recipients. Note the mechanics of the raw parameter.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
rawNoThe entire email message in base64url encoded RFC 2822 format, ignores params.to, cc, bcc, subject, body, includeBodyHtml if provided
threadIdNoThe thread ID to associate this message with
toNoList of recipient email addresses
ccNoList of CC recipient email addresses
bccNoList of BCC recipient email addresses
subjectNoThe subject of the email
bodyNoThe body of the email
includeBodyHtmlNoWhether to include the parsed HTML in the return for each body, excluded by default because they can be excessively large

Implementation Reference

  • Handler function for the 'send_message' tool. Handles input parameters, constructs raw message if needed using constructRawMessage, sends via Gmail API's users.messages.send, processes payload with processMessagePart, and formats response.
    async (params) => { return handleTool(config, async (gmail: gmail_v1.Gmail) => { let raw = params.raw if (!raw) raw = await constructRawMessage(gmail, params) const messageSendParams: MessageSendParams = { userId: 'me', requestBody: { raw } } if (params.threadId && messageSendParams.requestBody) { messageSendParams.requestBody.threadId = params.threadId } const { data } = await gmail.users.messages.send(messageSendParams) if (data.payload) { data.payload = processMessagePart( data.payload, params.includeBodyHtml ) } return formatResponse(data) }) }
  • Input schema (Zod) for the 'send_message' tool defining parameters like raw, threadId, to, cc, bcc, subject, body, and includeBodyHtml.
    raw: z.string().optional().describe("The entire email message in base64url encoded RFC 2822 format, ignores params.to, cc, bcc, subject, body, includeBodyHtml if provided"), threadId: z.string().optional().describe("The thread ID to associate this message with"), to: z.array(z.string()).optional().describe("List of recipient email addresses"), cc: z.array(z.string()).optional().describe("List of CC recipient email addresses"), bcc: z.array(z.string()).optional().describe("List of BCC recipient email addresses"), subject: z.string().optional().describe("The subject of the email"), body: z.string().optional().describe("The body of the email"), includeBodyHtml: z.boolean().optional().describe("Whether to include the parsed HTML in the return for each body, excluded by default because they can be excessively large")
  • src/index.ts:607-641 (registration)
    Registration of the 'send_message' tool on the MCP server using server.tool(), providing name, description, input schema, and handler function.
    server.tool("send_message", "Send an email message to specified recipients. Note the mechanics of the raw parameter.", { raw: z.string().optional().describe("The entire email message in base64url encoded RFC 2822 format, ignores params.to, cc, bcc, subject, body, includeBodyHtml if provided"), threadId: z.string().optional().describe("The thread ID to associate this message with"), to: z.array(z.string()).optional().describe("List of recipient email addresses"), cc: z.array(z.string()).optional().describe("List of CC recipient email addresses"), bcc: z.array(z.string()).optional().describe("List of BCC recipient email addresses"), subject: z.string().optional().describe("The subject of the email"), body: z.string().optional().describe("The body of the email"), includeBodyHtml: z.boolean().optional().describe("Whether to include the parsed HTML in the return for each body, excluded by default because they can be excessively large") }, async (params) => { return handleTool(config, async (gmail: gmail_v1.Gmail) => { let raw = params.raw if (!raw) raw = await constructRawMessage(gmail, params) const messageSendParams: MessageSendParams = { userId: 'me', requestBody: { raw } } if (params.threadId && messageSendParams.requestBody) { messageSendParams.requestBody.threadId = params.threadId } const { data } = await gmail.users.messages.send(messageSendParams) if (data.payload) { data.payload = processMessagePart( data.payload, params.includeBodyHtml ) } return formatResponse(data) }) } )
  • Shared helper function handleTool used by send_message (and other tools) to manage OAuth2 client creation, credential validation, Gmail client setup, execute the API call, and handle errors.
    const handleTool = async (queryConfig: Record<string, any> | undefined, apiCall: (gmail: gmail_v1.Gmail) => Promise<any>) => { try { const oauth2Client = queryConfig ? createOAuth2Client(queryConfig) : defaultOAuth2Client if (!oauth2Client) throw new Error('OAuth2 client could not be created, please check your credentials') const credentialsAreValid = await validateCredentials(oauth2Client) if (!credentialsAreValid) throw new Error('OAuth2 credentials are invalid, please re-authenticate') const gmailClient = queryConfig ? google.gmail({ version: 'v1', auth: oauth2Client }) : defaultGmailClient if (!gmailClient) throw new Error('Gmail client could not be created, please check your credentials') const result = await apiCall(gmailClient) return result } catch (error: any) { return `Tool execution failed: ${error.message}` } }
  • Helper function constructRawMessage that builds the RFC 2822 formatted raw email message in base64url encoding, handling headers, body, thread quoting, etc., called by send_message if raw not provided.
    const constructRawMessage = async (gmail: gmail_v1.Gmail, params: NewMessage) => { let thread: Thread | null = null if (params.threadId) { const threadParams = { userId: 'me', id: params.threadId, format: 'full' } const { data } = await gmail.users.threads.get(threadParams) thread = data } const message = [] if (params.to?.length) message.push(`To: ${wrapTextBody(params.to.join(', '))}`) if (params.cc?.length) message.push(`Cc: ${wrapTextBody(params.cc.join(', '))}`) if (params.bcc?.length) message.push(`Bcc: ${wrapTextBody(params.bcc.join(', '))}`) if (thread) { message.push(...getThreadHeaders(thread).map(header => wrapTextBody(header))) } else if (params.subject) { message.push(`Subject: ${wrapTextBody(params.subject)}`) } else { message.push('Subject: (No Subject)') } message.push('Content-Type: text/plain; charset="UTF-8"') message.push('Content-Transfer-Encoding: quoted-printable') message.push('MIME-Version: 1.0') message.push('') if (params.body) message.push(wrapTextBody(params.body)) if (thread) { const quotedContent = getQuotedContent(thread) if (quotedContent) { message.push('') message.push(wrapTextBody(quotedContent)) } } return Buffer.from(message.join('\r\n')).toString('base64url').replace(/\+/g, '-').replace(/\//g, '_').replace(/=+$/, '') }

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/HitmanLy007/gmail-mcp'

If you have feedback or need assistance with the MCP directory API, please join our Discord server