create_reply
Generate a natural reply draft for any email and return a Gmail compose URL with the draft content filled in.
Instructions
Generate a brief, natural reply draft and provide Gmail compose URL
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| messageId | Yes | Email message ID to reply to | |
| replyMessage | Yes | The reply message content to create as a draft |
Implementation Reference
- src/gmail-service.ts:149-199 (handler)The core handler: async createReply() method on GmailService. It reads the original email, constructs a reply draft using Gmail API, and returns the draft/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 }; } } - src/tools.ts:133-142 (handler)The tool dispatch case for 'create_reply' in handleToolCall(). Validates args, calls gmailService.createReply(), and formats the response text.
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\`\`\`` }] }; } - src/tools.ts:28-31 (schema)Zod schema definition for create_reply tool inputs: messageId (string) and replyMessage (string).
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)Tool definition registration via getToolDefinitions() which maps schemas to tool names/descriptions, including create_reply with its description.
export const getToolDefinitions = () => Object.entries(schemas).map(([name, schema]) => ({ name, description: toolDescriptions[name], inputSchema: zodToJsonSchema(schema) })); - src/index.ts:6-27 (helper)Entry point: starts the MCP server which registers and handles create_reply tool calls.
async function main() { // Check for command line authentication if (process.argv.includes('auth')) { try { await setupAuth(); console.error('Authentication completed successfully!'); } catch (error) { console.error('Authentication failed:', error instanceof Error ? error.message : error); process.exit(1); } process.exit(0); } // Start the MCP server await startGmailManagerServer(); } main().catch(console.error);