@emit-ia/gdrive-mcp-server
Version:
Google Drive MCP Server - Model Context Protocol server for Google Drive and Gmail APIs
223 lines • 7.72 kB
TypeScript
/**
* Gmail Service for MCP Server
*
* This service provides Gmail operations using OAuth2 authentication with automatic
* token management to prevent the common 6-month expiration issues. It includes
* message listing, sending, reading, searching, and profile management.
*/
/**
* Gmail service class that handles all Gmail API operations
*
* Uses OAuth2 authentication (required for personal Gmail access) with automatic
* token refresh to maintain long-term connectivity without user intervention.
*/
export declare class GmailService {
private gmail;
private auth;
private lastTokenRefresh;
private tokenRefreshInterval;
private requestQueue;
private isProcessingQueue;
private lastRequestTime;
private requestCount;
private quotaResetTime;
constructor();
/**
* Initialize OAuth2 authentication for Gmail
*
* OAuth2 is required for personal Gmail access (Service Accounts don't work
* well with personal Gmail accounts). This sets up the auth client with
* broader scopes for full message access and batch processing capabilities.
*/
private initializeAuth;
/**
* Set up automatic token maintenance to prevent Gmail token expiration
*
* This is crucial for long-running applications as Gmail tokens typically
* expire after 6 months if not refreshed regularly. We refresh every 30 minutes
* to keep the token active and avoid authentication issues.
*/
private setupTokenMaintenance;
/**
* Validate and refresh the Gmail access token
*
* This method forces a token refresh by requesting a new access token,
* which keeps the refresh token active and prevents expiration.
*/
private validateAndRefreshToken;
/**
* Manually refresh the Gmail access token
*
* This method allows external callers to force a token refresh,
* useful for troubleshooting or ensuring fresh credentials.
*/
refreshToken(): Promise<{
success: boolean;
lastRefresh: Date;
message: string;
}>;
/**
* Get the current status of the Gmail authentication token
*
* This provides visibility into token health and maintenance status,
* useful for monitoring and troubleshooting authentication issues.
*/
getTokenStatus(): {
hasRefreshToken: boolean;
lastRefresh: Date;
minutesSinceRefresh: number;
maintenanceActive: boolean;
};
/**
* Clean up resources and stop token maintenance
*
* This method should be called when the service is no longer needed
* to prevent memory leaks from the token refresh interval.
*/
destroy(): void;
/**
* Rate limiting helper - ensures we don't exceed Gmail API limits
* Gmail limits: 250 quota units per user per second, 1 billion per day
* Each message fetch = 5 units, send = 100 units
*/
private waitForRateLimit;
/**
* List Gmail messages with optional search query and automatic rate limiting
*
* @param query - Gmail search query (e.g., 'is:unread', 'from:example@gmail.com')
* @param maxResults - Maximum number of messages to return
* @returns Promise resolving to message list
*/
listMessages(query?: string, maxResults?: number): Promise<any>;
/**
* Get a specific Gmail message by ID with rate limiting
*
* @param messageId - The ID of the message to retrieve
* @returns Promise resolving to full message details
*/
getMessage(messageId: string): Promise<any>;
/**
* Send a Gmail message with rate limiting
*
* @param to - Recipient email address
* @param subject - Email subject line
* @param body - Email body content
* @param from - Optional sender email (uses authenticated user by default)
* @returns Promise resolving to sent message details
*/
sendMessage(to: string, subject: string, body: string, from?: string): Promise<any>;
markAsRead(messageId: string): Promise<any>;
markAsUnread(messageId: string): Promise<any>;
getProfile(): Promise<any>;
/**
* Search Gmail messages and return full message details
*
* @param query - Gmail search query
* @param maxResults - Maximum number of messages to return
* @returns Promise resolving to array of detailed message objects
*/
searchMessages(query: string, maxResults?: number): Promise<any[]>;
/**
* Extract plain text content from a Gmail message
*
* Gmail messages can have complex structure with multiple parts.
* This method finds and decodes the plain text content.
*
* @param message - The Gmail message object
* @returns The extracted plain text content
*/
extractTextFromMessage(message: any): string;
/**
* Extract a specific header value from a Gmail message
*
* @param message - The Gmail message object
* @param headerName - The name of the header to extract (case-insensitive)
* @returns The header value or empty string if not found
*/
getHeaderValue(message: any, headerName: string): string;
/**
* Batch process Gmail messages with intelligent rate limiting
*
* @param messageIds - Array of message IDs to process
* @param batchSize - Number of messages to process in each batch
* @returns Promise resolving to processed message details
*/
batchProcessMessages(messageIds: string[], batchSize?: number): Promise<{
results: ({
id: any;
threadId: any;
snippet: any;
subject: string;
from: string;
to: string;
date: string;
body: string;
size: any;
error?: undefined;
} | {
id: string;
error: string;
threadId?: undefined;
snippet?: undefined;
subject?: undefined;
from?: undefined;
to?: undefined;
date?: undefined;
body?: undefined;
size?: undefined;
})[];
summary: {
total: number;
successful: number;
failed: number;
processed: number;
};
}>;
/**
* Smart Gmail analysis for large accounts - processes messages efficiently
*
* @param analysisType - Type of analysis to perform
* @param options - Analysis options and parameters
* @returns Promise resolving to analysis results
*/
smartAnalysis(analysisType: 'recent' | 'unread' | 'important' | 'senders', options?: any): Promise<{
analysis: "recent" | "unread" | "important" | "senders";
results: never[];
summary: {
total: number;
};
} | {
results: ({
id: any;
threadId: any;
snippet: any;
subject: string;
from: string;
to: string;
date: string;
body: string;
size: any;
error?: undefined;
} | {
id: string;
error: string;
threadId?: undefined;
snippet?: undefined;
subject?: undefined;
from?: undefined;
to?: undefined;
date?: undefined;
body?: undefined;
size?: undefined;
})[];
summary: {
total: number;
successful: number;
failed: number;
processed: number;
};
analysis: "recent" | "unread" | "important" | "senders";
query: string;
}>;
}
//# sourceMappingURL=gmail.d.ts.map