Skip to main content
Glama

create_reply

Generate Gmail reply drafts with a compose URL to respond to specific emails quickly.

Instructions

Generate a brief, natural reply draft and provide Gmail compose URL

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
messageIdYesEmail message ID to reply to
replyMessageYesThe reply message content to create as a draft

Implementation Reference

  • The handler for the 'create_reply' tool within the main tool dispatcher. Validates input using the schema and calls GmailService.createReply, then formats a response with draft preview including Gmail draft URL.
    case "create_reply": {
        const v = validated as z.infer<typeof schemas.create_reply>;
        const result = await gmailService.createReply(v.messageId, v.replyMessage);
        return { 
            content: [{ 
                type: "text", 
                text: `${result.message}\n\n**Draft Preview:**\n\n**To:** ${result.to}\n**Subject:** ${result.subject}\n\n**Message:**\n\`\`\`\n${result.replyMessage}\n\`\`\`` 
            }]
        };
    }
  • Zod schema defining the input parameters for the 'create_reply' tool: messageId and replyMessage.
    create_reply: z.object({
        messageId: z.string().describe("Email message ID to reply to"),
        replyMessage: z.string().describe("The reply message content to create as a draft")
    }),
  • src/tools.ts:50-55 (registration)
    The registration function that exports tool definitions for all tools, including 'create_reply' with its schema and description, for MCP protocol.
    export const getToolDefinitions = () => 
        Object.entries(schemas).map(([name, schema]) => ({
            name,
            description: toolDescriptions[name],
            inputSchema: zodToJsonSchema(schema)
        }));
  • Core implementation of reply creation: reads original email, constructs Re: subject and raw email message, creates Gmail draft in the thread, provides draft URL or fallback compose URL.
    async createReply(messageId: string, replyMessage: string): Promise<{message: string, to: string, subject: string, replyMessage: string}> {
        const email = await this.readEmail(messageId);
    
        // Create email message for draft
        const subject = email.subject.startsWith('Re: ') ? email.subject : `Re: ${email.subject}`;
        const to = email.from;
        const inReplyTo = email.id;
        const references = email.threadId;
    
        const emailContent = [
            `To: ${to}`,
            `Subject: ${subject}`,
            `In-Reply-To: ${inReplyTo}`,
            `References: ${references}`,
            '',
            replyMessage
        ].join('\n');
    
        const encodedMessage = Buffer.from(emailContent).toString('base64').replace(/\+/g, '-').replace(/\//g, '_').replace(/=+$/, '');
    
        try {
            const { data: draft } = await this.gmail.users.drafts.create({
                userId: 'me',
                requestBody: {
                    message: {
                        threadId: email.threadId,
                        raw: encodedMessage
                    }
                }
            });
    
            const draftUrl = `https://mail.google.com/mail/u/0/#drafts/${draft.id}`;
            
            return {
                message: `Reply draft created and saved to Gmail drafts.\n\n**Gmail draft URL:** ${draftUrl}`,
                to,
                subject,
                replyMessage
            };
        } catch (error) {
            console.error('Failed to create draft:', error);
            // Fallback to compose URL
            const gmailComposeUrl = `https://mail.google.com/mail/?view=cm&fs=1&to=${encodeURIComponent(to)}&su=${encodeURIComponent(subject)}&body=${encodeURIComponent(replyMessage)}`;
            return {
                message: `Failed to create draft. Gmail compose URL: ${gmailComposeUrl}`,
                to,
                subject, 
                replyMessage
            };
        }
    }

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/muammar-yacoob/GMail-Manager-MCP'

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