Skip to main content
Glama
taylorwilsdon

Google Workspace MCP Server - Control Gmail, Calendar, Docs, Sheets, Slides, Chat, Forms & Drive

manage_gmail_label

Create, update, or delete Gmail labels for users, set visibility in label and message lists, and manage email organization efficiently.

Instructions

Manages Gmail labels: create, update, or delete labels.

Args:
    user_google_email (str): The user's Google email address. Required.
    action (Literal["create", "update", "delete"]): Action to perform on the label.
    name (Optional[str]): Label name. Required for create, optional for update.
    label_id (Optional[str]): Label ID. Required for update and delete operations.
    label_list_visibility (Literal["labelShow", "labelHide"]): Whether the label is shown in the label list.
    message_list_visibility (Literal["show", "hide"]): Whether the label is shown in the message list.

Returns:
    str: Confirmation message of the label operation.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
actionYes
label_idNo
label_list_visibilityNolabelShow
message_list_visibilityNoshow
nameNo
serviceYes
user_google_emailYes

Implementation Reference

  • The handler function for the 'manage_gmail_label' tool. It handles creating, updating, or deleting Gmail labels based on the provided action, using the Gmail API via the service object.
    @server.tool()
    @handle_http_errors("manage_gmail_label", service_type="gmail")
    @require_google_service("gmail", GMAIL_LABELS_SCOPE)
    async def manage_gmail_label(
        service,
        user_google_email: str,
        action: Literal["create", "update", "delete"],
        name: Optional[str] = None,
        label_id: Optional[str] = None,
        label_list_visibility: Literal["labelShow", "labelHide"] = "labelShow",
        message_list_visibility: Literal["show", "hide"] = "show",
    ) -> str:
        """
        Manages Gmail labels: create, update, or delete labels.
    
        Args:
            user_google_email (str): The user's Google email address. Required.
            action (Literal["create", "update", "delete"]): Action to perform on the label.
            name (Optional[str]): Label name. Required for create, optional for update.
            label_id (Optional[str]): Label ID. Required for update and delete operations.
            label_list_visibility (Literal["labelShow", "labelHide"]): Whether the label is shown in the label list.
            message_list_visibility (Literal["show", "hide"]): Whether the label is shown in the message list.
    
        Returns:
            str: Confirmation message of the label operation.
        """
        logger.info(
            f"[manage_gmail_label] Invoked. Email: '{user_google_email}', Action: '{action}'"
        )
    
        if action == "create" and not name:
            raise Exception("Label name is required for create action.")
    
        if action in ["update", "delete"] and not label_id:
            raise Exception("Label ID is required for update and delete actions.")
    
        if action == "create":
            label_object = {
                "name": name,
                "labelListVisibility": label_list_visibility,
                "messageListVisibility": message_list_visibility,
            }
            created_label = await asyncio.to_thread(
                service.users().labels().create(userId="me", body=label_object).execute
            )
            return f"Label created successfully!\nName: {created_label['name']}\nID: {created_label['id']}"
    
        elif action == "update":
            current_label = await asyncio.to_thread(
                service.users().labels().get(userId="me", id=label_id).execute
            )
    
            label_object = {
                "id": label_id,
                "name": name if name is not None else current_label["name"],
                "labelListVisibility": label_list_visibility,
                "messageListVisibility": message_list_visibility,
            }
    
            updated_label = await asyncio.to_thread(
                service.users()
                .labels()
                .update(userId="me", id=label_id, body=label_object)
                .execute
            )
            return f"Label updated successfully!\nName: {updated_label['name']}\nID: {updated_label['id']}"
    
        elif action == "delete":
            label = await asyncio.to_thread(
                service.users().labels().get(userId="me", id=label_id).execute
            )
            label_name = label["name"]
    
            await asyncio.to_thread(
                service.users().labels().delete(userId="me", id=label_id).execute
            )
            return f"Label '{label_name}' (ID: {label_id}) deleted successfully!"

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/taylorwilsdon/google_workspace_mcp'

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