Spaces:
Sleeping
Sleeping
File size: 2,158 Bytes
7dc28be | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 | import type { FastMCP } from 'fastmcp';
import { UserError } from 'fastmcp';
import { z } from 'zod';
import { getGmailClient } from '../../clients.js';
import { findHeaderValue, extractMessageBody } from './helpers.js';
export function register(server: FastMCP) {
server.addTool({
name: 'getDraft',
description:
'Fetches a single Gmail draft by ID with full headers and body. Use listDrafts to discover draft IDs.',
parameters: z.strictObject({
draftId: z.string().describe('The Gmail draft ID, typically from listDrafts results.'),
}),
execute: async (args, { log }) => {
const gmail = await getGmailClient();
log.info(`Getting Gmail draft ${args.draftId}`);
try {
const response = await gmail.users.drafts.get({
userId: 'me',
id: args.draftId,
format: 'full',
});
const draft = response.data;
const msg = draft.message;
const headers = msg?.payload?.headers;
const { text, html } = extractMessageBody(msg?.payload);
return JSON.stringify(
{
draftId: draft.id,
messageId: msg?.id,
threadId: msg?.threadId,
labelIds: msg?.labelIds ?? [],
snippet: msg?.snippet ?? '',
headers: {
from: findHeaderValue(headers, 'From'),
to: findHeaderValue(headers, 'To'),
cc: findHeaderValue(headers, 'Cc'),
bcc: findHeaderValue(headers, 'Bcc'),
subject: findHeaderValue(headers, 'Subject'),
date: findHeaderValue(headers, 'Date'),
},
body: { text, html },
},
null,
2
);
} catch (error: any) {
log.error(`Error getting draft: ${error.message || error}`);
if (error.code === 404) throw new UserError(`Draft not found (ID: ${args.draftId}).`);
if (error.code === 403)
throw new UserError('Permission denied. Confirm the gmail.modify scope was granted.');
throw new UserError(`Failed to get draft: ${error.message || 'Unknown error'}`);
}
},
});
}
|