list_labels
Retrieve all email labels from your Gmail account to organize and categorize messages for better email management.
Instructions
List all labels in the user's mailbox
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
No arguments | |||
Implementation Reference
- src/index.ts:467-476 (handler)Registration, schema (empty input), and handler for the 'list_labels' tool. The handler authenticates via handleTool, calls the Gmail API gmail.users.labels.list to fetch all labels for the user, and returns a formatted JSON response.server.tool("list_labels", "List all labels in the user's mailbox", {}, async () => { return handleTool(config, async (gmail: gmail_v1.Gmail) => { const { data } = await gmail.users.labels.list({ userId: 'me' }) return formatResponse(data) }) } )
- src/index.ts:50-66 (helper)Helper function used by list_labels (and other tools) to handle OAuth2 authentication, validate credentials, create Gmail client, execute the API call, and handle errors.const handleTool = async (queryConfig: Record<string, any> | undefined, apiCall: (gmail: gmail_v1.Gmail) => Promise<any>) => { try { const oauth2Client = queryConfig ? createOAuth2Client(queryConfig) : defaultOAuth2Client if (!oauth2Client) throw new Error('OAuth2 client could not be created, please check your credentials') const credentialsAreValid = await validateCredentials(oauth2Client) if (!credentialsAreValid) throw new Error('OAuth2 credentials are invalid, please re-authenticate') const gmailClient = queryConfig ? google.gmail({ version: 'v1', auth: oauth2Client }) : defaultGmailClient if (!gmailClient) throw new Error('Gmail client could not be created, please check your credentials') const result = await apiCall(gmailClient) return result } catch (error: any) { return `Tool execution failed: ${error.message}` } }
- src/index.ts:48-48 (helper)Helper function to format the API response into MCP content structure with JSON stringified text.const formatResponse = (response: any) => ({ content: [{ type: "text", text: JSON.stringify(response) }] })