write_email_draft
Create draft emails in Gmail by providing recipient address, subject line, and body content through API integration.
Instructions
Create a draft email using the Gmail API.
Args:
recipient_email (str): The email address of the recipient.
subject (str): The subject line of the email.
body (str): The main content/body of the email.
Returns:
dict or None: A dictionary containing the draft information including 'id' and 'message'
if successful, None if an error occurs.
Raises:
HttpError: If there is an error communicating with the Gmail API.
Note:
This function requires:
- Gmail API credentials to be properly configured
- USER_EMAIL environment variable to be set with the sender's email address
- Appropriate Gmail API permissions for creating drafts
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| recipient_email | Yes | ||
| subject | Yes | ||
| body | Yes |
Implementation Reference
- mcp-server-example.py:40-92 (handler)The handler function for the 'write_email_draft' tool, decorated with @mcp.tool() for registration. It creates a draft email in Gmail using the Gmail API, taking recipient_email, subject, and body as inputs.@mcp.tool() def write_email_draft(recipient_email: str, subject: str, body: str) -> dict: """Create a draft email using the Gmail API. Args: recipient_email (str): The email address of the recipient. subject (str): The subject line of the email. body (str): The main content/body of the email. Returns: dict or None: A dictionary containing the draft information including 'id' and 'message' if successful, None if an error occurs. Raises: HttpError: If there is an error communicating with the Gmail API. Note: This function requires: - Gmail API credentials to be properly configured - USER_EMAIL environment variable to be set with the sender's email address - Appropriate Gmail API permissions for creating drafts """ try: # create gmail api client service = get_gmail_service() message = EmailMessage() message.set_content(body) message["To"] = recipient_email message["From"] = os.getenv("USER_EMAIL") message["Subject"] = subject # encoded message encoded_message = base64.urlsafe_b64encode(message.as_bytes()).decode() create_message = {"message": {"raw": encoded_message}} # pylint: disable=E1101 draft = ( service.users() .drafts() .create(userId="me", body=create_message) .execute() ) print(f'Draft id: {draft["id"]}\nDraft message: {draft["message"]}') except HttpError as error: print(f"An error occurred: {error}") draft = None return draft