modify_message
Add or remove labels on Gmail messages to organize your inbox. Use this tool to categorize emails by applying or deleting specific labels based on message ID.
Instructions
Modify the labels on a message
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| id | Yes | The ID of the message to modify | |
| addLabelIds | No | A list of label IDs to add to the message | |
| removeLabelIds | No | A list of label IDs to remove from the message |
Implementation Reference
- src/index.ts:599-604 (handler)The handler function for the 'modify_message' tool. It invokes the Gmail API's users.messages.modify method via the shared handleTool helper to add or remove labels from a specific message.async (params) => { return handleTool(config, async (gmail: gmail_v1.Gmail) => { const { data } = await gmail.users.messages.modify({ userId: 'me', id: params.id, requestBody: { addLabelIds: params.addLabelIds, removeLabelIds: params.removeLabelIds } }) return formatResponse(data) }) }
- src/index.ts:594-598 (schema)Input schema for the 'modify_message' tool defined using Zod, specifying parameters for message ID and optional label IDs to add or remove.{ id: z.string().describe("The ID of the message to modify"), addLabelIds: z.array(z.string()).optional().describe("A list of label IDs to add to the message"), removeLabelIds: z.array(z.string()).optional().describe("A list of label IDs to remove from the message") },
- src/index.ts:592-604 (registration)Registration of the 'modify_message' tool on the MCP server, including name, description, input schema, and handler function.server.tool("modify_message", "Modify the labels on a message", { id: z.string().describe("The ID of the message to modify"), addLabelIds: z.array(z.string()).optional().describe("A list of label IDs to add to the message"), removeLabelIds: z.array(z.string()).optional().describe("A list of label IDs to remove from the message") }, async (params) => { return handleTool(config, async (gmail: gmail_v1.Gmail) => { const { data } = await gmail.users.messages.modify({ userId: 'me', id: params.id, requestBody: { addLabelIds: params.addLabelIds, removeLabelIds: params.removeLabelIds } }) return formatResponse(data) }) }
- src/index.ts:49-65 (helper)Shared helper function 'handleTool' used by the 'modify_message' handler (and other tools) to manage OAuth2 authentication, client creation, and execution of Gmail API calls.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:47-47 (helper)Helper function 'formatResponse' used to standardize tool responses in MCP format.const formatResponse = (response: any) => ({ content: [{ type: "text", text: JSON.stringify(response) }] })