Skip to main content
Glama

create_filter

Create Gmail filters to automatically organize emails by applying labels, forwarding messages, or removing labels based on sender, subject, attachments, or search queries.

Instructions

Creates a filter

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
criteriaYesFilter criteria
actionYesActions to perform on messages matching the criteria

Implementation Reference

  • The handler function for the 'create_filter' tool. It invokes the shared handleTool utility to create an OAuth-authenticated Gmail client and calls the Gmail API's users.settings.filters.create method with the provided criteria and action parameters, then formats the response.
    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)
      })
    }
  • Zod input schema for the 'create_filter' tool, validating criteria (conditions like from, to, subject, query, etc.) 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")
    },
  • src/index.ts:979-1005 (registration)
    Registration of the 'create_filter' tool on the MCP server, including name, description, input schema, and handler function.
    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)
        })
      }
    )
  • Shared helper function used by all Gmail API tools, including create_filter, to handle OAuth2 authentication, client creation, credential validation, API call execution, and error handling.
    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}`
      }
    }
Behavior1/5

Does the description disclose side effects, auth requirements, rate limits, or destructive behavior?

With no annotations provided, the description carries the full burden of behavioral disclosure but fails completely. It doesn't indicate that this is a write/mutation operation, doesn't mention permissions required, doesn't describe what happens after creation, and provides no information about error conditions or rate limits.

Agents need to know what a tool does to the world before calling it. Descriptions should go beyond structured annotations to explain consequences.

Conciseness2/5

Is the description appropriately sized, front-loaded, and free of redundancy?

While technically concise with just two words, this is under-specification rather than effective conciseness. The description fails to communicate essential information that would help an AI agent understand and use the tool correctly.

Shorter descriptions cost fewer tokens and are easier for agents to parse. Every sentence should earn its place.

Completeness1/5

Given the tool's complexity, does the description cover enough for an agent to succeed on first attempt?

For a mutation tool with 2 complex nested parameters and no annotations or output schema, the description is completely inadequate. It doesn't explain what a filter is, what it does, or what the expected outcome of creation is, leaving critical gaps in understanding.

Complex tools with many parameters or behaviors need more documentation. Simple tools need less. This dimension scales expectations accordingly.

Parameters3/5

Does the description clarify parameter syntax, constraints, interactions, or defaults beyond what the schema provides?

The schema description coverage is 100%, so the schema already documents all parameters thoroughly. The description adds no additional parameter information beyond what's in the schema, which meets the baseline expectation when schema coverage is complete.

Input schemas describe structure but not intent. Descriptions should explain non-obvious parameter relationships and valid value ranges.

Purpose2/5

Does the description clearly state what the tool does and how it differs from similar tools?

The description 'Creates a filter' is a tautology that merely restates the tool name without specifying what type of filter is being created or for what purpose. It doesn't distinguish this tool from sibling tools like 'create_label' or 'create_forwarding_address' that also create things in the Gmail context.

Agents choose between tools based on descriptions. A clear purpose with a specific verb and resource helps agents select the right tool.

Usage Guidelines1/5

Does the description explain when to use this tool, when not to, or what alternatives exist?

The description provides absolutely no guidance on when to use this tool versus alternatives. There's no mention of prerequisites, when this tool is appropriate, or how it differs from related tools like 'list_filters' or 'delete_filter' in the sibling list.

Agents often have multiple tools that could apply. Explicit usage guidance like "use X instead of Y when Z" prevents misuse.

Install Server

Other Tools

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/HitmanLy007/gmail-mcp'

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