create_filter
Create Gmail filters to automatically organize emails by applying labels, forwarding messages, or removing labels based on sender, subject, attachments, or search criteria.
Instructions
Creates a filter
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| criteria | Yes | Filter criteria | |
| action | Yes | Actions to perform on messages matching the criteria |
Implementation Reference
- src/index.ts:1012-1038 (registration)Full registration of the 'create_filter' MCP tool, including description, input schema using Zod, and handler function that delegates to handleTool for Gmail API call.server.tool("create_filter", "Creates a filter", { criteria: z.object({ from: z.string().optional().describe("The sender's display name or email address"), to: z.string().optional().describe("The recipient's display name or email address"), subject: z.string().optional().describe("Case-insensitive phrase in the message's subject"), query: z.string().optional().describe("A Gmail search query that specifies the filter's criteria"), negatedQuery: z.string().optional().describe("A Gmail search query that specifies criteria the message must not match"), hasAttachment: z.boolean().optional().describe("Whether the message has any attachment"), excludeChats: z.boolean().optional().describe("Whether the response should exclude chats"), size: z.number().optional().describe("The size of the entire RFC822 message in bytes"), sizeComparison: z.enum(['smaller', 'larger']).optional().describe("How the message size in bytes should be in relation to the size field") }).describe("Filter criteria"), action: z.object({ addLabelIds: z.array(z.string()).optional().describe("List of labels to add to messages"), removeLabelIds: z.array(z.string()).optional().describe("List of labels to remove from messages"), forward: z.string().optional().describe("Email address that the message should be forwarded to") }).describe("Actions to perform on messages matching the criteria") }, async (params) => { return handleTool(config, async (gmail: gmail_v1.Gmail) => { const { data } = await gmail.users.settings.filters.create({ userId: 'me', requestBody: params }) return formatResponse(data) }) } )
- src/index.ts:1033-1037 (handler)The handler function for the create_filter tool. It uses the shared handleTool to authenticate and execute the Gmail filters.create API call with the provided parameters, then formats the response.return handleTool(config, async (gmail: gmail_v1.Gmail) => { const { data } = await gmail.users.settings.filters.create({ userId: 'me', requestBody: params }) return formatResponse(data) }) }
- src/index.ts:1014-1032 (schema)Zod schema defining the input parameters for create_filter: criteria (matching conditions) and action (labels to add/remove or forward).{ criteria: z.object({ from: z.string().optional().describe("The sender's display name or email address"), to: z.string().optional().describe("The recipient's display name or email address"), subject: z.string().optional().describe("Case-insensitive phrase in the message's subject"), query: z.string().optional().describe("A Gmail search query that specifies the filter's criteria"), negatedQuery: z.string().optional().describe("A Gmail search query that specifies criteria the message must not match"), hasAttachment: z.boolean().optional().describe("Whether the message has any attachment"), excludeChats: z.boolean().optional().describe("Whether the response should exclude chats"), size: z.number().optional().describe("The size of the entire RFC822 message in bytes"), sizeComparison: z.enum(['smaller', 'larger']).optional().describe("How the message size in bytes should be in relation to the size field") }).describe("Filter criteria"), action: z.object({ addLabelIds: z.array(z.string()).optional().describe("List of labels to add to messages"), removeLabelIds: z.array(z.string()).optional().describe("List of labels to remove from messages"), forward: z.string().optional().describe("Email address that the message should be forwarded to") }).describe("Actions to perform on messages matching the criteria") }, async (params) => {
- src/index.ts:50-80 (helper)Shared helper function handleTool used by all Gmail tools, including create_filter. Handles OAuth2 client creation/validation, Gmail client instantiation, API execution, and error handling with special auth error responses.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) { // Check for specific authentication errors if ( error.message?.includes("invalid_grant") || error.message?.includes("refresh_token") || error.message?.includes("invalid_client") || error.message?.includes("unauthorized_client") || error.code === 401 || error.code === 403 ) { return formatResponse({ error: `Authentication failed: ${error.message}. Please re-authenticate by running: npx @shinzolabs/gmail-mcp auth`, }); } return formatResponse({ error: `Tool execution failed: ${error.message}` }); } }